> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# Source version mark

> Reporter records the source version that a regulatory filing read, and marks the filing when the source later shows another version.

> [!WARNING]
>
> This capability is under development. It is available in homologation on Lerian Cloud, and its behavior can change before release. Confirm availability with Lerian before integration.

A source can publish a correction after Reporter generated a regulatory filing. The source then shows another version of the data that the filing read. A **source version mark** tells you about this.

Your mapping declares which column of a table holds the version of the data. Reporter records that version with each filing that it generates. Reporter then checks the source on a schedule. When the source shows another version of the same data, Reporter adds a **mark** to the filing.

The capability applies to regulatory filings that Reporter generates from a catalog document. Declare the source version on a query that reads a database data source. For a mapping with no declaration, Reporter records no source version and marks no filing.

This page is for the engineer who writes the mapping and for the person who reviews filings.

## Declare the source version

***

Add the key `sourceVersion` to a query of your mapping. The key takes two entries:

| Key | Required | Meaning |
| - | - | - |
| `column` | Yes | The column of the table that holds the version. |
| `digest` | No | A second column that also identifies the version, for example a digest of the whole dataset. |

This example is trimmed from the reference mapping of SCR 3040:

```yaml theme={null}
queries:
  - name: clientes
    dataSource: lender
    table: scr_operation_current
    sourceVersion: {column: version, digest: dataset_digest}
    filters:
      - field: competencia
        operator: eq
        values: [{parameter: dataBase}]
```

Write the key on a query that reads a table of a database data source. A file dataset already records the revision of each day that it delivers. The query can aggregate. Reporter reads the declared columns of the rows that the filters keep, and it ignores the aggregation.

The declaration does not change what the generation reads, and it does not change the file.

### What Reporter refuses when you save

Reporter refuses the mapping with HTTP `400` and the code `RPT-0143` in these cases:

* The query reads the rows of another query.
* The query filters by the values of another query.
* The query reads the Midaz CRM data source.
* The key has no `column`, or `column` is empty.
* The key has an entry other than `column` and `digest`.

