Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions external_plugins/quota-auto-resume/.zcode-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "quota-auto-resume",
"version": "0.1.0",
"description": "额度恢复后自动恢复任务:套餐额度用到阈值即停用高消耗工具让 Agent 收尾,额度恢复后由定时任务自动续跑。",
"author": {
"name": "tech.leshang"
},
"hooks": "./hooks/hooks.json",
"commands": "./commands",
"license": "MIT",
"keywords": [
"quota",
"usage",
"hooks",
"auto-resume",
"coding-plan"
],
"description_i18n": {
"en": "Auto-resume tasks after quota recovery: stop high-cost tools when the plan quota reaches a threshold so the agent wraps up, then resume automatically through a scheduled task once quota recovers.",
"zh-CN": "额度恢复后自动恢复任务:套餐额度用到阈值即停用高消耗工具让 Agent 收尾,额度恢复后由定时任务自动续跑。"
},
"displayName": "额度恢复后自动恢复任务",
"displayName_i18n": {
"en": "Auto Resume After Quota Recovery",
"zh-CN": "额度恢复后自动恢复任务"
}
}
21 changes: 21 additions & 0 deletions external_plugins/quota-auto-resume/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 tech.leshang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions external_plugins/quota-auto-resume/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# quota-auto-resume

Stops high-cost agent tools when your ZCode plan quota hits a threshold, lets the agent wrap up, and resumes the paused session automatically once quota recovers.

## Why

Long agent tasks can burn through a daily plan quota mid-task and die without a handoff. quota-auto-resume watches the quota, cuts off the expensive tools at a configurable line (default 80%), and tells the agent to write a handoff and stop. A scheduled probe then resumes the paused session when quota is back.

## How it works

The plugin registers three hooks and one command. It reads the quota snapshots that the ZCode desktop client already writes to its local log after each poll. It makes **no network requests** and reads **no credentials**.

| Trigger | Behavior |
| --- | --- |
| `PreToolUse` | When model quota is at or above the threshold, denies high-cost tools (subagents, image and video generation, browser automation, etc.). When the official MCP call quota is at or above the threshold, denies `mcp__*` tools. Lightweight tools such as file reads, edits and ordinary shell commands stay available so the agent can wrap up. |
| `UserPromptSubmit` | From the warning line (default 60%) on, injects a one-line quota status into each turn. If this session was paused and quota has recovered, it tells the agent to continue from where it stopped. |
| `SessionStart` | Reports quota status on session start. On the first run only, asks the agent to create the 30-minute recovery probe with `CronCreate`. |
| `/quota-auto-resume` | Shows quota status, creates or rebuilds the probe, runs an interception dry run, or changes thresholds. |

Each denial records a pending-resume marker under `~/.zcode/quota-auto-resume/pending/`. The recovery probe is a recurring automation whose prompt contains the marker text `quota-auto-resume 额度恢复探测`; on each run the `UserPromptSubmit` hook injects what to do: resume the pending sessions through `SendToSession` if quota has recovered, otherwise reply with one line and stop.

## Configuration

Optional file `~/.zcode/quota-auto-resume/config.json`. Defaults:

```json
{
"threshold": 80,
"warnAt": 60,
"maxAgeMin": 20,
"highCost": "^(Agent|Task)$|image|video|browser|playwright|firefox|devtools|computer|(^|[^a-z])cua([^a-z]|$)|websearch|web_search|webfetch|web_reader|3d|tripo|blender|designcraft|screencast|node_repl"
}
```

- `threshold`: usage percentage at which high-cost tools are denied.
- `warnAt`: usage percentage at which the per-turn reminder starts.
- `maxAgeMin`: a snapshot older than this many minutes is treated as unknown and nothing is blocked.
- `highCost`: case-insensitive regular expression matched against tool names.

Set `threshold` to 100 to disable interception, or disable the plugin under Settings → Plugins.

## Requirements

- ZCode desktop client 4.x. The hook script runs on the Node runtime bundled with ZCode; no global Node or Python install is needed.
- The log directory is resolved from `ZCODE_DATA_BASE_DIR` (`.zcode/v2/logs` beneath it) and falls back to `~/.zcode/v2/logs`. Override with the `QUOTA_AUTO_RESUME_LOGS` environment variable.
- Quota snapshots are only written while the desktop client is running. If it has not polled within `maxAgeMin`, quota-auto-resume stays out of the way and blocks nothing.
- The log format is an internal detail of the desktop client and may change between versions. In that case the snapshot goes stale and the plugin fails open.

## Side effects

- Reads the tail (last 4 MB) of today's and yesterday's ZCode log files. Read-only.
- Writes `~/.zcode/quota-auto-resume/config.json` (only if you create it), `~/.zcode/quota-auto-resume/setup-notified`, and marker files under `~/.zcode/quota-auto-resume/pending/`.
- No network access, no credential access, no third-party code or services.

## Testing

```shell
node test/run-tests.cjs
node hooks/auto-resume-hook.cjs status
```

The test suite uses synthetic log fixtures and a temporary state directory; it does not touch your real quota state.

## License

MIT
66 changes: 66 additions & 0 deletions external_plugins/quota-auto-resume/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# quota-auto-resume

ZCode 套餐额度用到阈值时停用高消耗工具,让 Agent 收尾;额度恢复后自动续跑被中断的会话。

## 解决什么问题

长时间的 Agent 任务可能在半路把每日套餐额度用光,任务直接断掉,没有交接。quota-auto-resume 盯着额度,到设定的线(默认 80%)就停掉高消耗工具,并提示 Agent 写好交接说明后收尾;之后由定时探测在额度恢复时把中断的会话接着跑。

