From 2cfc56134284f4d9f05a5b3443eced604504ec1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 11:55:54 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(core):=20a=20cleared=20edge?= =?UTF-8?q?=20is=20not=20a=20circle,=20a=20finished=20one=20still=20is?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cycle walk read a dependency row's existence, not its state, so an unlinked edge kept blocking the link that would replace it, while a finished blocker — cleared on the screen, still in force in the graph — was walked as if it were gone. `cleared_at IS NULL` is the filter: only unlinking clears a row, finishing does not. Same read in `dependencies_for_task`, corrected with it. `a_cleared_dependency_is_not_a_circle` reverses an assertion made in slice 5, where the two states were treated as one. `an_orphan_row_is_refused` pins what the fixture note only guessed at: `PRAGMA foreign_keys = ON` is in force under turso 0.8.0-pre.7, and an insert naming a task that does not exist is refused by the engine rather than written and left hanging. --- Cargo.lock | 1 + crates/dizey-core/Cargo.toml | 1 + crates/dizey-core/src/store/turso_store.rs | 11 ++- crates/dizey-core/tests/store.rs | 106 ++++++++++++++++++++- 4 files changed, 112 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6bc0c4..77a49a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -963,6 +963,7 @@ dependencies = [ "leptos_meta", "leptos_router", "serde", + "serde_json", "thiserror", "time", "tokio", diff --git a/crates/dizey-core/Cargo.toml b/crates/dizey-core/Cargo.toml index 02703c1..9f21eae 100644 --- a/crates/dizey-core/Cargo.toml +++ b/crates/dizey-core/Cargo.toml @@ -38,3 +38,4 @@ uuid = { workspace = true, optional = true } async-trait = "0.1.92" serde_json.workspace = true tokio = { workspace = true } +turso = { workspace = true } diff --git a/crates/dizey-core/src/store/turso_store.rs b/crates/dizey-core/src/store/turso_store.rs index 6d02e97..ea70118 100644 --- a/crates/dizey-core/src/store/turso_store.rs +++ b/crates/dizey-core/src/store/turso_store.rs @@ -977,15 +977,18 @@ impl Store for TursoStore { .map_err(backend)?; let written = async { - // Every edge, cleared or not: a cleared edge is a link that is - // satisfied, not a link that is gone, and re-adding its other half - // would still be a circle on the screen. + // Live edges only. `cleared_at` is set by an unlink and nothing + // else, so a cleared edge is a link that was removed — walking it + // refuses a link the screen no longer shows as a circle. A link + // whose blocker is merely finished still walks: that is `done_at`, + // and the edge is still in force. let mut rows = tx .query( "SELECT d.blocked_task_id, d.blocking_task_id FROM task_dependency d \ JOIN task b ON b.id = d.blocked_task_id \ JOIN task k ON k.id = d.blocking_task_id \ - WHERE b.deleted_at IS NULL AND k.deleted_at IS NULL", + WHERE d.cleared_at IS NULL \ + AND b.deleted_at IS NULL AND k.deleted_at IS NULL", (), ) .await?; diff --git a/crates/dizey-core/tests/store.rs b/crates/dizey-core/tests/store.rs index bbda430..731bd03 100644 --- a/crates/dizey-core/tests/store.rs +++ b/crates/dizey-core/tests/store.rs @@ -1895,7 +1895,7 @@ async fn a_dependency_that_would_close_a_circle_is_refused() { } #[tokio::test] -async fn a_cleared_dependency_still_counts_as_a_circle() { +async fn a_cleared_dependency_is_not_a_circle() { let (scratch, workspace, admin) = workspace_with_admin().await; let store = &scratch.store; let now = OffsetDateTime::now_utc(); @@ -1905,8 +1905,38 @@ async fn a_cleared_dependency_still_counts_as_a_circle() { store.add_dependency(&a, &b, now).await.unwrap(); store.clear_dependency(&a, &b, now).await.unwrap(); - // A cleared edge is a link that is satisfied, not a link that is gone: the - // pair would still draw a circle on the screen. + // `cleared_at` is set by an unlink and nothing else, and a cleared row no + // longer appears on the screen. Refusing the other direction would refuse a + // circle nobody can see — and leave the pair unlinkable for good, because + // the cleared row never goes away. A link whose blocker is merely finished + // is a different thing: that is `done_at`, the edge is live, and it still + // walks. + store.add_dependency(&b, &a, now).await.unwrap(); + + let detail = load_detail(store, &workspace, &b).await.unwrap().unwrap(); + assert_eq!(detail.blocked_by.len(), 1, "the new link stands"); + let detail = load_detail(store, &workspace, &a).await.unwrap().unwrap(); + assert!(detail.blocked_by.is_empty(), "the cleared one is gone"); +} + +#[tokio::test] +async fn a_finished_blocker_still_counts_as_a_circle() { + let (scratch, workspace, admin) = workspace_with_admin().await; + let store = &scratch.store; + let now = OffsetDateTime::now_utc(); + + let a = add_task(store, &workspace, "Backlog", "a", None, &admin).await; + let b = add_task(store, &workspace, "Backlog", "b", None, &admin).await; + store.add_dependency(&a, &b, now).await.unwrap(); + let done = column_named(store, &workspace, "Done").await; + let backlog = column_named(store, &workspace, "Backlog").await; + store + .move_task(&b, &backlog, &done, &admin, now) + .await + .unwrap(); + + // b is finished, so the row reads as cleared on the screen — but the link + // is still in force, and the other direction is still a circle. assert!(matches!( store.add_dependency(&b, &a, now).await, Err(StoreError::Cycle) @@ -2365,3 +2395,73 @@ async fn a_deleted_card_does_not_move() { assert!(matches!(refused, Err(StoreError::NotFound))); } + +/// The schema's REFERENCES clauses are only worth anything if the engine acts +/// on them. A drive database once held members pointing at a workspace id with +/// no row behind it — those rows went in through the `sqlite3` CLI, which has +/// `foreign_keys` OFF by default, but the only way to know that is to prove the +/// store's own connections have it ON and refuse the same insert. +#[tokio::test] +async fn an_orphan_row_is_refused() { + let (scratch, workspace, _admin) = workspace_with_admin().await; + + // Through the store's own API first: a user needs a workspace that exists. + let refused = scratch + .store + .create_user(NewUser { + workspace_id: "no-such-workspace".into(), + email: "orphan@dizey.sh".into(), + display_name: "Orphan".into(), + role: Role::Member, + }) + .await; + assert!( + refused.is_err(), + "a user in a workspace that does not exist was accepted" + ); + + // And at the engine, where the pragma either fired or did not. Turso is + // single-writer, so this gets a file of its own rather than a second handle + // on the live one, which would fail with "database is locked" and tell us + // nothing about foreign keys. + let existing = scratch + .store + .users(&workspace) + .await + .expect("the workspace reads back") + .len(); + assert_eq!(existing, 1, "only the admin"); + + let dir = std::env::temp_dir().join(format!("dizey-fk-{}", Uuid::new_v4())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("dizey.db").to_str().unwrap().to_owned(); + { + TursoStore::open(&path).await.unwrap(); + } + + let db = turso::Builder::new_local(&path).build().await.unwrap(); + let conn = db.connect().unwrap(); + conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap(); + let mut rows = conn.query("PRAGMA foreign_keys", ()).await.unwrap(); + let on = rows.next().await.unwrap().expect("the pragma reads back"); + assert_eq!( + on.get::(0).unwrap(), + 1, + "foreign_keys did not stay ON for this connection" + ); + drop(rows); + + let refused = conn + .execute( + "INSERT INTO user (id, workspace_id, email, display_name, role, created_at) \ + VALUES ('u-orphan', 'no-such-workspace', 'orphan@dizey.sh', 'Orphan', \ + 'member', '2026-08-26T00:00:00Z')", + (), + ) + .await; + assert!( + refused.is_err(), + "the engine accepted a user pointing at no workspace" + ); + let _ = std::fs::remove_dir_all(&dir); +}