Files
GTI_GERENCIADOR_FISCAL/agent/tests/test_cfop_ippt_auto_apply.py
T
gtiandClaude Sonnet 5 3e3858054f feat: CFOP/IPPT write-capability core (inert, not wired, not activated)
Missao 3 / Bloco C.1-C.2: new agent/src/gti_fiscal_agent/cfop_ippt_auto_apply.py
mirrors the existing rtc_auto_apply.py safety model for a new, narrower
capability (CFOP_IPPT_WRITE_V1): compare-before-write, stale-write guard
("human override sempre vence" - a value changed since the candidate was
selected is never overwritten), one transaction per cell, readback
verification, and rollback that only restores a value still equal to what
was written. Candidates come exclusively from field_contract.resolve_product's
own AUTO_FILL_SAFE classification for CFOP/IPPT - no new fiscal rule.
14 new tests (agent/tests/test_cfop_ippt_auto_apply.py) prove: flag OFF means
the caller never reaches a write; only CHANGE+write_eligible CFOP/IPPT cells
become candidates; a human edit between decision and write blocks the write;
a second run over an already-applied cell writes zero; a technical failure on
one cell never stops the batch; rollback refuses when the value changed again.

Deliberately NOT wired into agent/scripts/run_agent_service.py::execute_job,
and the Hub-side authorization/audit plumbing (Installation.cfop_ippt_write_mode,
audit table, job-delivered authorization - the same pattern as
central/app/rtc_audit.py) does not exist yet. No currently-published Agent
behavior (1.3.8) changes with this commit - nothing imports this new module.
See docs/CFOP_IPPT_WRITE_CAPABILITY_V1.md for the acceptance roteiro (test
products only, never real ones) and what remains before this can ever be
activated for a real installation - deliberately left for review before any
further step, per this mission's explicit instruction not to publish it active.

Also: dossie_contador.md (internal + anonymized) updated with the Bloco B
finding that the ~9.5% CST=01 anomaly is confined to a single calendar month
(not the whole year), with no fiscal-dimension (NCM/CFOP/CSOSN/ICMS group)
explaining which items got it - consistent with a one-off technical event,
not a deliberate rule.

REAL_PRODUCT_FISCAL_WRITES=0 (no write path exists in this commit).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-28 10:12:39 -03:00

214 lines
11 KiB
Python

