rtk: Codex isn't given an rtk older than 0.50 (LeeHom: rtk's installer didn't add its hook to Codex) — rtk has a hook for Codex (rtk hook codex in hooks.json) from 0.50.0 on; an older rtk init -g --codex only puts @RTK.md in AGENTS.md, which rewrites no command, so switching Codex on ran it and then said the hook wasn't there. With an rtk before 0.50 the Library shows Codex with "Update RTK" (需更新 RTK), its switch off, the tooltip and magpie library rtk saying to update it (brew upgrade rtk, or its installer again) first

This commit is contained in:
yetone
2026-09-28 19:02:23 +08:00
parent e0c6892f56
commit c8d7a995d1
4 changed files with 99 additions and 3 deletions
+2
View File
@@ -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",
+2 -1
View File
@@ -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));
+40 -2
View File
@@ -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*)`)
+55
View File
@@ -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)
}
}
}