|
8 | 8 | tryBeginGatewayRootWorkAdmission, |
9 | 9 | tryBeginGatewaySuspendAdmission, |
10 | 10 | } from "../../process/gateway-work-admission.js"; |
| 11 | +import { createUserTurnTranscriptRecorder } from "../../sessions/user-turn-transcript.js"; |
11 | 12 | import { captureEnv, setTestEnvValue } from "../../test-utils/env.js"; |
12 | 13 | import type { FollowupRun, QueueSettings } from "./queue.js"; |
13 | 14 | import { |
@@ -396,38 +397,100 @@ describe("followup queue drain restart after idle window", () => { |
396 | 397 | expect(freshCalls[0]?.prompt).toBe("wait-for-lane"); |
397 | 398 | }); |
398 | 399 |
|
399 | | - it("preserves overflow summaries across deferred retries", async () => { |
400 | | - const key = `test-deferred-summary-retry-${Date.now()}`; |
401 | | - const prompts: string[] = []; |
402 | | - const settings: QueueSettings = { |
403 | | - mode: "followup", |
404 | | - debounceMs: 0, |
405 | | - cap: 1, |
406 | | - dropPolicy: "summarize", |
407 | | - }; |
408 | | - const retried = createDeferred<void>(); |
409 | | - let attempts = 0; |
| 400 | + it.each([ |
| 401 | + [true, "external_user", true], |
| 402 | + [true, "inter_session", false], |
| 403 | + [true, "internal_system", false], |
| 404 | + [false, "external_user", false], |
| 405 | + ] as const)( |
| 406 | + "preserves overflow summaries and human ownership for %s/%s", |
| 407 | + async (senderIsOwner, kind, owner) => { |
| 408 | + const key = `test-deferred-summary-retry-${Date.now()}`; |
| 409 | + const prompts: string[] = []; |
| 410 | + const followups: FollowupRun[] = []; |
| 411 | + const inputProvenance = { kind, sourceTool: "test" }; |
| 412 | + const settings: QueueSettings = { |
| 413 | + mode: "followup", |
| 414 | + debounceMs: 0, |
| 415 | + cap: 1, |
| 416 | + dropPolicy: "summarize", |
| 417 | + }; |
| 418 | + const retried = createDeferred<void>(); |
| 419 | + let attempts = 0; |
410 | 420 |
|
411 | | - const runFollowup = async (run: FollowupRun) => { |
412 | | - attempts++; |
413 | | - prompts.push(run.prompt); |
414 | | - if (attempts === 1) { |
415 | | - throw new FollowupRunDeferredError("reply lane busy"); |
416 | | - } |
417 | | - retried.resolve(); |
418 | | - }; |
| 421 | + const runFollowup = async (run: FollowupRun) => { |
| 422 | + attempts++; |
| 423 | + prompts.push(run.prompt); |
| 424 | + followups.push(run); |
| 425 | + if (attempts === 1) { |
| 426 | + throw new FollowupRunDeferredError("reply lane busy"); |
| 427 | + } |
| 428 | + retried.resolve(); |
| 429 | + }; |
419 | 430 |
|
420 | | - enqueueFollowupRun(key, createRun({ prompt: "dropped while busy" }), settings); |
421 | | - enqueueFollowupRun(key, createRun({ prompt: "kept while busy" }), settings); |
422 | | - scheduleFollowupDrain(key, runFollowup); |
| 431 | + for (const prompt of ["dropped while busy", "kept while busy"]) { |
| 432 | + const followup = createRun({ prompt }); |
| 433 | + followup.run.senderIsOwner = senderIsOwner; |
| 434 | + followup.run.inputProvenance = inputProvenance; |
| 435 | + enqueueFollowupRun(key, followup, settings); |
| 436 | + } |
| 437 | + scheduleFollowupDrain(key, runFollowup); |
423 | 438 |
|
424 | | - await retried.promise; |
| 439 | + await retried.promise; |
425 | 440 |
|
426 | | - expect(attempts).toBe(2); |
427 | | - expect(prompts[0]).toContain("Dropped 1 message"); |
428 | | - expect(prompts[0]).toContain("dropped while busy"); |
429 | | - expect(prompts[1]).toContain("Dropped 1 message"); |
430 | | - expect(prompts[1]).toContain("dropped while busy"); |
| 441 | + expect(attempts).toBe(2); |
| 442 | + for (const run of followups) { |
| 443 | + expect(run).toMatchObject({ |
| 444 | + run: { senderIsOwner, inputProvenance }, |
| 445 | + userTurnTranscriptRecorder: { |
| 446 | + message: { provenance: inputProvenance, __openclaw: { senderIsOwner: owner } }, |
| 447 | + }, |
| 448 | + }); |
| 449 | + } |
| 450 | + expect(prompts[0]).toContain("Dropped 1 message"); |
| 451 | + expect(prompts[0]).toContain("dropped while busy"); |
| 452 | + expect(prompts[1]).toContain("Dropped 1 message"); |
| 453 | + expect(prompts[1]).toContain("dropped while busy"); |
| 454 | + }, |
| 455 | + ); |
| 456 | + |
| 457 | + it.each([ |
| 458 | + [true, "external_user", true], |
| 459 | + [true, "inter_session", false], |
| 460 | + [true, "internal_system", false], |
| 461 | + [false, "external_user", false], |
| 462 | + ] as const)("keeps collected human ownership for %s/%s", async (senderIsOwner, kind, owner) => { |
| 463 | + const key = `test-collected-human-owner-${Date.now()}`; |
| 464 | + const inputProvenance = { kind, sourceTool: "test" }; |
| 465 | + const settings: QueueSettings = { mode: "collect", debounceMs: 0 }; |
| 466 | + const collected = createDeferred<FollowupRun>(); |
| 467 | + for (const prompt of ["first", "second"]) { |
| 468 | + const followup = createRun({ prompt }); |
| 469 | + followup.run.senderIsOwner = senderIsOwner; |
| 470 | + followup.run.inputProvenance = inputProvenance; |
| 471 | + followup.userTurnTranscriptRecorder = createUserTurnTranscriptRecorder({ |
| 472 | + input: { text: prompt, senderIsOwner, provenance: inputProvenance }, |
| 473 | + target: { |
| 474 | + agentId: followup.run.agentId, |
| 475 | + sessionId: followup.run.sessionId, |
| 476 | + sessionKey: key, |
| 477 | + sessionEntry: undefined, |
| 478 | + }, |
| 479 | + }); |
| 480 | + enqueueFollowupRun(key, followup, settings); |
| 481 | + } |
| 482 | + scheduleFollowupDrain(key, async (run) => collected.resolve(run)); |
| 483 | + const followup = await collected.promise; |
| 484 | + expect(followup.run.senderIsOwner).toBe(senderIsOwner); |
| 485 | + for (const message of [ |
| 486 | + followup.userTurnTranscriptRecorder?.message, |
| 487 | + await followup.userTurnTranscriptRecorder?.resolveMessage(), |
| 488 | + ]) { |
| 489 | + expect(message).toMatchObject({ |
| 490 | + provenance: inputProvenance, |
| 491 | + __openclaw: { senderIsOwner: owner }, |
| 492 | + }); |
| 493 | + } |
431 | 494 | }); |
432 | 495 |
|
433 | 496 | it("merges overflow summaries added while a deferred retry is waiting", async () => { |
|
0 commit comments