# V1 Block 19 — Repository & Persistence Abstraction

**Purpose**: Define repository interfaces and memory adapter.

> **Note for Codex**: Follow tasks exactly; avoid exploratory calls when tasks specify names/files/tests.

## Chunk 19.1 — Define repository interfaces
Recommended level: **XHIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Create Dart library **`packages/scheduler_persistence/lib/persistence.dart`** and declare abstract classes `TaskRepository`, `ProjectRepository`, `LockedBlockRepository`, `SettingsRepository`, `ScheduleSnapshotRepository`.
2. Each repository must expose **only** domain objects from `packages/scheduler_core` (import that package).
3. Add shared value objects in `scheduler_core` if missing (e.g., `Revision`, `OwnerId`, `PageRequest`, `Page<T>`).
4. Document every method with triple‑slash Dart doc.
5. Publish typedef `RepoResult<T>` = `Either<RepositoryFailure,T>` (use sealed class).

### Acceptance criteria
1. `dart analyze` shows 0 hints in `scheduler_persistence`.
2. Running `dart test` passes new autogenerated stub tests in `packages/scheduler_persistence/test/repo_contract_smoke_test.dart`.

### Completed implementation
1. Rehomed the core package to `packages/scheduler_core` with package name `scheduler_core`.
2. Added shared repository value objects in `scheduler_core`: `OwnerId`, `Revision`, `PageRequest`, and `Page<T>`.
3. Added package `packages/scheduler_persistence` with domain-only repository contracts for tasks, projects, locked blocks, settings, and schedule snapshots.
4. Added sealed repository failure types, sealed `Either<L,R>`, and `RepoResult<T>`.
5. Added `packages/scheduler_persistence/test/repo_contract_smoke_test.dart`.

### Verification
1. `dart format test/scheduler_core_test.dart packages/scheduler_core packages/scheduler_persistence`: passed.
2. `dart analyze`: passed, no issues found.
3. `cd packages/scheduler_persistence && dart analyze`: passed, no issues found.
4. `cd packages/scheduler_persistence && dart test`: passed, 3 tests.
5. `cd packages/scheduler_core && dart test`: passed, 298 tests.
6. `dart test`: passed, 301 tests.

---

## Chunk 19.2 — Memory adapter & conformance tests
Recommended level: **HIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Implement **`packages/scheduler_persistence_memory`** with class `InMemoryTaskRepository` etc. They must implement optimistic revision (increment int).
2. Add conformance test suite in `scheduler_persistence/test/repo_conformance.dart` with shared `runRepositoryComplianceTests()`.
3. Ensure cloning (deep copy) on return values using `jsonEncode/jsonDecode` for simplicity.

### Acceptance criteria
1. In‑memory adapter passes conformance suite covering CRUD, paging, optimistic revision conflict.
2. 100% statement coverage > 80% lines in this package.

### Completed implementation
1. Added package `packages/scheduler_persistence_memory` and workspace wiring.
2. Added in-memory implementations for task, project, locked block, settings, and schedule snapshot repository contracts.
3. Implemented owner scoping, duplicate-id checks, compare-and-set saves/deletes/archives, stale-revision failures, and deterministic offset paging.
4. Added JSON round-trip defensive cloning through the existing core document mappers.
5. Added shared conformance suite `packages/scheduler_persistence/test/repo_conformance.dart`.
6. Added `packages/scheduler_persistence_memory/test/memory_repository_conformance_test.dart` and root test wrapper wiring.

### Verification
1. `dart pub get`: passed.
2. `dart format test/scheduler_core_test.dart packages/scheduler_persistence/test/repo_conformance.dart packages/scheduler_persistence_memory`: passed.
3. `dart analyze`: passed, no issues found.
4. `cd packages/scheduler_persistence && dart analyze`: passed, no issues found.
5. `cd packages/scheduler_persistence_memory && dart analyze`: passed, no issues found.
6. `cd packages/scheduler_persistence && dart test`: passed, 3 tests.
7. `cd packages/scheduler_persistence_memory && dart test`: passed, 7 tests.
8. `cd packages/scheduler_core && dart test`: passed, 298 tests.
9. `dart test`: passed, 308 tests.
10. `cd packages/scheduler_persistence_memory && dart test --coverage=coverage`: passed; generated LCOV line coverage is 93.79% (302/322).

---
