Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/architecture/remote-workspace-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,9 @@ between identical OS credentials.
Discover capabilities at the provider/assembly boundary, not with scattered
`is_remote` gates in individual tools. Missing target programs, an unavailable
interactive desktop, network loss and platform-specific process behavior can
require explicit differences. A local hook executable or MCP command is not
silently relocated to the target. Keep its execution domain and source trust
require explicit differences. User and imported Hook commands remain host
owned; manually configured project Hook commands are explicitly target-owned
when the project gate is enabled. Keep execution domain and source trust
explicit; reuse the existing engine and approval mechanism.

Remote workspaces do not acquire Detached Dispatch semantics through this
Expand Down
30 changes: 23 additions & 7 deletions docs/features/agent-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ Both layers are additive: every matching handler runs, user handlers first.
There is no override or shadowing between them. Changes are picked up without
restarting OpenBitFun.

### RTK command rewrite example

The repository contains a reusable RTK example in
[`examples/rtk`](agent-hooks/examples/rtk/README.md). It includes a Windows
user hook and a POSIX project hook for `ExecCommand`.

Copy the POSIX files into a target repository's `.openbitfun/` directory when
the workspace runs on Linux, WSL, or an SSH host. The Python script resolves
`~/.local/bin/rtk` before consulting `PATH`, because remote non-interactive
shells may not load `.bashrc`. The Windows template is copied into the user
configuration and is suitable for local workspaces.

The templates are inactive until copied into one of the documented hook
locations. Project hooks still require `app.hooks.project_hooks_enabled` and
should only be enabled for trusted repositories.

## Turning hooks on

**Settings → Agent Hooks**, or directly under the `app` section of
Expand Down Expand Up @@ -117,9 +133,9 @@ Import, update, enable, disable, and remove take effect on the next matching
Hook event; an already running Hook finishes against the snapshot it started
with. OpenBitFun does not re-import on startup, poll, or watch Claude Code/Codex
files. Use **Refresh** or `/hooks refresh` to check for source changes, then
review an update explicitly. The management and execution paths are local-only;
remote workspaces return unsupported instead of running local commands against
a remote path.
review an update explicitly. Import management remains controller-owned. Remote
dispatch skips user/imported snapshots and runs only the manually configured
project layer through the target workspace provider.

OpenCode plugin Hooks are intentionally excluded. Their JavaScript callbacks
need the OpenCode plugin execution domain; the current OpenCode Hook catalog is
Expand Down Expand Up @@ -208,8 +224,8 @@ metadata. Native `hooks.json` keeps its Codex decision contract.

The master `app.hooks.enabled` gate applies. Project skills additionally require
`app.hooks.project_hooks_enabled`, including on subsequent dispatch. Activation
without a session/local workspace, or in an SSH/remote workspace, returns an
explicit error; no controller-local fallback executes. Remote control, Peer Device,
without a session/local workspace, or for a skill invocation in an SSH/remote workspace, returns an
explicit error; no controller-local fallback executes. Native project `hooks.json` still runs through the target provider. Remote control, Peer Device,
and Detached Dispatch reuse their target runtime's session and permission owners;
this change does not introduce a separate client-side hook runner.

Expand Down Expand Up @@ -289,7 +305,7 @@ Everything not listed here behaves as the Codex documentation describes.
| `[features] hooks = false` | use `app.hooks.enabled` |
| Plugin-bundled and managed hooks (`PLUGIN_ROOT`, `managed_dir`) | not supported |
| `prompt` and `agent` handler types | parsed so shared files stay valid, but skipped — only `type: "command"` executes |
| Remote workspaces | hooks are not dispatched: a local hook process and a remote workspace path do not describe the same filesystem. The skip is reported, not silent: when the host has rules for the event, the first skipped dispatch per session logs a warning, and the hook overview carries `remote_workspace_unsupported` so surfaces can say the configured rules did not run |
| Remote workspaces | user hooks and imported Hook snapshots are skipped; when `app.hooks.project_hooks_enabled` is enabled, the project `hooks.json` is read from the remote workspace and its command handlers run through the target workspace shell with the normal JSON stdin contract. Missing remote providers fail closed without controller-local fallback. |

### Fields not populated yet

Expand Down Expand Up @@ -342,7 +358,7 @@ every time its event fires. Treat `hooks.json` like a shell profile:

| Symptom | Cause |
| --- | --- |
| No hook runs at all | `app.hooks.enabled` is `false`, the file is not at the documented path, or the workspace is remote (the log then carries a warning for the first skipped dispatch of each session). |
| No hook runs at all | `app.hooks.enabled` is `false`, the file is not at the documented path, or a remote project Hook cannot read its target-side configuration/provider. Remote user hooks are skipped by design. |
| Project hooks do not run | `app.hooks.project_hooks_enabled` is `false` (the default). |
| The whole file is ignored | Invalid JSON, or a root key other than `description`/`hooks`. |
| One event is ignored | Misspelled event name — the names are case-sensitive. |
Expand Down
20 changes: 17 additions & 3 deletions docs/features/agent-hooks.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ Codex 读 `~/.codex/hooks.json`,OpenBitFun 改为读自己的配置目录。
两个层级是叠加关系:所有匹配的处理器都会执行,用户层优先,层级之间不存在覆盖或
屏蔽。修改后无需重启 OpenBitFun。

