Skip to content

feat(runtime): P3 Repo Hooks + Workspace Trust Gate (#491)#506

Merged
phantom5099 merged 4 commits into1024XEngineer:mainfrom
Cai-Tang-www:feat/hook-p3
Apr 28, 2026
Merged

feat(runtime): P3 Repo Hooks + Workspace Trust Gate (#491)#506
phantom5099 merged 4 commits into1024XEngineer:mainfrom
Cai-Tang-www:feat/hook-p3

Conversation

@Cai-Tang-www
Copy link
Copy Markdown
Collaborator

背景

实现父任务中 P3 阶段(#491):在 P2 user builtin hooks 基础上,新增项目级 repo hooks,并通过 workspace trust gate 默认拒绝执行,确保“可扩展”与“默认安全”同时成立。

变更摘要

  • 新增 repo hooks 加载入口:<workspace>/.neocode/hooks.yaml
  • 新增 trust gate:~/.neocode/trusted-workspaces.json
  • 执行顺序固定为:internal -> user -> repo
  • 新增 scope/source 双字段语义并全链路透传(runtime -> gateway -> TUI)
  • 新增 repo hooks 生命周期事件与 UI 可观测

详细实现

1) Hooks 元模型扩展(scope/source 分离)

  • 在 hooks 结果与事件模型中新增 source
  • scope 保持“权限/上下文裁剪等级”语义
  • source 用于“配置来源/可观测”语义
  • P3 阶段支持 internal|user|repo,但实现未将两者硬绑定

2) Registry 冲突与并存策略

  • 同来源内重复 ID:继续 fail-fast
  • 跨来源同 ID:允许并存(通过内部 key 加入 source 维度)
  • Resolve 维持优先级与稳定顺序语义

3) Repo hooks + Trust Gate

  • 发现并解析 <workspace>/.neocode/hooks.yaml
  • 仅开放 P2 builtin 子集:
    • kind=builtin
    • mode=sync
    • points: before_tool_call / after_tool_result / before_completion_decision
    • handlers: require_file_exists / warn_on_tool_call / add_context_note
  • Trust 判定文件:~/.neocode/trusted-workspaces.json
    • 路径匹配规则:Abs + Clean +(Windows 下大小写归一)
    • 容错:缺失/空文件/损坏 JSON/结构不匹配 -> 一律按 untrusted
    • 容错不阻断启动,发出告警事件

4) 事件与 UI 可观测

  • 新增 runtime 事件:
    • repo_hooks_discovered
    • repo_hooks_loaded
    • repo_hooks_skipped_untrusted
    • repo_hooks_trust_store_invalid
  • hook 事件与 blocked 事件新增 source
  • TUI 活动展示改为 <source>:<id>,消除跨源同 ID 歧义

5) 安全边界

  • repo hooks 权限不高于 user hooks
  • repo hooks 复用与 user 同级 sanitizer
  • 明确不可见:API key、capability token、service 指针、provider client、原始 tool args

测试

新增/更新覆盖包括:

  • trust gate:trusted/untrusted、缺失/空文件/损坏 JSON/结构错误容错
  • 执行顺序:internal -> user -> repo
  • 冲突策略:跨源同 ID 并存、同来源重复 ID fail-fast
  • 协议透传:scope/source 字段、repo hooks 事件解码
  • TUI 展示:<source>:<id> 与 repo hooks 生命周期事件处理

已验证(针对本次改动相关包):

  • go test ./internal/runtime/hooks
  • go test ./internal/runtime
  • go test ./internal/tui/services
  • go test ./internal/tui/core/app -run "TestRuntimeEventHookHandlers|TestRuntimeEventRepoHookLifecycleHandlers|TestRuntimeEventHandlerRegistryContainsRenamedEvents"

文档

  • 更新 docs/runtime-hooks-design.md:补充 P3 边界、trust gate、source 可观测
  • 更新 docs/guides/configuration.md:补充 repo hooks 与 trust 文件示例

兼容性与约束

  • runtime.hooks.enabled=false 时关闭全部 hooks(internal/user/repo)
  • 未引入 GUI/CLI trust 管理(当前仅文件驱动)
  • 未引入 external hook kinds(command/http/prompt/agent)

Closes #491

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

fennoai[bot]

This comment was marked as outdated.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

@Cai-Tang-www
Copy link
Copy Markdown
Collaborator Author

/code 提高测试率到100

@fennoai
Copy link
Copy Markdown

fennoai Bot commented Apr 28, 2026

处理结果

已按“提高测试率到100”补充并提交测试,改动已推送到当前工作分支。

已完成:

  • 补充 internal/runtime/repo_hooks_test.go:覆盖 dynamicRepoHookExecutor 早退分支、repo hooks 空文件与默认值分支、trust store 路径回退分支、生命周期事件发射与消息合并辅助函数分支。
  • 补充 internal/runtime/user_hooks_test.go:覆盖 repoComposedHookExecutor 阻断透传分支、resolveHookPathWithinWorkdir 异常分支与符号链接路径分支。
  • 补充 internal/tui/services/gateway_stream_client_test.go:覆盖 payload 版本不匹配时错误事件上报与流停止分支。
  • 执行 gofmt 并通过测试:
    • go test ./internal/runtime ./internal/tui/services ./internal/tui/core/app ./internal/runtime/hooks -count=1

提交信息:

  • 1f5f2f6 test(runtime,tui): add branch coverage for repo hooks and gateway stream

已推送:

未完成:

  • 无法在本地直接拿到 Codecov 的 patch 覆盖率最终判定值(以远端 Codecov 结果为准)。

View job run

xgopilot and others added 2 commits April 28, 2026 13:41
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: Cai-Tang-www <106404101+Cai-Tang-www@users.noreply.github.com>
test: improve coverage for hook P3 branches
@phantom5099
Copy link
Copy Markdown
Collaborator

/review

Copy link
Copy Markdown

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现 1 个需要修复的问题(已去重):repo hooks 加载失败被静默吞掉,会掩盖真实配置错误并造成行为与文档不一致。


loaded, loadErr := buildRepoHookExecutorForWorkspace(e.service, workspace, e.hooksCfg)
if loadErr != nil {
return runtimehooks.RunOutput{}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High: 这里直接吞掉 loadErr 并返回空输出,会把 repo hooks 配置错误(例如 hooks.yaml 解析失败、同来源重复 ID)静默降级为“没 hooks”,上层看不到任何失败信号。这样与文档中的“同来源重复 ID 会报错”不一致,也会让运维很难定位问题。建议至少发出 error/专用 runtime event(包含 workspace 和错误摘要),并避免静默忽略。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个需要现在修还是之后修

@phantom5099 phantom5099 merged commit 9995db5 into 1024XEngineer:main Apr 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P3] Repo Hooks 发现与 Workspace Trust Gate

3 participants