ReMe/reme/components/proactive_runtime/_internal/registry.py
jinliyl 524b1b6908
feat(studio): unify the workbench and proactive runtime (#764)
* fix(proactive): simplify round files and restore failed summaries

* fix(proactive): validate document-relative Markdown links

* feat(proactive_agent): implement v2 daily record workflow with single-file decision

- Add ProactiveAgentStepV2 class to support single daily note decision and reminder
- Introduce RoundOutputs for v2 round file tracking, and keep RoundArtifacts for v1 summary backup
- Refactor AgentOutbox to support both v1 standalone reminder files and v2 daily reminder records
- Add proactive_record utilities to parse and validate v2 daily record frontmatter and metadata
- Enhance proactive_agent to prepare, read, and manage artifacts for both v1 and v2 flows
- Add configuration profile proactive_v2 to enable new workflow with backward compatibility
- Update proactive.yaml and reactive proactive_agent_step configuration comment for easy switching
- Provide extensive schema validation for v2 reminders ensuring priority and message consistency
- Document v2 workflow and usage in English and Chinese runtime docs
- Add unit tests for v2 step verifying single file output and proper decision handling

* refactor(agent/runtime): replace ReminderRef with string keys and simplify reminder handling

- Remove ReminderRef model and switch to using workspace-relative string keys for reminders
- Simplify AgentOutbox to read, write, and manage reminders keyed by strings
- Change file locking and version checking to protect concurrent edits on daily record files
- Replace legacy and v2 reminder file handling with unified ReminderFiles storage
- Adjust ChatNotifications to operate with the new key scheme and update pending/read sets
- Refactor RoundOutputs to RoundRecord for safer round record tracking and rollback
- Remove unused ProactiveRunResult and related references from imports and exports
- Update InboxWakeRequest, AgentReminder, and related models for consistent ID and path validation
- Adjust proactive agent runtime and local runtime to check reminder versions before commit
- Correct language and docs to describe reminders as daily records instead of separate files
- Clean up roles of inbox and outbox for reminder ownership, delivery, and acknowledgments

* fix(proactive): serialize round commits and align desktop reminders

* refactor(proactive): unify task triggers in Proactive Runtime

* fix(proactive): bound reminder payloads and simplify receipts

* refactor(studio): share workbench and simplify proactive delivery

* fix(studio): update and centralize build dependencies

* fix(proactive): guard draft repair and verify Studio deployments

* chore(docs): update source-map-js

* feat(release): support publishing selected artifacts from a tag

* chore(studio): remove the public playground

* refactor: simplify reminders and release workflows

* fix: preserve proactive records and align release provenance

* test(proactive): remove brittle prompt and preset assertions

* fix(proactive): register reminders after input acknowledgement

* feat(notebook): list projects across todos and ideas

* feat(studio): connect mobile apps to configurable ReMe services

* fix(proactive): reserve registered reminder paths

* fix(studio): request Android local network access

* fix(proactive): reserve reminder paths referenced by receipts
2026-11-23 17:19:21 +08:00

124 lines
5.8 KiB
Python

"""Agent-owned configuration files; directory scans are the registry."""
from datetime import datetime
from pathlib import Path
import re
from ....constants import CHAT_AGENT_NAME
from ....utils.files.workspace_files import WorkspaceFiles
from ....schema.proactive_runtime import validate_agent_name
from ....schema.proactive_agent import CreateProactiveAgentConfig, ProactiveAgent, ProactiveAgentConfig
class AgentRegistry:
"""Keep one editable config per agent and no duplicate global index."""
def __init__(self, files: WorkspaceFiles, directory: str):
self.files = files
self.directory = files.path(directory)
if self.directory == files.workspace:
raise ValueError("reme_agent_dir must be a workspace subdirectory")
def directory_for(self, agent: ProactiveAgent):
"""Every task owns one stable directory directly under the Agent root."""
return self.files.checked(self.directory / agent.agent_name)
def path(self, agent_name: str):
"""Resolve chat or a retained task identity, including deleted tasks."""
validate_agent_name(agent_name)
if agent_name == CHAT_AGENT_NAME:
return self.files.checked(self.directory / CHAT_AGENT_NAME)
return self.directory_for(self.get(agent_name, include_deleted=True))
def load(self, *, include_deleted=False) -> dict[str, ProactiveAgent]:
"""Validate every config, including directory identity and global uniqueness."""
agents = []
for path in sorted(self.directory.glob("*/config.json")):
if path.parent == self.directory / CHAT_AGENT_NAME:
continue
agent = ProactiveAgent.model_validate_json(self.files.read(path))
if path.parent != self.directory_for(agent):
raise ValueError(f"agent config does not match its directory: {path}")
agents.append(agent)
self._validate_names(agents)
return {agent.agent_name: agent for agent in agents if include_deleted or agent.deleted_at is None}
def get(self, agent_name, *, include_deleted=False):
"""Resolve a missing identity explicitly rather than exposing dictionary errors."""
agents = self.load(include_deleted=include_deleted)
if agent_name not in agents:
raise ValueError(f"proactive agent not found: {agent_name}")
return agents[agent_name]
@staticmethod
def _validate_names(agents):
names = {agent.agent_name.casefold() for agent in agents}
if len(names) != len(agents):
raise ValueError("proactive agent names must be unique, including case")
for name in names:
for pattern in (r"(.+)_\d{6}", r"(.+)_\d{6}_\d+"):
match = re.fullmatch(pattern, name)
if match and match[1] in names:
raise ValueError(f"agent name conflicts with timestamped records: {name}")
def save(self, agent):
"""Publish one validated definition without rewriting other agents."""
self.files.write_json(self.directory_for(agent) / "config.json", agent.model_dump(mode="json"))
def chat_memory_path(self) -> Path:
"""Locate Remy's personal memory."""
return self.files.checked(self.path(CHAT_AGENT_NAME) / "MEMORY.md")
def task_path(self, agent: ProactiveAgent) -> Path:
"""Locate the task's editable description."""
return self.files.checked(self.directory_for(agent) / "TASK.md")
@staticmethod
def _validate_cron(config: ProactiveAgentConfig, now: datetime) -> None:
"""Reject schedules without a next occurrence before publishing configuration."""
if config.cron is not None:
from croniter import croniter
croniter(config.cron, now).get_next(datetime)
def create(self, config, now: datetime):
"""Publish the task and report restoration from the same registry snapshot."""
config = CreateProactiveAgentConfig.model_validate(config)
self._validate_cron(config, now)
agents = self.load(include_deleted=True)
previous = next(
(agent for key, agent in agents.items() if key.casefold() == config.agent_name.casefold()),
None,
)
if previous is not None:
if previous.agent_name != config.agent_name or previous.deleted_at is None:
raise ValueError(f"proactive agent name already exists: {config.agent_name}")
agent = ProactiveAgent(
**config.model_dump(exclude={"task_description"}),
created_at=previous.created_at if previous else now,
)
agents[agent.agent_name] = agent
self._validate_names(list(agents.values()))
self.files.write(self.task_path(agent), config.task_description, create_only=True)
self.save(agent)
return agent, previous is not None
def update(self, agent_name, patch, now: datetime):
"""Update execution settings independently of TASK.md."""
agent = self.get(agent_name)
if patch.get("agent_name", agent.agent_name) != agent.agent_name:
raise ValueError("agent_name is immutable")
values = agent.model_dump(include=set(ProactiveAgentConfig.model_fields))
config = ProactiveAgentConfig.model_validate({**values, **patch})
if config.cron != agent.cron or (config.enabled and not agent.enabled):
self._validate_cron(config, now)
agent = agent.model_copy(update={key: getattr(config, key) for key in ProactiveAgentConfig.model_fields})
self.save(agent)
return agent
def delete(self, agent_name, now):
"""Retain configuration and workspace history as a restorable tombstone."""
agent = self.get(agent_name)
agent = agent.model_copy(update={"deleted_at": now})
self.save(agent)
return agent