| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/event" |
| 12 | "reasonix/internal/hook" |
| 13 | ) |
| 14 | |
| 15 | func TestBuildResumedSkillKeepsHookSessionID(t *testing.T) { |
| 16 | isolateConfigHome(t) |
| 17 | dir := robustTempDir(t) |
| 18 | t.Chdir(dir) |
| 19 | registerBootSubagentTestProvider() |
| 20 | prov := &bootSubagentTestProvider{hookSessionProbe: true} |
| 21 | setBootSubagentTestProvider(t, prov) |
| 22 | writeFile(t, dir, "reasonix.toml", ` |
| 23 | default_model = "test-model" |
| 24 | |
| 25 | [[providers]] |
| 26 | name = "test-model" |
| 27 | kind = "boot-subagent-test" |
| 28 | model = "x" |
| 29 | `) |
| 30 | approveWorkspace(t, dir) |
| 31 | writeFile(t, dir, "marker.txt", "hook probe") |
| 32 | writeFile(t, dir, ".reasonix/skills/hook-probe.md", "---\ndescription: inspect a marker\nrunAs: subagent\nallowed-tools: read_file\n---\nRead the requested file.") |
| 33 | logPath := filepath.Join(dir, "hook.log") |
| 34 | script := filepath.Join(dir, "log-read.sh") |
| 35 | writeFile(t, dir, "log-read.sh", "#!/bin/sh\ncat >> "+shellQuoteForTest(logPath)+"\n") |
| 36 | if err := os.Chmod(script, 0o755); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | settings, err := json.Marshal(map[string]any{"hooks": map[string]any{"PreToolUse": []any{map[string]string{"match": "read_file", "command": script}}}}) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | writeFile(t, dir, ".reasonix/settings.json", string(settings)) |
| 44 | if err := hook.ApproveProjectHooks(hook.LoadOptions{ProjectRoot: dir}); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | |
| 48 | ctrl, err := Build(context.Background(), withTestSession(t, Options{Sink: event.Discard})) |
| 49 | if err != nil { |
| 50 | t.Fatalf("Build: %v", err) |
| 51 | } |
| 52 | defer ctrl.Close() |
| 53 | ctrl.EnsureSessionPath() |
| 54 | if err := ctrl.Run(context.Background(), "read marker with the skill"); err != nil { |
| 55 | t.Fatalf("first Run: %v", err) |
| 56 | } |
| 57 | ref := subagentRefFromHistory(t, ctrl.History()) |
| 58 | prov.setContinueRef(ref) |
| 59 | if err := ctrl.Run(context.Background(), "resume the skill and read marker again"); err != nil { |
| 60 | t.Fatalf("second Run: %v", err) |
| 61 | } |
| 62 | log, err := os.ReadFile(logPath) |
| 63 | if err != nil { |
| 64 | t.Fatalf("read hook log: %v", err) |
| 65 | } |
| 66 | lines := strings.Split(strings.TrimSpace(string(log)), "\n") |
| 67 | if len(lines) != 2 { |
| 68 | t.Fatalf("PreToolUse calls = %d, want two child reads; log=%q", len(lines), log) |
| 69 | } |
| 70 | var sessionIDs [2]string |
| 71 | for i, line := range lines { |
| 72 | var payload struct{ ToolName, SessionID string } |
| 73 | if err := json.Unmarshal([]byte(line), &payload); err != nil { |
| 74 | t.Fatalf("decode hook payload: %v", err) |
| 75 | } |
| 76 | if payload.ToolName != "read_file" || payload.SessionID == "" { |
| 77 | t.Fatalf("hook payload = %+v, want read_file with session ID", payload) |
| 78 | } |
| 79 | sessionIDs[i] = payload.SessionID |
| 80 | } |
| 81 | if sessionIDs[0] != sessionIDs[1] { |
| 82 | t.Fatalf("resumed skill hook session IDs differ: %q and %q", sessionIDs[0], sessionIDs[1]) |
| 83 | } |
| 84 | if sessionIDs[0] != "subagent:"+ref { |
| 85 | t.Fatalf("skill hook session ID = %q, want transcript identity %q", sessionIDs[0], "subagent:"+ref) |
| 86 | } |
| 87 | } |
| 88 |