-
Notifications
You must be signed in to change notification settings - Fork 7
feat(runtime): P1 接入 internal hooks 到工具与完成前生命周期(#489) #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
phantom5099
merged 3 commits into
1024XEngineer:main
from
Cai-Tang-www:feat/issue-489-runtime-hook-p1
Apr 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| runtimehooks "neo-code/internal/runtime/hooks" | ||
| ) | ||
|
|
||
| const ( | ||
| // hookErrorClassBlocked 标识由 before_tool_call hook 拦截产生的工具错误分类。 | ||
| hookErrorClassBlocked = "hook_blocked" | ||
| ) | ||
|
|
||
| type hookContextKey string | ||
|
|
||
| const hookRuntimeEnvelopeKey hookContextKey = "runtime_hook_envelope" | ||
|
|
||
| type hookRuntimeEnvelope struct { | ||
| RunID string | ||
| SessionID string | ||
| Turn int | ||
| Phase string | ||
| } | ||
|
|
||
| // HookExecutor 定义 runtime 调用 hook 的最小执行契约。 | ||
| type HookExecutor interface { | ||
| Run(ctx context.Context, point runtimehooks.HookPoint, input runtimehooks.HookContext) runtimehooks.RunOutput | ||
| } | ||
|
|
||
| type hookRuntimeEventEmitter struct { | ||
| service *Service | ||
| } | ||
|
|
||
| func newHookRuntimeEventEmitter(service *Service) *hookRuntimeEventEmitter { | ||
| return &hookRuntimeEventEmitter{service: service} | ||
| } | ||
|
|
||
| // EmitHookEvent 将 hooks 包内事件桥接为 runtime 事件,供 TUI 与日志统一消费。 | ||
| func (e *hookRuntimeEventEmitter) EmitHookEvent(ctx context.Context, event runtimehooks.HookEvent) error { | ||
| if e == nil || e.service == nil { | ||
| return nil | ||
| } | ||
| envelope, _ := runtimeHookEnvelopeFromContext(ctx) | ||
| kind := EventType(strings.TrimSpace(string(event.Type))) | ||
| if kind == "" { | ||
| return nil | ||
| } | ||
| return e.service.emitWithEnvelope(ctx, RuntimeEvent{ | ||
| Type: kind, | ||
| RunID: envelope.RunID, | ||
| SessionID: envelope.SessionID, | ||
| Turn: envelope.Turn, | ||
| Phase: envelope.Phase, | ||
| PayloadVersion: 0, | ||
| Payload: HookEventPayload{ | ||
| HookID: event.HookID, | ||
| Point: string(event.Point), | ||
| Scope: string(event.Scope), | ||
| Kind: string(event.Kind), | ||
| Mode: string(event.Mode), | ||
| Status: string(event.Status), | ||
| StartedAt: event.StartedAt, | ||
| DurationMS: event.DurationMS, | ||
| Error: event.Error, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // runHookPoint 在指定运行态上下文执行一个 hook 点,并自动注入 run/session 元数据。 | ||
| func (s *Service) runHookPoint( | ||
| ctx context.Context, | ||
| state *runState, | ||
| point runtimehooks.HookPoint, | ||
| input runtimehooks.HookContext, | ||
| ) runtimehooks.RunOutput { | ||
| if s == nil || s.hookExecutor == nil { | ||
| return runtimehooks.RunOutput{} | ||
| } | ||
| input.RunID = firstNonBlank(input.RunID, hookRunIDFromState(state)) | ||
| input.SessionID = firstNonBlank(input.SessionID, hookSessionIDFromState(state)) | ||
| scopedCtx := withRuntimeHookEnvelope(ctx, hookRuntimeEnvelope{ | ||
| RunID: hookRunIDFromState(state), | ||
| SessionID: hookSessionIDFromState(state), | ||
| Turn: hookTurnFromState(state), | ||
| Phase: hookPhaseFromState(state), | ||
| }) | ||
| return s.hookExecutor.Run(scopedCtx, point, input) | ||
| } | ||
|
|
||
| func withRuntimeHookEnvelope(ctx context.Context, envelope hookRuntimeEnvelope) context.Context { | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| return context.WithValue(ctx, hookRuntimeEnvelopeKey, envelope) | ||
| } | ||
|
|
||
| func runtimeHookEnvelopeFromContext(ctx context.Context) (hookRuntimeEnvelope, bool) { | ||
| if ctx == nil { | ||
| return hookRuntimeEnvelope{}, false | ||
| } | ||
| raw := ctx.Value(hookRuntimeEnvelopeKey) | ||
| envelope, ok := raw.(hookRuntimeEnvelope) | ||
| return envelope, ok | ||
| } | ||
|
|
||
| func hookRunIDFromState(state *runState) string { | ||
| if state == nil { | ||
| return "" | ||
| } | ||
| return strings.TrimSpace(state.runID) | ||
| } | ||
|
|
||
| func hookSessionIDFromState(state *runState) string { | ||
| if state == nil { | ||
| return "" | ||
| } | ||
| return strings.TrimSpace(state.session.ID) | ||
| } | ||
|
|
||
| func hookTurnFromState(state *runState) int { | ||
| if state == nil { | ||
| return turnUnspecified | ||
| } | ||
| return state.turn | ||
| } | ||
|
|
||
| func hookPhaseFromState(state *runState) string { | ||
| if state == nil { | ||
| return "" | ||
| } | ||
| if state.lifecycle == "" { | ||
| return "" | ||
| } | ||
| return string(state.lifecycle) | ||
| } | ||
|
|
||
| func firstNonBlank(values ...string) string { | ||
| for _, value := range values { | ||
| trimmed := strings.TrimSpace(value) | ||
| if trimmed != "" { | ||
| return trimmed | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func findHookBlockMessage(output runtimehooks.RunOutput) string { | ||
| if !output.Blocked { | ||
| return "" | ||
| } | ||
| for _, result := range output.Results { | ||
| if !strings.EqualFold(strings.TrimSpace(result.HookID), strings.TrimSpace(output.BlockedBy)) { | ||
| continue | ||
| } | ||
| message := strings.TrimSpace(result.Message) | ||
| if message != "" { | ||
| return message | ||
| } | ||
| errText := strings.TrimSpace(result.Error) | ||
| if errText != "" { | ||
| return errText | ||
| } | ||
| break | ||
| } | ||
| if blockedBy := strings.TrimSpace(output.BlockedBy); blockedBy != "" { | ||
| return "hook blocked by " + blockedBy | ||
| } | ||
| return "hook blocked" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xgopilot 分析hook的生命周期定义放在events.go里是否合适
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
结论
放在
internal/runtime/events.go总体是合适的,前提是你把它视为「runtime 对外事件契约层」而不是 hook 领域模型层。理由:
events.go本身就是 runtime 对外统一事件协议(RuntimeEvent+EventType+ payload)定义处,hook 生命周期事件在 runtime 总线上对 TUI/gateway 可见,属于该层职责。internal/runtime/hooks(如hooks/events.go、hooks/types.go),runtime 只是桥接并外发。边界风险(建议关注)
hooks与runtime存在事件字符串双份定义,漂移风险真实存在。internal/runtime/hooks/events.go(hook_started/finished/failed)internal/runtime/events.go(EventHookStarted/Finished/Failed)internal/runtime/hooks_integration.go中EventType(strings.TrimSpace(string(event.Type)))EventHookBlocked只在 runtime 层定义是合理的。这是 runtime 执行语义(
before_tool_call是否 enforced)产生的事件,不是 hooks executor 原生生命周期事件。建议落地
hooks包)。HookEventStarted -> EventHookStarted等,避免隐式字符串 cast。EventType。events.go体积继续增长,可仅做文件级拆分(如events_hook.go),包内职责不变。本次未做
View job run