Skip to main content
Y

agent-turn-guard

yolopunk/agent-turn-guard

Tracks completed turns and model requests with portable threshold rules, then logs reminders or hands the DeepSeek Harness session to a new session in the same workspace.

Install

dsh plugin --profile web add github:yolopunk/agent-turn-guard

README

Turn Guard

Turn Guard 按会话统计已完成的 Agent 轮次,并在指定轮次执行提醒、保存进度、续接会话或自定义提示。同一套规则和 SQLite 状态可以供 Codex、DeepSeek Harness 和其他支持生命周期事件的 Agent 使用。

运行环境需要 Python 3.10+,仅使用标准库。

先选接入方式

Agent接入方式是否内置
Codex安装本地插件,使用 hooks、MCP 设置面板和 $turn-guard 技能是
DeepSeek Harness通过 Plugin Market 或 dsh plugin add 安装 Cordis bundle是,支持 notify 和 handoff
其他 Agent在轮次完成时把规范 JSON 事件传给 turn_guard.py event通用 bridge

当前动作支持范围:

动作CodexDeepSeek Harness 适配器通用 Agent
notify支持支持适配器自行显示 systemMessage
checkpoint支持暂不支持声明 capability 后由 Agent 处理
handoff支持支持同 Workspace 新会话、续接提示和旧会话停止声明 capability 后由 Agent 处理
prompt支持暂不支持声明 capability 后由 Agent 处理
compact支持 Codex App Server暂不支持由适配器实现

Codex 的 handoff 在触发时从 transcript 固化当时仍为 active 的 /goal 目标,将 goal、上一份交接摘要和本次摘要传给新聊天,并在 create_thread 返回可用的 threadId 后通过 App Server 的 thread/goal/set 恢复同一 goal。新聊天会先确认 goal 已激活,再开始业务工具。收到 created 或 queued 回执后旧会话立即停止继续原目标;新会话创建失败时只保存摘要,旧会话保持原状态。

Codex 快速开始

从公开仓库添加 Codex marketplace:

codex plugin marketplace add yolopunk/agent-turn-guard --json
codex plugin add codex-turn-guard@agent-turn-guard --json

安装后打开 Codex 的 Settings → Hooks 或运行 /hooks,审阅并信任 Turn Guard 的六条 hooks。然后新开聊天,直接说:

使用 $turn-guard 打开插件设置面板

也可以直接让 Agent 修改规则:

使用 $turn-guard 把默认规则改成每 95 轮提醒一次,提醒内容是“已完成 {turns} 轮”。

长任务可以额外开启模型请求保护。它按 Codex 的模型采样次数计数,在第 80 次后触发摘要和新会话交接:

python .\hooks\run.py model-guard set --threshold 80 --enabled true --prompt "优先保留未完成阶段、CL 号、验证证据和下一步。"

插件面板支持增删规则、调整阈值、启停规则和查看运行状态。修改插件文件或安装缓存后,完整退出并重启 Codex Desktop,新的 MCP 配置才会重新加载。

DeepSeek Harness 快速开始

目录收录后,可直接在 DSH 的 Plugins → Add plugin 搜索并安装。尚未从市场搜索到时:

在 DSH Desktop 打开 Plugins → Add plugin,输入
github:yolopunk/agent-turn-guard
然后点击 Install / Enable。

纯 dsh web 使用命令行安装,并把 desktop 换成实际 profile:

dsh plugin --profile <profile> add github:yolopunk/agent-turn-guard
~/.dsh/turn-guard/config.json
~/.dsh/turn-guard/turns.sqlite3

让 Agent 修改共享配置,避免查找已安装包路径:

把 DeepSeek Harness 的 Turn Guard 默认规则设为 95 轮 handoff。
保留其他规则,运行 status 和 config validate,并确认交接文档目录。

适配器把 DSH 生命周期映射到共享核心:

  • assistant/message → 一次模型请求,用于 modelTurnGuard
  • tool/result → 安全续跑边界;达到模型请求阈值后在这里触发交接
  • turn/end → 一个已完成的用户轮次;触发普通 threshold 规则

handoff 会先在同一 Workspace 新建会话,再从旧会话事件中提取确定性摘要,写入项目目录下的 .turn-guard/handoffs/,发送续接提示,并停止旧会话当前 turn。Codex 专属的结构化 /goal 同步由 transcript 和 App Server 处理;DeepSeek Harness 的 goal 生命周期取决于宿主能力。详细配置与失败降级见 DeepSeek Harness adapter。

其他 Agent 快速开始

先选择一个共享数据目录并初始化:

$env:TURN_GUARD_HOME = "$env:LOCALAPPDATA\TurnGuard"
python <agent-turn-guard-root>\turn_guard.py init