### RTK 命令改写示例

仓库提供了可复用的 RTK 示例:[`examples/rtk`](agent-hooks/examples/rtk/README.md)。
其中包含一个 Windows 用户级 Hook 和一个 POSIX 项目级 Hook,用于改写
`ExecCommand`。

如果工作区运行在 Linux、WSL 或 SSH 主机上,把 POSIX 示例文件复制到目标仓库的
`.openbitfun/` 目录。Python 脚本会先查找 `~/.local/bin/rtk`,再查找 `PATH`;这是因为
远程非交互 shell 可能不会加载 `.bashrc`。Windows 示例复制到用户配置目录,适用于
本地工作区。

示例文件不会自动生效,只有复制到文档规定的 Hook 路径后才会加载。项目级 Hook 仍然
需要开启 `app.hooks.project_hooks_enabled`,并且只应在信任的仓库中启用。

## 开启 Hooks

**设置 → Agent Hooks**,或直接编辑 `<用户配置目录>/config/app.json` 的 `app` 段:
Expand Down Expand Up @@ -99,7 +113,7 @@ OpenBitFun 可以把 Claude Code 或 Codex 中兼容的 `type: "command"` Hook
导入、更新、启用、停用和移除会在下一个匹配的 Hook 事件生效;已经开始运行的 Hook
仍使用启动时捕获的快照完成。OpenBitFun 不会在启动时重新导入,也不会轮询或监听 Claude
Code/Codex 文件。请使用**刷新**或 `/hooks refresh` 检查来源变化,再显式审阅更新。
管理与执行均只支持本地工作区;远程工作区会明确返回不支持,不会用本地命令处理远程
管理仍在控制端;远程工作区跳过 user Hook 和导入快照,仅在开启项目 Hook 时通过远程工作区文件与目标端 shell 读取、执行项目 `hooks.json`,不会回退到控制端本地命令
路径。

OpenCode 插件 Hooks 明确不在本次范围内。其 JavaScript 回调依赖 OpenCode 插件执行域;
Expand Down Expand Up @@ -244,7 +258,7 @@ sys.exit(0)
| `[features] hooks = false` | 使用 `app.hooks.enabled` |
| 插件内置与托管 Hooks(`PLUGIN_ROOT`、`managed_dir`) | 不支持 |
| `prompt` 与 `agent` 处理器类型 | 会被解析(以便共享配置文件保持有效)但跳过 —— 只有 `type: "command"` 会执行 |
| 远程工作区 | 不派发 Hooks:本地 Hook 进程与远程工作区路径描述的不是同一个文件系统。跳过不是静默的:当宿主为该事件配置了规则时,每个会话第一次跳过会记录一条警告,Hook 概览也带有 `remote_workspace_unsupported`,供界面说明已配置的规则没有运行 |
| 远程工作区 | 跳过 user Hook 和导入快照;开启 `app.hooks.project_hooks_enabled` 后,从远程工作区读取项目 `hooks.json`,并通过目标端 shell 执行命令,stdin/stdout/stderr、超时和取消沿用 Hook 契约。远程 provider 不可用时失败关闭,不回退到控制端 |

### 尚未填充的字段

Expand Down Expand Up @@ -293,7 +307,7 @@ Hook 是以你的用户权限运行的任意代码,且每次对应事件触发

| 现象 | 原因 |
| --- | --- |
| 完全没有 Hook 运行 | `app.hooks.enabled` 为 `false`、文件不在文档所述路径,或工作区是远程工作区(此时日志会为每个会话第一次跳过的派发记录一条警告)。 |
| 完全没有 Hook 运行 | `app.hooks.enabled` 为 `false`、文件不在文档所述路径,或远程项目 Hook 无法读取目标端配置/provider。远程 user Hook 本来就会跳过;项目 Hook 的配置与命令都在目标端。 |
| 项目 Hooks 不运行 | `app.hooks.project_hooks_enabled` 为 `false`(默认值)。 |
| 整个文件被忽略 | JSON 无效,或存在 `description`/`hooks` 之外的根级字段。 |
| 某个事件被忽略 | 事件名拼写错误 —— 事件名区分大小写。 |
Expand Down
66 changes: 66 additions & 0 deletions docs/features/agent-hooks/examples/rtk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# RTK command rewrite example

