Skip to content

Commit f40ba5f

Browse files
committed
fix: add_task silently discarded status and recur
Replica.attrs is one list, used to build the map for both add and modify. The Rust side implemented it twice: update_task handled all nine attributes, add_task handled seven and dropped the rest into `_ => {}`, so a task created with a status or a repeat had both quietly thrown away while the call reported success. Nothing sends those on creation today — the add sheet collects description, due, priority, project, wait and tags — so this is a trap rather than a live fault. It is the same trap that already caught us once: `recurring` was writable through one entry point and silently dropped by the other, and the write reported success both times. Adding a repeat field to the add sheet is the obvious next step now that the detail view has one, and it would have failed exactly this way. - status parsing moves into one `parse_status`, so the set of valid statuses is defined once instead of once per entry point - add_task gains `recur` and `status` arms, and the same recurrence invariant update_task enforces: a repeating task with no due date is refused, because the Taskwarrior CLI deletes one on its next run - the catch-all now errors. Every key the Dart layer can send is handled explicitly, `uuid` included, so reaching it means the two sides have drifted and that should be loud Tested: six new tests covering both refusals, both new attributes, and a sweep asserting every attribute Replica.attrs can send is accepted — that last one fails if the new catch-all ever starts rejecting a real write. 32 Rust tests pass. Verified on device that adding a task through the sheet still works.
1 parent 3119b03 commit f40ba5f

4 files changed

Lines changed: 204 additions & 25 deletions

File tree

1.51 KB
Binary file not shown.
1.25 KB
Binary file not shown.
2.13 KB
Binary file not shown.

‎rust/src/api.rs‎

Lines changed: 204 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,7 @@ fn update_task_impl(
174174
);
175175
}
176176
"status" => {
177-
let status = match value.as_str() {
178-
"pending" => taskchampion::Status::Pending,
179-
"completed" => taskchampion::Status::Completed,
180-
"deleted" => taskchampion::Status::Deleted,
181-
// A recurrence template. Without this arm "recurring"
182-
// fell into the catch-all below and was silently
183-
// downgraded to Pending, so the status could not be set
184-
// at all — and the Taskwarrior CLI only generates
185-
// instances for a task whose status is `recurring`.
186-
"recurring" => taskchampion::Status::Recurring,
187-
// Refuse rather than coerce. This arm used to fall back
188-
// to Pending, which meant an unrecognised status was
189-
// silently downgraded instead of reported — exactly how
190-
// "recurring" went missing before it was added above.
191-
// A caller sending something unknown has a bug, and it
192-
// should be visible at the point it happens.
193-
other => {
194-
return Err(TcHelperError::InvalidInput(format!(
195-
"unknown status '{other}' — expected pending, \
196-
completed, deleted or recurring"
197-
)))
198-
}
199-
};
200-
let _ = t.set_status(status, &mut ops);
177+
let _ = t.set_status(parse_status(&value)?, &mut ops);
201178
}
202179
_ => {}
203180
}
@@ -209,6 +186,28 @@ fn update_task_impl(
209186
Ok(())
210187
}
211188

189+
/// The one place a status string is turned into a `Status`.
190+
///
191+
/// Both `add_task` and `update_task` go through this. They used to carry their
192+
/// own idea of which statuses existed, which is how `recurring` came to be
193+
/// writable through one entry point and silently dropped by the other. An
194+
/// unrecognised value is refused rather than coerced, so a caller with a bug
195+
/// hears about it instead of quietly getting a pending task.
196+
fn parse_status(value: &str) -> Result<taskchampion::Status, TcHelperError> {
197+
match value {
198+
"pending" => Ok(taskchampion::Status::Pending),
199+
"completed" => Ok(taskchampion::Status::Completed),
200+
"deleted" => Ok(taskchampion::Status::Deleted),
201+
// A recurrence template. The Taskwarrior CLI only generates instances
202+
// for a task whose status is `recurring`.
203+
"recurring" => Ok(taskchampion::Status::Recurring),
204+
other => Err(TcHelperError::InvalidInput(format!(
205+
"unknown status '{other}' — expected pending, completed, deleted \
206+
or recurring"
207+
))),
208+
}
209+
}
210+
212211
/// Create a new task from the supplied key/value map. The map must contain a
213212
/// `uuid` entry.
214213
#[frb]
@@ -225,6 +224,19 @@ fn add_task_impl(taskdb_dir_path: &str, map: HashMap<String, String>) -> Result<
225224
.ok_or_else(|| TcHelperError::InvalidUuid("<missing>".to_string()))?;
226225
let uuid = Uuid::parse_str(uuid_str).map_err(|_| TcHelperError::InvalidUuid(uuid_str.clone()))?;
227226

