garofalodeveloperThiago Garofalo

ICMS ST: watching tax rules that change without notice

Period: November 2026 to date · Role: concept, source survey, architecture, code, validation and publishing · Status: Phase 0 in production (37 sources watched); Phase 1 published (Convênio 142 and origin-to-destination protocol matrix); seven states with structured goods lists and MVA; parameters already checked against a real company’s quoting system

One-sentence summary: in two days I went from a pain every goods seller in Brazil knows (“the ICMS ST rule changes and nobody tells me”) to a monitor in production that watches 37 official sources and emails what changed and since when, plus open data with the history of every item, the matrix that says from which state to which the seller must withhold the tax, and the MVA of seven states, cross-checked against each other and already used to verify the calculation of a system in production.

1. Context and problem

Under ICMS tax substitution (ST), the seller pays in advance the state sales tax for the whole downstream chain. To do that it must know, for each product and each destination state, whether the goods are under the regime, which value-added margin (MVA) applies, which rate, and whether a poverty-fund surcharge is due. These rules change by decree, ordinance or interstate agreement, in 27 states and at Confaz, without notice to those affected.

In my group’s distribution company, the sales quote already calculates ST per item. The calculation is fine; the parameter is what goes stale. The accountants do not warn when an MVA changes, because they do not follow everything either, and the market solves this with tax consultancies costing more than a thousand reais a month that, in the end, read the same public sources.

The inspiration came from a project shown in my graduate program’s community: an “oracle” for a court’s internal rules, with hybrid search over thousands of resolutions. The key difference: here the answer is not a passage of text, it is a number that goes on an invoice. A mistake becomes a tax assessment. That is why the design is a monitor with source and date, not a chat.

2. How the rule works

A single answer (“this clamp, from SP to MG, has an MVA of X%”) depends on five layers of rules, each with its own source and pace.

Layer What it defines Who publishes
National goods list Which goods may be under ST, with CEST, NCM and description (Convênio ICMS 142/2018) Confaz
State adoption Which items the state puts under ST and with which original MVA Each state’s finance department
Interstate agreement Whether a seller in one state withholds ST for another, by segment Confaz protocols and agreements
Rates Destination internal rate, interstate rate (4%, 7% or 12%) and poverty fund State laws and regulations, Senate
Modality Domestic or imported, simplified-regime seller, base reduction Convênio 142 and state rules

The adjusted MVA is almost never published: it is calculated from the original MVA and the rates.

flowchart LR
  O[Original MVA<br/>of the state] --> F["Adjusted MVA =<br/>(1 + MVA) × (1 − interstate rate)<br/>÷ (1 − internal rate) − 1"]
  A[Interstate and<br/>internal rates] --> F
  F --> B[ICMS ST base<br/>on the invoice]

Two design consequences: the key is not the NCM but CEST plus description (one NCM may fall into several items); and every number needs an effective date and a legal basis, because tax audits look back in time.

3. What the source survey revealed

Before any code, three agents in parallel surveyed how Confaz and six states publish their rules, opening every page and confirming every address. Later, each state that was read brought its own surprise.

Finding Evidence (November 2026) What it changes in the project
There is no official API or spreadsheet for ST Confaz publishes Convênio 142 only as an HTML page of about 930 KB; none of the states read has open ST data The product is, first of all, a page extractor and versioner
São Paulo is leaving ST Auto parts, tools, electrical materials, tyres and paints left on 1 Oct 2026; construction materials leave on 1 Feb 2027 Removing a segment is a first-class event, with an effective date
Every state publishes in a different format MG in HTML by chapter; PR in a consolidated PDF of about 590 pages; RS the whole regulation in a single 10 MB page; BA one PDF per period, from 2012 to date One reader per state, all with the same output: item, CEST, NCM, MVA and effective dates
Incomplete or closed servers Two portals do not send the intermediate certificate in TLS; a third answers only a browser and refuses automated collection even from a server in Brazil Certificate chain completed from the address the certificate itself points to, with verification kept on; the closed state stays out of the monitor until another source exists
Competitors sell “daily updates” Web lookups and calculators; none offers per-item versioned history or alerts matched against an accounting firm’s client portfolio Differentiator: the parameter applied in the system, with what changed, when and under which rule

