agent-inspection.md 2.84 KB

Agent 执行状态检查技术

问题

cron job 的 last_status: ok 只代表 cron 本身执行没报错,不代表 agent 干了实事。 需要查 agent 的实际产出:检测到几个任务、写了几个 Plan、是 SILENT 还是有输出。

方法:查 state.db sessions + messages 表

import sqlite3, os, time

db = os.path.expanduser("~/.hermes/state.db")
conn = sqlite3.connect(db)
conn.row_factory = sqlite3.Row

# 1. 查某个 Agent 最近 N 轮 cron 运行
rows = conn.execute("""
    SELECT id, started_at, message_count, tool_call_count, title, end_reason
    FROM sessions 
    WHERE source = 'cron' AND title LIKE '%Plan Agent%'
    ORDER BY started_at DESC LIMIT 10
""").fetchall()

# 2. 看每轮最后一句话(判断 SILENT 还是有产出)
for r in rows:
    ts = time.strftime("%m-%d %H:%M", time.localtime(r['started_at']))
    msg = conn.execute("""
        SELECT content FROM messages
        WHERE session_id = ? AND role = 'assistant'
        ORDER BY timestamp DESC LIMIT 1
    """, (r['id'],)).fetchone()
    content = msg['content'][:200] if msg and msg['content'] else 'N/A'
    is_silent = 'SILENT' in content.upper() or '静默' in content
    print(f"{ts} | msgs={r['message_count']:3d} tools={r['tool_call_count']:3d} | {'SILENT' if is_silent else 'ACTIVE'} | {content[:100]}")

# 3. 看完整执行细节(所有消息+工具调用)
msgs = conn.execute("""
    SELECT role, content, tool_name FROM messages
    WHERE session_id = ? ORDER BY timestamp ASC
""", (target_session_id,)).fetchall()

判断 Agent 是否在干活

信号 含义
msgs=2 tools=0 只回了句 [SILENT],没干活
msgs=14 tools=6 有查询、审计、诊断,但最终 SILENT
msgs=36 tools=17 有大量工具调用 = 真在干活(写 Plan)
end_reason=cron_complete 正常完成
end_reason=其他 可能异常退出

手动触发 Agent 立即执行

不想等 cron 周期?用 cronjob action=run 立即触发:

cronjob(action='run', job_id='9f6ca9d65936')  # Plan Agent
cronjob(action='run', job_id='dccc73082265')  # Dev Agent
cronjob(action='run', job_id='de73ff46d901')  # Test Agent

触发后查 state.db 最新 session 即可看到这轮的完整执行内容。

常见问题:写了任务但 Agent 看不到

  1. 写错表了 — agents 读 fcfg.json 的 app_token,不是 memory 里的值。先读 fcfg.json 确认。
  2. Sprint审核 字段为空 — query_pending.py 筛选 Sprint审核 in (空, 待审核),空值 = None 应该被选中。但如果飞书 API 返回的 None 和 Python None 不匹配,可能漏掉。
  3. 飞书 API 缓存延迟 — 写入后立即查询可能看不到,等几分钟或手动触发 cron。
  4. 分页问题 — query_pending.py 用 page_size=500,但如果表刚加了记录,飞书 API 的分页可能还没更新。