|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import type { AttentionRecord } from "./attention" |
| 3 | +import { requestAgent, type AgentSources } from "./attention-agent" |
| 4 | + |
| 5 | +const record = (overrides: Partial<AttentionRecord> = {}) => ({ |
| 6 | + id: "att_1", editspace: "workbench-upgrade", revision: 1, status: "open", lane: "batch-orchestrator", kind: "decision", |
| 7 | + title: "Pick a worker", context: "", options: [], producer_session: "ses_joiner", origin_session: "ses_joiner", |
| 8 | + gate_key: "gate", source_ref: { session_id: "ses_joiner", directory: "/es/lanes/batch-orchestrator" }, |
| 9 | + decision: null, delivery_state: "not-requested", created_at: 1, updated_at: 1, ...overrides, |
| 10 | +}) as AttentionRecord |
| 11 | + |
| 12 | +const sessions: Record<string, { title?: string; directory?: string }> = { |
| 13 | + ses_joiner: { title: "Joiner", directory: "/es/lanes/batch-orchestrator" }, |
| 14 | + ses_tether: { title: "Tether", directory: "/es/lanes/squadron-v2" }, |
| 15 | + ses_next: { title: "Joiner (2)", directory: "/es/lanes/batch-orchestrator" }, |
| 16 | + ses_untitled: { title: "New session - 2026-10-22T10:00:00.000Z" }, |
| 17 | + ses_brief: { title: "Front desk brief" }, |
| 18 | +} |
| 19 | +const lane = (name: string) => JSON.stringify(["workbench-upgrade", name]) |
| 20 | +const sources = (overrides: Partial<AgentSources> = {}): AgentSources => ({ |
| 21 | + session: (id) => sessions[id], |
| 22 | + handoffs: [], |
| 23 | + owners: [{ lane: lane("batch-orchestrator"), owner: "ses_joiner" }, { lane: lane("squadron-v2"), owner: "ses_tether" }], |
| 24 | + ...overrides, |
| 25 | +}) |
| 26 | + |
| 27 | +test("names the requesting agent by its session title and links its own directory", () => { |
| 28 | + expect(requestAgent(record(), sources())).toEqual({ session: "ses_joiner", label: "Joiner", named: true, |
| 29 | + directory: "/es/lanes/batch-orchestrator", lane: "batch-orchestrator" }) |
| 30 | +}) |
| 31 | + |
| 32 | +test("agents sharing one project are told apart by their own sessions", () => { |
| 33 | + const tether = record({ id: "att_2", lane: "squadron-v2", origin_session: "ses_tether", producer_session: "ses_tether", |
| 34 | + source_ref: { session_id: "ses_tether", directory: "/es/lanes/squadron-v2" } }) |
| 35 | + expect([requestAgent(record(), sources()).label, requestAgent(tether, sources()).label]).toEqual(["Joiner", "Tether"]) |
| 36 | +}) |
| 37 | + |
| 38 | +test("an unknown or untitled requester falls back to its lane, then to an explicit unknown", () => { |
| 39 | + expect(requestAgent(record({ origin_session: "ses_gone", producer_session: "ses_gone" }), sources())).toMatchObject({ |
| 40 | + session: "ses_gone", label: "batch-orchestrator", named: false }) |
| 41 | + expect(requestAgent(record({ origin_session: "ses_untitled", producer_session: "ses_untitled" }), sources()).label).toBe("batch-orchestrator") |
| 42 | + expect(requestAgent(record({ lane: null, origin_session: "ses_gone", producer_session: "ses_gone" }), sources())).toMatchObject({ |
| 43 | + label: "Unknown agent", named: false }) |
| 44 | +}) |
| 45 | + |
| 46 | +test("a handed-off requester stays the requester; the verified successor is where answers now go", () => { |
| 47 | + const handoffs = [{ lane: lane("batch-orchestrator"), predecessor: "ses_joiner", successor: "ses_next" }] |
| 48 | + const owners = [{ lane: lane("batch-orchestrator"), owner: "ses_next" }] |
| 49 | + const agent = requestAgent(record(), sources({ handoffs, owners })) |
| 50 | + expect(agent.label).toBe("Joiner") |
| 51 | + expect(agent.destination).toEqual({ session: "ses_next", label: "Joiner (2)", named: true, directory: "/es/lanes/batch-orchestrator" }) |
| 52 | + // Lineage that does not reach the current owner is not trusted. |
| 53 | + expect(requestAgent(record(), sources({ handoffs, owners: [{ lane: lane("batch-orchestrator"), owner: "ses_other" }] })).destination).toBeUndefined() |
| 54 | + // A matching chain in another editspace's lane is not this record's lane. |
| 55 | + const elsewhere = [{ lane: JSON.stringify(["other", "batch-orchestrator"]), predecessor: "ses_joiner", successor: "ses_next" }] |
| 56 | + expect(requestAgent(record(), sources({ handoffs: elsewhere, owners: [{ lane: elsewhere[0]!.lane, owner: "ses_next" }] })).destination).toBeUndefined() |
| 57 | +}) |
| 58 | + |
| 59 | +test("a later content author is listed separately and never replaces the requester", () => { |
| 60 | + const agent = requestAgent(record({ producer_session: "ses_brief" }), sources()) |
| 61 | + expect(agent.label).toBe("Joiner") |
| 62 | + expect(agent.author).toMatchObject({ session: "ses_brief", label: "Front desk brief" }) |
| 63 | +}) |
0 commit comments