The idea was born for the group’s company, but the survey showed that the larger value is in serving accounting firms, which handle dozens of companies each, so the monitor is born as a module of Escrituro.

4. Architecture

The engine is a separate service with its own database; the accounting system talks to it through an internal API. A problem in the collector does not take down the accounting system, and the same engine can serve other systems.

flowchart TB
  F[Official sources<br/>Confaz, state departments,<br/>official gazettes] --> C[Collector<br/>schedule per source]
  C --> V[Versions and diff<br/>hash, original<br/>kept as evidence]
  V --> E[Per-source extractors<br/>tables, no AI]
  V --> I[AI interpreter<br/>decrees in prose]
  I --> R{Human review}
  E --> D[(Open data<br/>effective dates and basis)]
  R --> D
  V --> S[Source health]
  D --> A[Lookup and alerts<br/>by portfolio]
  S --> A
  A --> X[Calculation systems<br/>and email]

Three design rules that hold in every phase:

5. Phase 0: the monitor in production

The monitor now watches 37 sources: Convênio 142 and Confaz’s yearly indexes; the 13 valid protocols São Paulo has signed (11 for construction materials, plus auto parts and electronics); São Paulo’s goods list and MVA ordinances, rate articles, recent acts and the official gazette search; and the ST rules of MG, PR, RS, BA, MT, PE and AL. Each page becomes text with one line per table item, so that an MVA change shows up as one line:

- 2 | Argamassas | 10.002.00 | 3816.00.1 3824.50.00 | 70%
+ 2 | Argamassas | 10.002.00 | 3816.00.1 3824.50.00 | 75%

The finest work was avoiding false alarms, because a monitor that cries wolf gets switched off in the first week:

Rule Why
The portal’s “last updated” date is kept out of the comparison The portal republishes the page without changing the content
In lists, only what was added triggers an alert An act leaving the 30-day window is not news
One retry before recording an error Government portals fail for minutes
Extractor version recorded on every collection Improving the extractor changes the text; without this, every improvement would trigger dozens of false alerts
A monitor for the monitor Alerts when a source keeps failing, stops answering, loses the expected content or suddenly shrinks
Rules published as PDF read as text A regenerated PDF with the same content raises no alert, and a real change shows the passage, not just “the file changed”

The monitor for the monitor has already paid off: when the single 10 MB page from RS was added, the health check flagged right away that the construction table was not in the watched text (its title used a non-breaking space the selector did not match). Without it, the state would have stayed “unchanged” for months without actually being watched.

6. Phase 1: open data

Everything is published at github.com/thiagogardev/icms-st-dados, with code, tests and an open licence.

6.1 Convênio 142 with per-wording effective dates

The national list of goods under ST. Confaz marks each item’s history only by colour and free-text notes; the extractor reads those notes, typos and all, and turns each item into rows with start and end of validity.

Result Value
Valid CEST codes 1,043, one per CEST
Wordings in the history 1,220
CEST codes from São Paulo’s official list found 762 of 762
Inconsistencies in the source itself, published 5

6.2 The protocol matrix: from SP to which states

The 11 construction-materials protocols with SP, read with each item’s effective dates, become a matrix: for each NCM and destination state, whether a São Paulo seller withholds ST, under which protocol and with which MVA, when the protocol sets it. That is 2,290 wordings, 486 valid items and 672 rows for 12 states.

What the reading showed that was nowhere else:

