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
//! SQLite persistence for sync task status.
//!
//! Persists `TaskStatus<SyncReport>` so that poll() survives
//! MCP server session restarts. On startup, `recover_stale_running()`
//! marks any `running` tasks as `failed` (process crashed).
use chrono::Utc;
use rusqlite::params;
use rusqlite::OptionalExtension;
use crate::application::sdk::SyncReport;
use crate::application::task::{TaskId, TaskStatus};
use crate::infra::error::InfraError;
use super::{map_call_err, SqliteSyncStore};
impl SqliteSyncStore {
/// Insert a new task as Pending.
pub async fn insert_task(&self, id: &TaskId) -> Result<(), InfraError> {
let id_str = id.as_str().to_string();
let now = Utc::now().to_rfc3339();
self.conn
.call(move |conn| {
conn.execute(
"INSERT OR REPLACE INTO sync_tasks \
(task_id, status, phase, created_at, updated_at) \
VALUES (?1, 'pending', '', ?2, ?2)",
params![id_str, now],
)
.map(|_| ())
.map_err(|e| InfraError::Store {
op: "insert_task",
reason: format!("{e}"),
})
})
.await
.map_err(map_call_err)
}
/// Update task status to Running with a phase description.
pub async fn update_task_running(&self, id: &TaskId, phase: &str) -> Result<(), InfraError> {
let id_str = id.as_str().to_string();
let phase = phase.to_string();
let now = Utc::now().to_rfc3339();
self.conn
.call(move |conn| {
conn.execute(
"UPDATE sync_tasks SET status = 'running', phase = ?1, updated_at = ?2 \
WHERE task_id = ?3",
params![phase, now, id_str],
)
.map(|_| ())
.map_err(|e| InfraError::Store {
op: "update_task_running",
reason: format!("{e}"),
})
})
.await
.map_err(map_call_err)
}
/// Update task status to Completed with a serialized SyncReport.
pub async fn update_task_completed(
&self,
id: &TaskId,
report: &SyncReport,
) -> Result<(), InfraError> {
let id_str = id.as_str().to_string();
let json = serde_json::to_string(report).map_err(|e| InfraError::Store {
op: "update_task_completed",
reason: format!("serialize SyncReport: {e}"),
})?;
let now = Utc::now().to_rfc3339();
self.conn
.call(move |conn| {
conn.execute(
"UPDATE sync_tasks SET status = 'completed', result_json = ?1, updated_at = ?2 \
WHERE task_id = ?3",
params![json, now, id_str],
)
.map(|_| ())
.map_err(|e| InfraError::Store {
op: "update_task_completed",
reason: format!("{e}"),
})
})
.await
.map_err(map_call_err)
}
/// Update task status to Failed with an error message.
pub async fn update_task_failed(&self, id: &TaskId, error: &str) -> Result<(), InfraError> {
let id_str = id.as_str().to_string();
let error = error.to_string();
let now = Utc::now().to_rfc3339();
self.conn
.call(move |conn| {
conn.execute(
"UPDATE sync_tasks SET status = 'failed', error = ?1, updated_at = ?2 \
WHERE task_id = ?3",
params![error, now, id_str],
)
.map(|_| ())
.map_err(|e| InfraError::Store {
op: "update_task_failed",
reason: format!("{e}"),
})
})
.await
.map_err(map_call_err)
}
/// Load task status from DB. Returns None if task_id is unknown.
pub async fn load_task(
&self,
id: &TaskId,
) -> Result<Option<TaskStatus<SyncReport>>, InfraError> {
let id_str = id.as_str().to_string();
self.conn
.call(move |conn| {
let mut stmt = conn
.prepare(
"SELECT status, phase, result_json, error \
FROM sync_tasks WHERE task_id = ?1",
)
.map_err(|e| InfraError::Store {
op: "load_task",
reason: format!("{e}"),
})?;
let result = stmt
.query_row(params![id_str], |row| {
let status: String = row.get(0)?;
let phase: String = row.get(1)?;
let result_json: Option<String> = row.get(2)?;
let error: Option<String> = row.get(3)?;
Ok((status, phase, result_json, error))
})
.optional()
.map_err(|e| InfraError::Store {
op: "load_task",
reason: format!("{e}"),
})?;
match result {
None => Ok(None),
Some((status, phase, result_json, error)) => {
let task_status = match status.as_str() {
"pending" => TaskStatus::Pending,
"running" => TaskStatus::Running(phase),
"completed" => {
let report: SyncReport = result_json
.as_deref()
.map(serde_json::from_str)
.transpose()
.map_err(|e| InfraError::Store {
op: "load_task",
reason: format!("deserialize SyncReport: {e}"),
})?
.unwrap_or_default();
TaskStatus::Completed(report)
}
"failed" => TaskStatus::Failed(error.unwrap_or_default()),
other => {
return Err(InfraError::Store {
op: "load_task",
reason: format!("unknown status: {other}"),
});
}
};
Ok(Some(task_status))
}
}
})
.await
.map_err(map_call_err)
}
/// On startup, mark all `running` tasks as `failed`.
///
/// If a task was `running` when the process terminated, it will never
/// reach Completed/Failed. This recovers those zombie tasks.
pub async fn recover_stale_running(&self) -> Result<usize, InfraError> {
let now = Utc::now().to_rfc3339();
self.conn
.call(move |conn| {
let count = conn
.execute(
"UPDATE sync_tasks SET status = 'failed', \
error = 'session terminated while task was running', \
updated_at = ?1 \
WHERE status = 'running'",
params![now],
)
.map_err(|e| InfraError::Store {
op: "recover_stale_running",
reason: format!("{e}"),
})?;
Ok(count)
})
.await
.map_err(map_call_err)
}
}