# Scheduler Storage Mongo — Sprint 16 Handoff This module now consumes the canonical DTOs defined in `StellaOps.Scheduler.Models`. Samples covering REST shapes live under `samples/api/scheduler/` and are referenced from `docs/11_DATA_SCHEMAS.md#3.1`. ## Collections & DTO mapping | Collection | DTO | Notes | |-------------------|--------------------------|---------------------------------------------------------------------------------------| | `schedules` | `Schedule` | Persist `Schedule` as-is. `_id` → `Schedule.Id`. Use compound indexes on `{tenantId, enabled}` and `{whenCron}` per doc. | | `runs` | `Run` | Store `Run.Stats` inside the document; omit `deltas` array when empty. | | `impact_snapshots`| `ImpactSet` | Normalise selector filter fields exactly as emitted by the canonical serializer. | | `audit` | `AuditRecord` | Lower-case metadata keys are already enforced by the model. | All timestamps are persisted as UTC (`+00:00`). Empty selector filters remain empty arrays (see `impact-set.json` sample). ## Implementation guidance 1. Add a project reference to `StellaOps.Scheduler.Models` and reuse the records directly; avoid duplicate BSON POCOs. 2. When serialising/deserialising to MongoDB, call `CanonicalJsonSerializer` to keep ordering stable for diffable fixtures. 3. Integration tests should load the JSON samples and round-trip through the Mongo persistence layer to guarantee parity. 4. Follow `docs/11_DATA_SCHEMAS.md` for index requirements; update that doc if storage diverges. 5. Register `AddSchedulerMongoStorage` in the host and call `ISchedulerMongoInitializer.EnsureMigrationsAsync` during bootstrap so collections/indexes are created before workers/web APIs start. With these artefacts in place the dependency on SCHED-MODELS-16-101/102 is cleared—storage work can move to DOING. ## Repositories & Sessions (Sprint 16) - `AddSchedulerMongoStorage` now registers the scheduler repositories: - `IScheduleRepository` — CRUD helpers with tenant scoping and soft-delete markers (`deletedAt`, `deletedBy`). - `IRunRepository` — create/update/list helpers sorted by `createdAt`, honouring the TTL index on `finishedAt`. - `IImpactSnapshotRepository` — stores canonical `ImpactSet` snapshots with deterministic selector digests. - `IAuditRepository` — append/list audit log entries with optional category/schedule/run filters. - `IRunSummaryRepository` — persists the `run_summaries` materialised view keyed by tenant/schedule. - `IRunSummaryService` — projects run updates into the materialised view (latest run + rolling counters for the last 20 updates). - `ISchedulerAuditService` — convenience layer that stamps IDs/timestamps and writes `AuditRecord` instances with consistent metadata. - `ISchedulerMongoSessionFactory` provides causal-consistent Mongo sessions (default `CausalConsistency = true`) that repositories accept for read-after-write flows. - All repositories persist canonical JSON via `CanonicalJsonSerializer`; helper mappers strip internal fields before materialising DTOs. - Soft-deleted schedules keep historical documents but are excluded from queries unless `ScheduleQueryOptions.IncludeDeleted` is set; deletions also disable the schedule and bump `updatedAt/updatedBy`.