Agent 每完成一个用户请求,就调用一次 bridge:

$event = @{
  provider = "my-agent"
  event_type = "turn_completed"
  session_id = "stable-session-id"
  turn_id = "stable-turn-id"
  capabilities = @("notify")
  cwd = (Get-Location).Path
} | ConvertTo-Json -Compress

$event | python <agent-turn-guard-root>\turn_guard.py event

bridge 返回 JSON:

{
  "systemMessage": "[Turn Guard] 已完成 80 轮"
}

Agent 应把 systemMessage 显示在自己的 UI 或事件流中。返回 decision: "block" 时,由适配器决定如何继续模型、注入提示或阻止输入。

事件字段:

字段必填说明
provider是Agent 标识,例如 deepseek-harness、my-agent
event_type是turn_completed、prompt_submitted、interrupted 或 compaction_completed
session_id是稳定且唯一的会话 ID
turn_id完成事件必填稳定且唯一的轮次 ID,用于幂等去重
capabilities建议Agent 实际支持的动作,默认按 ["notify"] 处理
cwd可选当前工作目录
model可选当前模型
assistant_output可选续写或摘要完成时需要解析的最后一条回复
is_subagent可选子 Agent 事件应设为 true

让 Agent 快速配置

下面的提示词可以直接交给 Agent。配置前应先让它检查现状、保留已有规则,并在结束时展示验证结果。

Codex 一键配置

请在本机配置 Turn Guard Codex 插件。

1. 检查 codex-turn-guard@agent-turn-guard 是否已安装并启用。
2. 未安装时,从 yolopunk/agent-turn-guard 的公开 marketplace 安装。
3. 打开并检查现有 Turn Guard 配置,保留所有不相关规则。
4. 将规则配置为:
   - 80 轮:提醒“已完成 {turns} 轮,请整理当前进度。”
   - 95 轮:生成摘要并尝试新建会话续接。
5. 开启 modelTurnGuard,阈值为 80,动作固定为摘要并新建会话。
6. 运行 config show、config validate 和 status。
7. 告诉我需要信任哪些 hooks,以及最终配置文件路径。

不要重置轮次统计。修改失败时保持原配置。

DeepSeek Harness 一键配置

请在 DeepSeek Harness 中配置 agent-turn-guard。

1. 检查 agent-turn-guard 是否已安装并启用;未安装时运行
   dsh plugin --profile <实际profile> add github:yolopunk/agent-turn-guard
2. 检查 ~/.dsh/turn-guard/config.json,保留无关规则和现有统计。
3. 配置两条规则:
   - 80 轮:notify,显示“已完成 {turns} 轮”
   - 95 轮:handoff,生成交接文档并在同一 Workspace 新建会话
4. 需要限制单个长任务时,开启 modelTurnGuard,阈值设为 80。
5. 验证:同一 session + turn 事件重复发送只计数一次;handoff 顺序为
   create → prompt → stop old session。
6. 运行 status/config validate,报告实际配置路径与技术栈版本。

当前适配器支持 notify 和 handoff。不要声称 checkpoint、prompt 或 compact 已经可用。

其他 Agent 一键配置

请把 Turn Guard 接到当前 Agent 的生命周期。

1. 在“一个用户请求完成”事件中调用:
   <python> <agent-turn-guard>/turn_guard.py --config <config.json> event
2. 通过 stdin 发送:
   provider、event_type=turn_completed、session_id、turn_id、capabilities、cwd。
3. session_id 和 turn_id 必须稳定,同一轮不能重复计数。
4. 把返回 JSON 中的 systemMessage 显示给用户。
5. 当前只声明 Agent 已实现的能力,默认使用 capabilities=["notify"]。
6. 添加一个集成测试,证明同一 session_id + turn_id 调用两次只计一轮。

模型轮次和用户轮次

Turn Guard 统计的是一个用户请求完成后产生的 Stop 边界。一个持续目标或长任务可能在这个边界内连续执行几十次模型调用、工具调用和内部续跑,但仍然只算一个用户轮次。

模型服务提示的“当前会话模型已回复 100 轮”统计的是模型采样次数。它与普通规则的 95 轮不是同一个指标。modelTurnGuard 通过 PostModelRequest 单独记录模型请求,并在阈值后的第一个 PostToolUse 或 Stop 启动 handoff。

status 中的 modelRequests 是该会话已观察到的模型请求次数。若插件升级后才第一次看到一条长会话,Turn Guard 会从该会话 transcript 中的 token_usage_record 回填基线,再从当前请求继续计数,避免中途启用 hook 后从零计算。modelTurnGuard.threshold 应低于服务端限制,默认建议设为 80。

