Skip to content

feat(quota-auto-resume): add quota-aware tool guard with auto-resume - #385

Open
rebel117 wants to merge 1 commit into
zai-org:mainfrom
rebel117:feat/quota-auto-resume
Open

rebel117 wants to merge 1 commit into
zai-org:mainfrom
rebel117:feat/quota-auto-resume

Conversation

@rebel117

@rebel117 rebel117 commented Dec 5, 2026

Copy link
Copy Markdown

Plugin type (required) / 插件类型(必填)

  • UI Plugin / 是 UI 插件 — provides MCP Apps interactive pages / 提供 MCP Apps 交互页面
  • Standard plugin / 普通插件(非 UI) — does not provide MCP Apps interactive pages / 不提供 MCP Apps 交互页面
  • Not applicable / 不适用 — repository-only changes / 仅仓库级改动

Affected plugin name(s) / 涉及的插件名称:quota-auto-resume

What / 改动内容

New community plugin under external_plugins/quota-auto-resume/, registered in the root marketplace.json (category productivity, version 0.1.0).

Problem. A long agent task can exhaust the daily plan quota half way through and stop without a handoff.

Behavior. Three hooks and one command, all backed by a single Node script:

  • PreToolUse: when model quota reaches the threshold (default 80%), denies high-cost tools (subagents, image and video generation, browser automation, etc.) with a reason that tells the agent to write a handoff and stop. When the official MCP call quota reaches the threshold, denies mcp__* tools. Lightweight tools stay available so the agent can wrap up.
  • UserPromptSubmit: from a warning line (default 60%), injects a one-line quota status. If the session was paused and quota has recovered, tells the agent to continue. When the prompt carries the probe marker, it tells the agent which paused sessions to resume through SendToSession.
  • SessionStart: reports quota status; on first run asks the agent to create the 30-minute recovery probe.
  • /quota-auto-resume: status, probe setup or rebuild, interception dry run, threshold changes.

How it is tested.

  • node test/run-tests.cjs: 9 tests over synthetic log fixtures and a temporary state directory, covering log parsing, threshold decisions, MCP-only denial, fail-open on stale or missing snapshots, recovery decisions and malformed input.
  • node hooks/auto-resume-hook.cjs status reads real quota from the local ZCode log.
  • The scheduled-task channel was exercised on ZCode 4.1.2: a one-shot automation called SendToSession to deliver a message to an existing session, and the message arrived.
  • python3 scripts/validate.py and python3 scripts/build_dist.py both pass when run from a tree that contains only this plugin (a full checkout of the repository could not be fetched from this network, so they were not run against all 59 entries; the diff to marketplace.json is one appended entry and the original text is unchanged).

Dependencies, permissions and side effects.

  • No network access, no credentials, no MCP server, no third-party code.
  • Reads the tail (last 4 MB) of today's and yesterday's ZCode log files, read-only, to pick up the quota snapshots the desktop client already writes.
  • Writes only under ~/.zcode/quota-auto-resume/ (an optional config.json, a one-time setup flag, and pending-resume markers).
  • Fails open: on a stale snapshot (default 20 minutes), a missing log directory, or any internal error, nothing is blocked.

Licensing. MIT, authored by tech.leshang. No third-party material.

Known limitation. The quota snapshot is read from the desktop client's log format, which is an internal detail and may change between client versions; in that case the snapshot goes stale and the plugin stops intercepting.

Self-check / 自查清单

  • Plugin name is unique kebab-case; new external plugin directory is external_plugins/<name>/ / 插件名唯一且 kebab-case,外部新增插件位于 external_plugins/<name>/
  • Usage category is set independently of publisher ownership / 用途分类 category 已填写,与维护归属分开
  • Plugin type and affected names are declared above / 已在上方声明插件类型与涉及的插件名称
  • version bumped in BOTH plugin.json and marketplace.json (new plugin, 0.1.0 in both) / plugin.json 与 marketplace.json 两处版本号已同步
  • README.md (EN) and README_CN.md (中文) both updated and equivalent / 中英文 README 均已更新且语义一致
  • description_i18n has both en and zh-CN / description_i18n 包含 en 与 zh-CN
  • No secrets, machine-specific paths, or private dependencies / 不含密钥、机器专属路径或私有依赖
  • Model/API/network dependencies documented / 模型、API、网络依赖已写明
  • python3 scripts/validate.py and python3 scripts/build_dist.py pass locally / 本地校验与构建均通过(scope stated above)

@panda920

panda920 commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

✅ Internal validation passed. A ZCode maintainer will review the change next; after acceptance, release checks run separately and this PR is closed automatically.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants