// K2331-K2335: editor.undo — Bounded undo redo geçmişi // Invariant: Geçmiş quota dışında büyümez // Positive: On ekle sil geri al // Counterexample: Bin büyük edit adımı // Acceptance: Quota tanısı ve güncel metin korunur pub const EDITOR_UNDO_SCHEMA: &str = "turkos.os.editor.undo.v1"; pub const EDITOR_UNDO_INVARIANT: &str = "Geçmiş quota dışında büyümez"; pub const EDITOR_UNDO_POSITIVE: &str = "On ekle sil geri al"; pub const EDITOR_UNDO_COUNTEREXAMPLE: &str = "Bin büyük edit adımı"; pub const EDITOR_UNDO_ACCEPTANCE: &str = "Quota tanısı ve güncel metin korunur"; pub const EXPECTED_BOUNDED_UNDO_STEPS: u32 = 10; pub const EXPECTED_HISTORY_QUOTA_SLOTS: u32 = 16; pub const EXPECTED_QUOTA_DIAGNOSTICS_EMITTED: u32 = 1; pub const EXPECTED_UNBOUNDED_HISTORY_OVERFLOWS: u32 = 0; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum EditorUndoError { ThousandLargeEditStepsUnbounded = 1, HistoryGrewBeyondQuota = 2, QuotaDiagnosticOrCurrentTextLost = 3, BoundedUndoStepsMismatch = 4, HistoryQuotaSlotsMismatch = 5, QuotaDiagnosticsEmittedMismatch = 6, ZeroUndoEpoch = 7, NonMonotonicUndoEpoch = 8, ZeroUndoDigest = 9, UnboundEditorUndoDigest = 10, AlreadyCommitted = 11, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct EditorUndoRequest { pub thousand_large_edit_steps_unbounded: bool, pub history_grew_beyond_quota: bool, pub quota_diagnostic_and_current_text_preserved: bool, pub bounded_undo_steps: u32, pub history_quota_slots: u32, pub quota_diagnostics_emitted: u32, pub undo_epoch: u64, pub previous_undo_epoch: u64, pub undo_digest: u64, pub undo_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct EditorUndoReceipt { pub bounded_undo_steps: u32, pub history_quota_slots: u32, pub quota_diagnostics_emitted: u32, pub unbounded_history_overflows: u32, pub quota_diagnostic_and_current_text_preserved: bool, pub undo_epoch: u64, pub undo_digest: u64, pub receipt_digest: u64, } pub const fn compute_editor_undo_binding_digest( bounded_undo_steps: u32, history_quota_slots: u32, quota_diagnostics_emitted: u32, unbounded_history_overflows: u32, undo_epoch: u64, undo_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2335_4544_554e_444f).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (bounded_undo_steps as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (history_quota_slots as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (quota_diagnostics_emitted as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (unbounded_history_overflows as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ undo_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ undo_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct EditorUndoCoordinator { committed: bool, committed_total: u64, rejected_total: u64, quota_guard_refusals: u64, undo_accounting_refusals: u64, committed_receipt: Option, } impl EditorUndoCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, quota_guard_refusals: 0, undo_accounting_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn quota_guard_refusals(&self) -> u64 { self.quota_guard_refusals } pub fn undo_accounting_refusals(&self) -> u64 { self.undo_accounting_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_undo_history( &mut self, req: &EditorUndoRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(EditorUndoError::AlreadyCommitted); } if req.thousand_large_edit_steps_unbounded { self.rejected_total += 1; self.quota_guard_refusals += 1; return Err(EditorUndoError::ThousandLargeEditStepsUnbounded); } if req.history_grew_beyond_quota { self.rejected_total += 1; self.quota_guard_refusals += 1; return Err(EditorUndoError::HistoryGrewBeyondQuota); } if !req.quota_diagnostic_and_current_text_preserved { self.rejected_total += 1; self.quota_guard_refusals += 1; return Err(EditorUndoError::QuotaDiagnosticOrCurrentTextLost); } if req.bounded_undo_steps != EXPECTED_BOUNDED_UNDO_STEPS { self.rejected_total += 1; self.undo_accounting_refusals += 1; return Err(EditorUndoError::BoundedUndoStepsMismatch); } if req.history_quota_slots != EXPECTED_HISTORY_QUOTA_SLOTS { self.rejected_total += 1; self.undo_accounting_refusals += 1; return Err(EditorUndoError::HistoryQuotaSlotsMismatch); } if req.quota_diagnostics_emitted != EXPECTED_QUOTA_DIAGNOSTICS_EMITTED { self.rejected_total += 1; self.undo_accounting_refusals += 1; return Err(EditorUndoError::QuotaDiagnosticsEmittedMismatch); } if req.undo_epoch == 0 { self.rejected_total += 1; return Err(EditorUndoError::ZeroUndoEpoch); } if req.undo_epoch <= req.previous_undo_epoch { self.rejected_total += 1; return Err(EditorUndoError::NonMonotonicUndoEpoch); } if req.undo_digest == 0 { self.rejected_total += 1; return Err(EditorUndoError::ZeroUndoDigest); } let expected_binding = compute_editor_undo_binding_digest( req.bounded_undo_steps, req.history_quota_slots, req.quota_diagnostics_emitted, EXPECTED_UNBOUNDED_HISTORY_OVERFLOWS, req.undo_epoch, req.undo_digest, ); if req.undo_binding_digest != expected_binding { self.rejected_total += 1; self.undo_accounting_refusals += 1; return Err(EditorUndoError::UnboundEditorUndoDigest); } self.committed = true; self.committed_total += 1; let receipt = EditorUndoReceipt { bounded_undo_steps: req.bounded_undo_steps, history_quota_slots: req.history_quota_slots, quota_diagnostics_emitted: req.quota_diagnostics_emitted, unbounded_history_overflows: EXPECTED_UNBOUNDED_HISTORY_OVERFLOWS, quota_diagnostic_and_current_text_preserved: true, undo_epoch: req.undo_epoch, undo_digest: req.undo_digest, receipt_digest: expected_binding ^ 0x2335_ACE0_554e_444f, }; self.committed_receipt = Some(receipt); Ok(receipt) } }