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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! `ReminderManager` — business logic layer over a `ReminderStore`.
//!
//! All state transitions, schedule advancement, and permission checks
//! live here so the store layer stays a plain CRUD surface.
use std::sync::Arc;
use chrono::Utc;
use crate::error::ReminderError;
use crate::store::ReminderStore;
use crate::types::{
CreateReminder, ExecutionRecord, ExecutionResult, Reminder, ReminderFilter, ReminderStatus,
};
/// How many times a read-modify-write re-reads a reminder that changed under
/// it before giving up.
const MAX_UPDATE_ATTEMPTS: usize = 32;
fn write_conflict(id: &str) -> ReminderError {
ReminderError::Store(format!(
"write conflict: reminder {id} kept changing; gave up after {MAX_UPDATE_ATTEMPTS} attempts"
))
}
pub struct ReminderManager {
store: Arc<dyn ReminderStore>,
}
impl ReminderManager {
pub fn new(store: Arc<dyn ReminderStore>) -> Self {
Self { store }
}
// -----------------------------------------------------------------------
// Create / read
// -----------------------------------------------------------------------
/// Create and persist a new reminder. Returns the fully-populated record.
pub fn create(&self, input: CreateReminder) -> Result<Reminder, ReminderError> {
let reminder = input.into_reminder();
self.store.save(&reminder)?;
Ok(reminder)
}
/// Retrieve a reminder by ID.
pub fn get(&self, id: &str) -> Result<Reminder, ReminderError> {
self.store
.get(id)?
.ok_or_else(|| ReminderError::NotFound(id.to_string()))
}
// -----------------------------------------------------------------------
// List / remind_me
// -----------------------------------------------------------------------
/// Return all reminders matching `filter`.
pub fn list(&self, filter: &ReminderFilter) -> Result<Vec<Reminder>, ReminderError> {
self.store.list(filter)
}
/// Return all reminders that are currently actionable: status `Due` or
/// `AwaitingPermission`, ordered by priority then `due_at`.
///
/// Also promotes `Pending` reminders whose `due_at <= now` to `Due`
/// (lazy promotion — avoids the need for a background timer).
pub fn remind_me(&self) -> Result<Vec<Reminder>, ReminderError> {
let now = Utc::now();
// Promote any pending reminders whose due_at has passed.
let pending = self.store.list(&ReminderFilter {
status: Some(ReminderStatus::Pending),
..Default::default()
})?;
for read in pending {
if read.due_at <= now {
// Non-autonomous reminders require approval before they become
// actionable — route them to AwaitingPermission, not straight
// to Due. (Autonomous ones are pre-approved.)
let mut r = read.clone();
r.status = if r.autonomous {
ReminderStatus::Due
} else {
ReminderStatus::AwaitingPermission
};
// If it changed since the list (cancelled, snoozed), leave it:
// writing back this snapshot would revert that change.
self.store.update_if_unchanged(&read, &r)?;
}
}
// Also promote snoozed reminders whose snooze has expired.
let snoozed = self.store.list(&ReminderFilter {
status: Some(ReminderStatus::Snoozed),
..Default::default()
})?;
for read in snoozed {
let wake = read.snoozed_until.unwrap_or(read.due_at);
if wake <= now {
// Same approval gate on wake: a non-autonomous reminder must be
// re-approved before it can run again.
let mut r = read.clone();
r.status = if r.autonomous {
ReminderStatus::Due
} else {
ReminderStatus::AwaitingPermission
};
r.snoozed_until = None;
self.store.update_if_unchanged(&read, &r)?;
}
}
// Now return all actionable reminders.
let mut due = self.store.list(&ReminderFilter {
status: Some(ReminderStatus::Due),
..Default::default()
})?;
let mut awaiting = self.store.list(&ReminderFilter {
status: Some(ReminderStatus::AwaitingPermission),
..Default::default()
})?;
due.append(&mut awaiting);
due.sort_by(|a, b| a.priority.cmp(&b.priority).then(a.due_at.cmp(&b.due_at)));
Ok(due)
}
// -----------------------------------------------------------------------
// Lifecycle mutations
// -----------------------------------------------------------------------
/// Snooze a reminder until `until`. Valid from `Due`, `AwaitingPermission`,
/// or `InProgress` states.
pub fn snooze(
&self,
id: &str,
until: chrono::DateTime<Utc>,
) -> Result<Reminder, ReminderError> {
self.modify(id, |r| {
if r.status.is_terminal() {
return Err(ReminderError::AlreadyTerminal(id.to_string(), r.status));
}
r.status = ReminderStatus::Snoozed;
r.snoozed_until = Some(until);
Ok(())
})
}
/// Cancel a reminder. Idempotent if already cancelled.
pub fn cancel(&self, id: &str) -> Result<Reminder, ReminderError> {
self.modify(id, |r| {
if r.status == ReminderStatus::Completed {
return Err(ReminderError::AlreadyTerminal(id.to_string(), r.status));
}
r.status = ReminderStatus::Cancelled;
Ok(())
})
}
/// Approve a non-autonomous reminder for execution, recording who approved.
/// Transitions `AwaitingPermission` → `Due`.
pub fn approve(&self, id: &str, approved_by: &str) -> Result<Reminder, ReminderError> {
self.modify(id, |r| {
match r.status {
ReminderStatus::AwaitingPermission => {}
ReminderStatus::Due => {
// Already approved / promoted; record the approver but don't error.
}
s if s.is_terminal() => {
return Err(ReminderError::AlreadyTerminal(id.to_string(), s));
}
s => {
return Err(ReminderError::InvalidTransition {
id: id.to_string(),
from: s,
to: ReminderStatus::Due,
});
}
}
// Stamp the most recent pending execution record with the approver,
// or leave that to record_execution if none exists yet.
r.status = ReminderStatus::Due;
// Store the approved_by on the reminder directly so record_execution
// can pick it up without requiring the caller to pass it again.
// We use a sentinel in the first partial execution record if present.
if let Some(rec) = r.executions.last_mut() {
if rec.completed_at.is_none() {
rec.approved_by = Some(approved_by.to_string());
}
}
Ok(())
})
}
/// Mark a reminder as in-progress. Transitions `Due` → `InProgress`.
/// Non-autonomous reminders must be approved first.
pub fn start(&self, id: &str, agent_id: &str) -> Result<Reminder, ReminderError> {
self.modify(id, |r| {
match r.status {
ReminderStatus::Due => {}
ReminderStatus::AwaitingPermission => {
if !r.autonomous {
return Err(ReminderError::RequiresApproval(id.to_string()));
}
}
s if s.is_terminal() => {
return Err(ReminderError::AlreadyTerminal(id.to_string(), s));
}
s => {
return Err(ReminderError::InvalidTransition {
id: id.to_string(),
from: s,
to: ReminderStatus::InProgress,
});
}
}
// Open a partial execution record.
r.executions.push(crate::types::ExecutionRecord {
started_at: Utc::now(),
completed_at: None,
agent_id: agent_id.to_string(),
approved_by: None,
result: ExecutionResult::Deferred, // placeholder until record_execution
notes: Vec::new(),
task_id: None,
});
r.status = ReminderStatus::InProgress;
Ok(())
})
}
/// Complete an execution attempt, appending the `ExecutionRecord`.
///
/// On `Success`:
/// - If the reminder has a repeating schedule, compute `next_due` and
/// reset to `Pending`.
/// - Otherwise mark `Completed`.
///
/// On non-autonomous reminders that have not yet been approved, this
/// method transitions to `AwaitingPermission` instead of `Due` after
/// schedule advancement.
pub fn record_execution(
&self,
id: &str,
record: ExecutionRecord,
) -> Result<Reminder, ReminderError> {
self.modify(id, |r| {
if r.status.is_terminal() {
return Err(ReminderError::AlreadyTerminal(id.to_string(), r.status));
}
let completed_at = Utc::now();
let mut record = record.clone();
record.completed_at = Some(completed_at);
// If start() opened a partial record, update it in-place; otherwise push.
match r.executions.last_mut() {
Some(last) if last.completed_at.is_none() => {
last.completed_at = record.completed_at;
last.result = record.result.clone();
last.notes = record.notes.clone();
last.task_id = record.task_id.clone();
if record.approved_by.is_some() {
last.approved_by = record.approved_by.clone();
}
}
_ => r.executions.push(record.clone()),
}
match &record.result {
ExecutionResult::Success => {
match r.schedule.as_ref().and_then(|s| s.next_due(completed_at)) {
Some(next) => {
r.due_at = next;
r.snoozed_until = None;
r.status = if r.autonomous {
ReminderStatus::Pending
} else {
ReminderStatus::AwaitingPermission
};
}
None => {
r.status = ReminderStatus::Completed;
}
}
}
ExecutionResult::Snoozed => {
// Snooze was set separately via snooze(); just record.
if r.status != ReminderStatus::Snoozed {
r.status = ReminderStatus::Snoozed;
}
}
ExecutionResult::Cancelled => {
r.status = ReminderStatus::Cancelled;
}
ExecutionResult::Failed | ExecutionResult::Deferred => {
// Leave status as-is (InProgress or Due); caller decides next step.
if r.status == ReminderStatus::InProgress {
r.status = ReminderStatus::Due;
}
}
}
Ok(())
})
}
/// Mark a ref as stale by `ref_id`. Returns the number of refs updated.
pub fn mark_ref_stale(&self, id: &str, ref_id: &str) -> Result<usize, ReminderError> {
for _ in 0..MAX_UPDATE_ATTEMPTS {
let read = self.get(id)?;
let mut r = read.clone();
let mut count = 0;
for rf in r.refs.iter_mut() {
if rf.id == ref_id && !rf.stale {
rf.stale = true;
count += 1;
}
}
if count == 0 || self.store.update_if_unchanged(&read, &r)? {
return Ok(count);
}
}
Err(write_conflict(id))
}
/// Read-modify-write one reminder without losing a concurrent change:
/// read, apply `change` to a copy, and write it only if the stored record
/// still matches what was read; otherwise re-read and re-apply (which also
/// re-validates the transition). Every lifecycle method used to write back
/// its read unconditionally, so a concurrent cancel, snooze, or execution
/// record was silently reverted while both calls returned Ok.
fn modify<F>(&self, id: &str, mut change: F) -> Result<Reminder, ReminderError>
where
F: FnMut(&mut Reminder) -> Result<(), ReminderError>,
{
for _ in 0..MAX_UPDATE_ATTEMPTS {
let read = self.get(id)?;
let mut r = read.clone();
change(&mut r)?;
if self.store.update_if_unchanged(&read, &r)? {
return Ok(r);
}
}
Err(write_conflict(id))
}
}