Peach: Local-first video metadata scraper and media manager.
fix(processing): 修正默认档位分层与回归夹具
longmeidao5f1fa39123b7
diff --git a/src/peach/processing_control.py b/src/peach/processing_control.pyindex 99e94d7ee..a18828af1 100644--- a/src/peach/processing_control.py+++ b/src/peach/processing_control.py@@ -114,14 +114,20 @@ def current_control():return CURRENT.get() or TASK_CONTROL.get()+def default_mode(database):+ """从当前 profile 读取新任务默认档位。"""+ from .profiles import read_settings+ with database.read_connection() as connection:+ return read_settings(connection).get('taskSpeedMode', 'standard')++def execution_mode(database, requested=None, *, control):"""执行中的任务沿用本轮档位,独立调用选择显式档位或设置默认值。"""if control is not None:return control.modeif requested is not None:return valid_mode(requested)- from .web_settings import task_speed_mode- return task_speed_mode(database)+ return default_mode(database)def low_mode():diff --git a/src/peach/task_control.py b/src/peach/task_control.pyindex ec681d627..35e71d3c6 100644--- a/src/peach/task_control.py+++ b/src/peach/task_control.py@@ -70,13 +70,13 @@ def speed_mode(db, run_id, ancestors=(), *, default='standard'):def initialize_speed(store, run_id, parent=None):- from .web_settings import task_speed_mode+ from .processing_control import default_modefrom .update_tasks import _mutexdb = store.database.db_pathwith _mutex(state_for(db)):path = _root(db) / f'{run_id}.speed.json'mode = (speed_mode(db, parent.id, (parent.parent_run_id, parent.root_run_id))- if parent is not None else task_speed_mode(store.database))+ if parent is not None else default_mode(store.database))atomic_write_text(path, json.dumps({'speed_mode': mode, 'at': time.time_ns()}))diff --git a/src/peach/web_library_processing.py b/src/peach/web_library_processing.pyindex 49009035e..2ad67febc 100644--- a/src/peach/web_library_processing.py+++ b/src/peach/web_library_processing.py@@ -87,9 +87,9 @@ def _project_speed(contract, state, run, control):elif state.get('status') in ACTIVE:mode = control.modeelse:- from .web_settings import task_speed_mode+ from .processing_control import default_modefrom .repository import LedgerDatabase- mode = control.next_mode or task_speed_mode(LedgerDatabase(contract.db_path))+ mode = control.next_mode or default_mode(LedgerDatabase(contract.db_path))if state.get('status') in ACTIVE and previous_mode and previous_mode != mode:state.update(_speed_fields(state, mode))state['speed_mode'] = mode@@ -413,7 +413,7 @@ def retry_after_network_recovery(contract, sync=None):def _initial_speed(contract, control, previous, body, *, resume):"""恢复沿用任务及旧记录的档位,新任务消费显式选择、待选值或默认值。"""- from .web_settings import task_speed_mode+ from .processing_control import default_modefrom .repository import LedgerDatabasecontrol.refresh(force=True)if resume:@@ -424,7 +424,7 @@ def _initial_speed(contract, control, previous, body, *, resume):default=previous.get('speed_mode') or (body or {}).get('speed_mode') or control.mode)return valid_mode(control.mode)return valid_mode((body or {}).get('speed_mode') or control.next_mode- or task_speed_mode(LedgerDatabase(contract.db_path)))+ or default_mode(LedgerDatabase(contract.db_path)))def w_library_processing(contract, body, *, resume=False):diff --git a/src/peach/web_settings.py b/src/peach/web_settings.pyindex 0f9a1c619..86155eb56 100644--- a/src/peach/web_settings.py+++ b/src/peach/web_settings.py@@ -213,10 +213,6 @@ def q_settings(contract: SettingsContract, _args=None) -> dict:}-def task_speed_mode(contract: SettingsContract) -> str:- return _stored(contract).get("taskSpeedMode", "standard")--def w_settings(contract: SettingsContract, body) -> dict:"""写入跟人走的设置;只认白名单里的键。diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.pyindex a3859cbfa..a5d506c06 100644--- a/tests/test_metadata_library.py+++ b/tests/test_metadata_library.py@@ -2994,6 +2994,10 @@ class LibraryWatchdogTests(unittest.TestCase):contract = Mock()contract.db_path = self.root / 'database/ledger.db'contract.library_processing_job.snapshot.return_value = None+ from peach.repository import LedgerDatabase+ contract.db_path.parent.mkdir()+ fresh_ledger(contract.db_path.parent)+ contract.database = LedgerDatabase(contract.db_path)with patch('peach.web_library_processing.settings_file.active', return_value=config):self.assertEqual(q_library_processing(contract, {})['issues_log'], str(log))diff --git a/tests/test_processing_control.py b/tests/test_processing_control.pyindex 692d2c7c7..a4af206fa 100644--- a/tests/test_processing_control.py+++ b/tests/test_processing_control.py@@ -693,7 +693,7 @@ class ProcessingControlTests(unittest.TestCase):from peach.task_runs import TaskRunStorefrom peach.web_job_recovery import restorefrom peach.web_library_processing import q_library_processing- from peach.web_settings import task_speed_mode+ from peach.processing_control import default_modedatabase_root = self.config.directory('database')database_root.mkdir(parents=True)db = fresh_ledger(database_root)@@ -738,7 +738,7 @@ class ProcessingControlTests(unittest.TestCase):self.assertEqual(runs.get(run.id).status, 'succeeded')self.assertTrue(speed_file.exists())self.assertEqual(task_control.speed_mode(db, run.id), mode)- self.assertEqual(task_speed_mode(runs.database), 'standard')+ self.assertEqual(default_mode(runs.database), 'standard')def test_three_intensities_set_distinct_io_start_intervals(self):control = control_for(self.config)@@ -865,7 +865,7 @@ class ProcessingControlTests(unittest.TestCase):from peach.jobs import BackgroundJobfrom peach.task_runs import TaskRunStorefrom peach.web_library_processing import q_library_processing- from peach.web_settings import task_speed_mode+ from peach.processing_control import default_moderuns = TaskRunStore(self.db)run = runs.start('library-processing', trigger='manual')job = BackgroundJob('library-processing')@@ -880,7 +880,7 @@ class ProcessingControlTests(unittest.TestCase):state = w_library_processing(contract, {'action': 'set_speed', 'job_id': 'one', 'speed_mode': 'low'})self.assertEqual(state['speed_mode'], 'low')self.assertEqual(task_control.projection(runs, run)['speed_mode'], 'low')- self.assertEqual(task_speed_mode(runs.database), 'standard')+ self.assertEqual(default_mode(runs.database), 'standard')with task_control.bind(runs, run.id), control.bind('one', publish=lambda **_: None, active=lambda: True, db_path=self.db):self.assertEqual(control.mode, 'low')@@ -907,8 +907,8 @@ class ProcessingControlTests(unittest.TestCase):self.assertEqual(start.call_args.kwargs['recovery']['speed_mode'], 'low')chosen = w_library_processing(contract, {'action': 'set_speed', 'speed_mode': 'high'})self.assertEqual(chosen['speed_mode'], 'high')- from peach.web_settings import task_speed_mode- self.assertEqual(task_speed_mode(runs.database), 'low')+ from peach.processing_control import default_mode+ self.assertEqual(default_mode(runs.database), 'low')selected = w_library_processing(contract, {'stage': 'metadata'})self.assertEqual(selected['speed_mode'], 'high')from peach.web_library_processing import q_library_processingdiff --git a/tests/test_scripts.py b/tests/test_scripts.pyindex a5c74d9db..3e12edbfa 100644--- a/tests/test_scripts.py+++ b/tests/test_scripts.py@@ -1588,8 +1588,8 @@ class OperationalScriptTests(unittest.TestCase):provider = Flaky()clock = [0.0]health = root / "health.csv"- with mock.patch.object(self.scrape_codes.time, "monotonic",- side_effect=lambda: clock[0]):+ with mock.patch.object(self.scrape_codes, "time", wraps=self.scrape_codes.time) as scrape_time:+ scrape_time.monotonic.side_effect = lambda: clock[0]with redirect_stdout(io.StringIO()):self.scrape_codes.main(["--db", str(db), "--out", str(root / "c.csv"),@@ -1613,8 +1613,8 @@ class OperationalScriptTests(unittest.TestCase):clock[0] += self.scrape_codes.COOLDOWN_SECONDSreturn clock[0]- with mock.patch.object(self.scrape_codes.time, "monotonic",- side_effect=advancing):+ with mock.patch.object(self.scrape_codes, "time", wraps=self.scrape_codes.time) as scrape_time:+ scrape_time.monotonic.side_effect = advancingwith redirect_stdout(io.StringIO()):self.scrape_codes.main(["--db", str(db), "--out", str(root / "c2.csv"),diff --git a/tests/test_task_control.py b/tests/test_task_control.pyindex 4dffd0c42..fa514953e 100644--- a/tests/test_task_control.py+++ b/tests/test_task_control.py@@ -100,7 +100,8 @@ class TaskControlTests(unittest.TestCase):return [sys.executable, '-u', '-c', HEARTBEAT_CHILD, str(path), str(count)]def test_new_tasks_snapshot_the_default_and_runtime_speed_stays_local(self):- from peach.web_settings import task_speed_mode, w_settings+ from peach.processing_control import default_mode+ from peach.web_settings import w_settingsw_settings(self.store.database, {'taskSpeedMode': 'low'})first = self.store.start('scan', trigger='manual')w_settings(self.store.database, {'taskSpeedMode': 'high'})@@ -110,7 +111,7 @@ class TaskControlTests(unittest.TestCase):task_control.request(self.store, first.id, 'set_speed', mode='standard')self.assertEqual(self.state(first.id)['speed_mode'], 'standard')self.assertEqual(self.state(second.id)['speed_mode'], 'high')- self.assertEqual(task_speed_mode(self.store.database), 'high')+ self.assertEqual(default_mode(self.store.database), 'high')with self.assertRaises(ValueError):task_control.request(self.store, first.id, 'set_speed', mode='fast')self.assertEqual(self.state(first.id)['speed_mode'], 'standard')diff --git a/tests/test_web_settings.py b/tests/test_web_settings.pyindex bc1763ea1..1c10a3afe 100644--- a/tests/test_web_settings.py+++ b/tests/test_web_settings.py@@ -112,11 +112,12 @@ class SettingsRoundTripTests(unittest.TestCase):self.assertEqual(self._stored_json()["codePatterns"], saved)def test_default_task_speed_accepts_each_mode_and_rejects_unknown_values(self):+ from peach.processing_control import default_modefor mode in ('low', 'standard', 'high'):with self.subTest(mode=mode):saved = w_settings(self.contract, {'taskSpeedMode': mode})self.assertEqual(saved['taskSpeedMode'], mode)- self.assertEqual(web_settings.task_speed_mode(self.contract), mode)+ self.assertEqual(default_mode(self.contract), mode)for value in (None, '', 'fast', 1, ['low']):with self.subTest(value=value), self.assertRaises(ValueError):w_settings(self.contract, {'taskSpeedMode': value})