Skip to content
89 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Chat and workspace agents: channels, DMs and named agents you talk to1import { test } from "node:test";
2import assert from "node:assert/strict";
3
4import { MAX_HOPS, deliveries, delivery } from "./delivery.ts";
5
6const ship = { id: "a1", handle: "ship" };
7const docs = { id: "a2", handle: "docs" };
8
9test("a person's direct message wakes every agent in it, at hop 0", () => {
10 assert.deepEqual(deliveries({ author: "user:u", hops: 3, channelKind: "dm", agents: [ship, docs], mentioned: [] }), [
11 { agent_id: "a1", hops: 0 },
12 { agent_id: "a2", hops: 0 },
13 ]);
14});
15
16test("in a channel, a person wakes only the agent members they mention", () => {
17 assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: [] }), []);
18 assert.deepEqual(
19 deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: ["Ship", "nobody"] }),
20 [{ agent_id: "a1", hops: 0 }],
21 );
22});
23
24test("an agent wakes other agents only by mentioning them, one hop further", () => {
25 // In a direct message with two agents, an agent's reply wakes nobody unless addressed.
26 assert.deepEqual(deliveries({ author: "agent:a1", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: [] }), []);
27 assert.deepEqual(
28 deliveries({ author: "agent:a1", hops: 2, channelKind: "channel", agents: [ship, docs], mentioned: ["docs"] }),
29 [{ agent_id: "a2", hops: 3 }],
30 );
31});
32
33test("an agent is never handed its own message", () => {
34 assert.deepEqual(
35 deliveries({ author: "agent:a1", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: ["ship"] }),
36 [],
37 );
38});
39
40test("a chain stops at the hop limit", () => {
41 const at = (hops: number) =>
42 deliveries({ author: "agent:a1", hops, channelKind: "channel", agents: [ship, docs], mentioned: ["docs"] });
43 assert.equal(MAX_HOPS, 6);
44 assert.deepEqual(at(MAX_HOPS - 1), [{ agent_id: "a2", hops: MAX_HOPS }]);
45 assert.deepEqual(at(MAX_HOPS), []);
46 assert.deepEqual(at(50), []);
47});
48
49test("a delivery carries the chain's asker on g1t's own chat, and the wake's hops", () => {
50 const asker = { username: "ada", role: "member", can_write: false };
51 const place = { workspace: "acme", workspace_id: "w1", channel_id: "c1", channel_kind: "channel", channel_name: "ops" };
52 assert.deepEqual(
Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002)53 delivery(place, { agent_id: "a2", hops: 3 }, { id: "m1", thread_root: "m0" }, { hops: 2, asked_by: "u1", asker, chain: ["a1"] }),
54 { ...place, agent_id: "a2", message_id: "m1", thread_root: "m0", asked_by: "u1", asker, chain: ["a1"], hops: 3, surface: "g1t" },
Chat and workspace agents: channels, DMs and named agents you talk to55 );
56 // An agent that passed no asker back hands on none.
Merge the workspace shell: navigation and phone shell, g1t as orchestrator, agents in roles with audience-checked reads, reactions and custom emoji, live notifications and browser push, the homepage tour (agents 0002, chat 0002)57 assert.equal(delivery(place, { agent_id: "a2", hops: 1 }, { id: "m1", thread_root: null }, { hops: 0, asked_by: "u1", asker: null, chain: [] }).asker, null);
58});
59
60test("mentioning @g1t in a channel brings it in once; never into a DM", async () => {
61 const { addsOrchestrator } = await import("./delivery.ts");
62 assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["G1T"], orchestratorIsMember: false }), true);
63 assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["g1t"], orchestratorIsMember: true }), false, "already in");
64 assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["ship"], orchestratorIsMember: false }), false);
65 assert.equal(addsOrchestrator({ channelKind: "dm", mentioned: ["g1t"], orchestratorIsMember: false }), false);
66 // Once in, it is woken like any mentioned agent member.
67 const g1t = { id: "a9", handle: "g1t" };
68 assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, g1t], mentioned: ["g1t"] }), [{ agent_id: "a9", hops: 0 }]);
69});
70
71test("no ping-pong: an agent's message never goes back to the agent that sent it the work", async () => {
72 const { chainFor, sender } = await import("./delivery.ts");
73 const g1t = { id: "a9", handle: "g1t" };
74 // g1t handed the work to ship; ship's reply mentions g1t and docs.
75 const chain = chainFor(["a9"], "a1");
76 assert.deepEqual(chain, ["a9", "a1"]);
77 assert.equal(sender(chain), "a9");
78 assert.deepEqual(
79 deliveries({ author: "agent:a1", hops: 1, channelKind: "channel", agents: [ship, docs, g1t], mentioned: ["g1t", "docs"], notTo: sender(chain) }),
80 [{ agent_id: "a2", hops: 2 }],
81 );
82 assert.deepEqual(chainFor("nonsense", "a1"), ["a1"]);
83 assert.equal(sender(["a1"]), null, "a person sent it");
84});
85
86test("the hop limit holds along a chain of hand-offs", () => {
87 assert.deepEqual(deliveries({ author: "agent:a1", hops: MAX_HOPS - 1, channelKind: "channel", agents: [docs], mentioned: ["docs"] }), [{ agent_id: "a2", hops: MAX_HOPS }]);
88 assert.deepEqual(deliveries({ author: "agent:a1", hops: MAX_HOPS, channelKind: "channel", agents: [docs], mentioned: ["docs"] }), []);
Chat and workspace agents: channels, DMs and named agents you talk to89});

This file's history is long; its oldest lines are credited to the oldest commit read.