|
| 1 | +import { createHash } from "node:crypto"; |
1 | 2 | import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 3 | import { SUBAGENT_ENDED_REASON_COMPLETE } from "./subagent-lifecycle-events.js"; |
3 | 4 | import type { SubagentRunRecord } from "./subagent-registry.types.js"; |
@@ -40,6 +41,24 @@ describe("emitSubagentEndedHookOnce", () => { |
40 | 41 | }; |
41 | 42 | }; |
42 | 43 |
|
| 44 | + const readLastSubagentEndedEvent = () => { |
| 45 | + const calls = lifecycleMocks.runSubagentEnded.mock.calls as unknown as Array< |
| 46 | + [ |
| 47 | + { |
| 48 | + final?: { |
| 49 | + frozenResultTextAvailable: true; |
| 50 | + textSha256: string; |
| 51 | + byteLength: number; |
| 52 | + capturedAt?: number; |
| 53 | + }; |
| 54 | + }, |
| 55 | + ] |
| 56 | + >; |
| 57 | + const event = calls.at(-1)?.[0]; |
| 58 | + expect(event).toBeDefined(); |
| 59 | + return event!; |
| 60 | + }; |
| 61 | + |
43 | 62 | beforeAll(async () => { |
44 | 63 | mod = await import("./subagent-registry-completion.js"); |
45 | 64 | }); |
@@ -112,6 +131,82 @@ describe("emitSubagentEndedHookOnce", () => { |
112 | 131 | expect(params.persist).toHaveBeenCalledTimes(1); |
113 | 132 | }); |
114 | 133 |
|
| 134 | + it("includes privacy-minimal frozen final metadata on subagent_ended", async () => { |
| 135 | + lifecycleMocks.getGlobalHookRunner.mockReturnValue({ |
| 136 | + hasHooks: () => true, |
| 137 | + runSubagentEnded: lifecycleMocks.runSubagentEnded, |
| 138 | + }); |
| 139 | + |
| 140 | + const finalText = " child final answer 雪🚀 \n"; |
| 141 | + const capturedAt = Date.now() - 100; |
| 142 | + const entry = { |
| 143 | + ...createRunEntry(), |
| 144 | + frozenResultText: finalText, |
| 145 | + frozenResultCapturedAt: capturedAt, |
| 146 | + }; |
| 147 | + const params = createEmitParams({ entry }); |
| 148 | + const emitted = await mod.emitSubagentEndedHookOnce(params); |
| 149 | + |
| 150 | + expect(emitted).toBe(true); |
| 151 | + expect(lifecycleMocks.runSubagentEnded).toHaveBeenCalledTimes(1); |
| 152 | + const event = readLastSubagentEndedEvent(); |
| 153 | + expect(event).toMatchObject({ |
| 154 | + final: { |
| 155 | + frozenResultTextAvailable: true, |
| 156 | + textSha256: createHash("sha256").update(finalText, "utf8").digest("hex"), |
| 157 | + byteLength: Buffer.byteLength(finalText, "utf8"), |
| 158 | + capturedAt, |
| 159 | + }, |
| 160 | + }); |
| 161 | + expect(event?.final?.textSha256).not.toBe( |
| 162 | + createHash("sha256").update(finalText.trim(), "utf8").digest("hex"), |
| 163 | + ); |
| 164 | + expect(JSON.stringify(event)).not.toContain(finalText); |
| 165 | + }); |
| 166 | + |
| 167 | + it("includes frozen final metadata when capture timestamp is missing", async () => { |
| 168 | + lifecycleMocks.getGlobalHookRunner.mockReturnValue({ |
| 169 | + hasHooks: () => true, |
| 170 | + runSubagentEnded: lifecycleMocks.runSubagentEnded, |
| 171 | + }); |
| 172 | + |
| 173 | + const finalText = "final without timestamp"; |
| 174 | + const entry = { |
| 175 | + ...createRunEntry(), |
| 176 | + frozenResultText: finalText, |
| 177 | + }; |
| 178 | + const params = createEmitParams({ entry }); |
| 179 | + const emitted = await mod.emitSubagentEndedHookOnce(params); |
| 180 | + |
| 181 | + expect(emitted).toBe(true); |
| 182 | + const event = readLastSubagentEndedEvent(); |
| 183 | + expect(event.final).toEqual({ |
| 184 | + frozenResultTextAvailable: true, |
| 185 | + textSha256: createHash("sha256").update(finalText, "utf8").digest("hex"), |
| 186 | + byteLength: Buffer.byteLength(finalText, "utf8"), |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it("omits frozen final metadata when no useful final text was captured", async () => { |
| 191 | + lifecycleMocks.getGlobalHookRunner.mockReturnValue({ |
| 192 | + hasHooks: () => true, |
| 193 | + runSubagentEnded: lifecycleMocks.runSubagentEnded, |
| 194 | + }); |
| 195 | + |
| 196 | + const params = createEmitParams({ |
| 197 | + entry: { |
| 198 | + ...createRunEntry(), |
| 199 | + frozenResultText: " ", |
| 200 | + frozenResultCapturedAt: Date.now(), |
| 201 | + }, |
| 202 | + }); |
| 203 | + const emitted = await mod.emitSubagentEndedHookOnce(params); |
| 204 | + |
| 205 | + expect(emitted).toBe(true); |
| 206 | + const event = readLastSubagentEndedEvent(); |
| 207 | + expect(event.final).toBeUndefined(); |
| 208 | + }); |
| 209 | + |
115 | 210 | it("returns false when the global hook runner is not initialized yet", async () => { |
116 | 211 | lifecycleMocks.getGlobalHookRunner.mockReturnValue(null); |
117 | 212 |
|
|
0 commit comments