feat: 完善 hook runtime - #154
Open
lyfmt wants to merge 23 commits into
Open
Conversation
lyfmt
marked this pull request as ready for review
June 21, 2026 15:39
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.
背景
参考 pi 的
beforeToolCall/afterToolCall语义,为 ly-pi 补充第一阶段低侵入 hook 能力,并在工具 hook 基础上补齐本批 turn 生命周期接点。实现
lypi-contracts新增ToolHook/ToolHookRuntime、before/after context/result 与默认顺序调度实现。context.input()与context.request().input()一致;覆盖嵌套Map/List/Set和 null 值。lypi-tool新增ToolHookExecutionInterceptor,复用现有ToolExecutionInterceptor切点实现 before 阻断和 after 结果替换。lypi-boot收集 SpringToolHookbean,按 Spring order 接入默认工具 runtime;hook 在每次 runtime 创建时解析。TurnHook/TurnHookRuntime、before/after turn context/result 与默认顺序调度实现。DefaultTurnExecutor接入 turn hook:beforeTurn位于TurnStartEvent之后、unsafe continuation 和用户消息追加之前;afterTurn位于最终TurnState形成之后、TurnEndEvent发布之前。afterTurn只观察最终状态,不替换TurnState;hook 失败会转 failed turn 并追加错误消息,但TurnEndEvent.leafEntryId保持真实执行结果的稳定 fork point。AgentCorePort和AgentCoreFactoryPort中自动收集 SpringTurnHookbean,无 hook 时保持 no-op。边界
turn_hook_start/turn_hook_end,另起事件契约设计。验证
mvn -q -pl lypi-contracts,lypi-agent-core,lypi-boot -am testmvn -q testrg -n "import cn\\.lypi\\.(tool|boot|transport|ai)" lypi-contracts/src/main/java(无匹配)rg -n "import cn\\.lypi\\.transport|import cn\\.lypi\\.boot|import cn\\.lypi\\.tool\." lypi-agent-core/src/main/java(无匹配)rg -n "AfterTurnHookResult\\.replace|Optional<TurnState>|状态处理决定|替换 turn 状态|返回可选的最终替换状态" lypi-contracts/src/main/java/cn/lypi/contracts/hook lypi-agent-core/src/main/java lypi-contracts/src/test/java/cn/lypi/contracts/hook lypi-agent-core/src/test/java/cn/lypi/agent(无匹配)审查
afterTurn失败路径会漂移TurnEndEvent.leafEntryId;已通过0535797修复并补回归测试。