File size: 3,360 Bytes
4440aec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { describe, expect, it } from "vitest";
import { createQueueTestRun } from "../queue.test-helpers.js";
import { resolveFollowupDeliveryContextKey } from "./drain.js";

describe("followup delivery context", () => {
  it("separates runs with different gateway client capabilities", () => {
    const withoutCaps = createQueueTestRun({ prompt: "without caps" });
    const withInlineWidgets = createQueueTestRun({ prompt: "with inline widgets" });
    withInlineWidgets.run.clientCaps = ["inline-widgets"];

    expect(resolveFollowupDeliveryContextKey(withoutCaps)).not.toBe(
      resolveFollowupDeliveryContextKey(withInlineWidgets),
    );
  });

  it("normalizes capability order and duplicates", () => {
    const first = createQueueTestRun({ prompt: "first" });
    first.run.clientCaps = ["tool-events", "inline-widgets"];
    const second = createQueueTestRun({ prompt: "second" });
    second.run.clientCaps = ["inline-widgets", "tool-events", "inline-widgets"];

    expect(resolveFollowupDeliveryContextKey(first)).toBe(
      resolveFollowupDeliveryContextKey(second),
    );
  });

  it("never collect-batches runs bound to different tool targets", () => {
    const first = createQueueTestRun({ prompt: "first" });
    first.run.toolBindings = { browser: { kind: "tab", targetId: "tab-a" } };
    const second = createQueueTestRun({ prompt: "second" });
    second.run.toolBindings = { browser: { kind: "tab", targetId: "tab-b" } };

    expect(resolveFollowupDeliveryContextKey(first)).not.toBe(
      resolveFollowupDeliveryContextKey(second),
    );
  });

  it("canonicalizes equivalent tool bindings", () => {
    const first = createQueueTestRun({ prompt: "first" });
    first.run.toolBindings = { browser: { targetId: "tab-a", kind: "tab" } };
    const second = createQueueTestRun({ prompt: "second" });
    second.run.toolBindings = { browser: { kind: "tab", targetId: "tab-a" } };

    expect(resolveFollowupDeliveryContextKey(first)).toBe(
      resolveFollowupDeliveryContextKey(second),
    );
  });

  it("separates runs admitted under different conversation policies", () => {
    const restricted = createQueueTestRun({ prompt: "restricted" });
    restricted.run.conversationToolPolicy = { deny: ["exec"] };
    const unrestricted = createQueueTestRun({ prompt: "unrestricted" });

    expect(resolveFollowupDeliveryContextKey(restricted)).not.toBe(
      resolveFollowupDeliveryContextKey(unrestricted),
    );
  });

  it("canonicalizes equivalent conversation policies", () => {
    const first = createQueueTestRun({ prompt: "first" });
    first.run.conversationToolPolicy = { allow: ["read"], deny: ["exec"] };
    const second = createQueueTestRun({ prompt: "second" });
    second.run.conversationToolPolicy = { deny: ["exec"], allow: ["read"] };

    expect(resolveFollowupDeliveryContextKey(first)).toBe(
      resolveFollowupDeliveryContextKey(second),
    );
  });

  it("separates runs with different parent policy provenance", () => {
    const first = createQueueTestRun({ prompt: "first" });
    first.run.spawnedBy = "agent:main:telegram:group:first";
    const second = createQueueTestRun({ prompt: "second" });
    second.run.spawnedBy = "agent:main:telegram:group:second";

    expect(resolveFollowupDeliveryContextKey(first)).not.toBe(
      resolveFollowupDeliveryContextKey(second),
    );
  });
});