This example connects [RTK](https://github.com/rtk-ai/rtk) to OpenBitFun's
`PreToolUse` hook for `ExecCommand`. RTK can rewrite a command before it is
sent to the terminal and can reject a command with exit code 2.

The files in this directory are templates. They are deliberately not stored
under a live `.openbitfun/` directory, so cloning this repository does not
activate a hook in the development checkout.

## Windows local workspace

1. Install `rtk` and make sure it is visible to the OpenBitFun process.
2. Copy `rtk-hook.ps1` to `%USERPROFILE%\.openbitfun\hooks\rtk-hook.ps1`.
3. Merge `hooks.windows.user.json` into
`%APPDATA%\openbitfun\config\hooks.json` (merge the `hooks` entries if the
file already contains other hooks).
4. Keep `app.hooks.enabled` enabled.

The Windows template invokes PowerShell with `-NoProfile`. The script first
honors `RTK_BIN`, then searches `rtk.exe` and
`rtk` on `PATH`.

## POSIX remote workspace

Copy both project files into the target repository:

```text
<workspace>/.openbitfun/config/hooks.json
<workspace>/.openbitfun/hooks/rtk-hook.py
```

Use `hooks.posix.project.json` as the contents of the project `hooks.json`, or
merge its `PreToolUse` entry into an existing file. Enable
`app.hooks.project_hooks_enabled` in OpenBitFun.

The command runs on the workspace host:

```text
python3 .openbitfun/hooks/rtk-hook.py
```

The script looks for `~/.local/bin/rtk` before falling back to `PATH`. This is
intentional: SSH non-interactive shells often do not source `.bashrc`, so an
export in `.bashrc` alone is not sufficient. RTK must be installed on the
workspace host, not only on the controller.

Remote workspaces use the project layer only. User hooks and imported hook
snapshots stay on the controller and are skipped for remote dispatch.

## Manual check

From the target workspace, pipe a representative payload to the script:

```bash
printf '%s\n' '{"tool_name":"ExecCommand","tool_input":{"cmd":"git status"}}' \
| python3 .openbitfun/hooks/rtk-hook.py
```

The script emits a JSON `hookSpecificOutput.updatedInput` document when RTK
rewrites the command. An RTK exit code of 1 means "leave the command alone";
exit code 2 blocks the tool call and writes the reason to stderr. Exit code 3
rewrites without adding an `allow` decision.

Project hooks execute repository code with the target user account. Enable
them only for repositories you trust.
16 changes: 16 additions & 0 deletions docs/features/agent-hooks/examples/rtk/hooks.posix.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hooks": {
"PreToolUse": [
{
"matcher": "^ExecCommand$",
"hooks": [
{
"type": "command",
"command": "python3 .openbitfun/hooks/rtk-hook.py",
"timeout": 30
}
]
}
]
}
}
16 changes: 16 additions & 0 deletions docs/features/agent-hooks/examples/rtk/hooks.windows.user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hooks": {
"PreToolUse": [
{
"matcher": "^ExecCommand$",
"hooks": [
{
"type": "command",
"command": "powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"%USERPROFILE%\\.openbitfun\\hooks\\rtk-hook.ps1\"",
"timeout": 30
}
]
}
]
}
}
84 changes: 84 additions & 0 deletions docs/features/agent-hooks/examples/rtk/rtk-hook.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
$raw = [Console]::In.ReadToEnd()
$hookInput = $raw | ConvertFrom-Json

if (
$hookInput.tool_name -ne 'ExecCommand' -or
$null -eq $hookInput.tool_input -or
$null -eq $hookInput.tool_input.cmd
) {
exit 0
}

$rtk = $env:RTK_BIN
if (-not $rtk) {
$rtkCommand = Get-Command rtk.exe -ErrorAction SilentlyContinue
if (-not $rtkCommand) {
$rtkCommand = Get-Command rtk -ErrorAction SilentlyContinue
}
if ($rtkCommand) {
$rtk = $rtkCommand.Source
}
}
if (-not $rtk) {
[Console]::Error.WriteLine('RTK hook: rtk was not found; leaving the command unchanged.')
exit 0
}

$stderrPath = [IO.Path]::GetTempFileName()

try {
$rewrittenLines = @(
& $rtk rewrite -- ([string]$hookInput.tool_input.cmd) 2> $stderrPath
)
$rtkExit = $LASTEXITCODE
$rtkError = (
Get-Content -LiteralPath $stderrPath -Raw `
-ErrorAction SilentlyContinue
).Trim()
}
finally {
Remove-Item -LiteralPath $stderrPath -Force `
-ErrorAction SilentlyContinue
}

switch ($rtkExit) {
0 { $decision = 'allow' }
1 { exit 0 }
2 {
if ($rtkError) {
[Console]::Error.WriteLine($rtkError)
} else {
[Console]::Error.WriteLine('RTK denied this command.')
}
exit 2
}
3 { $decision = $null }
default { exit 0 }
}

$rewritten = ($rewrittenLines -join "`n").Trim()
if (-not $rewritten) {
exit 0
}

$updated = [ordered]@{}
foreach ($property in $hookInput.tool_input.PSObject.Properties) {
$updated[$property.Name] = $property.Value
}
$updated['cmd'] = $rewritten

$specific = [ordered]@{
hookEventName = 'PreToolUse'
updatedInput = [pscustomobject]$updated
}

if ($decision) {
$specific.permissionDecision = $decision
$specific.permissionDecisionReason = 'RTK auto-rewrite'
}

[ordered]@{
hookSpecificOutput = [pscustomobject]$specific
} | ConvertTo-Json -Compress -Depth 20

exit 0
Loading
Loading