fix(sync): a task delete takes its subtasks and tag links with it #197

Merged
maximus merged 3 commits from issue-196-task-delete-cascade into master 2026-11-08 18:30:35 +00:00

3 commits

Author SHA1 Message Date
le king fu
46d29241a2 fix(sync): lock the task row first in every link write
764d833 made link creates lock the task row before inserting, but link
removals (POST /api/sync taskTag delete, DELETE /api/tasks/[id]/tags/
[tagId]) still deleted the link before touching the task. With the link
already present, a create and a removal took the two locks in opposite
orders and Postgres aborted one (40P01).

lockTaskForLinks (lib/taskDelete.ts) states one rule: every transaction
that writes an sl_task_tags link for a task locks that task's row first,
FOR NO KEY UPDATE. The four link paths now call it; deleteTask already
starts with the task row update. REVIEW.md gets the rule. A tag delete
keeps its tag-first order (left as is by the review).

Replayed on a real Postgres (embedded-postgres, outside the repo), link
removal against a create of the same existing link: old order -> one
40P01 in each order; task row first -> both applied. Earlier scenarios
(two creates, link vs task delete) unchanged.

Refs #196

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:38:07 -05:00
le king fu
764d833d42 fix(sync): lock the task row FOR NO KEY UPDATE in link writes
The link transaction took FOR SHARE on the task row, then updated that
row (updatedAt, #180): two link writes on the same task each held the
share lock and waited for the other to upgrade, and Postgres aborted one
(40P01). FOR NO KEY UPDATE is the lock the update takes anyway: link
writes on one task now run one after the other, and a concurrent task
delete is still waited for. Tags keep FOR SHARE.

Replayed on a real Postgres (embedded-postgres, outside the repo):
FOR SHARE, two concurrent links -> one 40P01; FOR NO KEY UPDATE -> both
applied; link vs delete in either order -> no link left.

Refs #196

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:28:48 -05:00
le king fu
d44caa8014 fix(sync): a task delete takes its subtasks and tag links with it
A task delete on the companion only stamped deletedAt: its sl_task_tags
rows stayed, GET /api/sync sent them back, and probe control 3 counted
them. Subtasks stayed active under the deleted parent.

- web/src/lib/taskDelete.ts: deleteTask soft-deletes the task and its
  active subtasks of the same account (one level) and drops every link
  they hold, in one transaction. POST /api/sync task delete and
  DELETE /api/tasks/[id] both go through it.
- A link sent to a deleted task or tag is never written: taskTag create
  answers applied, POST /api/tasks/[id]/tags drops deleted tags and
  answers 404 for a deleted task. The check runs inside the write's
  transaction (FOR SHARE) so a concurrent delete cannot slip between.
- Migration 0004 removes the links already left by deleted tasks.
- probe.sql: control 3 is expected at 0 again; REVIEW.md gets the rule.
- probe.test.ts beforeAll gets 30 s: a second PGlite boot from the new
  migration test pushed it past the 10 s default under load.

test: reproduces #196 (11 regression tests red before the fix)

Refs #196

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:20:57 -05:00