|
| 1 | +# Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""Adapter: lift the email sidecar's in-process clock jobs into the daemon |
| 4 | +clock (V2-15, #2156). |
| 5 | +
|
| 6 | +This is the email-specific half of the reconciliation. It reads the two |
| 7 | +embedded clocks' durable state and maps it to the daemon's hub-safe |
| 8 | +:class:`~gaia.daemon.scheduler.models.MigratableJob` vocabulary, then hands the |
| 9 | +batch to the core reconciler. Living in the hub package keeps the dependency |
| 10 | +direction legal: hub -> core is fine, and core never learns anything |
| 11 | +email-specific. |
| 12 | +
|
| 13 | +Two sources fold in here: |
| 14 | +
|
| 15 | +- ``EmailJobScheduler`` / ``schedule_store`` (#1919) — persistent one-shot jobs |
| 16 | + (scheduled send, snooze) already keyed by a stable ``job_id``. Each pending |
| 17 | + job becomes a one-shot :class:`MigratableJob`. |
| 18 | +- ``BriefingScheduler`` (#1918) — a recurring daily brief configured from env, |
| 19 | + with no per-job row. When enabled it contributes a single recurring |
| 20 | + :class:`MigratableJob` with a synthetic-but-stable ``source_job_id`` so a |
| 21 | + re-run migrates it exactly once. |
| 22 | +
|
| 23 | +The reconcile is idempotent: run it every time the daemon (re)adopts the email |
| 24 | +sidecar; the ledger makes the second and later passes no-ops. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +from datetime import datetime |
| 30 | +from typing import List, Optional |
| 31 | + |
| 32 | +from gaia_agent_email import schedule_store |
| 33 | +from gaia_agent_email.briefing import ( |
| 34 | + BriefingScheduleConfig, |
| 35 | + seconds_until_next_run, |
| 36 | +) |
| 37 | + |
| 38 | +from gaia.daemon.scheduler import ( |
| 39 | + KIND_ONE_SHOT, |
| 40 | + KIND_RECURRING, |
| 41 | + MigratableJob, |
| 42 | + MigrationResult, |
| 43 | + assert_no_dropped, |
| 44 | + reconcile_jobs, |
| 45 | +) |
| 46 | + |
| 47 | +# Source names recorded in the daemon migration ledger. Stable strings — they |
| 48 | +# key the exactly-once guard, so they must never change once shipped. |
| 49 | +SOURCE_ONE_SHOT = "email:schedule_store" |
| 50 | +SOURCE_BRIEFING = "email:briefing" |
| 51 | + |
| 52 | +# The briefing has no per-job row; this fixed id gives it a stable ledger key so |
| 53 | +# re-adoption is idempotent. One daily brief per sidecar identity. |
| 54 | +BRIEFING_JOB_ID = "daily_inbox_briefing" |
| 55 | + |
| 56 | + |
| 57 | +def collect_one_shot_jobs(db) -> List[MigratableJob]: |
| 58 | + """Every still-pending one-shot email job as a :class:`MigratableJob`.""" |
| 59 | + schedule_store.init_schema(db) |
| 60 | + jobs: List[MigratableJob] = [] |
| 61 | + for row in schedule_store.list_jobs(db, status=schedule_store.STATUS_PENDING): |
| 62 | + jobs.append( |
| 63 | + MigratableJob( |
| 64 | + source=SOURCE_ONE_SHOT, |
| 65 | + source_job_id=row["job_id"], |
| 66 | + kind=KIND_ONE_SHOT, |
| 67 | + fire_at=row["due_at"], |
| 68 | + payload={ |
| 69 | + "kind": row["kind"], |
| 70 | + "mailbox": row["mailbox"], |
| 71 | + "payload": row["payload"], |
| 72 | + }, |
| 73 | + ) |
| 74 | + ) |
| 75 | + return jobs |
| 76 | + |
| 77 | + |
| 78 | +def collect_briefing_job( |
| 79 | + config: BriefingScheduleConfig, *, now: Optional[datetime] = None |
| 80 | +) -> List[MigratableJob]: |
| 81 | + """The daily briefing as a recurring :class:`MigratableJob`, or [] when off. |
| 82 | +
|
| 83 | + A disabled briefing contributes nothing — matching the embedded scheduler, |
| 84 | + which creates no task when disabled. The first ``fire_at`` is the next local |
| 85 | + occurrence of the configured time; the interval is a fixed 24h. |
| 86 | + """ |
| 87 | + config.validate() |
| 88 | + if not config.enabled: |
| 89 | + return [] |
| 90 | + reference = now or datetime.now() |
| 91 | + delay = seconds_until_next_run(config.time_of_day, reference) |
| 92 | + return [ |
| 93 | + MigratableJob( |
| 94 | + source=SOURCE_BRIEFING, |
| 95 | + source_job_id=BRIEFING_JOB_ID, |
| 96 | + kind=KIND_RECURRING, |
| 97 | + interval_seconds=86400, |
| 98 | + fire_at=reference.timestamp() + delay, |
| 99 | + payload={ |
| 100 | + "time_of_day": config.time_of_day, |
| 101 | + "max_messages": config.max_messages, |
| 102 | + }, |
| 103 | + ) |
| 104 | + ] |
| 105 | + |
| 106 | + |
| 107 | +def migrate_email_clocks( |
| 108 | + db, |
| 109 | + *, |
| 110 | + briefing_config: Optional[BriefingScheduleConfig] = None, |
| 111 | + now: Optional[datetime] = None, |
| 112 | + verify_no_dropped: bool = True, |
| 113 | +) -> MigrationResult: |
| 114 | + """Adopt both embedded email clocks into the daemon clock, exactly once. |
| 115 | +
|
| 116 | + ``db`` is a ``DatabaseMixin`` handle open on the daemon clock's store (the |
| 117 | + same file the daemon drives). Pass ``briefing_config`` to fold the daily |
| 118 | + brief in; omit it to migrate only the one-shot jobs. |
| 119 | +
|
| 120 | + When ``verify_no_dropped`` is set (the default) the one-shot batch is |
| 121 | + checked with :func:`assert_no_dropped` after the pass — every pending email |
| 122 | + job must have reached the ledger, or a :class:`DroppedJobError` is raised so |
| 123 | + the migration fails loudly instead of silently losing a scheduled send. |
| 124 | + """ |
| 125 | + one_shot = collect_one_shot_jobs(db) |
| 126 | + briefing = ( |
| 127 | + collect_briefing_job(briefing_config, now=now) |
| 128 | + if briefing_config is not None |
| 129 | + else [] |
| 130 | + ) |
| 131 | + result = reconcile_jobs(db, one_shot + briefing) |
| 132 | + |
| 133 | + if verify_no_dropped: |
| 134 | + assert_no_dropped( |
| 135 | + db, |
| 136 | + source=SOURCE_ONE_SHOT, |
| 137 | + source_job_ids=[j.source_job_id for j in one_shot], |
| 138 | + ) |
| 139 | + if briefing: |
| 140 | + assert_no_dropped( |
| 141 | + db, |
| 142 | + source=SOURCE_BRIEFING, |
| 143 | + source_job_ids=[BRIEFING_JOB_ID], |
| 144 | + ) |
| 145 | + return result |
| 146 | + |
| 147 | + |
| 148 | +__all__: List[str] = [ |
| 149 | + "BRIEFING_JOB_ID", |
| 150 | + "SOURCE_BRIEFING", |
| 151 | + "SOURCE_ONE_SHOT", |
| 152 | + "collect_briefing_job", |
| 153 | + "collect_one_shot_jobs", |
| 154 | + "migrate_email_clocks", |
| 155 | +] |
0 commit comments