A Library hook on "When a session starts" can now carry a matcher of
starts: one or more of startup, resume, clear, compact and fork (Claude
Code's words, library.HookStarts), joined by "|". NEKO-CwC's case is a
hook that re-injects a file after compaction alone; written whole, it
also ran at startup and once for every subagent fork.
Each agent's format was read from its own source:
- Claude Code 2.1.316 tests the matcher against source, which has all
five starts. Its entry is written as given.
- Codex (codex-rs/hooks, rust-v0.165.0): SessionStartSource has the
same five, and a "|" list is read as exact names. Written as given.
- Gemini CLI 0.63.0 has startup, resume and clear only, and its
matchesTrigger compares the matcher whole. It takes one of those.
- Grok 1.0.67 tells only "new" (fresh, /new, /clear) from "load"
(resumed). It takes a set that is exactly one of those groups.
- Cursor (cursor-agent 2026.10.01) takes no matcher: its Claude-hooks
importer warns that a SessionStart trigger matcher "is not supported
in Cursor, hooks will fire for all triggers".
startMatcher keeps the chosen starts that the agent has. With none
left, the agent is refused; with every one it has, it gets the hook
whole. An agent that can't run the hook at exactly the starts picked is
refused with why, and its chip is greyed, as for any other per-agent
limit. This is the least surprising choice. Widening the hook to every
start would run it at the very starts the user excluded (the fork runs
the issue is about), and a matcher the agent never matches would do
nothing without saying so. SomeHooksAgents (Select) gives a narrowed
hook only to the agents that can run it.
Tool-event validation is unchanged: only tool events take shell or
edit, and only session-start takes starts. check() refuses an unknown
start, a start named twice, an empty part and a wrong case. The matcher
as written is recorded in Applied.Hooks, so an agent's own edit of the
entry is still theirs, and is kept when the hook changes or is removed.
GUI: the editor shows an "At which starts" app menu for session-start
(openProtoMenu, no <select>), with Any start, A fresh session, A
resumed session, After /clear, After the context is compacted, A
forked session and Any start but a fork. HookAgentView.Starts and
StartSources tell the page which matchers each agent takes. A chip that
can't take the matcher greys with one of three reasons, in the editor
and on the row, and save leaves it out. Every new string is in zh,
zh-TW, ja and de. docs/subsystems/library.md's Hooks row says all this.
Run, at 7f60927fd (origin/main 61394481c + this), all on mote:
- go test -tags nogui -count=1 ./internal/library ./internal/backup
./internal/davsync: ok.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...:
ok. gofmt -l: clean.
- node --test --test-concurrency=4 library-*.test.cjs (57 files)
i18n-keys.test.cjs, both engines: 1183 of 1186 passed.
- i18n-keys "a plugin's words are its own" is red for three strings
already filed: #5482, #5426 and #5332/#5327. My "A new session"
hit that test too (internal/claudetui says it), so it became "A
fresh session".
- library-mcp-json chromium "ja 390" timed out once. It passed 11/11
run alone, twice. Filed as #5613.
Broken by hand:
- With the old hooks.go, TestHookSessionStartNarrowed and
TestHookMatcherKeptToItsEvent fail ("session-start can't be narrowed
to compact").
- With entry() writing no matcher and the sync check skipped, 17
assertions fail: Claude and Codex are given matcher "", Gemini,
Cursor and Grok get the hook whole, and no problems are shown.
- With the old client.js, library-hooks.test.cjs fails in every
language, waiting for .lib-hookstart.
Not tried:
- No real agent ran a narrowed hook. The formats come from each
agent's source and docs, not from a live session start.
- Old data: a hook saved before this has no matcher and is unchanged.
A narrowed hook carried in a backup or sync to an older magpie is
written whole there, because that magpie's sync doesn't check the
matcher.
- The full go test ./... and the whole GUI suite run after the push,
as agent-rules.md orders.
Reported-by: NEKO-CwC
206 lines
8.2 KiB
Go
206 lines
8.2 KiB
Go
package library
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// #5604 (NEKO-CwC): a session-start hook narrowed to some starts, as a
|
|
// "re-inject after compaction" hook is to compact alone. It reaches each
|
|
// agent whose session start takes a matcher in that agent's own words for
|
|
// those starts, and is given to no agent that can't run it at just those,
|
|
// with why: never widened to every start, nor written with a matcher the
|
|
// agent never matches. Only Read's view and the files are looked at, so
|
|
// this compiles against the code before the change and fails there.
|
|
func TestHookSessionStartNarrowed(t *testing.T) {
|
|
h := sandbox(t)
|
|
claude := filepath.Join(h, ".claude/settings.json")
|
|
codex := filepath.Join(h, ".codex/hooks.json")
|
|
gem := filepath.Join(h, ".gemini/settings.json")
|
|
cur := filepath.Join(h, ".cursor/hooks.json")
|
|
grok := filepath.Join(h, ".grok/config.toml")
|
|
write(t, grok, "model = \"grok-4.7\"\n")
|
|
every := []string{"claude", "codex", "cursor", "gemini", "grok"}
|
|
|
|
// save saves the hook, and says why each agent it couldn't be given
|
|
// wasn't, as the page shows it (Read with the change's problems)
|
|
save := func(old string, x Hook) map[string]string {
|
|
t.Helper()
|
|
res, err := SaveHook(old, x)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v, err := Read(res.Problems)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, y := range v.Hooks {
|
|
if y.Name == x.Name {
|
|
return y.Problems
|
|
}
|
|
}
|
|
t.Fatalf("no hook %s in the view", x.Name)
|
|
return nil
|
|
}
|
|
matcherOf := func(path, ev string) []string {
|
|
t.Helper()
|
|
var out []string
|
|
for _, e := range hooksOf(t, path, ev) {
|
|
out = append(out, gjson.Get(e, "matcher").String())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// compact alone: Claude Code and Codex take it as written; Gemini CLI
|
|
// and Grok never start a session after compaction, and Cursor's start
|
|
// takes no matcher at all
|
|
p := save("", Hook{Name: "reinject", Event: "session-start", Matcher: "compact", Command: "cat AGENTS.md", Agents: every})
|
|
if got := matcherOf(claude, "SessionStart"); !slices.Equal(got, []string{"compact"}) {
|
|
t.Errorf("claude SessionStart matchers: %q", got)
|
|
}
|
|
if got := matcherOf(codex, "SessionStart"); !slices.Equal(got, []string{"compact"}) {
|
|
t.Errorf("codex SessionStart matchers: %q", got)
|
|
}
|
|
if got := hooksOf(t, gem, "SessionStart"); len(got) != 0 {
|
|
t.Errorf("gemini was given a compact-only hook it would run at every start: %v", got)
|
|
}
|
|
if got := hooksOf(t, cur, "sessionStart"); len(got) != 0 {
|
|
t.Errorf("cursor was given a compact-only hook it would run at every start: %v", got)
|
|
}
|
|
if got := grokBlocks(t, grok, "SessionStart"); len(got) != 0 {
|
|
t.Errorf("grok was given a compact-only hook: %q", got)
|
|
}
|
|
for _, a := range []string{"gemini", "cursor", "grok"} {
|
|
if p[a] == "" {
|
|
t.Errorf("%s isn't said why it has no compact-only hook: %+v", a, p)
|
|
}
|
|
}
|
|
if p["claude"] != "" || p["codex"] != "" {
|
|
t.Errorf("problems for the agents that took it: %+v", p)
|
|
}
|
|
|
|
// an agent's own edit of the entry magpie wrote is theirs: changing the
|
|
// hook leaves it, and the new one is written beside it
|
|
s := read(t, claude)
|
|
write(t, claude, strings.Replace(s, "cat AGENTS.md", "cat AGENTS.md CLAUDE.md", 1))
|
|
save("reinject", Hook{Name: "reinject", Event: "session-start", Matcher: "resume", Command: "cat AGENTS.md", Agents: every})
|
|
if got := matcherOf(claude, "SessionStart"); !slices.Equal(got, []string{"compact", "resume"}) {
|
|
t.Errorf("claude SessionStart after the change, the user's edit kept: %q", got)
|
|
}
|
|
// one start each tells apart: Gemini CLI's own word, Grok's "load"
|
|
if got := matcherOf(gem, "SessionStart"); !slices.Equal(got, []string{"resume"}) {
|
|
t.Errorf("gemini SessionStart matchers: %q", got)
|
|
}
|
|
if got := grokBlocks(t, grok, "SessionStart"); len(got) != 1 || !strings.Contains(got[0], `matcher = "load"`) {
|
|
t.Errorf("grok SessionStart: %q", got)
|
|
}
|
|
if got := matcherOf(codex, "SessionStart"); !slices.Equal(got, []string{"resume"}) {
|
|
t.Errorf("codex SessionStart, compact's taken out: %q", got)
|
|
}
|
|
|
|
// every start but a fork: Claude Code and Codex are told so; Gemini CLI
|
|
// and Grok never fork a session, so for them it is every start, written
|
|
// whole; Cursor can't be told
|
|
save("reinject", Hook{Name: "reinject", Event: "session-start", Matcher: "startup|resume|clear|compact", Command: "cat AGENTS.md", Agents: every})
|
|
if got := matcherOf(codex, "SessionStart"); !slices.Equal(got, []string{"startup|resume|clear|compact"}) {
|
|
t.Errorf("codex SessionStart, every start but a fork: %q", got)
|
|
}
|
|
if got := matcherOf(gem, "SessionStart"); !slices.Equal(got, []string{""}) {
|
|
t.Errorf("gemini SessionStart, every start it has: %q", got)
|
|
}
|
|
if got := grokBlocks(t, grok, "SessionStart"); len(got) != 1 || strings.Contains(got[0], "matcher") {
|
|
t.Errorf("grok SessionStart, every start it has: %q", got)
|
|
}
|
|
if got := hooksOf(t, cur, "sessionStart"); len(got) != 0 {
|
|
t.Errorf("cursor was given a hook it would run at a fork too: %v", got)
|
|
}
|
|
|
|
// a fresh session or one after /clear: Grok's "new" is both; Gemini CLI
|
|
// compares its matcher whole, so it can't be told two of its starts
|
|
p = save("reinject", Hook{Name: "reinject", Event: "session-start", Matcher: "startup|clear", Command: "cat AGENTS.md", Agents: every})
|
|
if got := grokBlocks(t, grok, "SessionStart"); len(got) != 1 || !strings.Contains(got[0], `matcher = "new"`) {
|
|
t.Errorf("grok SessionStart: %q", got)
|
|
}
|
|
if got := hooksOf(t, gem, "SessionStart"); len(got) != 0 || p["gemini"] == "" || p["grok"] != "" {
|
|
t.Errorf("gemini given two of its starts it can't name together: %v", got)
|
|
}
|
|
|
|
// turned on for many agents at once, it goes to those that can run it
|
|
if _, err := SaveHook("", Hook{Name: "late", Event: "session-start", Matcher: "compact", Command: "x"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := SomeHooksAgents([]string{"late"}, every, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a := mustLoad(t).hook("late").Agents; !slices.Equal(a, []string{"claude", "codex"}) {
|
|
t.Errorf("late's agents: %v", a)
|
|
}
|
|
|
|
// the page is told which starts each agent can be narrowed to
|
|
v, err := Read(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := json.Marshal(v.HookAgents)
|
|
starts := func(id string) []string {
|
|
var out []string
|
|
for _, a := range gjson.ParseBytes(raw).Array() {
|
|
if a.Get("id").String() == id {
|
|
for _, s := range a.Get("starts").Array() {
|
|
out = append(out, s.String())
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
if s := starts("claude"); !slices.Contains(s, "compact") || !slices.Contains(s, "startup|resume|clear|compact") {
|
|
t.Errorf("claude's starts: %v", s)
|
|
}
|
|
if s := starts("gemini"); !slices.Contains(s, "resume") || slices.Contains(s, "compact") || slices.Contains(s, "startup|clear") {
|
|
t.Errorf("gemini's starts: %v", s)
|
|
}
|
|
if s := starts("grok"); !slices.Contains(s, "startup|clear") || slices.Contains(s, "startup") {
|
|
t.Errorf("grok's starts: %v", s)
|
|
}
|
|
if s := starts("cursor"); len(s) != 0 {
|
|
t.Errorf("cursor's starts: %v", s)
|
|
}
|
|
|
|
ok(t)(RemoveHooks([]string{"reinject", "late"}))
|
|
if got := matcherOf(claude, "SessionStart"); !slices.Equal(got, []string{"compact"}) {
|
|
t.Errorf("claude SessionStart after removal, the user's own edit kept alone: %q", got)
|
|
}
|
|
}
|
|
|
|
// Only the tool events take a tool, and only session-start takes a start:
|
|
// the validation of each stays as it was.
|
|
func TestHookMatcherKeptToItsEvent(t *testing.T) {
|
|
sandbox(t)
|
|
for _, c := range []Hook{
|
|
{Event: "turn-end", Matcher: "compact"},
|
|
{Event: "after-tool", Matcher: "compact"},
|
|
{Event: "before-tool", Matcher: "startup"},
|
|
{Event: "session-start", Matcher: "shell"},
|
|
{Event: "session-start", Matcher: "compact|compact"},
|
|
{Event: "session-start", Matcher: "compact|"},
|
|
{Event: "session-start", Matcher: "Compact"},
|
|
{Event: "prompt", Matcher: "resume"},
|
|
} {
|
|
c.Name, c.Command = "x", "x"
|
|
if _, err := SaveHook("", c); err == nil {
|
|
t.Errorf("%s was narrowed to %q", c.Event, c.Matcher)
|
|
ok(t)(RemoveHooks([]string{"x"}))
|
|
}
|
|
}
|
|
for _, m := range []string{"startup", "fork", "clear|fork", "startup|resume|clear|compact|fork"} {
|
|
ok(t)(SaveHook("", Hook{Name: "x", Event: "session-start", Matcher: m, Command: "x"}))
|
|
ok(t)(RemoveHooks([]string{"x"}))
|
|
}
|
|
ok(t)(SaveHook("", Hook{Name: "x", Event: "after-tool", Matcher: "shell", Command: "x"}))
|
|
}
|