ReMe/reme/steps/proactive/agent/base.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

92 lines
3.9 KiB
Python

"""Typed runtime access and workspace authorization shared by agent steps."""
import asyncio
from uuid import uuid4
from ...base_step import BaseStep, Ref
from ...file_io import check_path_permission
from ....components.proactive_runtime import BaseProactiveRuntime
from ....enumeration import ComponentEnum
from ....schema import AgentActivity, Response, collect_reply
from ._activity import ActivityTracker
class BaseAgentStep(BaseStep):
"""Keep request handlers stateless; the application component owns the runtime."""
proactive_runtime: BaseProactiveRuntime = Ref(BaseProactiveRuntime, ComponentEnum.PROACTIVE_RUNTIME)
async def query_reply(self, agent_name, query, *, on_chunk=None):
"""Ask a disposable fork while retaining the selected task session."""
runtime = self.proactive_runtime
options = {"job_tools": [], "sandbox": "read-only", "approval_mode": "deny_all"}
async with runtime.query_session(agent_name, options) as session_id:
async def observe(chunk):
await on_chunk(chunk.model_copy(update={"session_id": chunk.session_id or session_id}))
return await collect_reply(
runtime.agent_wrapper.reply_stream(
query,
resume=session_id,
**options,
),
on_chunk=observe if on_chunk else None,
)
async def stream_reply(self, query: str, *, session_id: str, agent_name: str, on_chunk, **options) -> Response:
"""Relay a formal conversation and preserve its terminal outcome and native metadata."""
runtime = self.proactive_runtime
tracker = ActivityTracker(
AgentActivity(
session_id=session_id,
execution_session_id=session_id,
turn_id=uuid4().hex,
phase="thinking",
started_at=runtime.now(),
),
)
runtime.activities[agent_name] = tracker.activity
async def observe(chunk):
if chunk.turn_id:
tracker.activity = tracker.activity.model_copy(
update={
"turn_id": chunk.turn_id,
"execution_session_id": chunk.session_id or session_id,
},
)
runtime.activities[agent_name] = tracker.update(chunk)
publish(chunk)
await on_chunk(chunk.model_copy(update={"session_id": chunk.session_id or session_id}))
try:
with runtime.live_turns.observe(agent_name, session_id, query) as publish:
result = await collect_reply(
runtime.agent_wrapper.reply_stream(query, resume=session_id, **options),
on_chunk=observe,
)
except BaseException as exc:
phase = "interrupted" if isinstance(exc, asyncio.CancelledError) else "failed"
runtime.activities[agent_name] = tracker.activity.model_copy(update={"phase": phase, "tool": None})
raise
response = self.context.response
response.success = result["status"] == "completed" and self.context.stream_error is None
response.answer = result["result"]
response.metadata.update(result, agent_name=agent_name)
return response
def require_paths(self, *paths):
"""Preserve fail-closed tool bindings before accessing runtime files."""
if any(
not check_path_permission(self.workspace_path, path, self.context.get("_allowed_paths")) for path in paths
):
raise PermissionError("no permission to access agent runtime files")
def parameters(self):
"""Separate server-injected context from public request arguments."""
return {
key: value
for key, value in self.context.data.items()
if key not in {"metadata", "_allowed_paths", "_agent_name", "_inbox_turn"}
}