规则怎么配

查看现有配置:

python .\hooks\run.py config show
python .\hooks\run.py status

常用修改:

# 每 80 轮提醒
python .\hooks\run.py rule set remind-80 --threshold 80 --repeat true --action notify --message "已完成 {turns} 轮"

# 第 20 轮执行一次自定义检查
python .\hooks\run.py rule set review-20 --threshold 20 --repeat false --action prompt --prompt "检查当前任务遗漏并简要报告,不修改业务文件。"

# 修改默认规则的阈值
python .\hooks\run.py rule set default --threshold 100

# 暂停或恢复规则
python .\hooks\run.py rule disable remind-80
python .\hooks\run.py rule enable remind-80

# 删除规则
python .\hooks\run.py rule remove review-20

# 第 80 次模型请求后自动摘要并开新会话
python .\hooks\run.py model-guard set --threshold 80 --enabled true

# 暂停或恢复模型请求保护
python .\hooks\run.py model-guard disable
python .\hooks\run.py model-guard enable

完整配置示例:

{
  "version": 2,
  "enabled": true,
  "rules": [
    {
      "id": "remind-80",
      "enabled": true,
      "threshold": 80,
      "repeat": true,
      "action": "notify",
      "message": "已完成 {turns} 轮,请整理当前进度。"
    },
    {
      "id": "default",
      "enabled": true,
      "threshold": 95,
      "repeat": true,
      "action": "handoff"
    }
  ],
  "modelTurnGuard": {
    "enabled": true,
    "threshold": 80,
    "prompt": "优先保留未完成阶段、CL 号、验证证据和下一步。"
  },
  "codexCommand": ["codex"]
}

repeat: true 会在 N、2N、3N……触发。repeat: false 只在第 N 轮触发一次。规则 ID 用于保持历史去重和触发状态,修改阈值时应保留原 ID。

配置位置

Codex 默认使用:

%USERPROFILE%\.codex\turn-guard\config.json

跨 Agent 共享时设置:

$env:TURN_GUARD_HOME = "$env:LOCALAPPDATA\TurnGuard"

此时默认配置文件为:

%LOCALAPPDATA%\TurnGuard\config.json

也可以显式指定:

$env:TURN_GUARD_CONFIG = "D:\Shared\TurnGuard\config.json"

config.json、turns.sqlite3、摘要和日志都保存在这个数据目录中。升级插件不会覆盖用户配置和计数。

验证

检查配置:

python .\hooks\run.py config validate
python .\hooks\run.py config show
python .\hooks\run.py status

运行测试:

python -m unittest discover -s tests -v

模拟一个其他 Agent 的完成事件:

$env:TURN_GUARD_HOME = "$env:TEMP\TurnGuard-demo"
python .\turn_guard.py init
'{"provider":"test-agent","event_type":"turn_completed","session_id":"demo","turn_id":"1","capabilities":["notify"]}' |
  python .\turn_guard.py event

常见问题

Codex 里看不到设置面板

确认插件已启用,完整退出并重启 Codex Desktop,再新开聊天。当前聊天不会热加载新安装的 MCP 服务。

DeepSeek Harness 没有计数

确认 Cordis 组合已加载适配器,turnGuardRoot 和 configPath 是绝对路径,且 Harness 中存在 @deepseek-ai/dsh-session。适配器只处理已提交的 turn/end。

规则到了但不提醒

运行 status 检查规则是否启用、阈值是否到达,以及 capabilities 是否包含该动作。DeepSeek Harness 当前只应声明 notify。

同一轮被计数两次

检查 session_id 和 turn_id 是否稳定。不要用时间戳、随机数或每次调用都会变化的 ID 作为 turn_id。

Turn Guard 状态里没有当前会话

确认 hooks 已启用并信任。升级后如果 hook 定义变化,需要重新审阅并信任。插件 hooks 使用 ${PLUGIN_ROOT} 定位脚本,Codex 会把它展开为已安装插件目录。中途启用模型请求保护时,第一次完成的新请求会先回填 transcript 里的历史模型请求,避免从零开始。

修改规则后没有立即触发

规则修改只作用于后续轮次,不会补发历史动作。已触发的一次性规则也不会因为改阈值而重放。

开发与接口

  • Codex 生命周期:Stop、UserPromptSubmit、PostModelRequest、PostToolUse、Interrupt、PostCompact
  • 通用生命周期:turn_completed、prompt_submitted、interrupted、compaction_completed
  • 状态存储:turns.sqlite3
  • 设置面板:Codex MCP App,入口为 turn_guard_open_settings
  • DeepSeek Harness:Cordis session/event 中的 assistant/message、tool/result 和 turn/end

相关文档:

Related plugins