From c8d7a995d18023bcde718c4f9871993751f7c24f Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 28 Sep 2026 19:02:20 +0800 Subject: [PATCH] =?UTF-8?q?rtk:=20Codex=20isn't=20given=20an=20rtk=20older?= =?UTF-8?q?=20than=200.50=20(LeeHom:=20rtk's=20installer=20didn't=20add=20?= =?UTF-8?q?its=20hook=20to=20Codex)=20=E2=80=94=20rtk=20has=20a=20hook=20f?= =?UTF-8?q?or=20Codex=20(rtk=20hook=20codex=20in=20hooks.json)=20from=200.?= =?UTF-8?q?50.0=20on;=20an=20older=20rtk=20init=20-g=20--codex=20only=20pu?= =?UTF-8?q?ts=20@RTK.md=20in=20AGENTS.md,=20which=20rewrites=20no=20comman?= =?UTF-8?q?d,=20so=20switching=20Codex=20on=20ran=20it=20and=20then=20said?= =?UTF-8?q?=20the=20hook=20wasn't=20there.=20With=20an=20rtk=20before=200.?= =?UTF-8?q?50=20the=20Library=20shows=20Codex=20with=20"Update=20RTK"=20(?= =?UTF-8?q?=E9=9C=80=E6=9B=B4=E6=96=B0=20RTK),=20its=20switch=20off,=20the?= =?UTF-8?q?=20tooltip=20and=20magpie=20library=20rtk=20saying=20to=20updat?= =?UTF-8?q?e=20it=20(brew=20upgrade=20rtk,=20or=20its=20installer=20again)?= =?UTF-8?q?=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/i18n.js | 2 ++ internal/gui/assets/library.js | 3 +- internal/library/rtk.go | 42 ++++++++++++++++++++++++-- internal/library/rtk_test.go | 55 ++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 65feef825..56b321c17 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1026,6 +1026,8 @@ const I18N = { "RTK missing": "RTK 缺失", "{agent}'s hook calls rtk, which isn't installed, so its shell commands fail. Install RTK, or switch this off.": "{agent} 的 hook 会调用 rtk,但 rtk 没有安装,它的 shell 命令会失败。请安装 RTK,或关掉这个开关。", "Not for OpenCode 2": "不支持 OpenCode 2", + "Update RTK": "需更新 RTK", + "{agent}'s hook needs RTK 0.50 or newer: older ones only add @RTK.md to AGENTS.md, which rewrites no command. Update RTK (brew upgrade rtk, or its installer again), then switch it on.": "{agent} 的 hook 需要 RTK 0.50 或更新版本:旧版只会在 AGENTS.md 里加一行 @RTK.md,并不会改写命令。请先更新 RTK(brew upgrade rtk,或重新运行它的安装脚本),再打开。", "RTK's plugin is written for OpenCode 1, and OpenCode 2 refuses to load it. Switch this off until RTK supports OpenCode 2.": "RTK 的插件是为 OpenCode 1 写的,OpenCode 2 会拒绝加载它。在 RTK 支持 OpenCode 2 之前,请关掉这个开关。", "RTK's plugin is written for OpenCode 1, and OpenCode 2 refuses to load it. It can be switched on once RTK supports OpenCode 2.": "RTK 的插件是为 OpenCode 1 写的,OpenCode 2 会拒绝加载它。等 RTK 支持 OpenCode 2 后才能打开。", "Get RTK": "获取 RTK", diff --git a/internal/gui/assets/library.js b/internal/gui/assets/library.js index 54fb0e8b8..c65d6d11c 100644 --- a/internal/gui/assets/library.js +++ b/internal/gui/assets/library.js @@ -403,7 +403,8 @@ if (a.on && !rtk.path) row.append(tag(t("RTK missing"), "warn", t("{agent}'s hook calls rtk, which isn't installed, so its shell commands fail. Install RTK, or switch this off.", { agent: a.name }))); // OpenCode 2 won't load rtk's plugin (written for OpenCode 1): it can't // be switched on, and one already there can be switched off - if (a.blocked) row.append(tag(t("Not for OpenCode 2"), "warn", a.on + if (a.blocked && a.id !== "opencode") row.append(tag(t("Update RTK"), "warn", t("{agent}'s hook needs RTK 0.50 or newer: older ones only add @RTK.md to AGENTS.md, which rewrites no command. Update RTK (brew upgrade rtk, or its installer again), then switch it on.", { agent: a.name }))); + else if (a.blocked) row.append(tag(t("Not for OpenCode 2"), "warn", a.on ? t("RTK's plugin is written for OpenCode 1, and OpenCode 2 refuses to load it. Switch this off until RTK supports OpenCode 2.") : t("RTK's plugin is written for OpenCode 1, and OpenCode 2 refuses to load it. It can be switched on once RTK supports OpenCode 2."))); const sw = toggle(a.on, t("{agent} runs its commands through RTK", { agent: a.name }), (on) => setRTK(a, on)); diff --git a/internal/library/rtk.go b/internal/library/rtk.go index 129893cc8..66c8bf579 100644 --- a/internal/library/rtk.go +++ b/internal/library/rtk.go @@ -97,8 +97,9 @@ var rtkSpecs = map[string]rtkSpec{ dir: func(*agent.Agent) string { return claudeDir() }, }, "codex": { - flags: []string{"--codex"}, - has: func(*agent.Agent) bool { return contains(filepath.Join(codexDir(), "hooks.json"), "rtk hook codex") }, + flags: []string{"--codex"}, + blocked: codexBlocked, + has: func(*agent.Agent) bool { return contains(filepath.Join(codexDir(), "hooks.json"), "rtk hook codex") }, files: func(*agent.Agent) []string { d := codexDir() return []string{filepath.Join(d, "hooks.json"), filepath.Join(d, "AGENTS.md"), filepath.Join(d, "RTK.md")} @@ -202,6 +203,43 @@ func openCodeBlocked() string { return "" } +// codexBlocked: rtk has a hook for Codex from 0.50.0 on (rtk hook codex in +// hooks.json); before that rtk init --codex only puts @RTK.md in AGENTS.md, +// which rewrites no command, so an older rtk isn't run for Codex. +func codexBlocked() string { + v := rtkVersion() + if v == "" || !older(v, 0, 50) { + return "" + } + return fmt.Sprintf("Codex's hook needs RTK 0.50 or newer, and this RTK is %s: older ones only add @RTK.md to AGENTS.md, which rewrites no command. Update RTK (brew upgrade rtk, or its installer again), then switch it on", v) +} + +// older says whether version v is before major.minor. +func older(v string, major, minor int) bool { + var a, b int + if n, _ := fmt.Sscanf(v, "%d.%d", &a, &b); n < 2 { + return false + } + return a < major || a == major && b < minor +} + +// rtkVersion is the version the rtk here says it is, "" when there's none +// or it doesn't say; a test sets it. +var rtkVersion = func() string { + bin := rtkPath() + if bin == "" { + return "" + } + out, err := rtkRun(bin, "--version") + if err != nil { + return "" + } + if m := semver.FindStringSubmatch(out); m != nil { + return m[1] + } + return "" +} + // semver finds the version in what opencode --version prints: OpenCode 1 // prints "1.18.32", OpenCode 2 "opencode v2.0.18". var semver = regexp.MustCompile(`(?:^|[^\w.])v?(\d+\.\d+\.\d+\S*)`) diff --git a/internal/library/rtk_test.go b/internal/library/rtk_test.go index 4db6287b1..9049b38af 100644 --- a/internal/library/rtk_test.go +++ b/internal/library/rtk_test.go @@ -290,3 +290,58 @@ func TestDropHermesPlugin(t *testing.T) { } } } + +// TestRTKCodexOld: rtk has a hook for Codex from 0.50 on; an older one's +// rtk init --codex only puts @RTK.md in AGENTS.md, so it isn't run for +// Codex, and the Library says to update it. A newer one is. +func TestRTKCodexOld(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("the fake rtk is a shell script") + } + h := sandbox(t) + codex := filepath.Join(h, ".codex") + bin := filepath.Join(h, "bin") + rtk := func(version, init string) { + t.Helper() + write(t, filepath.Join(bin, "rtk"), "#!/bin/sh\ncase \"$1\" in\n--version) echo \"rtk "+version+"\"; exit 0 ;;\ngain) exit 1 ;;\nesac\n"+init+"\n") + if err := os.Chmod(filepath.Join(bin, "rtk"), 0o755); err != nil { + t.Fatal(err) + } + } + t.Setenv("PATH", bin) + write(t, filepath.Join(codex, "config.toml"), "") + blocked := func() string { + t.Helper() + for _, a := range ReadRTK().Agents { + if a.ID == "codex" { + return a.Blocked + } + } + t.Fatal("codex isn't listed") + return "" + } + + rtk("0.49.0", `echo "@$CODEX_HOME/RTK.md" > "$HOME/.codex/AGENTS.md"`) + if b := blocked(); !strings.Contains(b, "0.49.0") { + t.Fatalf("blocked: %q", b) + } + if _, err := SetRTK("codex", true); err == nil || !strings.Contains(err.Error(), "RTK 0.50 or newer") { + t.Fatalf("switched on with rtk 0.49: %v", err) + } + if exists(filepath.Join(codex, "AGENTS.md")) { + t.Fatal("rtk 0.49's installer was run") + } + + rtk("0.50.0", `echo '{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"rtk hook codex"}]}]}}' > "$HOME/.codex/hooks.json"`) + if b := blocked(); b != "" { + t.Fatalf("rtk 0.50 blocked: %q", b) + } + if _, err := SetRTK("codex", true); err != nil { + t.Fatal(err) + } + for v, want := range map[string]bool{"0.49.9": true, "0.28.2": true, "0.50.0": false, "0.51.0-rc.473": false, "1.0.0": false, "x": false} { + if older(v, 0, 50) != want { + t.Errorf("older(%q) = %v", v, !want) + } + } +}