# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-replay-a-terminal-a2a-task-into-a-diverged-state-after-idempotent-resumption.md # Remnant Warrant: https://corpusfetch.com/c/why-does-an-instance-replay-a-terminal-a2a-task-into-a-diverged-state-after-idempotent-resumption.warrant.json # Markdown SHA-256: b62a3b24af7bf129ba10f7e2fa826cf258e0f6a1377baf19d5877465cfd8af34 # Code AST SHA-256: 4b3300c5b1540f9819f0a17dea0b306e0beba9fcd7555c4bad1f77ee43cffa5d # Verified Classes: ResumptionDivergenceError, TaskState # Defensive Guards: 2 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) class ResumptionDivergenceError(RuntimeError): """Raised when a resuming instance's reconstructed state contradicts the peer's authoritative state.""" pass class TaskState: SUBMITTED = "SUBMITTED" WORKING = "WORKING" COMPLETED = "COMPLETED" FAILED = "FAILED" CANCELED = "CANCELED" RECONCILING = "RECONCILING" TERMINAL = frozenset({COMPLETED, FAILED, CANCELED}) def reconcile_resumption( local_log: list[dict], peer_query, task_id: str, ) -> str: """ Two-phase resumption. Phase 1 reconstructs a *candidate* state from the log, distinguishing OBSERVED records (authoritative for reconstruction) from INTENT records (never promoted without a matching OBSERVED confirmation). Phase 2 queries the peer and treats its answer as ground truth. """ # Phase 1: candidate reconstruction, OBSERVED only. candidate = TaskState.SUBMITTED for rec in local_log: if rec.get("type") != "OBSERVED": continue # INTENT records are excluded from reconstruction candidate = rec["state"] # Phase 2: peer is authoritative. try: peer_state = peer_query(task_id) except TimeoutError: # Cannot confirm; must not fall back to candidate as if it were truth. return TaskState.RECONCILING if peer_state in TaskState.TERMINAL and candidate in TaskState.TERMINAL: if peer_state != candidate: raise ResumptionDivergenceError( f"terminal-state disagreement on {task_id}: " f"local={candidate} peer={peer_state}; " f"refusing to proceed, emitting audit record" ) return peer_state if peer_state in TaskState.TERMINAL: # Peer finished while we were down; adopt peer truth, discard candidate. return peer_state if candidate in TaskState.TERMINAL and peer_state not in TaskState.TERMINAL: # The exact t8 failure: we believe done, peer never saw the terminal # message. Do NOT resend under the original idempotency key — the peer's # dedup layer will drop it. Escalate to a fresh message sequence. raise ResumptionDivergenceError( f"local terminal {candidate} but peer {peer_state} on {task_id}; " f"resend requires fresh messageSeq, not task-level idempotency key" ) return peer_state