Peach: Local-first video metadata scraper and media manager.
fix(ledger): 清理任务历史的失效关联并维护删除引用
Louiseb5f6b49210e
diff --git a/docs/REUSE.md b/docs/REUSE.mdindex 98a4f9fff..c41c40450 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -1,5 +1,9 @@# 复用清单+## 任务历史关联清理++任务历史复用现有 `task_run` 自引用字段、SQLite 迁移和原生删除触发器,按 [SQLite 外键动作](https://www.sqlite.org/foreignkeys.html#fk_actions) 的 `SET NULL` 语义清空已删除任务的引用。运行时连接不依赖外键开关;迁移仅修复不存在的父任务和根任务引用,保留任务内容、有效关联及媒体记录。隔离账本回归覆盖外键开关两种连接、跨任务类型的多层后继、活跃任务保留、批量删除回滚和迁移幂等。不引入外部库或新增依赖,也不全局改变连接的外键策略。+## 检查输入与源码文案测试差异复用 Git 文件快照、Python AST、标准库 TOML/JSON 解析器及现有 filelock。Python 注释和清单说明字段通过有效内容比较识别;真实依赖、执行模式和无法解析的输入保留完整验证。完整批次按测试文件保存结果,仅补齐受影响范围,不延长结果有效期。diff --git a/migrations/0048_task_run_references.sql b/migrations/0048_task_run_references.sqlnew file mode 100644index 000000000..171ca7509--- /dev/null+++ b/migrations/0048_task_run_references.sql@@ -0,0 +1,21 @@+-- 任务历史保留有效的父任务与根任务关联;已清退历史的引用为空。+UPDATE task_run+SET parent_run_id = CASE WHEN parent_run_id IN (SELECT id FROM task_run)+ THEN parent_run_id ELSE NULL END,+ root_run_id = CASE WHEN root_run_id IN (SELECT id FROM task_run)+ THEN root_run_id ELSE NULL END+WHERE (parent_run_id IS NOT NULL AND NOT EXISTS+ (SELECT 1 FROM task_run parent WHERE parent.id = task_run.parent_run_id))+ OR (root_run_id IS NOT NULL AND NOT EXISTS+ (SELECT 1 FROM task_run root WHERE root.id = task_run.root_run_id));++-- 运行时连接不依赖 PRAGMA foreign_keys,删除时由触发器维护 SET NULL 语义。+-- 两个引用在同一条 UPDATE 中处理,外键开启时也能通过即时检查。+CREATE TRIGGER task_run_clear_deleted_references+BEFORE DELETE ON task_run+BEGIN+ UPDATE task_run+ SET parent_run_id = CASE WHEN parent_run_id = OLD.id THEN NULL ELSE parent_run_id END,+ root_run_id = CASE WHEN root_run_id = OLD.id THEN NULL ELSE root_run_id END+ WHERE parent_run_id = OLD.id OR root_run_id = OLD.id;+END;diff --git a/src/peach/task_runs.py b/src/peach/task_runs.pyindex 0c358a216..7136abcd5 100644--- a/src/peach/task_runs.py+++ b/src/peach/task_runs.py@@ -649,8 +649,8 @@ class TaskRunStore:def prune(self, task_key: str, keep: int = DEFAULT_KEEP) -> int:"""每个 task_key 只留最近 `keep` 条终态记录,返回删掉多少条。- 活跃行一条都不动:它们不是历史。阈值用 `LIMIT 1 OFFSET keep` 取「第 keep+1 新- 的那条 id」,不用窗口函数——这张表上要的只是一个分界点。+ 活跃任务保留,指向已清退历史的父任务和根任务关联由删除触发器置空。+ 阈值用 `LIMIT 1 OFFSET keep` 取「第 keep+1 新的那条 id」。"""if not self.enabled or keep < 0:return 0diff --git a/tests/test_migrations.py b/tests/test_migrations.pyindex 5b6c03d2a..7b34f6c93 100644--- a/tests/test_migrations.py+++ b/tests/test_migrations.py@@ -3,6 +3,7 @@ import sqlite3import shutilimport tempfileimport unittest+from contextlib import closingfrom pathlib import Pathfrom peach.migrations import plan, upgrade@@ -12,6 +13,44 @@ MIGRATIONS = Path(__file__).resolve().parents[1] / "migrations"class MigrationTests(unittest.TestCase):+ def test_task_reference_migration_preserves_rows_and_existing_links(self):+ prior = self.root.resolve() / "prior"+ prior.mkdir()+ for path in MIGRATIONS.glob("*.sql"):+ if path.name < "0048_":+ shutil.copyfile(path, prior / path.name)+ upgrade(self.db, prior)+ with closing(sqlite3.connect(self.db)) as connection:+ connection.executescript("""+ INSERT INTO task_run(id,task_key,trigger,status,finished_at,parent_run_id,root_run_id,result_summary,error)+ VALUES(10,'parent','manual','succeeded','t',NULL,10,'{}',NULL),+ (11,'child','manual','succeeded','t',10,10,'{"changed":1}',NULL),+ (12,'child','manual','failed','t',100,100,'{}','保留错误'),+ (13,'child','manual','pending',NULL,100,10,'{}',NULL),+ (14,'child','manual','running',NULL,11,100,'{}',NULL),+ (15,'standalone','manual','succeeded','t',NULL,NULL,'{}',NULL);+ INSERT INTO asset(id,location,path,medium) VALUES(1,'local','sample.mp4','video');+ INSERT INTO asset_tag(asset_id,tag,source) VALUES(1,'示例','user');+ """)+ columns = [row[1] for row in connection.execute("PRAGMA table_info(task_run)")+ if row[1] not in ("parent_run_id", "root_run_id")]+ query = f"SELECT {','.join(columns)} FROM task_run ORDER BY id"+ tasks_before = connection.execute(query).fetchall()+ assets_before = connection.execute("SELECT * FROM asset").fetchall()+ tags_before = connection.execute("SELECT * FROM asset_tag").fetchall()+ self.assertEqual(len(connection.execute("PRAGMA foreign_key_check").fetchall()), 4)++ self.assertEqual([migration.version for migration in upgrade(self.db, MIGRATIONS)], ["0048"])+ self.assertEqual(connection.execute("PRAGMA foreign_key_check").fetchall(), [])+ self.assertEqual(connection.execute("PRAGMA integrity_check").fetchone()[0], "ok")+ self.assertEqual(connection.execute(+ "SELECT id,parent_run_id,root_run_id FROM task_run ORDER BY id").fetchall(),+ [(10, None, 10), (11, 10, 10), (12, None, None), (13, None, 10), (14, 11, None), (15, None, None)])+ self.assertEqual(connection.execute(query).fetchall(), tasks_before)+ self.assertEqual(connection.execute("SELECT * FROM asset").fetchall(), assets_before)+ self.assertEqual(connection.execute("SELECT * FROM asset_tag").fetchall(), tags_before)+ self.assertEqual(upgrade(self.db, MIGRATIONS), [])+def test_disposal_reason_migration_preserves_assets_tags_and_unknown_reasons(self):prior = self.root.resolve() / 'prior'prior.mkdir()@@ -24,7 +63,7 @@ class MigrationTests(unittest.TestCase):connection.execute("INSERT INTO asset(id,location,path,medium,disposal) VALUES(1,'local','sample.mp4','video','trash')")connection.execute("INSERT INTO asset_tag(asset_id,tag,source) VALUES(1,'示例','user')")connection.commit()- self.assertEqual([migration.version for migration in upgrade(self.db, MIGRATIONS)], ['0046', '0047'])+ self.assertEqual([migration.version for migration in upgrade(self.db, MIGRATIONS)], ['0046', '0047', '0048'])self.assertEqual(connection.execute("SELECT disposal,disposal_reason FROM asset WHERE id=1").fetchone(), ('trash', None))self.assertEqual(connection.execute("SELECT tag,source FROM asset_tag WHERE asset_id=1").fetchall(), [('示例', 'user')])self.assertEqual(connection.execute("SELECT count(*) FROM asset").fetchone()[0], 1)@@ -43,7 +82,7 @@ class MigrationTests(unittest.TestCase):sqlite3.connect(self.db).close()backup = self.root / "before.db"done = upgrade(self.db, MIGRATIONS, backup)- self.assertEqual([m.version for m in done], [f'{number:04d}' for number in range(48)])+ self.assertEqual([m.version for m in done], [f'{number:04d}' for number in range(49)])self.assertTrue(backup.exists())con = sqlite3.connect(self.db)tables = {row[0] for row in con.execute(@@ -61,7 +100,7 @@ class MigrationTests(unittest.TestCase):"asset_tag_preference", "asset_search", "follow_playback","genre_decision", "asset_subtitle", "task_run", "performer_profile", "code_sample_image","entity_classification", "entity_identity_link", "seed_pack_history", "schema_migration"} <= tables)- self.assertEqual(versions, [f'{number:04d}' for number in range(48)])+ self.assertEqual(versions, [f'{number:04d}' for number in range(49)])self.assertEqual(upgrade(self.db, MIGRATIONS), [])self.assertEqual(plan(self.db, MIGRATIONS)[1], [])diff --git a/tests/test_task_runs.py b/tests/test_task_runs.pyindex 5eb5e0fb0..fbad92e57 100644--- a/tests/test_task_runs.py+++ b/tests/test_task_runs.py@@ -4,6 +4,7 @@ import sqlite3import tempfileimport unittestfrom datetime import datetime, timedelta, timezone+from dataclasses import replacefrom pathlib import Pathfrom unittest import mock@@ -210,6 +211,70 @@ class TaskRunStoreTests(unittest.TestCase):self.assertEqual(len(remaining), 3)self.assertEqual(len(self.store.query(task_key="other")), 1)+ def test_prune_preserves_followups_with_only_existing_task_references(self):+ connect = self.database.connect+ for foreign_keys in (0, 1):+ with self.subTest(foreign_keys=foreign_keys):+ def configured_connect(*args, _foreign_keys=foreign_keys, **kwargs):+ connection = connect(*args, **kwargs)+ connection.execute(f"PRAGMA foreign_keys={_foreign_keys}")+ return connection++ with mock.patch.object(self.database, "connect", configured_connect):+ key = f"parent-{foreign_keys}"+ active = self.store.start(key, trigger="manual")+ parent = self.store.start(key, trigger="manual")+ children = self.store.enqueue_followups(parent.id, [+ (f"{key}:running", f"child-{foreign_keys}", "正在处理"),+ (f"{key}:finished", "child", "已经处理"),+ (f"{key}:pending", "child", "等待处理"),+ ])["queued"]+ self.store.claim_followup([f"child-{foreign_keys}"])+ self.store.finish(children[1], "succeeded", summary={"changed": 2})+ grandchild = self.store.enqueue_followups(children[0], [+ (f"{key}:grandchild", "grandchild", "后续处理"),+ ])["queued"][0]+ self.store.finish(parent.id, "succeeded")+ kept = self.store.start(key, trigger="manual")+ kept_child = self.store.enqueue_followups(kept.id, [+ (f"{key}:kept", "child", "保留关联"),+ ])["queued"][0]+ self.store.finish(kept.id, "succeeded")+ snapshots = {run_id: self.store.get(run_id) for run_id in+ [active.id, kept.id, kept_child, grandchild, *children]}++ self.assertEqual(self.store.prune(key, keep=1), 1)+ self.assertIsNone(self.store.get(parent.id))+ for run_id, before in snapshots.items():+ expected = replace(+ before,+ parent_run_id=None if before.parent_run_id == parent.id else before.parent_run_id,+ root_run_id=None if before.root_run_id == parent.id else before.root_run_id,+ )+ self.assertEqual(self.store.get(run_id), expected)+ with self.database.read_connection() as connection:+ self.assertEqual(connection.execute("PRAGMA foreign_key_check").fetchall(), [])+ self.assertEqual(self.store.prune(key, keep=1), 0)+ self.assertEqual(self.store.prune(key, keep=0), 1)+ self.assertEqual(self.store.get(active.id), snapshots[active.id])+ self.assertIsNone(self.store.get(kept_child).parent_run_id)+ self.assertIsNone(self.store.get(kept_child).root_run_id)++ def test_task_reference_cleanup_rolls_back_with_a_batch_delete(self):+ parents = [self.store.start(f"parent-{i}", trigger="manual") for i in range(2)]+ for parent in parents:+ self.store.enqueue_followups(parent.id, [(f"child:{parent.id}", "child", "待处理")])+ self.store.finish(parent.id, "succeeded")+ with self.database.write_transaction(notify=False) as connection:+ before = connection.execute("SELECT * FROM task_run ORDER BY id").fetchall()+ connection.execute("SAVEPOINT cleanup")+ connection.execute("DELETE FROM task_run WHERE id IN (?,?)", tuple(p.id for p in parents))+ self.assertEqual(connection.execute("PRAGMA foreign_key_check").fetchall(), [])+ self.assertEqual(connection.execute("SELECT count(*) FROM task_run").fetchone()[0], 2)+ connection.execute("ROLLBACK TO cleanup")+ connection.execute("RELEASE cleanup")+ self.assertEqual(connection.execute("SELECT * FROM task_run ORDER BY id").fetchall(), before)+def test_query_filters_by_status_group_and_task(self):first = self.store.start("demo", trigger="manual")second = self.store.start("other", trigger="cli")