## 工作方式

插件注册三个 hook 和一个命令。额度数据取自 ZCode 桌面端每次轮询后写在本机日志里的快照,**不联网、不读凭据**。

| 触发点 | 行为 |
| --- | --- |
| `PreToolUse` | 模型额度达到阈值时,拒绝高消耗工具(子代理、生图生视频、浏览器自动化等);官方 MCP 调用额度达到阈值时,拒绝 `mcp__*` 工具。读写文件、编辑、普通命令等轻量工具照常可用,Agent 得以收尾。 |
| `UserPromptSubmit` | 从提醒线(默认 60%)起,每轮注入一行额度状态。本会话若曾被中断且额度已恢复,提示 Agent 从中断处继续。 |
| `SessionStart` | 会话开始时报告额度状态;仅首次运行时,请 Agent 用 `CronCreate` 建 30 分钟一次的恢复探测。 |
| `/quota-auto-resume` | 查看额度状态、建或重建探测任务、试跑拦截、修改阈值。 |

每次拒绝都会在 `~/.zcode/quota-auto-resume/pending/` 记一个待续跑标记。恢复探测是一个循环定时任务,提示词里带有标记文字 `quota-auto-resume 额度恢复探测`;每次运行时由 `UserPromptSubmit` hook 注入该做什么:额度已恢复就用 `SendToSession` 续跑待续会话,否则只回一行并结束。

## 配置

可选文件 `~/.zcode/quota-auto-resume/config.json`,默认值:

```json
{
"threshold": 80,
"warnAt": 60,
"maxAgeMin": 20,
"highCost": "^(Agent|Task)$|image|video|browser|playwright|firefox|devtools|computer|(^|[^a-z])cua([^a-z]|$)|websearch|web_search|webfetch|web_reader|3d|tripo|blender|designcraft|screencast|node_repl"
}
```

- `threshold`:用到百分之几停用高消耗工具。
- `warnAt`:用到百分之几开始每轮提醒。
- `maxAgeMin`:快照超过多少分钟视为未知,此时不拦截任何工具。
- `highCost`:匹配工具名的大小写不敏感正则。

把 `threshold` 改成 100 即停用拦截,也可在 设置 → 插件 里停用本插件。

## 使用条件

- ZCode 桌面端 4.x。hook 脚本运行在 ZCode 自带的 Node 上,不需要另装 Node 或 Python。
- 日志目录取自 `ZCODE_DATA_BASE_DIR`(其下 `.zcode/v2/logs`),找不到时用 `~/.zcode/v2/logs`;可用环境变量 `QUOTA_AUTO_RESUME_LOGS` 指定。
- 额度快照只在桌面端运行期间写入。若 `maxAgeMin` 分钟内没有新快照,quota-auto-resume 不拦截任何工具。
- 日志格式属于桌面端内部细节,版本间可能变化;届时快照过期,插件自动放行。

## 副作用

- 读取今天和昨天 ZCode 日志文件的尾部(各最后 4 MB),只读。
- 写入 `~/.zcode/quota-auto-resume/config.json`(仅当用户自己创建)、`~/.zcode/quota-auto-resume/setup-notified`,以及 `~/.zcode/quota-auto-resume/pending/` 下的标记文件。
- 不联网、不读凭据、不包含第三方代码或服务。

## 测试

```shell
node test/run-tests.cjs
node hooks/auto-resume-hook.cjs status
```

测试使用构造的日志样本和临时状态目录,不触碰真实的额度状态。

## 许可证

MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
description: 查看 quota-auto-resume 额度状态、建或重建恢复探测定时任务、试跑拦截
---

先运行 `node "${ZCODE_PLUGIN_ROOT}/hooks/auto-resume-hook.cjs" status`,把输出口头报告给用户(额度、是否拦截、待续跑会话数、阈值)。

再按用户的要求办事:

- 用户要“建/重建定时任务”:用 CronCreate 建 recurring=true、cron=`*/30 * * * *` 的任务,标题「每30分钟 quota-auto-resume 额度恢复探测」,prompt 为单行:「quota-auto-resume 额度恢复探测。按系统注入的 quota-auto-resume 提示判断:有待续会话且额度已恢复才用 SendToSession 续跑;否则只回复一行,不做任何别的事。」
- 用户要“改阈值”:编辑 `~/.zcode/quota-auto-resume/config.json`(`threshold` 阈值、`warnAt` 提醒线、`maxAgeMin` 快照有效期分钟、`highCost` 高消耗工具名正则),改完再跑一次 status 核对。改动前先把新值念给用户确认。
- 用户要“试跑拦截”:在临时目录放一份 config.json 把 `threshold` 设为 0,用 `QUOTA_AUTO_RESUME_HOME=<临时目录>` 跑下面两条,看输出;试完删掉临时目录。

```bash
echo '{"toolName":"Agent","sessionId":"test"}' | QUOTA_AUTO_RESUME_HOME=<临时目录> node "${ZCODE_PLUGIN_ROOT}/hooks/auto-resume-hook.cjs" PreToolUse
echo '{"toolName":"Read","sessionId":"test"}' | QUOTA_AUTO_RESUME_HOME=<临时目录> node "${ZCODE_PLUGIN_ROOT}/hooks/auto-resume-hook.cjs" PreToolUse
```

预期:第一条输出 deny 及收尾提示,第二条无输出(放行)。

- 用户要“停用拦截”:把 config.json 里 `threshold` 改成 100,或在 设置 → 插件 里停用本插件。
Loading