Repository navigation
refactor(core): extract hook registries - #5157
Conversation
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1fdb95dd27
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| suspend(item) { | ||
| setPaused(item.id, item.point, true, 'failed'); | ||
| }, |
There was a problem hiding this comment.
Suspend only the failed registration
When an asynchronous hook is removed while still pending and a replacement is registered with the same ID and hook point before the original invocation rejects, suspend looks up the current group by ID/point and pauses the replacement rather than the failed item. The same race exists for non-blocking hooks through failNonBlocking, so the newly registered hook can be silently disabled; verify that the exact HookItem is still registered before pausing it.
AGENTS.md reference: AGENTS.md:L210-L212
Useful? React with 👍 / 👎.
| const invokeHook = (item: HookItem, args: readonly unknown[]): unknown => { | ||
| const hook = item.hook as (...args: readonly unknown[]) => unknown; | ||
| return hook(...args); | ||
| }; |
There was a problem hiding this comment.
Preserve the registered hook receiver
For callers migrating from composeToolExecutorHooks, registering an object method such as policy.validating now invokes it as a bare function, so hooks that use this receive undefined and fail even though the old composer explicitly called each method with its owning hook object. The migrated test only passes because its helper manually binds every method, masking this lost contract; retain the receiver in registrations or provide an object-level registration API that performs the binding.
AGENTS.md reference: AGENTS.md:L206-L208
Useful? React with 👍 / 👎.
| }): D { | ||
| return { [hookChainResultMarker]: true, ...result } as D; |
There was a problem hiding this comment.
Return the declared decision from registry hooks
The exported loop registry exposes hook as LoopHooks, but wrapHookChainResult casts an internal envelope to the declared decision type and returns that envelope at runtime. A direct call such as registry.hook['turn.starting'](...) therefore receives { decision, failures, ... } instead of the promised TurnStartingVerdict, and using one registry's hook as a hook in another registry either loses the decision or reports it as invalid. Keep the failure metadata out of the public hook result or expose a distinct internal hook type rather than masquerading the envelope as D.
Useful? React with 👍 / 👎.
Port c6c7177. packages/core/src/hook/loop now owns the loop hook registry, chain runners, failure helpers and the max-steps builtin, and hook/index.ts is reachable through a new ./hook package entry. LoopActor takes Partial<LoopHooks> instead of building a per-loop registry; Loop.hooks, HookRegistration, HookInfo and HookLifecycleEvent are gone, and CreateLoopOptions.hooks became hook?: Partial<LoopHooks>. tool/hook-composer.ts is deleted in favor of createToolExecutorHookRegistry, and AgentToolExecutorService composes through that registry. Fork adaptations: - hook/result.ts only gains rewrittenBy; the fork's later (MoonshotAI#5211) HookFailure shape with suspend stays, and the chain failures suspend through it. - hook/loop/{failure,registry,run}.ts import Message from @moonshot-ai/core/llm/message like loop/contract/hook.ts already does: the agent-core-v2 tsconfig maps #/llm/message to src/human/llm/message, which would otherwise split the Message identity. - AgentLoopService registers turn.starting, message.appending, step.starting, step.stopping and max-steps once on a createLoopHookRegistry() field and keeps the fork's bypassStep max-steps condition; registerToolBatchStarting adds and removes by id. - AgentToolExecutorService keeps the fork-only forkHook in the chain. - test/harness/toolHooks.ts, the tool executor stubs and the hook-registry test were already ported; test/agent/task/stubs.ts needs no change because the fork carries no composeToolExecutorHooks there. Verified: - packages/core: typecheck clean; bun test --isolate test/tool/ 89 pass. - packages/agent-core-v2: typecheck clean and 25 pass (loop core hooks/record/requester/wait), 18 (hooks.test alone, including the new step.stopping block-policy case), 19 (coreLoopService), 64 (toolExecutor), 55 (toolScheduler + taskService), run through a temporary worktree-local node_modules junction so @moonshot-ai/core resolves to this worktree. - check:no-comments OK (2414 files), check-deep-imports OK, agent-core-v2 lint:imports OK (2058 files), scoped oxlint 0 errors. - test/agent/loop/loop.test.ts 65 pass / 3 fail: pre-existing snapshot drift (loopEnd missing from those snapshots, system-reminder wrapper text differs from src/agent/toolSelect/dynamicTools.ts), untouched by this port. - scripts/checks/ratchet.mjs aborts on pre-existing .oxlintrc vs .oxlintrc.ratchet.json ignorePatterns drift; both files are untouched here.
Requirement or Bug
Loop 与 ToolExecutor 分别维护 hook 注册表和 chain 组合逻辑,使生命周期事件、失败处理及 non-blocking 能力难以统一。这个 PR 将 core 的 hook 注册与执行收敛到独立模块,并让 v2 在保持现有服务门面和 hook 行为的前提下接入统一实现。
Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路是由 Registry 统一持有注册项、执行 chain 并发出生命周期事件,Loop 与 ToolExecutor 只接收单个 hook 视图。现有 hook 全部保持 blocking;non-blocking 仅作为新的 Registry 注册能力提供。
flowchart LR V2[v2 loop / toolExecutor services] --> Registry[core hook registries] Registry --> LoopHook[Single Loop hook view] Registry --> ToolHook[Single ToolExecutor hook] LoopHook --> Loop[Loop] ToolHook --> Executor[ToolExecutor] Registry --> Lifecycle[registry.subscribe lifecycle events] Loop --> Failed[Loop hook.failed event]旧路径功能清单
registry.subscribe发出,并携带 blocking modehook/loopchain;保留顺序、逐条改写、first-decision 短路、cause 签名及 pause/block 失败策略hook/tool-executorchain;保留同步 validating、reject 短路、args 改写、skipPermitting、结果替换及 observe 异常隔离hook.failed与hookFailureMessagehook/loop/failure与 Loop actor;保留原事件和消息形状loopService/toolExecutorService;registerToolBatchStarting、registerHook、registerLegacyStarting签名和注册顺序不变同步 hook 的 first-decision 与 step-ending 路径直接返回结果,只有实际返回 Promise 时才进入异步续链。这保留了原有 token counting 与
turn.ended的事件顺序,而不是仅保证 decision 结果一致。阅读顺序建议:先看
packages/core/src/hook/registry.ts了解注册与通知模型,再看hook/loop/registry.ts、hook/tool-executor/registry.ts的分点 chain,最后看两个 v2 service 的接线。hook/loop/run.ts与loop-actor.ts负责把 chain result 和失败暂停接回现有 Loop 流程。Behavior Changes and Affected Users
CreateLoopOptions.hooks数组和Loop.hooks注册表hook;注册由外部createLoopHookRegistry管理hook视图传给 Loop;该公共面调整已在任务契约中明确批准hook.added/paused/resumed/removed进入 Loop 事件流registry.subscribe发出;Loop 只保留hook.failedcomposeToolExecutorHooks组合 hook 数组registerHook门面不变;直接使用 composer 的 core 调用方迁移到 ToolExecutorHookRegistryblocking: false先触发且不等待,参数只读、返回值不影响结果;失败暂停并发 Registry 事件@moonshot-ai/core/hook及既有深层子路径KAP、node-sdk、ACP、配置文件、环境变量、会话持久化格式和 CLI/Desktop/VS Code 用户界面均未改变。没有提示词文本变化,也没有旧会话数据迁移。
覆盖
验证明细
pnpm lint:通过。packages/core与packages/agent-core-v2的tsc --noEmit:均通过。test/tool:5 个文件、91/91,通过。Checklist