227+
// Same invariant update_task enforces: Taskwarrior deletes a recurring task
228+
// that has no due date the next time it runs. On a new task the map is the
229+
// whole of the state, so there is nothing to read back.
230+
if map.get("recur").is_some_and(|v| !v.trim().is_empty())
231+
&& map.get("due").and_then(|v| parse_datetime(v)).is_none()
232+
{
233+
return Err(TcHelperError::InvalidInput(
234+
"a repeating task needs a due date — Taskwarrior deletes a \
235+
recurring task that has none"
236+
.to_string(),
237+
));
238+
}
239+
228240
let mut t = replica
229241
.create_task(uuid, &mut ops)
230242
.map_err(|e| TcHelperError::Champion(e.to_string()))?;
@@ -257,7 +269,35 @@ fn add_task_impl(taskdb_dir_path: &str, map: HashMap<String, String>) -> Result<
257269
"project" => {
258270
let _ = t.set_user_defined_attribute("project", value, &mut ops);
259271
}
260-
_ => {}
272+
// Both of these used to fall into the catch-all below. Replica.attrs
273+
// on the Dart side lists them as round-trippable and sends them
274+
// through this same map, so a task created with a repeat or an
275+
// explicit status had that silently discarded while the write
276+
// reported success.
277+
"recur" => {
278+
let trimmed = value.trim();
279+
let _ = t.set_value(
280+
"recur",
281+
if trimmed.is_empty() {
282+
None
283+
} else {
284+
Some(trimmed.to_string())
285+
},
286+
&mut ops,
287+
);
288+
}
289+
"status" => {
290+
let _ = t.set_status(parse_status(&value)?, &mut ops);
291+
}
292+
// Consumed above to create the task.
293+
"uuid" => {}
294+
// Every key Replica.attrs can send is handled above, so reaching
295+
// here means the two sides have drifted apart. Say so.
296+
other => {
297+
return Err(TcHelperError::InvalidInput(format!(
298+
"unknown attribute '{other}'"
299+
)))
300+
}
261301
}
262302
}
263303
replica
@@ -1292,3 +1332,142 @@ mod status_validation_tests {
12921332
fs::remove_dir_all(&tmp).ok();
12931333
}
12941334
}
1335+
1336+
#[cfg(test)]
1337+
mod add_task_attribute_tests {
1338+
//! `add_task` and `update_task` are handed the same key set — Replica.attrs
1339+
//! on the Dart side drives both — but add_task only implemented seven of the
1340+
//! nine, dropping the other two into a catch-all that returned success.
1341+
//! These pin the two entry points to the same contract.
1342+
use super::*;
1343+
use serde_json::Value;
1344+
use std::{collections::HashMap, env, fs};
1345+
1346+
fn dir() -> (std::path::PathBuf, String) {
1347+
let tmp = env::temp_dir().join(format!("taskdb_addattr_{}", Uuid::new_v4()));
1348+
fs::create_dir_all(&tmp).expect("create temp taskdb dir");
1349+
let path = tmp.to_string_lossy().into_owned();
1350+
(tmp, path)
1351+
}
1352+
1353+
fn add(path: &str, pairs: &[(&str, &str)]) -> (String, Result<(), String>) {
1354+
let uuid = Uuid::new_v4().to_string();
1355+
let mut map: HashMap<String, String> = pairs
1356+
.iter()
1357+
.map(|(k, v)| (k.to_string(), v.to_string()))
1358+
.collect();
1359+
map.insert("uuid".to_string(), uuid.clone());
1360+
map.entry("description".to_string())
1361+
.or_insert_with(|| "chore".to_string());
1362+
let res = add_task(path.to_string(), map);
1363+
(uuid, res)
1364+
}
1365+
1366+
fn field(path: &str, uuid: &str, key: &str) -> Option<String> {
1367+
let json = get_all_tasks_json(path.to_string()).expect("get_all_tasks_json");
1368+
let tasks: Vec<Value> = serde_json::from_str(&json).expect("parse json");
1369+
tasks
1370+
.into_iter()
1371+
.find(|t| t.get("uuid").and_then(|u| u.as_str()) == Some(uuid))
1372+
.and_then(|t| t.get(key).and_then(|v| v.as_str()).map(|s| s.to_string()))
1373+
}
1374+
1375+
fn exists(path: &str, uuid: &str) -> bool {
1376+
field(path, uuid, "uuid").is_some()
1377+
}
1378+
1379+
#[test]
1380+
fn status_supplied_at_creation_is_honoured() {
1381+
let (tmp, path) = dir();
1382+
// add_task sets Pending before applying the map; an explicit status has
1383+
// to win over that default rather than be discarded by the catch-all.
1384+
let (uuid, res) = add(&path, &[("status", "completed")]);
1385+
res.expect("add_task");
1386+
1387+
assert_eq!(field(&path, &uuid, "status").as_deref(), Some("completed"));
1388+
fs::remove_dir_all(&tmp).ok();
1389+
}
1390+
1391+
#[test]
1392+
fn recur_supplied_at_creation_is_stored() {
1393+
let (tmp, path) = dir();
1394+
let (uuid, res) = add(
1395+
&path,
1396+
&[
1397+
("recur", "weekly"),
1398+
("due", "2026-09-01T09:00:00Z"),
1399+
("status", "recurring"),
1400+
],
1401+
);
1402+
res.expect("add_task");
1403+
1404+
assert_eq!(field(&path, &uuid, "recur").as_deref(), Some("weekly"));
1405+
assert_eq!(field(&path, &uuid, "status").as_deref(), Some("recurring"));
1406+
fs::remove_dir_all(&tmp).ok();
1407+
}
1408+
1409+
#[test]
1410+
fn recur_without_a_due_date_is_refused_at_creation() {
1411+
let (tmp, path) = dir();
1412+
// update_task already refuses this. Creation had no such check, so the
1413+
// same task the editor cannot produce could be created outright — and
1414+
// the CLI deletes it on the next run.
1415+
let (uuid, res) = add(&path, &[("recur", "weekly")]);
1416+
1417+
let err = res.expect_err("must be refused");
1418+
assert!(err.contains("needs a due date"), "unhelpful: {err}");
1419+
assert!(!exists(&path, &uuid), "nothing should have been created");
1420+
fs::remove_dir_all(&tmp).ok();
1421+
}
1422+
1423+
#[test]
1424+
fn unknown_status_is_refused_at_creation() {
1425+
let (tmp, path) = dir();
1426+
let (uuid, res) = add(&path, &[("status", "archived")]);
1427+
1428+
let err = res.expect_err("must be refused");
1429+
assert!(err.contains("unknown status 'archived'"), "unhelpful: {err}");
1430+
assert!(!exists(&path, &uuid), "nothing should have been created");
1431+
fs::remove_dir_all(&tmp).ok();
1432+
}
1433+
1434+
#[test]
1435+
fn an_attribute_neither_side_agrees_on_is_refused() {
1436+
let (tmp, path) = dir();
1437+
// The failure this guards against is silent drift: Dart grows an entry
1438+
// in attrs, Rust does not, and the write reports success either way.
1439+
let (uuid, res) = add(&path, &[("energy", "high")]);
1440+
1441+
let err = res.expect_err("must be refused");
1442+
assert!(err.contains("unknown attribute 'energy'"), "unhelpful: {err}");
1443+
assert!(!exists(&path, &uuid), "nothing should have been created");
1444+
fs::remove_dir_all(&tmp).ok();
1445+
}
1446+
1447+
#[test]
1448+
fn every_attribute_the_dart_side_can_send_is_accepted() {
1449+
let (tmp, path) = dir();
1450+
// Mirrors Replica.attrs plus the two keys the Dart layer adds itself.
1451+
// If this fails, the catch-all above will be rejecting a real write.
1452+
let (uuid, res) = add(
1453+
&path,
1454+
&[
1455+
("description", "full sweep"),
1456+
("due", "2026-09-01T09:00:00Z"),
1457+
("start", "2026-08-01T09:00:00Z"),
1458+
("wait", "2026-08-20T09:00:00Z"),
1459+
("priority", "H"),
1460+
("project", "home"),
1461+
("status", "recurring"),
1462+
("recur", "monthly"),
1463+
("tags", "one two"),
1464+
],
1465+
);
1466+
res.expect("every declared attribute must be accepted");
1467+
1468+
assert_eq!(field(&path, &uuid, "description").as_deref(), Some("full sweep"));
1469+
assert_eq!(field(&path, &uuid, "project").as_deref(), Some("home"));
1470+
assert_eq!(field(&path, &uuid, "recur").as_deref(), Some("monthly"));
1471+
fs::remove_dir_all(&tmp).ok();
1472+
}
1473+
}

0 commit comments

Comments
 (0)