After construction, the same reading moved on to other segments. Auto parts came in through the multilateral protocol São Paulo signs: the whole auto-parts list of Convênio 142, minus six codes, for 15 destination states. The six protocols SP had with Rio de Janeiro in other segments taught another lesson: five were revoked in 2026 (beverages, pharmaceuticals, cosmetics, cleaning products and tools), and only electronics remains. The reader did not notice when a whole protocol was revoked, which is stated in a note at the top of the page, and now it does. A concrete case: from SP to RJ, pliers lost ST when the tools protocol ended, but the electric drill keeps it, because it is also in the electronics protocol.

Finally, the matrix stopped being only about São Paulo. The direction of each protocol is in its wording: “destined to the State of MG or to the State of SP” works both ways, “destined to the State of ES” only from SP. With that, the matrix answers for any signatory seller: 24,512 rows across 16 origins. Rows with SP as the destination are flagged, because applying them to goods entering SP depends on an act of the State Finance Secretary that was never issued.

6.3 The states’ MVA, cross-checked against the protocols

For the states whose protocol does not set the MVA, the next step was reading the state law. Each has its own format, and each reader returns the same structure.

State Source and format Items Cross-check against the SP protocol
MG RICMS/2023, Annex VII, HTML by chapter 1,032 69 consistent, 4 real conflicts, 3 matching by NCM only
PR SEFA Resolution 571/2019, consolidated PDF 817 wordings with dates 43 with a single MVA, 9 with more than one (the description decides), 2 without MVA
RS RICMS, Appendix II, single 10 MB page 288 100% consistent on SP exclusions
BA RICMS-BA, Annex 1, PDF per period 311 wordings 77 consistent, 2 real conflicts, 4 by NCM only
MT RICMS Annex X appendix (list) and an MVA ordinance in a Lotus Notes database 1,006 valid 39 consistent, no protocol CEST missing from the list
PE Consolidated decree, Annex 37-C, a 14 MB HTML page 73 47 equal, 7 equal in one of the CESTs, 1 difference
AL Consolidated ST decree, PDF delivered by an API 83 52 consistent, no differences

The real conflicts are the kind of thing neither source shows alone. Example: the SP-MG protocol no longer lists concrete roof tiles and copper fittings, but MG’s law still says ST applies to goods coming from SP; the other way round, the protocol still lists ceramic tiles and copper pipes that MG treats as an exception for SP. Bahia still cites SP for copper pipes the protocol revoked on 1 Aug 2026.

Each state added a rule that the protocol does not show. Mato Grosso has two MVAs for the same item, depending on the buyer’s tax regime, and uses no adjusted MVA. Alagoas and the Pernambuco protocol split the MVA with and without freight in the tax base, which explained the Pernambuco “difference”: the protocol has 41% and 56% for fiber cement, and Pernambuco only adopted 41%. Alagoas computes the adjusted MVA adding the poverty fund to the internal rate, and Rio de Janeiro does not.

The PDFs required reading each word by its position on the page: in plain text the MVA is centred in the cell and falls between the item lines. Bahia’s previous wordings come in quotes in the middle of the table and are separated with their dates; in Paraná, the table swap of 1 Apr 2026 stays in the history with both values.

6.4 The states that cannot be read

Not every official source can be watched by a program, and the case records this instead of hiding it:

State Why
RJ The legislation portal answers only a browser, and the ST list is a .doc file
DF The rules system answers any non-browser client with a firewall block
ES The database that publishes the MVAs dropped out of DNS
PA The legislation system serves only a browser
AP No findable consolidated text is published

In these cases, coverage from SP still comes from the protocol, which is on Confaz. Getting around a public body’s block by pretending to be a browser is not an option. For RJ, the company’s products were checked once by hand, against the official file and the commercial database, confirming that the update of 1 Nov 2026 did not change their MVAs.

6.5 Comparison with a commercial database

During a trial of a commercial tax database, I checked item by item the products of a real company. The database was mostly up to date, but stale on points the matrix caught, such as the Mato Grosso protocol above and two São Paulo base ordinances revoked months earlier. The commercial database is a yardstick; none of its content goes into the open data, which comes only from official sources.

