|
1 | 1 | import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { clearAgentHarnesses } from "../../agents/harness/registry.js"; |
| 3 | +import { |
| 4 | + onInternalDiagnosticEvent, |
| 5 | + resetDiagnosticEventsForTest, |
| 6 | + type DiagnosticEventPayload, |
| 7 | +} from "../../infra/diagnostic-events.js"; |
3 | 8 | import type { PluginHookReplyDispatchResult } from "../../plugins/hooks.js"; |
4 | 9 | import { createInternalHookEventPayload } from "../../test-utils/internal-hook-event-payload.js"; |
5 | 10 | import { |
@@ -33,6 +38,7 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => { |
33 | 38 | clearAgentHarnesses(); |
34 | 39 | setDiscordTestRegistry(); |
35 | 40 | resetInboundDedupe(); |
| 41 | + resetDiagnosticEventsForTest(); |
36 | 42 | mocks.routeReply.mockReset().mockResolvedValue({ ok: true, messageId: "mock" }); |
37 | 43 | mocks.tryFastAbortFromMessage.mockReset().mockResolvedValue({ |
38 | 44 | handled: false, |
@@ -200,6 +206,199 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => { |
200 | 206 | expect(sessionStoreMocks.currentEntry?.pendingFinalDeliveryContext).toBeUndefined(); |
201 | 207 | }); |
202 | 208 |
|
| 209 | + it("audits substantive finals suppressed by message-tool-only delivery", async () => { |
| 210 | + hookMocks.runner.hasHooks.mockReturnValue(false); |
| 211 | + const diagnosticEvents: DiagnosticEventPayload[] = []; |
| 212 | + const stopDiagnostics = onInternalDiagnosticEvent((event) => { |
| 213 | + diagnosticEvents.push(event); |
| 214 | + }); |
| 215 | + |
| 216 | + try { |
| 217 | + const result = await dispatchReplyFromConfig({ |
| 218 | + ctx: { |
| 219 | + ...createHookCtx(), |
| 220 | + ChatType: "channel", |
| 221 | + Surface: "discord", |
| 222 | + Provider: "discord", |
| 223 | + }, |
| 224 | + cfg: emptyConfig, |
| 225 | + dispatcher: createDispatcher(), |
| 226 | + replyResolver: async () => ({ text: "model reply" }), |
| 227 | + }); |
| 228 | + |
| 229 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 230 | + const auditEvent = diagnosticEvents.find( |
| 231 | + (event) => |
| 232 | + event.type === "log.record" && |
| 233 | + event.message.includes("Normal assistant final reply was suppressed"), |
| 234 | + ); |
| 235 | + |
| 236 | + expect(result.queuedFinal).toBe(false); |
| 237 | + expect(auditEvent?.type).toBe("log.record"); |
| 238 | + if (auditEvent?.type === "log.record") { |
| 239 | + expect(auditEvent.level).toBe("warning"); |
| 240 | + expect(auditEvent.attributes).toMatchObject({ |
| 241 | + channel: "discord", |
| 242 | + sessionKey: "agent:test:session", |
| 243 | + sourceReplyDeliveryMode: "message_tool_only", |
| 244 | + suppressedFinalCount: 1, |
| 245 | + suppressedFinalTextChars: 11, |
| 246 | + sendPolicyDenied: false, |
| 247 | + }); |
| 248 | + expect(auditEvent.message).not.toContain("model reply"); |
| 249 | + } |
| 250 | + } finally { |
| 251 | + stopDiagnostics(); |
| 252 | + } |
| 253 | + }); |
| 254 | + |
| 255 | + it("does not audit silent NO_REPLY finals suppressed by message-tool-only delivery", async () => { |
| 256 | + hookMocks.runner.hasHooks.mockReturnValue(false); |
| 257 | + const diagnosticEvents: DiagnosticEventPayload[] = []; |
| 258 | + const stopDiagnostics = onInternalDiagnosticEvent((event) => { |
| 259 | + diagnosticEvents.push(event); |
| 260 | + }); |
| 261 | + |
| 262 | + try { |
| 263 | + await dispatchReplyFromConfig({ |
| 264 | + ctx: { |
| 265 | + ...createHookCtx(), |
| 266 | + ChatType: "channel", |
| 267 | + Surface: "discord", |
| 268 | + Provider: "discord", |
| 269 | + }, |
| 270 | + cfg: emptyConfig, |
| 271 | + dispatcher: createDispatcher(), |
| 272 | + replyResolver: async () => ({ text: "NO_REPLY" }), |
| 273 | + }); |
| 274 | + |
| 275 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 276 | + expect( |
| 277 | + diagnosticEvents.some( |
| 278 | + (event) => |
| 279 | + event.type === "log.record" && |
| 280 | + event.message.includes("Normal assistant final reply was suppressed"), |
| 281 | + ), |
| 282 | + ).toBe(false); |
| 283 | + } finally { |
| 284 | + stopDiagnostics(); |
| 285 | + } |
| 286 | + }); |
| 287 | + |
| 288 | + it("does not audit suppressed finals when send policy denies delivery", async () => { |
| 289 | + hookMocks.runner.hasHooks.mockReturnValue(false); |
| 290 | + const diagnosticEvents: DiagnosticEventPayload[] = []; |
| 291 | + const stopDiagnostics = onInternalDiagnosticEvent((event) => { |
| 292 | + diagnosticEvents.push(event); |
| 293 | + }); |
| 294 | + |
| 295 | + try { |
| 296 | + await dispatchReplyFromConfig({ |
| 297 | + ctx: { |
| 298 | + ...createHookCtx(), |
| 299 | + ChatType: "channel", |
| 300 | + Surface: "discord", |
| 301 | + Provider: "discord", |
| 302 | + }, |
| 303 | + cfg: { |
| 304 | + ...emptyConfig, |
| 305 | + session: { sendPolicy: { default: "deny" } }, |
| 306 | + }, |
| 307 | + dispatcher: createDispatcher(), |
| 308 | + replyResolver: async () => ({ text: "model reply" }), |
| 309 | + }); |
| 310 | + |
| 311 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 312 | + expect( |
| 313 | + diagnosticEvents.some( |
| 314 | + (event) => |
| 315 | + event.type === "log.record" && |
| 316 | + event.message.includes("Normal assistant final reply was suppressed"), |
| 317 | + ), |
| 318 | + ).toBe(false); |
| 319 | + } finally { |
| 320 | + stopDiagnostics(); |
| 321 | + } |
| 322 | + }); |
| 323 | + |
| 324 | + it("audits media-only finals suppressed by message-tool-only delivery", async () => { |
| 325 | + hookMocks.runner.hasHooks.mockReturnValue(false); |
| 326 | + const diagnosticEvents: DiagnosticEventPayload[] = []; |
| 327 | + const stopDiagnostics = onInternalDiagnosticEvent((event) => { |
| 328 | + diagnosticEvents.push(event); |
| 329 | + }); |
| 330 | + |
| 331 | + try { |
| 332 | + await dispatchReplyFromConfig({ |
| 333 | + ctx: { |
| 334 | + ...createHookCtx(), |
| 335 | + ChatType: "channel", |
| 336 | + Surface: "discord", |
| 337 | + Provider: "discord", |
| 338 | + }, |
| 339 | + cfg: emptyConfig, |
| 340 | + dispatcher: createDispatcher(), |
| 341 | + replyResolver: async () => ({ mediaUrls: ["file:///tmp/result.png"] }), |
| 342 | + }); |
| 343 | + |
| 344 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 345 | + const auditEvent = diagnosticEvents.find( |
| 346 | + (event) => |
| 347 | + event.type === "log.record" && |
| 348 | + event.message.includes("Normal assistant final reply was suppressed"), |
| 349 | + ); |
| 350 | + expect(auditEvent?.type).toBe("log.record"); |
| 351 | + if (auditEvent?.type === "log.record") { |
| 352 | + expect(auditEvent.attributes).toMatchObject({ |
| 353 | + suppressedFinalCount: 1, |
| 354 | + suppressedFinalTextChars: 0, |
| 355 | + }); |
| 356 | + } |
| 357 | + } finally { |
| 358 | + stopDiagnostics(); |
| 359 | + } |
| 360 | + }); |
| 361 | + |
| 362 | + it("audits aggregate suppressed final counts without recording text", async () => { |
| 363 | + hookMocks.runner.hasHooks.mockReturnValue(false); |
| 364 | + const diagnosticEvents: DiagnosticEventPayload[] = []; |
| 365 | + const stopDiagnostics = onInternalDiagnosticEvent((event) => { |
| 366 | + diagnosticEvents.push(event); |
| 367 | + }); |
| 368 | + |
| 369 | + try { |
| 370 | + await dispatchReplyFromConfig({ |
| 371 | + ctx: { |
| 372 | + ...createHookCtx(), |
| 373 | + ChatType: "channel", |
| 374 | + Surface: "discord", |
| 375 | + Provider: "discord", |
| 376 | + }, |
| 377 | + cfg: emptyConfig, |
| 378 | + dispatcher: createDispatcher(), |
| 379 | + replyResolver: async () => [{ text: "first" }, { text: "second reply" }], |
| 380 | + }); |
| 381 | + |
| 382 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 383 | + const auditEvent = diagnosticEvents.find( |
| 384 | + (event) => |
| 385 | + event.type === "log.record" && |
| 386 | + event.message.includes("Normal assistant final reply was suppressed"), |
| 387 | + ); |
| 388 | + expect(auditEvent?.type).toBe("log.record"); |
| 389 | + if (auditEvent?.type === "log.record") { |
| 390 | + expect(auditEvent.attributes).toMatchObject({ |
| 391 | + suppressedFinalCount: 2, |
| 392 | + suppressedFinalTextChars: 17, |
| 393 | + }); |
| 394 | + expect(auditEvent.message).not.toContain("first"); |
| 395 | + expect(auditEvent.message).not.toContain("second reply"); |
| 396 | + } |
| 397 | + } finally { |
| 398 | + stopDiagnostics(); |
| 399 | + } |
| 400 | + }); |
| 401 | + |
203 | 402 | it("preserves pending final delivery when final dispatch fails", async () => { |
204 | 403 | hookMocks.runner.hasHooks.mockReturnValue(false); |
205 | 404 | sessionStoreMocks.currentEntry = { |
|
0 commit comments