feat: 桥接 prompt/enhance 进 ACP (同步 + 异步 job)#6
Merged
Conversation
App 3.3.0 引入 prompt/enhance 协议方法族, 让外部调用方用 ZCode 内置的
提示词增强能力。本次桥接同步 + 异步两种模式:
同步 prompt/enhance:
发请求 → 阻塞返回 {enhanced}。照 generateText 模板, timeout 90s。
异步 prompt/enhance/start + result 通知 + cancel:
start 立即返回 {requestId, accepted}, 结果由 server 主动推送
prompt/enhance/result notification (按 requestId 路由)。
bridge 转成阻塞语义: 先注册 listener 再 start, 阻塞等待结果通知 (120s 兜底),
收到 completed/failed/cancelled 后返回。
cancel 纯透传 requestId → {requestId, cancelled}。
ZCodeBackend 新增 enhance result 通知路由 (仿 _response_queues):
reader 收到 prompt/enhance/result 按 requestId 路由到等待中的 start;
reader 退出时唤醒所有等待中的 enhance queue 防止永久挂起。
文档: version_described → App 3.3.0; key_methods + acp_methods_implemented
补 prompt/* 分组; README 加扩展方法表 + 版本兼容性 + MCP 规范兼容性说明。
测试: 158 passed (+15)。真实 e2e 验证同步 + 异步两条路径均返回真实 enhanced 文本。
Refs: ZCode App 3.3.0 (CLI 仍 0.15.0)
Codex (GPT-5.5 high reasoning) 对 PR #6 的 deep review 结论: P0 无问题, 3 个 P1 + 1 个 P2。本次修复全部 P1 + 补测试: P1-1 (cancel 在 start 阻塞期间不可用): 主循环 run() 串行处理请求, start 阻塞在 result_q.get 时, client 发的 cancel 卡在 _inbox 直到 start 返回。改 result_q.get(timeout=120) 为 短轮询循环 (每轮 get 0.5s + _drain_enhance_inbox), drain 检测到 cancel 就转发后端并写响应。总超时改用 ZCODE_ENHANCE_TIMEOUT 可配 (测试加速)。 P1-2 (重复 requestId 覆盖旧 queue): register_enhance_listener 对同 requestId 直接覆盖, 第一个等待者被孤立。 改返回 (queue, error): 已有 pending 时返回 error, start handler 据此报 -32602。 P1-3 (迟到 result 进通用队列无界积压): start 超时 unregister 后, 迟到的 result notification 进 _notification_queue (无消费者) 永久积压。改: 无人等的 result 直接 log 丢弃。 P2 (docstring 时序描述): start docstring 原写 start后注册, 与实际先注册再start 相反, 已修正。 测试: 161 passed (+3: 重复 requestId / 空串 enhanced / cancel-during-wait)。 真实 e2e 重验: 同步 + 异步两条路径在修复后仍返回真实 enhanced 文本。 ruff 全过, exec bit 100755。
code-reviewer (P1): PE18 测试假绿 — 原先没断言 cancel 真被转发, 删掉 drain 转发逻辑测试仍过。加 cancel_forwarded 断言 (fake.calls 含 cancel 调用) + 调整 timing (result_delay 0.4→1.0) 确保 cancel 在等待窗口被 drain 处理。 现已验证: 破坏 drain 转发会让 PE18 失败 (非假绿)。 code-reviewer (P2) + simplifier: 超时消息硬编码 "120s" 改用 enhance_timeout 变量, 与 ZCODE_ENHANCE_TIMEOUT 实际值一致。 simplifier: _drain_enhance_inbox replay 列表 → 直接 put 回 inbox 尾部 (语义不变, 省 ~4 行); cancel_resp is not None 永真去除; start handler docstring 压缩 (删复述 what 的段落, 留 why); 注释去重。 保留 (经判断不精简): sessionId/context 两处样板 (仅 2 处, 提取反增间接层, 与 generateText 风格一致); ValueError 兜底 (防生产误配); 测试 helper msg 参数。 net: -7 行。161 passed, ruff 全过, exec bit 100755, e2e 双路验证通过。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
ZCode App 3.3.0(CLI 仍 0.15.0)引入
prompt/enhance协议方法族——让外部调用方能用 ZCode 内置的"提示词增强"能力。本 PR 把它桥接进 ACP bridge,同步 + 异步两种模式全做。协议契约(从
glm/zcode.cjs二进制挖出)prompt/enhance{enhanced: string}prompt/enhance/start{requestId, accepted},结果由 server 推送prompt/enhance/result{requestId, status, enhanced?, errorMessage?}prompt/enhance/cancel{requestId, cancelled: bool}实现
packages/acp-bridge/zcode-acp-bridge(+168 行)prompt/enhance:照workspace/generateText模板,timeout=90(比 generateText 的 60s 宽裕,因 enhance 含 prompt 构造 + 清洗)prompt/enhance/start:转成 ACP 阻塞语义——先注册 listener 再 start(参考 session/prompt "先 subscribe 再 send" 教训),阻塞等待 result 通知(120s 兜底),收到 completed/failed/cancelled 后返回prompt/enhance/cancel:纯透传 requestIdZCodeBackend新增 enhance result 通知路由:仿_response_queues模式,reader 收到prompt/enhance/result按 requestId 路由到等待中的 start;reader 退出时唤醒所有等待中的 enhance queue 防止永久挂起packages/agent-help/zcode-agent-helpversion_described:App 3.2.1→App 3.3.0key_methods+acp_methods_implemented补 prompt/* 分组README.md2024-11-05是有意识的稳定选择,非落后测试
ruff==0.15.17)prompt/enhance:「帮我写个函数」→ 50 字精确提示词 ✅prompt/enhance/start:「分析这段代码」→ 74 字专业审查提示词 ✅风险与边界
_notification_queue兜底AbortSignal.timeout兜底宽裕验证清单
python3 -m pytest tests/ -q→ 158 passedruff check→ All checks passedRefs: ZCode App 3.3.0 (CLI 0.15.0)