feat(R07): task repository, schedule calculator, Qt clock adapter, task/AI-planner services

Team Hoa, EPIC R07 (Scheduling & Workflow Runtime) - Team Hoa scope only
(R07-T01 -> T05; R07-T06 Co4EWorkflowService is Team Nam's).

- R07-T01: infrastructure/persistence/json/task_repository_impl.py wraps
  core/tasks.py's CRUD; core/tasks.py::save_task now writes through
  atomic_write.write_json (same durability fix as R06-T02, save_task was
  still doing a plain write_text).
- R07-T02: domain/tasks/schedule_calculator.py::ScheduleCalculator - the
  cron/interval/daily/weekly/monthly due-time math extracted from
  core/tasks.py, pure Python with is_holiday/make_cron injected so domain/
  never imports core (ADR-001 I2). core/tasks.py keeps its old function
  names as thin wrappers so every existing caller is unchanged. This was
  previously untested; now has its own unit suite.
- R07-T03: infrastructure/qt/qt_scheduler_clock.py::QtSchedulerClock wraps
  the QTimer TaskScheduler used to own directly, injected via a new
  `clock=` constructor param (defaults to a real one). Originally planned
  at platform/qt/... ; moved after confirming that name shadows the
  stdlib platform module (used by core/windows_sandbox_vm.py,
  core/appcontainer_sandbox.py) whenever the repo root is on sys.path.
  tests/fakes/fake_clock.py lets scheduler dispatch be tested tick-by-tick
  with no Qt event loop.
- R07-T04: application/scheduling/task_application_service.py centralizes
  run_now/duplicate/pause/delete/bulk_delete and the Kanban drag-drop
  business rules (move_to_status), currently only reachable by driving
  the real ui/schedule_task_tab.py widget.
- R07-T05: application/scheduling/ai_task_planner_service.py wraps
  core/ai_task_planner.py::plan_tasks and core/task_import.py::import_tasks
  as a seam, plus the attachment-stamping step that used to only exist
  inside the AI-create dialog's worker closure.

pytest: 328 pass (same 4 pre-existing failures as the R05/R06 baseline,
unrelated to this work - see docs/refactor/BaoCao_TeamHoa_R05_R06.md).
scripts/check_imports.py: PASS.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-27 17:27:53 +09:00
co-authored by Claude Sonnet 5
parent 8ab29800db
commit 69ab8e125b
20 changed files with 1347 additions and 95 deletions
@@ -0,0 +1,87 @@
"""EPIC R07-T05: AiTaskPlannerService — AI-generate + import, no Qt, no network.
``core/ai_task_planner.py::plan_tasks`` and ``core/task_import.py::
import_tasks`` are exercised through a FakeProvider / real tmp files rather
than reimplemented — this service is a seam, not a new planner.
"""
from __future__ import annotations
import json
import pytest
from cowork_local.application.scheduling.ai_task_planner_service import (
AiTaskPlannerService,
)
from tests.fakes import FakeProvider, ScriptedTurn
_PLAN_REPLY = json.dumps({
"tasks": [
{"title": "Draft report", "description": "d", "task_type": "cowork",
"priority": "medium", "schedule": {"enabled": False}}
]
})
def test_plan_uses_the_constructor_injected_provider_factory():
provider = FakeProvider([ScriptedTurn(text=_PLAN_REPLY)])
service = AiTaskPlannerService(provider_factory=lambda: provider)
tasks = service.plan("Write a weekly report")
assert len(tasks) == 1
assert tasks[0]["title"] == "Draft report"
assert provider.call_count == 1
def test_plan_prefers_an_explicit_provider_over_the_factory():
factory_provider = FakeProvider([ScriptedTurn(text=_PLAN_REPLY)], strict=False)
explicit_provider = FakeProvider([ScriptedTurn(text=_PLAN_REPLY)])
service = AiTaskPlannerService(provider_factory=lambda: factory_provider)
service.plan("Write a weekly report", provider=explicit_provider)
assert explicit_provider.call_count == 1
assert factory_provider.call_count == 0
def test_plan_without_any_provider_raises_runtime_error():
service = AiTaskPlannerService(provider_factory=None)
with pytest.raises(RuntimeError):
service.plan("Write a weekly report")
def test_plan_stamps_attachments_onto_every_generated_task():
two_tasks_reply = json.dumps({"tasks": [
{"title": "A", "task_type": "cowork"},
{"title": "B", "task_type": "cowork"},
]})
provider = FakeProvider([ScriptedTurn(text=two_tasks_reply)])
service = AiTaskPlannerService(provider_factory=lambda: provider)
tasks = service.plan("do two things", file_paths=["a.txt"], links=["https://x"])
assert len(tasks) == 2
for t in tasks:
assert t["input"]["file_paths"] == ["a.txt"]
assert t["input"]["links"] == ["https://x"]
def test_import_file_delegates_to_core_task_import(tmp_path):
csv_path = tmp_path / "tasks.csv"
csv_path.write_text("title,task_type,priority\nMy Task,cowork,medium\n", encoding="utf-8")
service = AiTaskPlannerService()
tasks = service.import_file(csv_path)
assert len(tasks) == 1
assert tasks[0]["title"] == "My Task"
def test_import_file_raises_value_error_on_unsupported_extension(tmp_path):
bogus = tmp_path / "tasks.txt"
bogus.write_text("nope", encoding="utf-8")
service = AiTaskPlannerService()
with pytest.raises(ValueError):
service.import_file(bogus)