1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use thiserror::Error;
/// Error classification for FFI consumers (nothing is exposed on the wire
/// except this code — SDK retry loops must not string-match, issue #71/F5).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ErrorKind {
/// No error recorded on this thread.
None = 0,
/// Normal failure — do not retry as-is.
Generic = 1,
/// Retryable without repair: serializable-transaction read-set conflict
/// or an index still being backfilled. Retry with the SAME input after
/// a backoff; the engine state may legitimately have advanced.
RetryableConflict = 2,
/// Engine-enforced constraint violation — retrying unchanged input
/// deterministically fails again; the WRITE is the fix.
UniqueViolation = 3,
/// Constraint target is not comparable against its declared constraint
/// (schema drift) — repair by aligning the write.
ConstraintMismatch = 4,
/// The caller's [`crate::CancelToken`] stopped the operation. Safe to
/// retry with a fresh token if the work is still wanted.
Cancelled = 5,
}
/// Errors returned by the Rust API and classified for FFI consumers.
#[derive(Debug, Error)]
pub enum HakoError {
/// Filesystem or OS failure.
#[error("io error: {0}")]
Io(#[from] std::io::Error),
/// A lock guard was poisoned by a panic in another thread.
#[error("lock poisoned: {0}")] // <--- ADD THIS
LockPoisoned(String),
/// Storage-layer failure (WAL, mmap, or segment file).
#[error("storage error: {0}")]
StorageError(String),
/// Query planner or execution failure.
#[error("query error: {0}")]
QueryError(String),
/// Index creation or lookup failure.
#[error("index error: {0}")]
IndexError(String),
/// Data does not match the expected on-disk contract.
#[error("corrupt data: {0}")]
Corrupt(String),
/// Caller supplied an invalid argument or path.
#[error("invalid input: {0}")]
InvalidInput(String),
/// Serializable-transaction read-set validation failed. A NORMAL
/// concurrency outcome (two writers raced on a read-modify-write),
/// never corruption (issue #71/F5). Retry the whole transaction.
#[error("transaction conflict on `{key}`")]
Conflict {
key: String,
},
/// A constrained (unique) index rejected the write (issue #71/F1).
/// `key` is the offending tuple, repr'd for diagnostics only — do NOT
/// parse it; re-derive from the doc.
#[error("unique index `{index}` on `{collection}` violated by `{key}`")]
UniqueViolation {
collection: String,
index: String,
key: String,
},
/// The target collection of a constrained write is still being
/// backfilled (create-time scan or touch-load); retry after quiescence
/// (`await_quiescent` or the readiness signal).
#[error("index `{index}` on `{collection}` is still backfilling — retry after quiescence")]
IndexBackfilling {
collection: String,
index: String,
},
/// A per-collection schema rejected the write (issue #71/F8): a
/// declared field is missing (and has no default) or has the wrong
/// declared type. Retrying unchanged input deterministically fails.
#[error("schema violation on `{collection}.{field}`: expected {expected}")]
SchemaViolation {
collection: String,
field: String,
expected: String,
},
/// A cooperative [`crate::CancelToken`] stopped the operation.
#[error("operation cancelled")]
Cancelled,
}
impl HakoError {
/// Stable classification for retry policy (Rust) and `hk_error_kind`
/// (C ABI). Never string-match on the message.
pub fn kind(&self) -> ErrorKind {
match self {
HakoError::Conflict { .. } | HakoError::IndexBackfilling { .. } => {
ErrorKind::RetryableConflict
}
HakoError::UniqueViolation { .. } => ErrorKind::UniqueViolation,
HakoError::SchemaViolation { .. } => ErrorKind::ConstraintMismatch,
HakoError::Cancelled => ErrorKind::Cancelled,
_ => ErrorKind::Generic,
}
}
/// True iff a retry of identical input can plausibly succeed (normal
/// backoff; the engine state may have moved on).
pub fn is_retryable(&self) -> bool {
matches!(self.kind(), ErrorKind::RetryableConflict)
}
}
pub type Result<T> = std::result::Result<T, HakoError>;