from gti_fiscal_agent import cfop_ippt_auto_apply as apply_mod
from central.app.field_contract import ALREADY, CHANGE, MANUAL, NA
class FakeFirebird:
"""A tiny C000025: every statement is recorded; only SELECT <field> and the one allowed UPDATE per field are understood."""
def __init__(self, products):
self.products = {c: dict(v) for c, v in products.items()} # code -> {"CFOP": ..., "IPPT": ...}
self.statements, self.commits, self.rollbacks = [], 0, 0
self._pending = {}
def cursor(self):
return FakeCursor(self)
def commit(self):
for (code, field), value in self._pending.items():
self.products[code][field] = value
self._pending = {}
self.commits += 1
def rollback(self):
self._pending = {}
self.rollbacks += 1
class FakeCursor:
def __init__(self, db):
self.db, self._rows, self.rowcount = db, [], -1
def fetchall(self):
return self._rows
def fetchone(self):
return self._rows[0] if self._rows else None
def execute(self, sql, params=()):
db = self.db
db.statements.append((sql, tuple(params)))
text = " ".join(sql.split())
up = text.upper()
for field in ("CFOP", "IPPT"):
if up == f"SELECT {field} FROM C000025 WHERE CODIGO = ?":
code = params[0]
self._rows = [(db.products[code][field],)] if code in db.products else []
if code in db.products and db.products[code].get("DUP"):
self._rows = self._rows * 2
return
if up.startswith(f"UPDATE C000025 SET {field} = ? WHERE CODIGO = ? AND"):
proposed, code, expected = params
product = db.products.get(code)
current = (product.get(field) if product else None) or None
hit = product is not None and current == (expected or None)
self.rowcount = 1 if hit else 0
if hit:
db._pending[(code, field)] = proposed
return
raise AssertionError(f"unexpected SQL: {sql!r}")
def candidate(code, field, expected_before, proposed, reason="X"):
return {"product_code": code, "field": field, "expected_before": expected_before, "proposed": proposed, "reason": reason,
"fingerprint": apply_mod.candidate_fingerprint(code, field, expected_before, proposed)}
def results_for(code, field, *, state, current, proposed, write_eligible):
return {code: {field: {"state": state, "current": current, "proposed": proposed, "write_eligible": write_eligible, "reason": "R"}}}
# ------------------------------------------------------------------ write_gate (flag semantics)
def test_write_gate_default_off_blocks_every_mode_but_enabled_and_approval_required():
assert apply_mod.write_gate(mode="DISABLED")["allowed"] is False
assert apply_mod.write_gate(mode="DRY_RUN")["allowed"] is False
assert apply_mod.write_gate(mode="APPROVAL_REQUIRED", approved=False)["allowed"] is False
assert apply_mod.write_gate(mode="APPROVAL_REQUIRED", approved=True)["allowed"] is True
assert apply_mod.write_gate(mode="ENABLED")["allowed"] is True
assert apply_mod.write_gate(mode="ENABLED", capability=False)["allowed"] is False
assert apply_mod.write_gate(mode="NOT_A_REAL_MODE")["allowed"] is False
# ------------------------------------------------------------------ select_candidates only picks AUTO_FILL_SAFE CFOP/IPPT
def test_select_candidates_only_change_and_write_eligible_cfop_or_ippt():
results = {
"000001": {"CFOP": {"state": CHANGE, "current": "5102", "proposed": "5101", "write_eligible": True, "reason": "R1"},
"IPPT": {"state": MANUAL, "current": None, "proposed": None, "write_eligible": False, "reason": "R2"},
"CSOSN": {"state": CHANGE, "current": None, "proposed": "102", "write_eligible": True, "reason": "R3"}}, # not CFOP/IPPT: never a candidate
"000002": {"CFOP": {"state": ALREADY, "current": "5102", "proposed": None, "write_eligible": False, "reason": "R4"},
"IPPT": {"state": CHANGE, "current": "T", "proposed": "P", "write_eligible": False, "reason": "R5"}}, # write_eligible=False: never a candidate
"000003": {"IPPT": {"state": CHANGE, "current": "T", "proposed": "P", "write_eligible": True, "reason": "R6"}},
}
cands, total = apply_mod.select_candidates(results)
assert total == 2
keys = {(c["product_code"], c["field"]) for c in cands}
assert keys == {("000001", "CFOP"), ("000003", "IPPT")}
assert all(c["field"] in apply_mod.ALLOWED_FIELDS for c in cands)
def test_select_candidates_respects_limit():
results = {f"{i:06d}": {"CFOP": {"state": CHANGE, "current": None, "proposed": "5102", "write_eligible": True, "reason": "R"}} for i in range(5)}
cands, total = apply_mod.select_candidates(results, limit=2)
assert total == 5 and len(cands) == 2
# ------------------------------------------------------------------ apply_candidate: happy path, idempotency, stale guard
def test_apply_candidate_writes_verifies_and_commits():
db = FakeFirebird({"000001": {"CFOP": "5102"}})
c = candidate("000001", "CFOP", "5102", "5101")
rec = apply_mod.apply_candidate(db, c, now="t0", job_id=1)
assert rec["outcome"] == "APPLIED" and rec["old_value"] == "5102" and rec["new_value"] == "5101" and rec["verified_after"] is True
assert db.products["000001"]["CFOP"] == "5101" and db.commits >= 1 and db.rollbacks == 0
def test_second_run_over_the_same_candidate_writes_zero_no_write_required():
db = FakeFirebird({"000001": {"CFOP": "5101"}}) # already at the proposed value (as if a first pass already applied it)
c = candidate("000001", "CFOP", "5102", "5101")
try:
apply_mod.apply_candidate(db, c, now="t0", job_id=1)
assert False, "expected CfopIpptApplyAborted"
except apply_mod.CfopIpptApplyAborted as e:
assert e.reason == "NO_WRITE_REQUIRED"
assert db.products["000001"]["CFOP"] == "5101" # unchanged, no write attempted
def test_human_override_wins_stale_value_aborts_without_writing():
db = FakeFirebird({"000001": {"CFOP": "9999"}}) # a human (or anything else) set a THIRD value since the candidate was selected
c = candidate("000001", "CFOP", "5102", "5101") # candidate still expects the OLD "5102"
try:
apply_mod.apply_candidate(db, c, now="t0", job_id=1)
assert False, "expected CfopIpptApplyAborted"
except apply_mod.CfopIpptApplyAborted as e:
assert e.reason == "STALE_VALUE_CHANGED_SINCE_DECISION"
assert db.products["000001"]["CFOP"] == "9999" # the human's value is preserved, never overwritten
def test_apply_candidate_blank_expected_before_and_fill():
db = FakeFirebird({"000801": {"CFOP": None}})
c = candidate("000801", "CFOP", None, "5102")
rec = apply_mod.apply_candidate(db, c, now="t0", job_id=1)
assert rec["outcome"] == "APPLIED" and rec["old_value"] is None and rec["new_value"] == "5102"
def test_apply_candidate_product_not_found_or_duplicate_row_aborts():
db = FakeFirebird({})
try:
apply_mod.apply_candidate(db, candidate("999999", "CFOP", None, "5102"), now="t0")
assert False
except apply_mod.CfopIpptApplyAborted as e:
assert e.reason == "PRODUCT_NOT_FOUND"
db2 = FakeFirebird({"000001": {"CFOP": "5102", "DUP": True}})
try:
apply_mod.apply_candidate(db2, candidate("000001", "CFOP", "5102", "5101"), now="t0")
assert False
except apply_mod.CfopIpptApplyAborted as e:
assert e.reason == "PRODUCT_ROW_NOT_UNIQUE"
# ------------------------------------------------------------------ apply_candidates: batch semantics, authorize() gate, flag OFF => zero writes
def test_apply_candidates_batch_never_stops_on_one_abort_and_reports_reasons():
db = FakeFirebird({"000001": {"CFOP": "5102"}, "000002": {"CFOP": "9999"}})
cands = [candidate("000001", "CFOP", "5102", "5101"), candidate("000002", "CFOP", "5102", "5101")]
out = apply_mod.apply_candidates(db, cands, now="t0", job_id=1)
assert out["write_successes"] == 1 and out["write_attempts"] == 2
assert out["aborted_reasons"] == {"STALE_VALUE_CHANGED_SINCE_DECISION": 1}
assert db.products["000001"]["CFOP"] == "5101" and db.products["000002"]["CFOP"] == "9999"
def test_flag_off_means_caller_never_invokes_apply_zero_writes():
"""The Agent must gate BEFORE calling apply_candidates at all when write_gate(...).allowed is False -
this is the actual "flag OFF => zero writes" contract; apply_candidates itself is only ever reached
once the gate already said yes."""
gate = apply_mod.write_gate(mode="DISABLED")
assert not gate["allowed"]
db = FakeFirebird({"000001": {"CFOP": "5102"}})
# execute_job's real code path: `if gate["allowed"]: apply_candidates(...)` - simulate the guard directly.
if gate["allowed"]:
apply_mod.apply_candidates(db, [candidate("000001", "CFOP", "5102", "5101")], now="t0")
assert db.products["000001"]["CFOP"] == "5102" and db.commits == 0
def test_authorize_callback_can_veto_a_candidate_right_before_the_write():
db = FakeFirebird({"000001": {"CFOP": "5102"}})
out = apply_mod.apply_candidates(db, [candidate("000001", "CFOP", "5102", "5101")], now="t0", authorize=lambda code, field: "AUTHORIZATION_CHANGED")
assert out["write_successes"] == 0 and out["aborted_reasons"] == {"AUTHORIZATION_CHANGED": 1}
assert db.products["000001"]["CFOP"] == "5102"
# ------------------------------------------------------------------ rollback: only if the value still equals what was written
def test_rollback_restores_old_value_when_unchanged():
db = FakeFirebird({"000001": {"CFOP": "5101"}})
item = {"product_code": "000001", "field": "CFOP", "old_value": "5102", "new_value": "5101", "audit_id": 42}
rec = apply_mod.rollback_one(db, item, now="t1", job_id=1)
assert rec["outcome"] == "ROLLED_BACK" and db.products["000001"]["CFOP"] == "5102"
def test_rollback_refuses_when_a_human_changed_the_value_since():
db = FakeFirebird({"000001": {"CFOP": "7777"}}) # someone changed it after the write, before the rollback
item = {"product_code": "000001", "field": "CFOP", "old_value": "5102", "new_value": "5101", "audit_id": 42}
rec = apply_mod.rollback_one(db, item, now="t1", job_id=1)
assert rec["outcome"] == "ABORTED" and rec["reason"] == "ROLLBACK_CONFLICT_VALUE_CHANGED"
assert db.products["000001"]["CFOP"] == "7777" # untouched
def test_rollback_records_batch():
db = FakeFirebird({"000001": {"CFOP": "5101"}, "000002": {"IPPT": "P"}})
items = [{"product_code": "000001", "field": "CFOP", "old_value": "5102", "new_value": "5101"},
{"product_code": "000002", "field": "IPPT", "old_value": "T", "new_value": "P"}]
out = apply_mod.rollback_records(db, items, now="t1", job_id=1)
assert out["rolled_back"] == 2