Skip to content

Commit d7712d2

Browse files
authored
Fix/reminderbug 修复定时任务的bug (#349)
* update * fix: 修复cron任务执行时的agent引用问题并优化提醒行为 1. 修复 AttributeError: 'function' object has no attribute 'process_direct' - 使用 mutable holder pattern 解决 agent 初始化顺序问题 - 在 prepare_agent_loop 中设置 cron._agent_holder['agent'] 引用 2. 优化 cron 提醒的 agent 行为 - 给 agent 发送明确的 [CRON TASK] 指令 - 告知 agent 这是定时任务,不是用户对话输入 - 避免 agent 把提醒消息当作用户问题来回复
1 parent 695a07e commit d7712d2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

bot/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ build/
3535
coverage/
3636

3737
test/
38+
data/
3839

3940
CLAUDE.md

bot/vikingbot/cli/commands.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ def prepare_agent_loop(config, bus, session_manager, cron):
257257
sandbox_manager=sandbox_manager,
258258
config=config,
259259
)
260+
# Set the agent reference in cron if it uses the holder pattern
261+
if hasattr(cron, '_agent_holder'):
262+
cron._agent_holder['agent'] = agent
260263
return agent
261264

262265

@@ -265,12 +268,35 @@ def prepare_cron(bus) -> CronService:
265268
cron_store_path = get_data_dir() / "cron" / "jobs.json"
266269
cron = CronService(cron_store_path)
267270

271+
# Use a mutable holder for the agent reference
272+
agent_holder = {"agent": None}
273+
268274
# Set cron callback (needs agent)
269275
async def on_cron_job(job: CronJob) -> str | None:
270276
"""Execute a cron job through the agent."""
271277
session_key = SessionKey(**json.loads(job.payload.session_key_str))
272-
response = await agent.process_direct(
273-
job.payload.message,
278+
message = job.payload.message
279+
280+
if agent_holder["agent"] is None:
281+
raise RuntimeError("Agent not initialized yet")
282+
283+
# Clear instructions: let agent know this is a cron task to deliver
284+
cron_instruction = f"""[CRON TASK]
285+
This is a scheduled task triggered by cron job: '{job.name}'
286+
Your task is to deliver the following reminder message to the user.
287+
288+
IMPORTANT:
289+
- This is NOT a user message - it's a scheduled reminder you need to send
290+
- You should acknowledge/confirm the reminder and send it in a friendly way
291+
- DO NOT treat this as a question from the user
292+
- Simply deliver the reminder message as requested
293+
294+
Reminder message to deliver:
295+
\"\"\"{message}\"\"\"
296+
"""
297+
298+
response = await agent_holder["agent"].process_direct(
299+
cron_instruction,
274300
session_key=session_key,
275301
)
276302
if job.payload.deliver:
@@ -285,6 +311,7 @@ async def on_cron_job(job: CronJob) -> str | None:
285311
return response
286312

287313
cron.on_job = on_cron_job
314+
cron._agent_holder = agent_holder
288315

289316
cron_status = cron.status()
290317
if cron_status["jobs"] > 0:

0 commit comments

Comments
 (0)