Skip to content

Commit c384439

Browse files
committed
perf(cron): enhance future task session isolation
fixes: #5392
1 parent 87d2750 commit c384439

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

astrbot/core/tools/cron_tools.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import Any
23

34
from pydantic import Field
45
from pydantic.dataclasses import dataclass
@@ -8,6 +9,14 @@
89
from astrbot.core.astr_agent_context import AstrAgentContext
910

1011

12+
def _extract_job_session(job: Any) -> str | None:
13+
payload = getattr(job, "payload", None)
14+
if not isinstance(payload, dict):
15+
return None
16+
session = payload.get("session")
17+
return str(session) if session is not None else None
18+
19+
1120
@dataclass
1221
class CreateActiveCronTool(FunctionTool[AstrAgentContext]):
1322
name: str = "create_future_task"
@@ -119,9 +128,15 @@ async def call(
119128
cron_mgr = context.context.context.cron_manager
120129
if cron_mgr is None:
121130
return "error: cron manager is not available."
131+
current_umo = context.context.event.unified_msg_origin
122132
job_id = kwargs.get("job_id")
123133
if not job_id:
124134
return "error: job_id is required."
135+
job = await cron_mgr.db.get_cron_job(str(job_id))
136+
if not job:
137+
return f"error: cron job {job_id} not found."
138+
if _extract_job_session(job) != current_umo:
139+
return "error: you can only delete future tasks in the current umo."
125140
await cron_mgr.delete_job(str(job_id))
126141
return f"Deleted cron job {job_id}."
127142

@@ -148,8 +163,13 @@ async def call(
148163
cron_mgr = context.context.context.cron_manager
149164
if cron_mgr is None:
150165
return "error: cron manager is not available."
166+
current_umo = context.context.event.unified_msg_origin
151167
job_type = kwargs.get("job_type")
152-
jobs = await cron_mgr.list_jobs(job_type)
168+
jobs = [
169+
job
170+
for job in await cron_mgr.list_jobs(job_type)
171+
if _extract_job_session(job) == current_umo
172+
]
153173
if not jobs:
154174
return "No cron jobs found."
155175
lines = []

0 commit comments

Comments
 (0)