mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-27 23:58:47 +00:00
Some checks are pending
CI / Python packages / Build and verify distributions (push) Waiting to run
CI / Python quality / GitHub Actions (push) Waiting to run
CI / Python quality / Pre-commit (push) Waiting to run
CI / Python tests / Hermes embedded - ubuntu-latest (push) Waiting to run
CI / Python tests / Hermes embedded - windows-latest (push) Waiting to run
CI / Python tests / Unit Tests - py3.11 (push) Waiting to run
CI / Python tests / Unit Tests - py3.12 (push) Waiting to run
CI / Python tests / Unit Tests - py3.13 (push) Waiting to run
CI / Python tests / Unit Tests - py3.14 (push) Waiting to run
CI / ReMe Studio / Studio checks (push) Waiting to run
CI / Windows / CLI smoke - py3.11 (push) Waiting to run
Deploy / Documentation / Build documentation (push) Waiting to run
Deploy / Documentation / deploy (push) Blocked by required conditions
Security / CodeQL / Analyze javascript-typescript (push) Waiting to run
Security / CodeQL / Analyze python (push) Waiting to run
* refactor(proactive)!: separate runtime ownership and unify delivery * refactor(proactive): finish runtime and Studio migration * test(proactive): make scheduled skip coverage deterministic * refactor(studio): unify navigation and notebook resource loading * feat(studio): edit workspace documents and update proactive task names * refactor(studio): simplify task settings and navigation * feat(studio): secure and supervise backend settings * feat(studio): unify workbench timelines and refine desktop updates Use shared timeline panes for notebook, memory, and resources. Add Todo creation ordering, persist companion visibility, and align desktop update handling and release artifacts. Recheck editor file versions at read and publication boundaries. Retry reminder detail requests from the resource view. * fix(studio): support unsigned GitHub preview releases * fix: verify container state and isolate proactive view errors Mount private supervisor state in Docker smoke tests and verify token and settings persistence. Exercise Studio startup with a loopback-only model fixture, and exclude packaged desktop output from Docker contexts. Keep Remy subscriptions and healthy tasks available when an individual task has an invalid workspace path or session history. * fix(home): align candidate Docker context exclusions * chore(deps): update openai-codex dependency to v0.172.0 - Bump openai-codex from version 0.169.0 to 0.172.0 in pyproject.toml - Keep other dependencies unchanged - Ensure compatibility with latest openai-codex features and fixes * fix(daemon): discover parent dotenv and isolate listeners Restore parent-directory environment discovery in workers while retaining system precedence. Partition the default supervisor profile and lock by the configured bind address and port.
258 lines
11 KiB
Python
258 lines
11 KiB
Python
"""Execute and schedule one named Proactive Runtime."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from contextlib import asynccontextmanager
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import TYPE_CHECKING
|
|
|
|
from ...schema.agent_activity import AgentActivity
|
|
from ...schema.agent_inbox import InboxReceipt
|
|
from ...schema.event import Event
|
|
from ...schema.proactive_agent import CRON_EVENT_SOURCE, ProactiveRuntimeDefinition
|
|
from ...utils.async_utils import finish_cleanup
|
|
from ...utils.cron_utils import next_cron_time
|
|
from ...utils.timezone_utils import resolve_timezone
|
|
|
|
if TYPE_CHECKING:
|
|
from ..proactive_manager.local_proactive_manager import LocalProactiveManager
|
|
|
|
|
|
class ProactiveRuntime:
|
|
"""Own one named inbox, active worker, execution lock and retry state."""
|
|
|
|
def __init__(self, manager: LocalProactiveManager, agent_name: str):
|
|
self.manager = manager
|
|
self.agent_name = agent_name
|
|
self.worker: asyncio.Task | None = None
|
|
self.tasks: set[asyncio.Task] = set()
|
|
self.lock = asyncio.Lock()
|
|
self.schedule: tuple[tuple[str, str | None], datetime | None] | None = None
|
|
self.started_at: datetime | None = None
|
|
self.error: str | None = None
|
|
self.scheduler_error: str | None = None
|
|
self.retry_at: datetime | None = None
|
|
self.retry_items: list[str] | None = None
|
|
self.retry_delay = 0.0
|
|
|
|
async def definition(self) -> ProactiveRuntimeDefinition:
|
|
"""Read the current definition with the manager's mutation lock held."""
|
|
if self.manager.runtimes.get(self.agent_name) is not self:
|
|
raise ValueError(f"Proactive Runtime is no longer registered: {self.agent_name}")
|
|
return await self.manager.definition(self.agent_name)
|
|
|
|
async def mount(self, definition: ProactiveRuntimeDefinition):
|
|
"""Route this runtime's events to its workspace inbox directory."""
|
|
manager = self.manager
|
|
directory = manager.registry.directory_for(definition) / "inbox"
|
|
await manager.inbox.mount(self.agent_name, directory.relative_to(manager.workspace_path).as_posix())
|
|
|
|
async def append(self, events: list[Event]) -> list[str]:
|
|
"""Append to this inbox and wake the scheduler."""
|
|
identifiers = await self.manager.inbox.append(self.agent_name, events, wait=False)
|
|
self.manager.changed.set()
|
|
return identifiers
|
|
|
|
@asynccontextmanager
|
|
async def track(self):
|
|
"""Track in-flight requests before they can block."""
|
|
if self.manager.closed:
|
|
raise RuntimeError("Proactive Manager is closed")
|
|
task = asyncio.current_task()
|
|
self.tasks.add(task)
|
|
try:
|
|
yield
|
|
finally:
|
|
self.tasks.discard(task)
|
|
|
|
async def stop(self):
|
|
"""Cancel this runtime's worker and requests before unregistering it."""
|
|
tasks = self.tasks.copy()
|
|
if self.worker is not None:
|
|
tasks.add(self.worker)
|
|
for task in tasks:
|
|
task.cancel()
|
|
if tasks:
|
|
await finish_cleanup(asyncio.gather(*tasks, return_exceptions=True))
|
|
|
|
def configure(self, definition: ProactiveRuntimeDefinition):
|
|
"""Forget a scheduled trigger when it ceases to apply."""
|
|
if not definition.enabled or (self.schedule is not None and self.schedule[0][0] != definition.cron):
|
|
self.schedule = None
|
|
|
|
def next_trigger(self, definition: ProactiveRuntimeDefinition) -> datetime | None:
|
|
"""Read the next scheduled trigger without mutating execution state."""
|
|
if definition.enabled and definition.cron is not None:
|
|
return self._cron_schedule(definition.cron, self.manager.now())[1]
|
|
return None
|
|
|
|
def recover_receipt(self, receipt: InboxReceipt | None) -> None:
|
|
"""Clear a retry selection if its acknowledgement was already committed."""
|
|
if self.retry_items and receipt is not None and set(self.retry_items).issubset(receipt.item_ids):
|
|
self.retry_items = None
|
|
|
|
async def run(self, execute, *, record=None):
|
|
"""Commit successful execution before handing asynchronous delivery to the outbox."""
|
|
manager = self.manager
|
|
agent_name = self.agent_name
|
|
async with self.track(), self.lock:
|
|
async with manager.management_lock:
|
|
agent = await self.definition()
|
|
if not agent.enabled:
|
|
return {"agent_name": agent_name, "skipped": "disabled"}
|
|
await self.mount(agent)
|
|
if self.retry_items:
|
|
self.recover_receipt((await manager.inbox.snapshot(agent_name))["receipt"])
|
|
is_cron = agent.cron is not None
|
|
batch = await manager.inbox.claim(
|
|
agent_name,
|
|
limit=1 if is_cron else agent.max_batch_events,
|
|
item_ids=self.retry_items,
|
|
)
|
|
if batch is None:
|
|
return {"agent_name": agent_name, "processed": 0}
|
|
self.retry_items = [item["id"] for item in batch["items"]]
|
|
owns_worker = self.worker is None
|
|
if owns_worker:
|
|
self.worker = asyncio.current_task()
|
|
try:
|
|
self.started_at = manager.now()
|
|
manager.activities[agent_name] = AgentActivity(
|
|
session_id="",
|
|
turn_id=batch["token"],
|
|
source="task",
|
|
task_id=agent_name,
|
|
started_at=self.started_at,
|
|
phase="working",
|
|
)
|
|
if any((item["source"] == CRON_EVENT_SOURCE) != is_cron for item in batch["items"]):
|
|
raise ValueError(f"Inbox inputs do not match the trigger type for {agent_name}")
|
|
reminder, session_id = await execute(agent, batch["items"])
|
|
manager.activities[agent_name] = manager.activities[agent_name].model_copy(
|
|
update={"session_id": session_id},
|
|
)
|
|
|
|
async def commit():
|
|
async with manager.outbox.handoff(agent_name, reminder, record) as ref:
|
|
await manager.inbox.ack(
|
|
agent_name,
|
|
batch["token"],
|
|
metadata={"reminder_path": ref, "session_id": session_id},
|
|
retain=ref is not None,
|
|
)
|
|
self.retry_items = None
|
|
|
|
await finish_cleanup(commit())
|
|
manager.logger.info(
|
|
f"Proactive agent={agent_name} session={session_id} result=committed "
|
|
f"processed={len(batch['items'])} notifications={int(reminder is not None)}",
|
|
)
|
|
manager.activities[agent_name] = manager.activities[agent_name].model_copy(
|
|
update={"phase": "completed"},
|
|
)
|
|
manager.changed.set()
|
|
return {
|
|
"agent_name": agent_name,
|
|
"processed": len(batch["items"]),
|
|
"notifications": [reminder.model_dump()] if reminder is not None else [],
|
|
}
|
|
except BaseException as exc:
|
|
phase = "interrupted" if isinstance(exc, asyncio.CancelledError) else "failed"
|
|
manager.activities[agent_name] = manager.activities[agent_name].model_copy(update={"phase": phase})
|
|
raise
|
|
finally:
|
|
try:
|
|
await finish_cleanup(manager.inbox.nack(agent_name, batch["token"]))
|
|
except ValueError:
|
|
pass
|
|
except RuntimeError as exc:
|
|
manager.logger.warning(f"Could not release failed inbox claim for {agent_name}: {exc}")
|
|
finally:
|
|
if owns_worker:
|
|
self.worker = None
|
|
|
|
def _next_cron_time(self, expression: str, now: datetime) -> datetime | None:
|
|
"""Return no trigger for an exhausted schedule without hiding its retained task."""
|
|
from croniter import CroniterBadDateError
|
|
|
|
anchor = now.astimezone(resolve_timezone(self.manager.app_context.app_config.timezone))
|
|
try:
|
|
return next_cron_time(expression, anchor)
|
|
except CroniterBadDateError:
|
|
return None
|
|
|
|
def _cron_schedule(self, expression: str, now: datetime):
|
|
key = (expression, self.manager.app_context.app_config.timezone)
|
|
if self.schedule is not None and self.schedule[0] == key:
|
|
return self.schedule
|
|
return key, self._next_cron_time(expression, now)
|
|
|
|
async def _schedule_cron(self, agent):
|
|
now = self.manager.now()
|
|
self.schedule = self._cron_schedule(agent.cron, now)
|
|
key, scheduled = self.schedule
|
|
if scheduled is None or scheduled.timestamp() > now.timestamp():
|
|
return
|
|
await self.append(
|
|
[
|
|
Event(
|
|
id=f"cron-{self.agent_name}-{int(scheduled.timestamp())}",
|
|
event_ts=scheduled,
|
|
source=CRON_EVENT_SOURCE,
|
|
description=f"Scheduled trigger: {scheduled.isoformat()}",
|
|
),
|
|
],
|
|
)
|
|
self.schedule = key, self._next_cron_time(agent.cron, now)
|
|
|
|
async def tick(self, definition, run_job):
|
|
"""Queue a due trigger and start at most one eligible worker."""
|
|
self.configure(definition)
|
|
if not definition.enabled:
|
|
return
|
|
await self.mount(definition)
|
|
manager = self.manager
|
|
if definition.cron is not None:
|
|
try:
|
|
await self._schedule_cron(definition)
|
|
except TimeoutError:
|
|
manager.logger.warning(f"Cron input deferred for {self.agent_name}: inbox capacity is full")
|
|
if self.worker is not None or (
|
|
self.retry_at is not None and self.retry_at.timestamp() > manager.now().timestamp()
|
|
):
|
|
return
|
|
snapshot = await manager.inbox.snapshot(self.agent_name)
|
|
if not snapshot["pending_count"]:
|
|
return
|
|
receipt = snapshot["receipt"]
|
|
if (
|
|
definition.cron is None
|
|
and receipt is not None
|
|
and definition.min_trigger_interval_seconds
|
|
and manager.now().timestamp() - receipt.consume_ts.timestamp() < definition.min_trigger_interval_seconds
|
|
):
|
|
return
|
|
self.worker = asyncio.create_task(self._work(run_job), name=f"agent:{self.agent_name}")
|
|
|
|
async def _work(self, run_job):
|
|
manager = self.manager
|
|
agent_name = self.agent_name
|
|
try:
|
|
response = await run_job("run_proactive_agent", agent_name=agent_name)
|
|
if not response.success:
|
|
raise RuntimeError(str(response.answer))
|
|
self.error = self.retry_at = None
|
|
self.retry_delay = 0
|
|
except Exception as exc:
|
|
self.error = str(exc)[:1000]
|
|
delay = min(
|
|
max(300, manager.retry_seconds),
|
|
self.retry_delay * 2 if self.retry_delay else manager.retry_seconds,
|
|
)
|
|
self.retry_delay = delay
|
|
self.retry_at = manager.now().astimezone(timezone.utc) + timedelta(seconds=delay)
|
|
manager.logger.exception(f"Proactive Runtime {agent_name} failed; retry in {delay:g}s: {exc}")
|
|
finally:
|
|
self.worker = None
|
|
manager.changed.set()
|