{"record":{"id":"aa5758960f596179","repo":"Hmbown/CodeWhale","slug":"attempt-receipt-identity-does-not-match-its-terminal-event","errorCode":null,"errorMessage":"attempt receipt identity does not match its terminal event","messagePattern":"attempt receipt identity does not match its terminal event","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/fleet/ledger.rs","lineNumber":979,"sourceCode":"            FleetWorkerEventPayload::Completed { .. }\n                | FleetWorkerEventPayload::Failed { .. }\n                | FleetWorkerEventPayload::Cancelled { .. }\n        ) {\n            bail!(\"attempt finalization requires a terminal worker event\");\n        }\n        if final_status.is_some_and(|status| {\n            !matches!(\n                status,\n                FleetTaskLedgerStatus::Completed\n                    | FleetTaskLedgerStatus::Failed\n                    | FleetTaskLedgerStatus::Cancelled\n            )\n        }) {\n            bail!(\"attempt finalization status must be terminal\");\n        }\n        if receipt.run_id != *run_id || receipt.task_id != task_id || receipt.worker_id != worker_id\n        {\n            bail!(\"attempt receipt identity does not match its terminal event\");\n        }\n        if receipt\n            .attempt\n            .is_some_and(|attempt| attempt != expected_attempts)\n        {\n            bail!(\"attempt receipt generation does not match its lease\");\n        }\n        self.with_write_lock(|| {\n            let state = self.rebuild_state_unlocked()?;\n            let key = task_key(&run_id.0, task_id);\n            let Some(task) = state.tasks.get(&key) else {\n                return Ok(None);\n            };\n            if task.status != FleetTaskLedgerStatus::Leased\n                || task.leased_to.as_deref() != Some(worker_id)\n                || task.entry.attempts != expected_attempts\n            {\n                return Ok(None);","sourceCodeStart":961,"sourceCodeEnd":997,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/fleet/ledger.rs#L961-L997","documentation":"FleetLedger::finalize_task_attempt_if_leased binds the receipt to the terminal event it accompanies. The guard at crates/tui/src/fleet/ledger.rs:979 rejects the call when the receipt's run_id, task_id, or worker_id does not match the run/task/worker arguments of the call. A mismatched receipt would attach evidence from one attempt (or another task entirely) to a different attempt's terminal record.","triggerScenarios":"Calling finalize_task_attempt_if_leased with a FleetReceipt built for a different run, task, or worker — e.g. a cached receipt from a previous attempt, a receipt copied across workers, or arguments swapped at the call site.","commonSituations":"Reusing a stored receipt after the task was restarted on another worker; aggregating receipts in a map keyed by the wrong id; a driver loop that holds a stale receipt while task/worker identifiers were rebound.","solutions":["Rebuild the FleetReceipt from the current attempt's run_id/task_id/worker_id before finalizing.","Check the receipt fields against the call arguments and regenerate it when they diverge.","Key receipt storage by (run_id, task_id, worker_id) so the wrong receipt cannot be fetched."],"exampleFix":"// before\nlet receipt = old_receipt; // built for another attempt\nledger.finalize_task_attempt_if_leased(&run_id, &worker_id, &task_id, attempts, &ts, payload, status, receipt)?;\n// after\nassert_eq!(old_receipt.run_id, *run_id);\nassert_eq!(old_receipt.task_id, task_id);\nassert_eq!(old_receipt.worker_id, worker_id);\nledger.finalize_task_attempt_if_leased(&run_id, &worker_id, &task_id, attempts, &ts, payload, status, old_receipt)?;","handlingStrategy":"validation","validationCode":"assert_eq!(receipt.run_id, *run_id);\nassert_eq!(receipt.task_id, task_id);\nassert_eq!(receipt.worker_id, worker_id);","typeGuard":null,"tryCatchPattern":"if let Err(e) = finalize_result {\n    if e.to_string().contains(\"receipt identity does not match\") { /* rebuild receipt for this attempt */ }\n}","preventionTips":["Store receipts keyed by (run_id, task_id, worker_id) so lookups cannot cross attempts.","Rebuild receipts at finalize time from current identifiers instead of caching them.","Never copy a receipt across loop iterations that rebind task/worker variables."],"tags":["fleet","ledger","identity-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-22T06:17:15.046Z"}