feat(runtime): P1 接入 internal hooks 到工具与完成前生命周期(#489)#500
Merged
phantom5099 merged 3 commits into1024XEngineer:mainfrom Apr 28, 2026
Merged
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Collaborator
Author
|
/code 提升覆盖率到100 |
|
已完成本轮“提升覆盖率到100”的测试补齐,并已推送到当前 PR 分支 本次完成
本地验证
说明
|
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: Cai-Tang-www <106404101+Cai-Tang-www@users.noreply.github.com>
phantom5099
reviewed
Apr 28, 2026
| StartedAt time.Time `json:"started_at,omitempty"` | ||
| DurationMS int64 `json:"duration_ms,omitempty"` | ||
| Error string `json:"error,omitempty"` | ||
| } |
There was a problem hiding this comment.
结论
放在 internal/runtime/events.go 总体是合适的,前提是你把它视为「runtime 对外事件契约层」而不是 hook 领域模型层。
理由:
events.go本身就是 runtime 对外统一事件协议(RuntimeEvent+EventType+ payload)定义处,hook 生命周期事件在 runtime 总线上对 TUI/gateway 可见,属于该层职责。- 真正的 hook 领域定义仍在
internal/runtime/hooks(如hooks/events.go、hooks/types.go),runtime 只是桥接并外发。
边界风险(建议关注)
-
hooks与runtime存在事件字符串双份定义,漂移风险真实存在。- hook 侧:
internal/runtime/hooks/events.go(hook_started/finished/failed) - runtime 侧:
internal/runtime/events.go(EventHookStarted/Finished/Failed) - 桥接处当前是字符串直转:
internal/runtime/hooks_integration.go中EventType(strings.TrimSpace(string(event.Type)))
- hook 侧:
-
EventHookBlocked只在 runtime 层定义是合理的。
这是 runtime 执行语义(before_tool_call是否 enforced)产生的事件,不是 hooks executor 原生生命周期事件。
建议落地
- 保持当前放置位置不变(不建议把 runtime 对外事件再下沉回
hooks包)。 - 为降低漂移,建议补一个显式映射或守卫测试:
- 映射:
HookEventStarted -> EventHookStarted等,避免隐式字符串 cast。 - 测试:断言 hooks 事件全集都能被 runtime 桥接成预期
EventType。
- 映射:
- 若你担心
events.go体积继续增长,可仅做文件级拆分(如events_hook.go),包内职责不变。
本次未做
- 未改代码,仅按你的问题完成职责边界分析。
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.
背景
目标与范围
本 PR 仅实现 P1 约定能力:
before_tool_call:支持在工具执行前拦截(可阻断)。after_tool_result:支持在工具执行后观测(只读,不改写工具结果)。before_completion_decision:在完成判定前触发(P1 仅观测,不接管终态决策)。明确非目标:
关键实现
1) Runtime 注入 Hook Executor 与事件桥接
hook_started/hook_finished/hook_failed。2) 工具前拦截:
before_tool_callblock:ToolResult错误;ToolResult.ErrorClass = "hook_blocked";hook_blocked事件(含 hook_id/point/tool_name/reason)。3) 工具后观测:
after_tool_resultafter_tool_result。ToolResult。4) 完成前观测:
before_completion_decisionblock,P1 仅记录事件与日志(enforced=false),不改变 terminal state。5) 契约扩展
tools.ToolResult新增ErrorClass string,用于机器可判别错误分类。hook_blocked事件与 payload。行为约束(与 #489 对齐)
before_tool_call的block必须“可执行拦截”(阻断工具执行)。after_tool_result必须“只观测,不改写结果”。before_completion_decision在 P1 阶段“只观测,不接管完成判定”。修复的接入细节问题(本 PR 内)
tool_start事件时序:before_tool_call放行后发tool_start;tool_start。after_tool_result:context canceled分支也会触发after_tool_result,保证观测闭环一致。测试
已补齐并通过的测试覆盖:
before_tool_callblock 时:工具不执行,返回ErrorClass=hook_blocked。before_tool_callpass 时:工具正常执行。after_tool_result:可收到结果摘要,且不可改写原结果。before_completion_decisionblock:仅观测,不改变完成路径。tool_start时序与阻断场景。context canceled仍触发after_tool_result。本地执行:
go test ./internal/runtime/...go test ./internal/tools/...go test ./internal/tui/services/...影响面与风险
internal/runtime、internal/tools、internal/tui/services、internal/tui/core/app。回滚方案
后续计划
before_completion_decision与 acceptance/verifier 闭环真正接管完成决策。