The detail of the refusal names the query to correct. See the [regulatory mapping reference](https://docs.lerian.studio/en/products/reporter/regulatory-mapping-reference.md) for the other checks that Reporter makes when you save.

### What your data must hold

Each declared column must hold one value for the rows that the query reads. A generation can fail with `RPT-0143` when a row holds no version, or when the rows hold more than one value.

## What a filing records

***

Reporter stores the source version with the draft.

The filing then shows the field `sourceReads` on `GET /v1/reports/{id}` and on the list of reports. The field is a list of reads. A filing from a mapping with no declaration has no `sourceReads`. A report from a template has none either.

| Field | Type | Meaning |
| - | - | - |
| `key` | text | The identifier of the read. Treat it as opaque. A mark refers to it. |
| `dataSource` | text | The data source that Reporter read. |
| `table` | text | The table, as the mapping names it. |
| `version` | text | The value of the declared version column, as text. Absent with `noRows`. |
| `digest` | text | The value of the declared digest column. Absent when the read holds no digest. |
| `noRows` | boolean | Present, and `true`, when no row matched the filters and the scope of the read. |

## When the source changes during a generation

***

The source can change while Reporter reads it. The filing would then mix two versions of the data. Reporter does not accept this.

The generation fails with `RPT-0261`. The report ends in the status `Error`. Its `metadata` holds the code, the title, and a message that names the data source and the table. Generate the filing again.

## How Reporter marks a filing

***

Reporter checks the sources on a schedule. The check runs once every hour by default.

The check reads a filing only when it meets these conditions:

* The filing is `Draft` or `Final`.
* The filing carries `sourceReads`.
* The filing was created within the window. The window is 400 days by default.

Reporter reads the source again with the mapping, the parameters, and the institution that the filing holds from its generation. It does not use your current mapping. It checks only the reads that the filing recorded.

Reporter compares the state that the source shows now with the last known state of each read. The last known state is the newest mark of that read. A read with no mark yet uses the state that the filing recorded.

* When the states differ, Reporter adds one mark to `republishedReads`.
* When the states are the same, Reporter writes nothing and sends nothing.

A mark has these fields:

| Field | Type | Meaning |
| - | - | - |
| `key` | text | The `key` of the entry in `sourceReads` that the mark is about. |
| `currentVersion` | text | The version that the source shows now. Absent with `noRows`. |
| `currentDigest` | text | The digest that the source shows now. Absent when the source shows no digest. |
| `noRows` | boolean | Present, and `true`, when the source now shows no row for the read. |
| `seenAt` | timestamp | The time, in UTC, when Reporter saw that state. |

The list `republishedReads` only grows. Reporter does not remove a mark. A source can return to the state that the filing read. Reporter then adds a new mark, because the new state differs from the newest mark.

If the source of a read does not answer, Reporter adds no mark for that read. It tries again at the next check. Reporter does not check a filing again after its window ends. The marks that the filing already has stay.

## What a mark does not do

***

A mark does not say that the file would come out different. It records a change in what the source shows for the data that the filing read.

Reporter never refuses an approval because of a mark.

A mark changes neither the file nor the status of the filing. Reporter does not generate a new filing by itself.

What you do with a marked filing is your decision.

## Find the filings to review

***

Use the query parameter `republished` on `GET /v1/reports`. The values are `true` and `false`. The value `false` narrows nothing.

Send `republished=true` with `regulatory=true`, with `document_id`, or with `subscription_id`.

With `republished=true`, Reporter returns a filing only when all three conditions are true:

1. The filing is `Draft` or `Final`.
2. No newer `Draft` or `Final` exists for the same document, the same CNPJ of the institution, and the same filing period.
3. The source shows another version of what the filing read.

The third condition is true in two cases. A file dataset day that the filing read has a newer revision. Or the newest mark of at least one read differs from what the filing read.

A filing leaves the result in two cases. A newer `Draft` or `Final` of the same document, CNPJ, and filing period exists. Or the source returns to the state that the filing read. The marks stay on the filing in both cases. A newer generation that is still `Processing`, or ended in `Error`, does not take the filing out of the result.

The list keeps its cursor pagination. A page holds only filings that pass the filter.

Reporter refuses the request with HTTP `400` and `RPT-0019` in these cases:

* You send `republished=true` on a list that is not a list of filings.
* You send `republished=true` with `current=true`.
* The value is not `true` or `false`.

## The event

***

Reporter emits the event `regulatory.source_republished` when it adds a mark. The delivery profile is Critical.

| Field | Meaning |
| - | - |
| `report_id` | The filing. |
| `document_id` | The catalog document. |
| `document_version` | The version of the document that the filing pinned. |
| `institution_id` | The institution of the filing. The payload carries no name and no CNPJ. |
| `filing_period` | The period of the filing, as recorded on the report. |
| `read_key` | The `key` of the entry in `sourceReads`. |
| `data_source` | The data source of the read. |
| `table` | The table of the read. |
| `current_version` | The version that the source shows now. Empty with `no_rows`. |
| `current_digest` | The digest that the source shows now. Empty when the source shows no digest. |
| `no_rows` | A boolean. `true` when the source shows no row. |
| `seen_at` | The time of the check, in UTC, as RFC 3339. |

The payload carries no report status, because a status can be old when you read the event. Read the filing to get its status. The `ce-type` of the event is `studio.lerian.reporter.regulatory.source_republished`.

Reporter sends events only when event publication is enabled. When it is off, Reporter still writes the marks and sends no event. Reporter does not send later an event for a mark that it wrote while publication was off.

If a Streaming Hub subscription lists its event types, add `regulatory.source_republished` to its `event_types`.

For the deduplication by event id, read [Delivery](https://docs.lerian.studio/en/products/reporter/reporter-events.md#delivery). A repeated delivery of the same mark carries the same event id.

## Settings

***

Two environment variables set the check.

| Variable | Description | Format | Default | Required |
| - | - | - | - | - |
| `SOURCE_VERSION_WATCH_INTERVAL_SEC` | The time between two checks, in seconds. | Integer above `0` | `3600` | No |
| `SOURCE_VERSION_WATCH_WINDOW_DAYS` | The age of the filings that Reporter checks, in days, counted from the creation of the report. | Integer above `0` | `400` | No |

A value of zero or less stops the service at start. See [Environment variables](https://docs.lerian.studio/en/products/reporter/reporter-environment-variables.md) for the other settings.

## SCR 3040

***

From version `1.1.0` of the SCR 3040 document, the reference mapping declares the source version. It declares it on the queries that read the three Lender views. Reporter then stores the version of each view that a filing read. It marks the filing when a view shows another version of the same reference month. For the document itself, read [SCR 3040](https://docs.lerian.studio/en/products/reporter/generating-scr-3040.md).

The version of the views is per reference month of the source database. It is not per institution. A correction that Lender publishes for another institution of the same database also marks your filing.

A subscription that you activated before version `1.1.0` gets the declaration in one of two ways:

1. **Adopt version `1.1.0`, then restart the mapping from the reference.** [Adopting a version](https://docs.lerian.studio/en/reference/products/reporter/adopt-document-version.md) alone keeps your current mapping. [The restart](https://docs.lerian.studio/en/reference/products/reporter/restart-the-mapping.md) builds your mapping again from the reference mapping, and that brings in the declaration. Copy your mapping before the restart. The restart does not keep the `manualOverrides` values or your edits to the queries of the reference. Enter them again after the restart.
2. **Add the declaration by hand.** [Edit the mapping](https://docs.lerian.studio/en/reference/products/reporter/replace-subscription-mapping.md) and add `sourceVersion: {column: version, digest: dataset_digest}` to each query that reads one of the three views. Use this way when you customized your mapping and want to keep it.

If you do neither, your mapping has no declaration. Reporter then marks none of your filings.

## Next steps

***

> [card] [SCR 3040](https://docs.lerian.studio/en/products/reporter/generating-scr-3040.md)
>
> The catalog document, its data sources, and its reference mapping.

> [card] [Regulatory mapping reference](https://docs.lerian.studio/en/products/reporter/regulatory-mapping-reference.md)
>
> The keys, values, and refusals of a mapping.

> [card] [Reporter events](https://docs.lerian.studio/en/products/reporter/reporter-events.md)
>
> The event catalog, the envelope, and the delivery rules.

> [card] [File datasets](https://docs.lerian.studio/en/products/reporter/file-datasets.md)
>
> Files that your systems deliver each day. Reporter records the revision of each day.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.