6.6 Real use: the quoting system

The distribution company’s quoting system now checks its ST rules against the open data: a script downloads the published matrix and state MVAs, builds the rules per NCM and state, compares them with what is registered and writes only on explicit order.

The first manual check, before the script, found states missing for products the company actually sells, and the fix was applied. After that, with the script, the production result was: 63 rules equal to the open data, none different, 14 kept where the protocol covers but the state MVA has not been read yet, and three products without rules shown only as suggestions, because ST depends on NCM and description. The decision on them was human: one now has ST, two were recorded as not having it. Once Mato Grosso’s law was read, two fittings got the MVA that was missing for that state, and saving it was also decided by a person: today there are 65 rules equal to the open data and none different.

7. How it is measured

The system is only worth something if measured against an independent yardstick, with metrics an accounting firm understands.

Question Metric How to measure Initial target
Does it notice when a rule changes? Detection recall Month’s amendments built from the official gazette, compared with the events generated 98% in SP, 95% elsewhere
Does it alert for nothing? Detection precision Share of events that match a real parameter change 90%
Is the extracted number right? Per-field accuracy Sample checked against the rule and real tax returns, by path (table or text) 99% tables, 95% text before review
Does it arrive in time? Latency Time between publication and detection Median under 24 hours

What is already measured:

For the graduate program, the central experiment is still extracting parameters from regulatory text: comparing AI with a fixed output schema against deterministic extraction, measuring per-field and per-state accuracy. The table readers in this case became the baseline and the label generator for that experiment.

8. Numbers

All as of 10 November 2026.

Measure Value
Official sources watched in production 37
Protocols read with per-item effective dates 18 (13 valid, 5 revoked in 2026)
States with structured goods list and MVA 7 (MG, PR, RS, BA, MT, PE, AL)
Rows in the SP-to-destination matrix 2,648, for 18 states
Rows in the origin-to-destination matrix 24,512, across 16 origins
Matrix gold-set cases right 44 of 44
Automated tests 20 in the monitor, 47 in the open data
Quoting-system rules confirmed by the open data 65, none divergent
Real conflicts between state law and protocol, published 7

What is not done, said just as plainly: RJ, DF, ES, AP and PA have no source a program can read (section 6.4); RS effective dates exist only where the page notes an item-level change; beverages, fuels and medicines in Bahia have cases for review; the change email has not yet fired for a watched rule actually changing; the AI interpreter and the review queue are not built yet.

9. Lessons

10. Next steps

  1. Phase 0, foundation (live). Gate: 30 days of collection without silent failure and every change detected within 24 hours.
  2. Phase 1, national base (published). Next: the protocols São Paulo does not sign, to complete the matrix between the other states.
  3. States. SC and the states with no protocol with SP; RJ, DF, ES, AP and PA when a source a program can read exists. Gate: goods list and MVA cross-checked against the protocol, as for the first seven.
  4. AI interpreter and review queue for decrees in prose, measured against the table readers.
  5. Escrituro. Alerts by the firm’s client portfolio and lookup by NCM with adjusted MVA.

The horizon is part of the design too: the tax reform phases ICMS down from 2029 and ST will tend to shrink. The legislative-watch engine (collect, version, detect, interpret, alert) is the same one accounting firms will need for the new IBS and CBS taxes.

11. Stack and code

Python 3 · httpx · lxml · pdfplumber and pypdf for rules published as PDF · SQLite in the monitor, designed for PostgreSQL · cron scheduling on a server in Brazil · email alerts via Resend · hourly backup with an immutable copy · TypeScript in the quoting system.

The open data and the readers (Convênio 142, protocols, MG, PR, RS, BA, MT, PE and AL) are public: github.com/thiagogardev/icms-st-dados. The monitor is private, because it will run inside a commercial product; it can be shown in an interview or an academic review.