|
| 1 | +"""Run the curated seed jobs to grow B's memory when idle. |
| 2 | +
|
| 3 | +One pass audits every seed job with Agent B in learning mode (each gets a fresh, |
| 4 | +monotonic run index so patterns recur across distinct runs), then runs the janitor |
| 5 | +once to consolidate the diary into the playbook. A bot-blocked seed page degrades |
| 6 | +to a SERP-only audit (see fetch_page) rather than aborting the pass. |
| 7 | +
|
| 8 | +The agent / janitor / diary are injectable so the orchestration is testable without |
| 9 | +live LLM calls. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import asyncio |
| 15 | +import logging |
| 16 | +from collections.abc import Awaitable, Callable |
| 17 | +from typing import Protocol |
| 18 | + |
| 19 | +from pydantic import BaseModel |
| 20 | + |
| 21 | +from app.agents.agent_b import run_agent_b |
| 22 | +from app.jobs.seed_jobs import SEED_JOBS |
| 23 | +from app.memory.janitor import run_janitor |
| 24 | +from app.models import AgentRunResult |
| 25 | +from app.models.memory import PlaybookEntry |
| 26 | +from app.repositories.diary import DiaryRepository |
| 27 | +from app.tools.registry import build_default_registry |
| 28 | + |
| 29 | +logger = logging.getLogger("seed") |
| 30 | + |
| 31 | +AgentFn = Callable[..., Awaitable[AgentRunResult]] |
| 32 | +JanitorFn = Callable[[], Awaitable[list[PlaybookEntry]]] |
| 33 | + |
| 34 | + |
| 35 | +class RunCounter(Protocol): |
| 36 | + async def next_run(self) -> int: ... |
| 37 | + |
| 38 | + |
| 39 | +class SeedResult(BaseModel): |
| 40 | + jobs_run: int |
| 41 | + playbook_size: int |
| 42 | + |
| 43 | + |
| 44 | +async def run_seed_pass( |
| 45 | + *, |
| 46 | + jobs: list[tuple[str, str]] | None = None, |
| 47 | + passes: int = 1, |
| 48 | + replay: bool = False, |
| 49 | + agent: AgentFn | None = None, |
| 50 | + janitor: JanitorFn | None = None, |
| 51 | + diary: RunCounter | None = None, |
| 52 | +) -> SeedResult: |
| 53 | + """Audit every seed job with learning on, then consolidate once.""" |
| 54 | + jobs = jobs if jobs is not None else SEED_JOBS |
| 55 | + agent = agent or run_agent_b |
| 56 | + janitor = janitor or run_janitor |
| 57 | + diary = diary or DiaryRepository() |
| 58 | + registry = build_default_registry(replay=replay) |
| 59 | + |
| 60 | + jobs_run = 0 |
| 61 | + for _ in range(passes): |
| 62 | + for url, keyword in jobs: |
| 63 | + run_index = await diary.next_run() |
| 64 | + await agent( |
| 65 | + url, |
| 66 | + keyword, |
| 67 | + registry=registry, |
| 68 | + replay=replay, |
| 69 | + memory_on=True, |
| 70 | + learn=True, |
| 71 | + run_index=run_index, |
| 72 | + persist=True, |
| 73 | + ) |
| 74 | + jobs_run += 1 |
| 75 | + |
| 76 | + promoted = await janitor() |
| 77 | + logger.info("seed pass done: %d jobs, %d rules promoted", jobs_run, len(promoted)) |
| 78 | + return SeedResult(jobs_run=jobs_run, playbook_size=len(promoted)) |
| 79 | + |
| 80 | + |
| 81 | +async def run_idle_loop(*, interval_seconds: int, passes: int = 1) -> None: |
| 82 | + """Forever: run one seed pass, then sleep. Gated by config; off by default.""" |
| 83 | + while True: |
| 84 | + try: |
| 85 | + await run_seed_pass(passes=passes) |
| 86 | + except Exception: # never let one bad pass kill the idle loop |
| 87 | + logger.exception("seed pass failed; will retry next tick") |
| 88 | + await asyncio.sleep(interval_seconds) |
0 commit comments