|
1 | | -import { describe, expect, test } from "bun:test" |
2 | | -import { sessionObserver } from "../../src/plugin/observer" |
3 | | -import * as fs from "fs/promises" |
4 | | -import * as os from "os" |
5 | | -import * as path from "path" |
6 | | - |
7 | | -function restoreUserProfile(value: string | undefined) { |
8 | | - if (value === undefined) delete process.env.USERPROFILE |
9 | | - else process.env.USERPROFILE = value |
10 | | -} |
11 | | - |
12 | | -describe("SessionObserverManager.onToolAfter", () => { |
13 | | - test("records a tool call with string output", async () => { |
14 | | - const sessionId = "obs-str-1" |
15 | | - const callId = "call-str-1" |
16 | | - sessionObserver.onToolBefore(sessionId, callId, "read", { filePath: "/tmp/a.md" }) |
17 | | - sessionObserver.onToolAfter(sessionId, callId, "read", "file contents here") |
18 | | - |
19 | | - const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
20 | | - if (!retro) throw new Error("no retrospective found") |
21 | | - expect(retro.totalToolCalls).toBe(1) |
22 | | - expect(retro.toolsUsed).toEqual(["read"]) |
23 | | - }) |
24 | | - |
25 | | - test("flags MCP tool failures in the retrospective", async () => { |
26 | | - const sessionId = "obs-err-1" |
27 | | - const callId = "call-err-1" |
28 | | - sessionObserver.onToolBefore(sessionId, callId, "mcp__keystone__execute-tool", {}) |
29 | | - sessionObserver.onToolAfter(sessionId, callId, "mcp__keystone__execute-tool", "Error: Unauthorized") |
30 | | - |
31 | | - const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
32 | | - expect(retro).not.toBeNull() |
33 | | - const anomaly = retro!.anomalies.find((a) => a.type === "MCP_TOOL_FAILURE") |
34 | | - expect(anomaly?.tool).toBe("mcp__keystone__execute-tool") |
35 | | - }) |
36 | | - |
37 | | - test("handles non-string output (CallToolResult from MCP catalog)", async () => { |
38 | | - const sessionId = "obs-mcp-1" |
39 | | - const callId = "call-mcp-1" |
40 | | - // Simulates the raw CallToolResult shape returned by convertTool in src/mcp/catalog.ts |
41 | | - const mcpResult = { |
42 | | - content: [{ type: "text", text: "some data" }], |
43 | | - structuredContent: { foo: "bar" }, |
44 | | - meta: {}, |
45 | | - } |
46 | | - |
47 | | - sessionObserver.onToolBefore(sessionId, callId, "imprint_imprint_get_instructions", {}) |
48 | | - // This used to throw: undefined is not an object (evaluating 'output.includes') |
49 | | - expect(() => { |
50 | | - sessionObserver.onToolAfter(sessionId, callId, "imprint_imprint_get_instructions", mcpResult) |
51 | | - }).not.toThrow() |
52 | | - }) |
53 | | - |
54 | | - test("handles null and undefined output gracefully", () => { |
55 | | - const sessionId = "obs-null-1" |
56 | | - sessionObserver.onToolBefore(sessionId, "call-null-1", "some-tool", {}) |
57 | | - expect(() => { |
58 | | - sessionObserver.onToolAfter(sessionId, "call-null-1", "some-tool", null) |
59 | | - }).not.toThrow() |
60 | | - |
61 | | - sessionObserver.onToolBefore(sessionId, "call-undef-1", "some-tool", {}) |
62 | | - expect(() => { |
63 | | - sessionObserver.onToolAfter(sessionId, "call-undef-1", "some-tool", undefined) |
64 | | - }).not.toThrow() |
65 | | - }) |
66 | | - |
67 | | - test("finalizes without crashing when mixed string and object outputs are recorded", async () => { |
68 | | - const sessionId = "obs-mixed-1" |
69 | | - |
70 | | - sessionObserver.onToolBefore(sessionId, "call-a", "read", {}) |
71 | | - sessionObserver.onToolAfter(sessionId, "call-a", "read", "success") |
72 | | - |
73 | | - sessionObserver.onToolBefore(sessionId, "call-b", "rag_ask", {}) |
74 | | - sessionObserver.onToolAfter(sessionId, "call-b", "rag_ask", { answer: "ok", sources: [] }) |
75 | | - |
76 | | - sessionObserver.onToolBefore(sessionId, "call-c", "bash", {}) |
77 | | - sessionObserver.onToolAfter(sessionId, "call-c", "bash", "") |
78 | | - |
79 | | - const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
80 | | - expect(retro).not.toBeNull() |
81 | | - expect(retro!.totalToolCalls).toBe(3) |
82 | | - }) |
83 | | -}) |
84 | | - |
85 | | - |
86 | | -describe("rule visibility", () => { |
87 | | - test("injects a rule that exists only in the project store", async () => { |
88 | | - const global = await fs.mkdtemp(path.join(os.tmpdir(), "oc-g-")) |
89 | | - const ws = await fs.mkdtemp(path.join(os.tmpdir(), "oc-p-")) |
90 | | - const prev = process.env.USERPROFILE |
91 | | - process.env.USERPROFILE = global |
92 | | - try { |
93 | | - const wsFile = path.join(ws, ".system_generated", "logs", "learnings.json") |
94 | | - await fs.mkdir(path.dirname(wsFile), { recursive: true }) |
95 | | - await fs.writeFile( |
96 | | - wsFile, |
97 | | - JSON.stringify([ |
98 | | - { id: "p1", timestamp: "2026-09-23T00:00:00.000Z", lesson: "project only rule", source: "user_feedback" }, |
99 | | - ]), |
100 | | - "utf8", |
101 | | - ) |
102 | | - const listed = await sessionObserver.listLearnings(ws) |
103 | | - const row = listed.find((r) => r.lesson === "project only rule")! |
104 | | - expect(row.origin).toBe("project") |
105 | | - expect(row.injected).toBe(true) |
106 | | - } finally { |
107 | | - restoreUserProfile(prev) |
108 | | - await fs.rm(global, { recursive: true, force: true }) |
109 | | - await fs.rm(ws, { recursive: true, force: true }) |
110 | | - } |
111 | | - }) |
112 | | - |
113 | | - test("reports the real injection window and counts a rule once across both stores", async () => { |
114 | | - const central = await fs.mkdtemp(path.join(os.tmpdir(), "oc-central-")) |
115 | | - const ws = await fs.mkdtemp(path.join(os.tmpdir(), "oc-ws-")) |
116 | | - const prev = process.env.USERPROFILE |
117 | | - process.env.USERPROFILE = central |
118 | | - const centralFile = path.join(central, ".local", "share", "opencode", "learnings.json") |
119 | | - const wsFile = path.join(ws, ".system_generated", "logs", "learnings.json") |
120 | | - try { |
121 | | - await fs.mkdir(path.dirname(centralFile), { recursive: true }) |
122 | | - const rows = Array.from({ length: 7 }, (_, i) => ({ |
123 | | - id: "r" + (i + 1), |
124 | | - timestamp: "2026-09-" + String(i + 1).padStart(2, "0") + "T00:00:00.000Z", |
125 | | - lesson: "rule " + (i + 1), |
126 | | - source: "user_feedback", |
127 | | - })) |
128 | | - await fs.writeFile(centralFile, JSON.stringify(rows), "utf8") |
129 | | - await fs.mkdir(path.dirname(wsFile), { recursive: true }) |
130 | | - await fs.writeFile(wsFile, JSON.stringify([rows[0]]), "utf8") |
131 | | - |
132 | | - const listed = await sessionObserver.listLearnings(ws) |
133 | | - expect(listed.length).toBe(7) |
134 | | - const injected = Object.fromEntries(listed.map((r) => [r.lesson, r.injected])) |
135 | | - expect(injected["rule 1"]).toBe(true) |
136 | | - expect(injected["rule 5"]).toBe(true) |
137 | | - // newest by timestamp, but outside the first-five window the prompt actually uses |
138 | | - expect(injected["rule 6"]).toBe(false) |
139 | | - expect(injected["rule 7"]).toBe(false) |
140 | | - expect(listed.find((r) => r.lesson === "rule 1")!.origin).toBe("global") |
141 | | - |
142 | | - expect(await sessionObserver.forgetLearning("rule 1", ws)).toBe(1) |
143 | | - const after = await sessionObserver.listLearnings(ws) |
144 | | - expect(after.map((r) => r.lesson)).not.toContain("rule 1") |
145 | | - expect(await sessionObserver.forgetLearning("rule 1", ws)).toBe(0) |
146 | | - } finally { |
147 | | - restoreUserProfile(prev) |
148 | | - await fs.rm(central, { recursive: true, force: true }) |
149 | | - await fs.rm(ws, { recursive: true, force: true }) |
150 | | - } |
151 | | - }) |
152 | | - |
153 | | - test("dedupes a re-recorded rule by text and still injects it", async () => { |
154 | | - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "oc-mix-")) |
155 | | - const prev = process.env.USERPROFILE |
156 | | - process.env.USERPROFILE = dir |
157 | | - try { |
158 | | - const file = path.join(dir, ".local", "share", "opencode", "learnings.json") |
159 | | - await fs.mkdir(path.dirname(file), { recursive: true }) |
160 | | - await fs.writeFile( |
161 | | - file, |
162 | | - JSON.stringify([{ id: "A", timestamp: "2026-09-23T00:00:00.000Z", lesson: "shared rule", source: "user_feedback" }]), |
163 | | - "utf8", |
164 | | - ) |
165 | | - // The text already exists on disk under id A, so recording it again must not |
166 | | - // duplicate it, and it must still be reported as injected. |
167 | | - await sessionObserver.recordLearning({ lesson: "shared rule", source: "user_feedback" }, dir) |
168 | | - const listed = await sessionObserver.listLearnings(dir) |
169 | | - expect(listed.find((r) => r.lesson === "shared rule")!.injected).toBe(true) |
170 | | - } finally { |
171 | | - restoreUserProfile(prev) |
172 | | - await fs.rm(dir, { recursive: true, force: true }) |
173 | | - } |
174 | | - }) |
175 | | -}) |
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { sessionObserver } from "../../src/plugin/observer" |
| 3 | +import { tmpdir } from "../fixture/fixture" |
| 4 | +import * as fs from "fs/promises" |
| 5 | +import * as path from "path" |
| 6 | + |
| 7 | +function restoreUserProfile(value: string | undefined) { |
| 8 | + if (value === undefined) delete process.env.USERPROFILE |
| 9 | + else process.env.USERPROFILE = value |
| 10 | +} |
| 11 | + |
| 12 | +describe("SessionObserverManager.onToolAfter", () => { |
| 13 | + test("records a tool call with string output", async () => { |
| 14 | + const sessionId = "obs-str-1" |
| 15 | + const callId = "call-str-1" |
| 16 | + sessionObserver.onToolBefore(sessionId, callId, "read", { filePath: "/tmp/a.md" }) |
| 17 | + sessionObserver.onToolAfter(sessionId, callId, "read", "file contents here") |
| 18 | + |
| 19 | + const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
| 20 | + if (!retro) throw new Error("no retrospective found") |
| 21 | + expect(retro.totalToolCalls).toBe(1) |
| 22 | + expect(retro.toolsUsed).toEqual(["read"]) |
| 23 | + }) |
| 24 | + |
| 25 | + test("flags MCP tool failures in the retrospective", async () => { |
| 26 | + const sessionId = "obs-err-1" |
| 27 | + const callId = "call-err-1" |
| 28 | + sessionObserver.onToolBefore(sessionId, callId, "mcp__keystone__execute-tool", {}) |
| 29 | + sessionObserver.onToolAfter(sessionId, callId, "mcp__keystone__execute-tool", "Error: Unauthorized") |
| 30 | + |
| 31 | + const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
| 32 | + expect(retro).not.toBeNull() |
| 33 | + const anomaly = retro!.anomalies.find((a) => a.type === "MCP_TOOL_FAILURE") |
| 34 | + expect(anomaly?.tool).toBe("mcp__keystone__execute-tool") |
| 35 | + }) |
| 36 | + |
| 37 | + test("handles non-string output (CallToolResult from MCP catalog)", async () => { |
| 38 | + const sessionId = "obs-mcp-1" |
| 39 | + const callId = "call-mcp-1" |
| 40 | + // Simulates the raw CallToolResult shape returned by convertTool in src/mcp/catalog.ts |
| 41 | + const mcpResult = { |
| 42 | + content: [{ type: "text", text: "some data" }], |
| 43 | + structuredContent: { foo: "bar" }, |
| 44 | + meta: {}, |
| 45 | + } |
| 46 | + |
| 47 | + sessionObserver.onToolBefore(sessionId, callId, "imprint_imprint_get_instructions", {}) |
| 48 | + // This used to throw: undefined is not an object (evaluating 'output.includes') |
| 49 | + expect(() => { |
| 50 | + sessionObserver.onToolAfter(sessionId, callId, "imprint_imprint_get_instructions", mcpResult) |
| 51 | + }).not.toThrow() |
| 52 | + }) |
| 53 | + |
| 54 | + test("handles null and undefined output gracefully", () => { |
| 55 | + const sessionId = "obs-null-1" |
| 56 | + sessionObserver.onToolBefore(sessionId, "call-null-1", "some-tool", {}) |
| 57 | + expect(() => { |
| 58 | + sessionObserver.onToolAfter(sessionId, "call-null-1", "some-tool", null) |
| 59 | + }).not.toThrow() |
| 60 | + |
| 61 | + sessionObserver.onToolBefore(sessionId, "call-undef-1", "some-tool", {}) |
| 62 | + expect(() => { |
| 63 | + sessionObserver.onToolAfter(sessionId, "call-undef-1", "some-tool", undefined) |
| 64 | + }).not.toThrow() |
| 65 | + }) |
| 66 | + |
| 67 | + test("finalizes without crashing when mixed string and object outputs are recorded", async () => { |
| 68 | + const sessionId = "obs-mixed-1" |
| 69 | + |
| 70 | + sessionObserver.onToolBefore(sessionId, "call-a", "read", {}) |
| 71 | + sessionObserver.onToolAfter(sessionId, "call-a", "read", "success") |
| 72 | + |
| 73 | + sessionObserver.onToolBefore(sessionId, "call-b", "rag_ask", {}) |
| 74 | + sessionObserver.onToolAfter(sessionId, "call-b", "rag_ask", { answer: "ok", sources: [] }) |
| 75 | + |
| 76 | + sessionObserver.onToolBefore(sessionId, "call-c", "bash", {}) |
| 77 | + sessionObserver.onToolAfter(sessionId, "call-c", "bash", "") |
| 78 | + |
| 79 | + const retro = await sessionObserver.finalizeSessionRetrospective(sessionId) |
| 80 | + expect(retro).not.toBeNull() |
| 81 | + expect(retro!.totalToolCalls).toBe(3) |
| 82 | + }) |
| 83 | +}) |
| 84 | +describe("rule visibility", () => { |
| 85 | + test("injects a rule that exists only in the project store", async () => { |
| 86 | + await using home = await tmpdir() |
| 87 | + await using ws = await tmpdir() |
| 88 | + const prev = process.env.USERPROFILE |
| 89 | + process.env.USERPROFILE = home.path |
| 90 | + try { |
| 91 | + const wsFile = path.join(ws.path, ".system_generated", "logs", "learnings.json") |
| 92 | + await fs.mkdir(path.dirname(wsFile), { recursive: true }) |
| 93 | + await fs.writeFile( |
| 94 | + wsFile, |
| 95 | + JSON.stringify([ |
| 96 | + { id: "p1", timestamp: "2026-09-23T00:00:00.000Z", lesson: "project only rule", source: "user_feedback" }, |
| 97 | + ]), |
| 98 | + "utf8", |
| 99 | + ) |
| 100 | + const row = (await sessionObserver.listLearnings(ws.path)).find((r) => r.lesson === "project only rule")! |
| 101 | + expect(row.origin).toBe("project") |
| 102 | + expect(row.injected).toBe(true) |
| 103 | + } finally { |
| 104 | + restoreUserProfile(prev) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + test("reports the real injection window and counts a rule once across both stores", async () => { |
| 109 | + await using home = await tmpdir() |
| 110 | + await using ws = await tmpdir() |
| 111 | + const prev = process.env.USERPROFILE |
| 112 | + process.env.USERPROFILE = home.path |
| 113 | + try { |
| 114 | + const globalFile = path.join(home.path, ".local", "share", "opencode", "learnings.json") |
| 115 | + await fs.mkdir(path.dirname(globalFile), { recursive: true }) |
| 116 | + const rows = Array.from({ length: 7 }, (_, i) => ({ |
| 117 | + id: "r" + (i + 1), |
| 118 | + timestamp: "2026-09-" + String(i + 1).padStart(2, "0") + "T00:00:00.000Z", |
| 119 | + lesson: "rule " + (i + 1), |
| 120 | + source: "user_feedback", |
| 121 | + })) |
| 122 | + await fs.writeFile(globalFile, JSON.stringify(rows), "utf8") |
| 123 | + const wsFile = path.join(ws.path, ".system_generated", "logs", "learnings.json") |
| 124 | + await fs.mkdir(path.dirname(wsFile), { recursive: true }) |
| 125 | + await fs.writeFile(wsFile, JSON.stringify([rows[0]]), "utf8") |
| 126 | + |
| 127 | + const listed = await sessionObserver.listLearnings(ws.path) |
| 128 | + expect(listed.length).toBe(7) |
| 129 | + const injected = Object.fromEntries(listed.map((r) => [r.lesson, r.injected])) |
| 130 | + expect(injected["rule 1"]).toBe(true) |
| 131 | + expect(injected["rule 5"]).toBe(true) |
| 132 | + // newest by timestamp, but outside the first-five window the prompt actually uses |
| 133 | + expect(injected["rule 6"]).toBe(false) |
| 134 | + expect(injected["rule 7"]).toBe(false) |
| 135 | + expect(listed.find((r) => r.lesson === "rule 1")!.origin).toBe("global") |
| 136 | + |
| 137 | + expect(await sessionObserver.forgetLearning("rule 1", ws.path)).toBe(1) |
| 138 | + expect((await sessionObserver.listLearnings(ws.path)).map((r) => r.lesson)).not.toContain("rule 1") |
| 139 | + expect(await sessionObserver.forgetLearning("rule 1", ws.path)).toBe(0) |
| 140 | + } finally { |
| 141 | + restoreUserProfile(prev) |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + test("re-recording a rule moves it to the front so it re-enters the window", async () => { |
| 146 | + await using home = await tmpdir() |
| 147 | + const prev = process.env.USERPROFILE |
| 148 | + process.env.USERPROFILE = home.path |
| 149 | + try { |
| 150 | + const file = path.join(home.path, ".local", "share", "opencode", "learnings.json") |
| 151 | + await fs.mkdir(path.dirname(file), { recursive: true }) |
| 152 | + const rows = Array.from({ length: 6 }, (_, i) => ({ |
| 153 | + id: "r" + (i + 1), |
| 154 | + timestamp: "2026-09-0" + (i + 1) + "T00:00:00.000Z", |
| 155 | + lesson: "rule " + (i + 1), |
| 156 | + source: "user_feedback", |
| 157 | + })) |
| 158 | + await fs.writeFile(file, JSON.stringify(rows), "utf8") |
| 159 | + |
| 160 | + expect((await sessionObserver.listLearnings()).find((r) => r.lesson === "rule 6")!.injected).toBe(false) |
| 161 | + |
| 162 | + await sessionObserver.recordLearning({ lesson: "rule 6", source: "user_feedback" }) |
| 163 | + |
| 164 | + const after = await sessionObserver.listLearnings() |
| 165 | + expect(after.find((r) => r.lesson === "rule 6")!.injected).toBe(true) |
| 166 | + expect(after.length).toBe(6) |
| 167 | + } finally { |
| 168 | + restoreUserProfile(prev) |
| 169 | + } |
| 170 | + }) |
| 171 | +}) |
0 commit comments