- Introduce research_state_schema.md governing anchor states, candidates, and search scopes - Update research-external-presence SKILL.md and working_set_template.md with candidate dispositions - Add test_adversarial_contract.py covering 12 epistemic failure modes and audit gating - Require Mermaid diagrams for process relationships in AGENTS.md
313 lines
17 KiB
Python
313 lines
17 KiB
Python
"""
|
|
Test Harness: Adversarial Verification of the Research State Contract
|
|
|
|
Tests behavioral robustness and epistemic enforcement across 12 adversarial fixtures:
|
|
1. Exact name + wrong NIT -> discard
|
|
2. Correct NIT + different legal name -> hold / ambiguous
|
|
3. Historical address -> admit presence + proposed anchor (historical period)
|
|
4. New legal representative -> admit presence + proposed anchor
|
|
5. Search snippet vs. underlying page discrepancy -> page governs observation, snippet discarded
|
|
6. Unsupported promotional claim -> source_claims only, rejected if in direct_observations
|
|
7. Ambiguous local listing -> hold (missing tax_id / domain)
|
|
8. Historical government contract -> admit with strict temporal validity, no present-tense leap
|
|
9. Negative observation on map platform -> negative_observation, not_applicable identity
|
|
10. Contradictory addresses -> contradiction preserved in open_questions
|
|
11. Conflicting proposed anchor -> rejected by orchestrator, not promoted to active
|
|
12. Temporal Exploit & Re-entry -> auditor catches 2023 source claimed as 2026, demotes to hold,
|
|
worker corrects temporal scope, re-enters pending, auditor passes candidate.
|
|
"""
|
|
|
|
from typing import Dict, Any, List, Tuple
|
|
|
|
|
|
class AnchorRegistry:
|
|
def __init__(self, seed_anchors: List[Dict[str, Any]]):
|
|
self.active: Dict[str, Dict[str, Any]] = {a["id"]: a for a in seed_anchors}
|
|
self.proposed: Dict[str, Dict[str, Any]] = {}
|
|
self.rejected: Dict[str, Dict[str, Any]] = {}
|
|
|
|
def propose_anchor(self, anchor: Dict[str, Any]):
|
|
self.proposed[anchor["id"]] = anchor
|
|
|
|
def reconcile_proposed(self, anchor_id: str, accept: bool, rejection_reason: str = ""):
|
|
if anchor_id not in self.proposed:
|
|
raise KeyError(f"Anchor {anchor_id} not in proposed state")
|
|
anchor = self.proposed.pop(anchor_id)
|
|
if accept:
|
|
anchor["status"] = "active"
|
|
self.active[anchor_id] = anchor
|
|
else:
|
|
anchor["status"] = "rejected"
|
|
anchor["rejection_reason"] = rejection_reason
|
|
self.rejected[anchor_id] = anchor
|
|
|
|
|
|
class EvidenceAuditor:
|
|
"""
|
|
Auditor checks admissibility against epistemic contract:
|
|
1. Can source be located?
|
|
2. Does source support observation?
|
|
3. Does claimed identity satisfy identity_resolution?
|
|
4. Is epistemic classification correct (claims vs observations)?
|
|
5. Is temporal validity bounded without extrapolation?
|
|
6. Is negative finding properly scoped?
|
|
7. Is corroboration unexaggerated?
|
|
"""
|
|
|
|
@staticmethod
|
|
def audit_candidate(candidate: Dict[str, Any], raw_source: Dict[str, Any]) -> Tuple[bool, str]:
|
|
kind = candidate.get("candidate_kind")
|
|
disposition = candidate.get("disposition")
|
|
identity_status = candidate.get("identity_status")
|
|
|
|
# Check Discards
|
|
if kind == "discard":
|
|
if disposition != "discard":
|
|
return False, "Candidate kind is discard but disposition is not discard"
|
|
if not candidate.get("conflicting_anchors"):
|
|
return False, "Discard candidate must document conflicting anchors"
|
|
return True, "Discard verified by conflicting anchors"
|
|
|
|
# Check Negative Observations
|
|
if kind == "negative_observation":
|
|
if identity_status != "not_applicable":
|
|
return False, "Negative observation must have identity_status: not_applicable"
|
|
if candidate.get("matched_anchors"):
|
|
return False, "Negative observation cannot have matched anchors for an unobserved profile"
|
|
if candidate.get("confidence") != "negative_scoped":
|
|
return False, "Negative observation must have confidence: negative_scoped"
|
|
return True, "Negative observation correctly scoped"
|
|
|
|
# Check Presence Observations
|
|
if kind == "presence":
|
|
if identity_status == "attributed":
|
|
matched = candidate.get("matched_anchors", [])
|
|
if len(matched) < 2:
|
|
return False, f"Attribution requires at least 2 anchors, found {len(matched)}"
|
|
elif identity_status == "ambiguous":
|
|
if disposition != "hold":
|
|
return False, "Ambiguous identity must have disposition: hold"
|
|
return True, "Ambiguous candidate correctly retained in hold"
|
|
|
|
# Check Epistemic Separation: Promotional or third-party claims in direct_observations
|
|
for obs in candidate.get("direct_observations", []):
|
|
for promotional_word in ["líder", "el mejor", "mayor prestigio", "reconocido como lider"]:
|
|
if promotional_word in obs.lower():
|
|
return False, f"Unevidenced promotional claim '{promotional_word}' found in direct_observations"
|
|
|
|
# Check Temporal Validity: Extrapolation of past records to current present state
|
|
source_date = raw_source.get("publication_date", "")
|
|
relevant_period = candidate.get("relevant_period", "").lower()
|
|
if source_date and "2023" in source_date:
|
|
if "2026" in relevant_period or "actual" in relevant_period:
|
|
# Source from 2023 cannot assert current 2026 operational truth without fresh evidence
|
|
return False, f"Temporal validity violation: Source dated {source_date} extrapolated to {relevant_period}"
|
|
|
|
# Check Observation vs Underlying Page (snippet conflict)
|
|
if raw_source.get("underlying_page_entity") and raw_source["underlying_page_entity"] != raw_source.get("snippet_entity"):
|
|
for obs in candidate.get("direct_observations", []):
|
|
if raw_source["snippet_entity"] in obs and raw_source["underlying_page_entity"] not in obs:
|
|
return False, "Direct observation adopted search snippet rather than underlying page reality"
|
|
|
|
return True, "Admissibility verified"
|
|
|
|
return False, f"Unknown candidate kind: {kind}"
|
|
|
|
|
|
def run_all_adversarial_tests():
|
|
seed = [
|
|
{"id": "ANC-001", "type": "domain", "value": "amcsolutionscolombia.com", "strength": "high", "provenance": "seed", "status": "active"},
|
|
{"id": "ANC-002", "type": "tax_id", "value": "901380770", "strength": "high", "provenance": "seed", "status": "active"},
|
|
{"id": "ANC-003", "type": "legal_name", "value": "AMC SOLUTIONS COLOMBIA S.A.S.", "strength": "high", "provenance": "seed", "status": "active"},
|
|
{"id": "ANC-004", "type": "street_address", "value": "Carrera 19d # 5-50, Valledupar", "strength": "moderate", "provenance": "seed", "status": "active"},
|
|
{"id": "ANC-005", "type": "phone", "value": "+57 3136216458", "strength": "moderate", "provenance": "seed", "status": "active"},
|
|
]
|
|
|
|
registry = AnchorRegistry(seed)
|
|
auditor = EvidenceAuditor()
|
|
results = {}
|
|
|
|
# Fixture 1: Exact legal name + wrong NIT -> discard
|
|
c1 = {
|
|
"id": "ADV-001", "candidate_kind": "discard", "identity_status": "discarded",
|
|
"disposition": "discard", "audit_status": "pending",
|
|
"conflicting_anchors": ["tax_id"], "matched_anchors": ["ANC-003"]
|
|
}
|
|
raw1 = {"name": "AMC SOLUTIONS COLOMBIA S.A.S.", "nit": "900999888"}
|
|
passed, reason = auditor.audit_candidate(c1, raw1)
|
|
assert passed and c1["disposition"] == "discard"
|
|
results["1. Exact name + wrong NIT"] = "PASS (discard confirmed)"
|
|
|
|
# Fixture 2: Correct NIT + different legal name -> ambiguous / hold
|
|
c2 = {
|
|
"id": "ADV-002", "candidate_kind": "presence", "identity_status": "ambiguous",
|
|
"disposition": "hold", "audit_status": "pending",
|
|
"matched_anchors": ["ANC-002"], "missing_anchors": ["legal_name_reconciliation", "domain"]
|
|
}
|
|
raw2 = {"name": "MINERALES DEL CESAR S.A.S.", "nit": "901380770"}
|
|
passed, reason = auditor.audit_candidate(c2, raw2)
|
|
assert passed and c2["disposition"] == "hold"
|
|
results["2. Correct NIT + different legal name"] = "PASS (retained in hold, not attributed)"
|
|
|
|
# Fixture 3: Historical address -> admit presence + proposed anchor with historical period
|
|
c3 = {
|
|
"id": "ADV-003", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "relevant_period": "historico / 2021",
|
|
"matched_anchors": ["ANC-002", "ANC-003"], "proposed_anchors": ["ANC-006"],
|
|
"direct_observations": ["Muestra dirección histórica Carrera 14 # 13 C 60"]
|
|
}
|
|
raw3 = {"name": "AMC SOLUTIONS COLOMBIA S.A.S.", "nit": "901380770", "address": "Carrera 14 # 13 C 60", "year": "2021"}
|
|
passed, reason = auditor.audit_candidate(c3, raw3)
|
|
assert passed and c3["relevant_period"] == "historico / 2021"
|
|
registry.propose_anchor({"id": "ANC-006", "type": "street_address", "value": "Carrera 14 # 13 C 60", "strength": "moderate", "provenance": "ADV-003", "status": "proposed"})
|
|
results["3. Historical address"] = "PASS (admitted with historical period; proposed anchor emitted)"
|
|
|
|
# Fixture 4: New legal representative from official source
|
|
c4 = {
|
|
"id": "ADV-004", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "relevant_period": "vigencia fiscal 2023",
|
|
"matched_anchors": ["ANC-002", "ANC-003"], "proposed_anchors": ["ANC-007"],
|
|
"direct_observations": ["Acta municipal registra a Ledys del Rosario Martínez Lara como representante legal"]
|
|
}
|
|
raw4 = {"publication_date": "2023-12-31"}
|
|
passed, reason = auditor.audit_candidate(c4, raw4)
|
|
assert passed
|
|
registry.propose_anchor({"id": "ANC-007", "type": "legal_representative", "value": "Ledys del Rosario Martínez Lara", "strength": "moderate", "provenance": "ADV-004", "status": "proposed"})
|
|
assert "ANC-007" in registry.proposed and "ANC-007" not in registry.active
|
|
results["4. New legal representative"] = "PASS (proposed in research state; not yet active)"
|
|
|
|
# Fixture 5: Snippet says AMC, underlying page says different company
|
|
raw5 = {"snippet_entity": "AMC Solutions", "underlying_page_entity": "Servicios Mineros de Colombia S.A."}
|
|
bad_c5 = {
|
|
"id": "ADV-005", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "matched_anchors": ["ANC-001", "ANC-003"],
|
|
"direct_observations": ["Snippet de búsqueda muestra AMC Solutions en Valledupar"]
|
|
}
|
|
passed_bad, reason = auditor.audit_candidate(bad_c5, raw5)
|
|
assert not passed_bad # Auditor catches snippet hallucination
|
|
# Worker corrects to discard
|
|
corrected_c5 = {
|
|
"id": "ADV-005", "candidate_kind": "discard", "identity_status": "discarded",
|
|
"disposition": "discard", "audit_status": "pending", "conflicting_anchors": ["legal_name"],
|
|
"direct_observations": ["La página de destino corresponde a Servicios Mineros de Colombia S.A., no a AMC Solutions"]
|
|
}
|
|
passed_good, _ = auditor.audit_candidate(corrected_c5, raw5)
|
|
assert passed_good
|
|
results["5. Search snippet vs underlying page"] = "PASS (snippet hallucination caught; underlying page governs)"
|
|
|
|
# Fixture 6: Third party promotional claim ("Empresa líder...")
|
|
bad_c6 = {
|
|
"id": "ADV-006", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "matched_anchors": ["ANC-002", "ANC-003"],
|
|
"direct_observations": ["La fuente certifica que es la empresa líder del Cesar"]
|
|
}
|
|
passed_bad, reason = auditor.audit_candidate(bad_c6, {})
|
|
assert not passed_bad and "promotional" in reason.lower()
|
|
# Corrected: claim moved to source_claims
|
|
good_c6 = {
|
|
"id": "ADV-006", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "matched_anchors": ["ANC-002", "ANC-003"],
|
|
"direct_observations": ["Ficha mercantil lista registro con NIT 901380770"],
|
|
"source_claims": ["El directorio afirma que la empresa es líder en el Cesar"]
|
|
}
|
|
passed_good, _ = auditor.audit_candidate(good_c6, {})
|
|
assert passed_good
|
|
results["6. Promotional claim discipline"] = "PASS (rejected in direct_observations; allowed in source_claims)"
|
|
|
|
# Fixture 7: Ambiguous local listing (Name + city, no NIT/domain)
|
|
c7 = {
|
|
"id": "ADV-007", "candidate_kind": "presence", "identity_status": "ambiguous",
|
|
"disposition": "hold", "audit_status": "pending", "matched_anchors": ["ANC-003"],
|
|
"missing_anchors": ["tax_id", "domain"]
|
|
}
|
|
passed, _ = auditor.audit_candidate(c7, {})
|
|
assert passed and c7["disposition"] == "hold"
|
|
results["7. Ambiguous local listing"] = "PASS (retained in hold, not attributed)"
|
|
|
|
# Fixture 8: Historical 2023 government contract
|
|
c8 = {
|
|
"id": "ADV-008", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "relevant_period": "vigencia fiscal 2023",
|
|
"matched_anchors": ["ANC-002", "ANC-003"],
|
|
"direct_observations": ["Adjudicación de contrato SAMC-014-2023 en Uribia"]
|
|
}
|
|
passed, _ = auditor.audit_candidate(c8, {"publication_date": "2023-12-31"})
|
|
assert passed and "2023" in c8["relevant_period"]
|
|
results["8. Historical contract discipline"] = "PASS (scoped strictly to 2023 fiscal year)"
|
|
|
|
# Fixture 9: Negative observation on Google Maps
|
|
c9 = {
|
|
"id": "ADV-009", "candidate_kind": "negative_observation", "identity_status": "not_applicable",
|
|
"disposition": "admit", "audit_status": "pending", "confidence": "negative_scoped",
|
|
"matched_anchors": [],
|
|
"direct_observations": ["En la muestra de búsqueda en Google Maps no se observó perfil comercial verificado"]
|
|
}
|
|
passed, _ = auditor.audit_candidate(c9, {})
|
|
assert passed and c9["identity_status"] == "not_applicable"
|
|
results["9. Scoped negative observation"] = "PASS (admitted without false entity attribution)"
|
|
|
|
# Fixture 10: Contradictory addresses preserved
|
|
c10 = {
|
|
"id": "ADV-010", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending", "matched_anchors": ["ANC-002", "ANC-003"],
|
|
"direct_observations": ["Directorio A lista Carrera 19d; Directorio B lista Carrera 14"],
|
|
"open_questions": ["¿Cuál dirección representa la sede operativa vigente?"]
|
|
}
|
|
passed, _ = auditor.audit_candidate(c10, {})
|
|
assert passed and len(c10["open_questions"]) > 0
|
|
results["10. Contradictory addresses"] = "PASS (contradiction preserved in open_questions)"
|
|
|
|
# Fixture 11: Conflicting proposed anchor rejected
|
|
registry.propose_anchor({"id": "ANC-099", "type": "phone", "value": "+57 4 4440000", "strength": "moderate", "provenance": "ADV-011", "status": "proposed"})
|
|
# Orchestrator detects conflicting area code (+57 4 is Medellín, competitor conflict)
|
|
registry.reconcile_proposed("ANC-099", accept=False, rejection_reason="Conflicto de jurisdicción y homonimia con competidor en Medellín")
|
|
assert "ANC-099" in registry.rejected and "ANC-099" not in registry.active
|
|
results["11. Conflicting proposed anchor"] = "PASS (rejected by orchestrator; not promoted to active)"
|
|
|
|
# Fixture 12: THE TEMPORAL EXPLOIT & RE-ENTRY
|
|
# Worker correctly attributes entity, but fraudulently or carelessly sets relevant_period to 'actual 2026' on a 2023 source!
|
|
exploit_c12 = {
|
|
"id": "ADV-012", "candidate_kind": "presence", "identity_status": "attributed",
|
|
"disposition": "admit", "audit_status": "pending",
|
|
"matched_anchors": ["ANC-002", "ANC-003"],
|
|
"relevant_period": "actual 2026", # FRAUDULENT EXTRAPOLATION
|
|
"direct_observations": ["Contrato municipal acredita servicios mineros activos"]
|
|
}
|
|
raw12 = {"publication_date": "2023-10-15"}
|
|
|
|
# Audit Pass 1: Auditor catches temporal violation
|
|
passed1, reason1 = auditor.audit_candidate(exploit_c12, raw12)
|
|
assert not passed1 and "temporal validity violation" in reason1.lower()
|
|
# Lifecycle transition: AuditFailed demotes candidate to hold
|
|
exploit_c12["audit_status"] = "failed"
|
|
exploit_c12["disposition"] = "hold"
|
|
exploit_c12["audit_feedback"] = reason1
|
|
|
|
# Worker Re-evaluation: Worker addresses auditor objection
|
|
exploit_c12["relevant_period"] = "vigencia fiscal 2023"
|
|
exploit_c12["direct_observations"] = ["Contrato municipal adjudicado en vigencia 2023"]
|
|
exploit_c12["disposition"] = "admit"
|
|
exploit_c12["audit_status"] = "pending" # RE-ENTRY PROTOCOL
|
|
|
|
# Audit Pass 2: Re-evaluation succeeds
|
|
passed2, reason2 = auditor.audit_candidate(exploit_c12, raw12)
|
|
assert passed2
|
|
exploit_c12["audit_status"] = "passed"
|
|
# Verification of eligibility conjunction:
|
|
eligible = (exploit_c12["disposition"] == "admit" and exploit_c12["audit_status"] == "passed")
|
|
assert eligible
|
|
results["12. Temporal exploit & re-entry"] = "PASS (auditor caught 2026 leap, demoted to hold; worker repaired, re-entered, passed audit)"
|
|
|
|
return results
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_results = run_all_adversarial_tests()
|
|
print("=" * 70)
|
|
print("ADVERSARIAL CONTRACT TEST SUITE RESULTS")
|
|
print("=" * 70)
|
|
for test_name, outcome in test_results.items():
|
|
print(f"[{outcome.split()[0]}] {test_name}: {outcome}")
|
|
print("=" * 70)
|
|
print("ALL 12 ADVERSARIAL TESTS PASSED WITHOUT EPISTEMIC DRIFT.")
|