ENGINE 31
SFS-E31-NRW · CROSS-BORDER WITHHOLDING · REV 2026-07-24 · PRODUCTION
PRODUCTION = runnable end-to-end, CI-backed, full test suite passing. All data fictional and seeded.
Non-Resident Withholding & 1042-S
Does each cross-border rate recompute from the evidence, and does the 1042 tie to the slips?
A US withholding agent that pays cross-border interest, dividends, royalties and rent to foreign recipients has to withhold Chapter 3 tax at the right rate, support every reduced rate with a valid Form W-8, deposit what it withheld, and reconcile the annual Form 1042 to the 1042-S slips it transmits -- and every one of those is a determination, not a number that can be typed and trusted. The rate applied to a payment is decided in a fixed order of precedence: the FATCA (Chapter 4) override on a non-participating FFI first, then the portfolio-interest exemption on non-contingent interest, then a treaty rate behind an unexpired W-8, then the statutory default. The tax withheld is that rate applied to the gross, the slips aggregate the reportable payments, the Form 1042 totals sum the slips, and the deposits tie the liability. This engine reads the eight artifacts a withholding record carries -- the payee register, the treaty rate table, the income-code table, the payment register, the deposit register, the 1042-S slip register, the W-8 renewal watchlist and the Form 1042 panel -- and runs twenty-eight deterministic controls over them. Is the file complete and the review date readable, are the payee ids unique and every foreign country in the rate table with a classified Chapter 3, Chapter 4 and LOB code, does the treaty table define and bound every rate a payment needs, is every payment well-formed and attributable to a payee, is the US payee excluded and the accrued coupon un-withheld, does every applied rate recompute from the payee's status through the shared kernel and every tax equal rate times gross, is each treaty reduction supported by an unexpired W-8 with the portfolio and FATCA boundaries held, do the deposits tie the tax withheld and was the return filed on time, and do the 1042 gross, tax and 1042-T count tie the slips with one slip per reportable payee. It compares every rate, tax and total with exact ==, treats the W-8 expiry gate as <= so a form lapsing on the review date is already unsupported, and never writes to a source artifact.
A withholding determination is a snapshot of a payee's status on the day a payment went out, and it goes stale -- or was wrong to begin with -- in ways nobody watches. A treaty rate is applied while the W-8 that would support it is missing or expired past its three-year term; the paper shows a low rate and the support for it is not there. A US payee is withheld on when it should not be, an accrued coupon is treated as paid, a non-participating FFI is handed treaty relief the FATCA override bars it from, a contingent coupon is coded as exempt portfolio interest -- each one internally consistent, none of them wrong on their face. The deposits drift a cent from the liability, or the return is filed a week late. The Form 1042 total gross or total tax is stated on a number nobody rebuilt from the slips underneath it, and a reportable payee has no slip at all. The order of precedence is the whole of the rule and it does not commute: a payee who is both a treaty claimant and a non-participating FFI is withheld at the statutory rate, not the treaty rate, and only one traversal order gets that right. This engine rebuilds every rate through the one kernel that produced the data, gates every reduction on a valid W-8, ties the deposits and the 1042 to the slips with exact ==, ships a planted-defect file for every registered control, and states its benchmark as a command rather than a claim. A rate reduced without support, a US payee withheld, a total a cent off the slips -- each is invisible until an examiner recomputes the withholding the agent already reported.
Architecture
functional block stack · static overviewSelect a layer
Eight control families run in registry order over each withholding file. The first proves the others had something to read and that the review date is legible, because a control that passes on absent evidence reports assurance it never performed.
Key specifications
at a glanceWhat it does for you
plain termsA US withholding agent that pays cross-border interest, dividends, royalties or rent to foreign recipients has to withhold Chapter 3 tax at the right rate, support every reduced rate with a valid Form W-8, deposit what it withheld, and reconcile the annual Form 1042 to the 1042-S slips it transmits. A withholding determination is a snapshot of a payee's status on the day a payment went out, and it goes stale -- or was wrong to begin with -- in ways nobody watches. Four failures hide inside a folder of withholding workpapers, and none of them look wrong. The rate is reduced without support: a treaty rate is applied while the W-8 behind it is missing or expired past its three-year term. The classification is off: a US payee is withheld on, an accrued-but-unpaid coupon is treated as paid, a non-participating FFI is given treaty relief the FATCA override bars it from, or a contingent coupon is coded as exempt portfolio interest. The deposit drifts from the liability, or the return is filed after its due date. And the return does not tie to the slips: the Form 1042 total gross or total tax is stated on a number nobody rebuilt from the 1042-S slips underneath it, or a reportable payee has no slip at all. None of these are judgment calls. They are equalities and comparisons -- an applied rate against the determination the kernel rebuilds with exact ==, a tax against rate times gross, a deposit and a 1042 total against the slips, a W-8 term against the review date with <= -- which a deterministic control settles better than a spreadsheet inherited by whoever tracks the withholding this year.
The seeded demo runs every control over every withholding file7. The CLI generates thirty fictional withholding files -- one clean baseline and one carrying each of the twenty-nine planted defects -- then runs all twenty-eight controls over each. The clean file returns PASS with no flags; every defect file returns REVIEW or FAIL and names the control it tripped along with the reason that control exists.
The order of precedence does not commute8. A payee is seeded as both a treaty claimant and a non-participating FFI. The FATCA override must win: the payment is withheld at the statutory 30%, not the treaty rate. One defect file hands that payee a treaty-reduced 15% and trips exm_fatca_override, while another removes the W-8 behind a genuine treaty reduction and trips exm_w8_present_for_treaty -- proving the two exemption branches catch different failures at the edge boundary the kernel encodes.
The rate is recomputed, not read back9. A defect file under-rates a treaty payee, leaving a stated rate that no longer matches the determination. wh_rate_recomputes rebuilds the rate from the payee's status through the shared kernel -- FATCA, then portfolio, then treaty behind a valid W-8, then statutory -- and fails, proving the rate is recomputed from the evidence rather than trusted as stated.
Functional block diagram
engineering · each block links to its sourcePlain terms
- Seeded Withholding Files. fictional Chapter 3/4 withholding files enter the control registry
- Structural Precondition. is the withholding file complete and the review date legible
- Payee Register. is the register sound and every status classified
- Treaty Rate Table. does the table define and bound every rate a payment needs
- Payment Register. is every payment well-formed, coded and attributable
- Withholding Gate & Recompute. does every rate and tax recompute from the evidence
- Exemption, Validity & Expiry. is every reduction supported and the boundaries held
- Deposits & Filing. do the deposits tie the liability and was the return timely
- Form 1042 Reconciliation. does the 1042 recompute from the slips it transmits
- Registers, Table & Kernel. the artifacts and shared determination every rule resolves against
- Verdict and Findings. every finding, with the reason it exists, ends at a person
Engineering
- Seeded Withholding Files. generate_corpus writes one clean baseline plus one planted-defect file for every named control, thirty files in total. Only the payees, the treaty table, the income-code table, the payment base facts (gross, income code, sub-character, status) and the deposits are stated; each payment's applied rate and tax withheld, the 1042-S slips, the Form 1042 totals and the W-8 renewal watchlist are re-derived through the same withholding kernel the engine later recomputes with, the rate through withholding_engine.withholding. So the relationships the engine tests are the same relationships that produced the data: a defect on a base fact re-derives the rollup and confines the break to one control, a defect on a stated determination edits that figure alone.
- Structural Precondition. One control. FAILs a file missing any of the eight artifact types or carrying a duplicate, or an unreadable as_of review date, so no downstream rule holds vacuously on absent evidence.
- Payee Register. Five controls: unique payee ids, every foreign residence country in the treaty table, and known Chapter 3, Chapter 4 and limitation-on-benefits codes on the payees that need them.
- Treaty Rate Table. Two controls: the treaty table carries a row for every (country, income-type) a reportable payment needs, and every rate sits between 0.00% and the statutory 30.00%.
- Payment Register. Four controls: the required descriptive fields present, a known 1042-S income code, a payee that exists in the register, and a known paid-or-accrued status.
- Withholding Gate & Recompute. Four controls: the US payee excluded and the accrued coupon un-withheld, each applied rate rebuilt through the shared kernel, and each tax equal to rate times gross with truncating division.
- Exemption, Validity & Expiry. Five controls: a valid W-8 behind every treaty reduction, an unexpired term at the review date, a renewal flag inside the lead-time window, portfolio interest only on non-contingent coupons, and the FATCA override forcing statutory.
- Deposits & Filing. Two controls: the deposit total equals the tax withheld across reportable payments in exact cents, and Form 1042 was filed on or before its due date.
- Form 1042 Reconciliation. Five controls: one slip per reportable payee, the 1042 gross and tax tied to the slips, the 1042-T transmitted count tied to the slips, and the W-8 watchlist tied to the payee evidence.
- Registers, Table & Kernel. The payee register carries each payee's country, Chapter 3/4 status, LOB code and W-8; the treaty rate table carries the (country, income-type) rates; the income-code table maps each 1042-S code to an income type and portfolio flag; the payment register carries the base facts and the stated rate and tax; the deposit register, slip register, W-8 watchlist and Form 1042 panel carry the derived figures. Every rate, tax, exemption, deposit and reconciliation control resolves against these through the shared kernel in withholding_engine.withholding, so a determination cannot disagree with the data that produced it.
- Verdict and Findings. Findings roll up per withholding file: any FAIL is FAIL, FLAGs without FAILs are REVIEW, clean is PASS. The CLI exit code is the verdict, so a pipeline can gate on it. Reports carry no timestamps or absolute paths, which is what makes the committed report diffable.
Instruction set
every public command| Command | Operation | Output | Exit | Artifacts |
|---|---|---|---|---|
python run.py | regenerate the seeded fictional corpus, run all twenty-eight controls, write both reports | per-file verdicts and every actionable finding, then the overall verdict | 2 for the bundled corpus, which carries a planted defect for every control by design | nrw_report.json, nrw_report.md, samples/ |
python -m withholding_engine samples | analyze an existing folder of withholding files without regenerating it | per-file verdicts and findings | 0 PASS / 1 REVIEW / 2 FAIL / 3 usage | none unless --json or --md is passed |
python -m withholding_engine samples --generate --quiet | regenerate the corpus and print only the overall verdict | a single verdict line | 0 PASS / 1 REVIEW / 2 FAIL / 3 usage | samples/ |
python -m pytest withholding_engine/tests -q | run the full test suite for this engine | 10410 passed | 0 when every test passes | none |
Benchmarks
measured demo results| Measure | Result |
|---|---|
| Engine tests10 | 10,410 tests collected live collection from the engine directory |
| Registered controls11 | 28 controls counted from the registry, not from documentation |
| Planted defects12 | 29 defect files every registered control has at least one file that trips it |
| Control coverage13 | 100 percent of controls with a planted defect no control ships without a file demonstrating it firing |
Control characteristics
engineeringPlain terms
The engine has no write path and therefore no autonomy to gate. It produces findings; a person decides whether a rate is carried into the return and whether a W-8 or deposit is chased.
Engineering
Deterministic envelope. seeded fictional inputs, read-only operation, offline default mode.
Demo gate. FAIL on the bundled corpus, by design -- it carries a planted defect for every registered control
| Severity | Verdict | Action |
|---|---|---|
| No findings above PASS | PASS | carry the mechanically clean withholding file into the documented sign-off and 1042 review |
| One or more FLAG, no FAIL | REVIEW | a human resolves each flag; a W-8 due for renewal inside the lead-time window and a watchlist that does not tie the payee evidence both land here |
| One or more FAIL | FAIL | the rate or total is not carried into the return until the failing control is cleared -- a reduction with no valid W-8, a US payee withheld, an accrued coupon withheld, a rate that does not recompute, or a deposit or 1042 total that does not tie |
- Read-only: withholding files are parsed and never written back, so the engine cannot introduce the break it reports.
- Integer cents and basis points throughout, compared with exact ==; there is no tolerance band on a rate, a tax or a total, and a cent off the re-derivation is reported as a break.
- A figure that should be integer cents or basis points but is not produces an AMOUNT_INVALID finding contained to the one row it was read on, rather than being coerced into the number the engine is meant to audit.
- One withholding kernel: the controls and the generator share the determination in withholding_engine.withholding, so a rate cannot disagree with the data that produced it.
- The order of precedence is encoded once: FATCA override, then portfolio-interest exemption on non-contingent interest, then a treaty rate behind a valid W-8, then the statutory default -- so a non-participating FFI that also holds a treaty claim is withheld at statutory.
- Every expiry and renewal test is measured to the as_of review date carried in the file, never the system clock, and the W-8 expiry gate is <= so a form lapsing on the review date is already unsupported.
- Deterministic and byte-stable: same inputs produce the same findings in the same order, with no timestamps or absolute paths in any output.
- Absent evidence is never a passing control; a missing artifact fails completeness rather than letting the controls that read it pass unread.
Operating limits
what it refuses to do- The engine never files a return, remits a deposit, issues or requests a W-8, contacts a payee, or writes to a source artifact. It reads the withholding file and stops.14
- It reads the eight artifacts the withholding record emits. It does not connect to a payment system, a general ledger, the IRS, or the underlying Forms W-8.15
- It proves a rate clears the mechanical determination -- FATCA override, portfolio exemption, treaty behind a valid unexpired W-8, statutory default -- not that a payee's stated Chapter 3/4 status is genuinely correct or that a W-8 is authentic. That judgment is a reviewer's.16
- It re-derives each rate at the treaty and statutory rates the file carries; it does not decide whether the treaty rate table or the statutory 30% default is the correct one for the year and taxpayer.17
- It takes the payment and deposit records at face value: it confirms the registers foot, tie and reconcile, not that a payment or deposit actually occurred. All shipped data is fictional and the reporting period is set in a fictional future.18
See it run
control architecture
Integration
how to run itDistribution: public repository, MIT license.
- 1python -m pytest --collect-only -q -o addopts= (withholding-automation/)
- 2len(withholding_engine.engine.REGISTRY)
- 3distinct rule-id prefixes in withholding_engine.engine.REGISTRY
- 4samples/*.json after python run.py
- 5len(withholding_engine.model.DOC_TYPES)
- 6withholding_engine/tests boundary tests on the cent, the basis point and the date threshold
- 7python run.py (withholding-automation/); withholding_engine/tests planted-defect suite
- 8withholding_engine/generate.py DEFECTS fatca_override_ignored / treaty_without_w8
- 9withholding_engine/generate.py DEFECTS rate_applied_wrong -> wh_rate_recomputes
- 10python -m pytest --collect-only -q -o addopts= (withholding-automation/)
- 11len(withholding_engine.engine.REGISTRY)
- 12len(withholding_engine.generate.DEFECTS)
- 13withholding_engine/tests: every registered rule has a planted defect
- 14withholding-automation/README.md and withholding_engine read-only / determinism tests
- 15withholding_engine/model.py DOC_TYPES
- 16withholding_engine/engine.py wh_ and exm_ families
- 17withholding_engine/withholding.py STATUTORY_RATE_BPS and the rate_ / wh_ families
- 18withholding_engine/generate.py
Show us where the hours go.
One conversation: you describe the work that consumes your team's month; we tell you plainly what this engine can take over, what it can't, and what a scoped first phase would cost. Your people keep approval authority.
Book a free consultation