Multiplayer agent harness for work.
Move Slack relay attribution into a quiet footer (#1194)
Regan Bell933f93e3c21e
diff --git a/docs/screenshots/slack-relay-footer.png b/docs/screenshots/slack-relay-footer.pngnew file mode 100644index 000000000..368122ac3Binary files /dev/null and b/docs/screenshots/slack-relay-footer.png differdiff --git a/src/reach/reach.ts b/src/reach/reach.tsindex ac9779a5e..317102f70 100644--- a/src/reach/reach.ts+++ b/src/reach/reach.ts@@ -256,11 +256,6 @@ export async function resolveReachTarget(};}-export function attributeRelay(text: string, senderDisplayName: string | undefined): string {- const who = senderDisplayName?.trim();- return who ? `${who} asked me to pass on:\n${text}` : text;-}-export interface ReachEnqueueInput {deliveries: DeliveryStore;destination: Destination;@@ -312,12 +307,10 @@ export async function reachEnqueue(input: ReachEnqueueInput): Promise<Delivery>...(input.shadow ? { shadow: true } : {}),});}- const relayPrefixed = isThirdPartyRelay(input.destination, input.attributeAs)- ? attributeRelay(input.text, input.attributeAs)- : input.text;+ const relaySender = isThirdPartyRelay(input.destination, input.attributeAs) ? input.attributeAs?.trim() : undefined;return input.deliveries.enqueue({- destination: input.destination,- text: relayPrefixed,+ destination: relaySender ? { ...input.destination, relaySender } : input.destination,+ text: input.text,...(input.attachments?.length ? { attachments: input.attachments } : {}),idempotencyKey: input.idempotencyKey,...(input.provenance ? { provenance: input.provenance } : {}),diff --git a/src/slack/deliveries.ts b/src/slack/deliveries.tsindex b8f94b179..aa6b9fbba 100644--- a/src/slack/deliveries.ts+++ b/src/slack/deliveries.ts@@ -145,15 +145,20 @@ export function createDeliveryPoller(deps: {const ackDelivery = (id: string, body?: unknown): Promise<void> =>core.ackDelivery(id, body as { recipientThreadRef?: string; slackApiMs?: number } | undefined);- function cronFooter(d: Delivery): Array<Record<string, unknown>> {+ function deliveryFooter(d: Delivery): Array<Record<string, unknown>> {const base = deps.webUiPublicUrl?.trim().replace(/\/+$/, "");const id = d.provenance?.trigger === "cron" ? cronIdOf(d.provenance.sourceThreadRef) : null;- if (!base || !id) return [];+ const sender = d.destination.relaySender?.trim().replace(/^@+/, "");+ const attribution = sender ? [{ type: "plain_text", text: `Sent for @${sender}`, emoji: false }] : [];+ if (!base || !id) return attribution;const title = (d.provenance?.sourceTitle?.trim() || "Cron").replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");- return [{ type: "mrkdwn", text: `${title} · <${base}/crons/${encodeURIComponent(id)}|Settings>`, verbatim: true }];+ return [+ ...attribution,+ { type: "mrkdwn", text: `${title} · <${base}/crons/${encodeURIComponent(id)}|Settings>`, verbatim: true },+ ];}const deliveryTracker = createDeliveryTracker();@@ -242,9 +247,9 @@ export function createDeliveryPoller(deps: {.catch(swallowAs("slack: post upload-failure note", undefined));}};- const settingsFooter = cronFooter(d);+ const messageFooter = deliveryFooter(d);const footer = [- ...settingsFooter,+ ...messageFooter,...(d.destination.debugFooter ? [{ type: "mrkdwn", text: d.destination.debugFooter }] : []),];const taskList = d.destination.taskList?.length ? renderTaskList(d.destination.taskList) : undefined;@@ -256,7 +261,7 @@ export function createDeliveryPoller(deps: {...(footer.length ? [{ type: "context", elements: footer }] : []),]: undefined;- if (!text.trim() && !(settingsFooter.length && d.attachments?.length)) {+ if (!text.trim() && !(messageFooter.length && d.attachments?.length)) {if (taskList) {let preserved = false;if (d.destination.editRef) {@@ -368,7 +373,7 @@ export function createDeliveryPoller(deps: {if (!text.trim() && !d.attachments?.length) return undefined;const channel = await openConversationFor(client, [d.destination.target]);const threadTs = d.destination.threadTs;- const footer = cronFooter(d);+ const footer = deliveryFooter(d);const blocks = footer.length? [...(text.trim() ? slackSectionBlocks(text) : []), { type: "context", elements: footer }]: undefined;diff --git a/src/slack/delivery.ts b/src/slack/delivery.tsindex a98096fe3..dc979313a 100644--- a/src/slack/delivery.ts+++ b/src/slack/delivery.ts@@ -500,12 +500,24 @@ export async function postWithVerify(opts?: { attempts?: number; verifyFirst?: boolean; verifyOldest?: string; verifyBestEffort?: boolean },): Promise<{ ts: string | undefined; channel: string; reused?: boolean; parts?: PostedPart[] }> {const fullText = typeof args.text === "string" ? args.text : "";- if (args.blocks) {- if (fullText.length > SLACK_TEXT_LIMIT - 1_000) args.text = safeClip(fullText, SLACK_TEXT_LIMIT - 1_000);- } else if (fullText.length > SLACK_POST_SPLIT_LIMIT) {+ const batches: PostMessageArgs[] = [];+ if (Array.isArray(args.blocks) && args.blocks.length > 50) {+ for (let offset = 0; offset < args.blocks.length; offset += 50) {+ const blocks = args.blocks.slice(offset, offset + 50);+ const text = blocks+ .filter((block) => block.type === "section" && typeof block.text?.text === "string")+ .map((block) => block.text.text)+ .join("\n");+ batches.push({ ...args, blocks, text });+ }+ } else if (!args.blocks && fullText.length > SLACK_POST_SPLIT_LIMIT) {+ batches.push(...safeChunks(fullText, SLACK_POST_SPLIT_LIMIT).map((text) => ({ ...args, text })));+ }+ if (batches.length) {const parts: PostedPart[] = [];- for (const [i, text] of safeChunks(fullText, SLACK_POST_SPLIT_LIMIT).entries()) {- const res = await postWithVerify(client, { ...args, text }, splitPostKey(idempotencyKey, i), {+ for (const [i, batch] of batches.entries()) {+ const text = batch.text ?? "";+ const res = await postWithVerify(client, batch, splitPostKey(idempotencyKey, i), {...opts,verifyFirst: true,verifyBestEffort: !opts?.verifyFirst,@@ -525,6 +537,9 @@ export async function postWithVerify(parts,};}+ if (args.blocks && fullText.length > SLACK_TEXT_LIMIT - 1_000) {+ args.text = safeClip(fullText, SLACK_TEXT_LIMIT - 1_000);+ }const maxAttempts = opts?.attempts ?? 3;const verifyOldest = opts?.verifyOldest ?? String((Date.now() - 5_000) / 1000);args.metadata = deliveryMetadata(idempotencyKey);diff --git a/src/types.ts b/src/types.tsindex 8eef23602..8543036be 100644--- a/src/types.ts+++ b/src/types.ts@@ -175,6 +175,7 @@ export interface Destination {target: string;audienceScopeId?: ScopeId;onBehalfOf?: string;+ relaySender?: string;threadTs?: string;editRef?: string;taskList?: Array<{diff --git a/test/dm-relay.test.ts b/test/dm-relay.test.tsindex f55b6b093..f62390ba7 100644--- a/test/dm-relay.test.ts+++ b/test/dm-relay.test.ts@@ -325,7 +325,8 @@ describe("agent → teammate DM: the cron recipient route (§10)", () => {const pending = await built.app.pendingDeliveries("principal");const d = pending.find((x) => x.id === body.deliveryId);assert.ok(d, "delivery is in the principal queue");- assert.equal(d!.text, "Carol asked me to pass on:\nship it 🚀");+ assert.equal(d!.text, "ship it 🚀");+ assert.equal(d!.destination.relaySender, "Carol");assert.equal(d!.destination.onBehalfOf, "U-carol");});@@ -356,7 +357,8 @@ describe("agent → teammate DM: the cron recipient route (§10)", () => {const pending = await built.app.pendingDeliveries("group");const d = pending.find((x) => x.id === body.deliveryId);assert.ok(d, "delivery is in the group queue");- assert.equal(d!.text, "Carol asked me to pass on:\nheads up");+ assert.equal(d!.text, "heads up");+ assert.equal(d!.destination.relaySender, "Carol");assert.equal(d!.destination.type, "group");assert.equal(d!.destination.target, "G-jrs");});diff --git a/test/reach.test.ts b/test/reach.test.tsindex a7dc62a76..debb2b757 100644--- a/test/reach.test.ts+++ b/test/reach.test.ts@@ -3,7 +3,6 @@ import assert from "node:assert/strict";import {resolveReachTarget,- attributeRelay,reachEnqueue,withReact,withDelete,@@ -184,11 +183,6 @@ describe("Reach: person-keyed parity (delivery never re-derives it from scope la});describe("Reach: verbatim relay attribution", () => {- it("prefixes a third-party relay with the sender's name; passes through when no name", () => {- assert.equal(attributeRelay("ship it", "Carol"), "Carol asked me to pass on:\nship it");- assert.equal(attributeRelay("ship it", undefined), "ship it");- });-it("reachEnqueue attributes a third-party DM but not a self-reminder or a channel post", async () => {const deliveries = createDeliveryStore();const toAlice: Destination = {@@ -225,7 +219,10 @@ describe("Reach: verbatim relay attribution", () => {idempotencyKey: "k4",attributeAs: "Carol",});- assert.equal(d1.text, "Carol asked me to pass on:\nping");+ assert.equal(d1.text, "ping");+ assert.equal(d1.destination.relaySender, "Carol");+ assert.equal(d2.destination.relaySender, undefined);+ assert.equal(d3.destination.relaySender, undefined);assert.equal(d2.text, "remember milk");assert.equal(d3.text, "heads up team");});@@ -403,7 +400,8 @@ describe("runTrigger: cron produces deliveries, Reach gates them", () => {assert.equal(out.authzFailed, false);const pending = await deps.deliveries.pending("principal");assert.equal(pending.length, 1);- assert.equal(pending[0]!.text, "Carol asked me to pass on:\nthe deploy is done 🚀");+ assert.equal(pending[0]!.text, "the deploy is done 🚀");+ assert.equal(pending[0]!.destination.relaySender, "Carol");});it("a fire's attachments ride the delivery with the reply (the tweet-digest regression)", async () => {diff --git a/test/slack-deliveries.test.ts b/test/slack-deliveries.test.tsindex 0fa9d8de6..e1527aaa4 100644--- a/test/slack-deliveries.test.ts+++ b/test/slack-deliveries.test.ts@@ -223,3 +223,68 @@ for (const type of ["slack", "group", "principal"]) {assert.deepEqual(acknowledgements, ["D1"]);});}++for (const type of ["group", "principal"]) {+ for (const sender of ["josh", "@josh", "<@U123> & <!channel>"]) {+ test(`${type} relay attribution is a plain-text footer for ${sender}`, async () => {+ const { posts } = await deliver({ type, relaySender: sender }, undefined, undefined, "Ship it");+ assert.equal(posts.length, 1);+ assert.equal(posts[0]!.text, "Ship it");+ assert.deepEqual(posts[0]!.blocks, [+ { type: "section", text: { type: "mrkdwn", text: "Ship it" } },+ {+ type: "context",+ elements: [{ type: "plain_text", text: `Sent for @${sender.replace(/^@+/, "")}`, emoji: false }],+ },+ ]);+ });+ }++ test(`${type} attachment-only relay retains its attribution alongside cron settings`, async () => {+ const { posts, uploads } = await deliver(+ { type, relaySender: "josh" },+ "cron:c1:fire:123",+ "https://agent.example/web-ui",+ "",+ );+ assert.equal(posts.length, 1);+ const blocks = posts[0]!.blocks as Array<{ type: string; elements: Array<{ type: string; text: string }> }>;+ assert.equal(blocks.length, 1);+ assert.equal(blocks[0]!.type, "context");+ assert.deepEqual(blocks[0]!.elements[0], { type: "plain_text", text: "Sent for @josh", emoji: false });+ assert.equal(blocks[0]!.elements[1]!.type, "mrkdwn");+ assert.ok(uploads.length);+ });+}++for (const type of ["group", "principal"]) {+ test(`${type} long relays split within Slack's block limit and keep the footer last`, async () => {+ const text = "x".repeat(145_000);+ const { posts, mirrors } = await deliver({ type, relaySender: "josh" }, undefined, undefined, text);+ assert.equal(+ mirrors+ .map((mirror) => mirror.text)+ .join("")+ .replaceAll("\n", ""),+ text,+ );+ assert.equal(posts.length, 2);+ const blocks = posts.flatMap((post) => {+ const batch = post.blocks as Array<{ type: string; text?: { text: string }; elements?: unknown[] }>;+ assert.ok(batch.length <= 50);+ return batch;+ });+ assert.equal(+ blocks+ .filter((block) => block.type === "section")+ .map((block) => block.text!.text)+ .join(""),+ text,+ );+ assert.deepEqual(blocks.at(-1), {+ type: "context",+ elements: [{ type: "plain_text", text: "Sent for @josh", emoji: false }],+ });+ assert.equal(blocks.filter((block) => block.type === "context").length, 1);+ });+}diff --git a/test/slack-delivery.test.ts b/test/slack-delivery.test.tsindex 62802529b..e9221bd78 100644--- a/test/slack-delivery.test.ts+++ b/test/slack-delivery.test.ts@@ -1149,3 +1149,26 @@ test("split parts verify strictly when the caller asked for verification, best-e"a caller that asked for verification keeps strict verification on every part",);});++test("postWithVerify: a replay of split blocks posts only the missing batch", async () => {+ const blocks = Array.from({ length: 51 }, (_, i) => ({+ type: "section",+ text: { type: "mrkdwn", text: `Part ${i}` },+ }));+ const h = verifyHarness({+ postResults: [{ ok: { ts: "9.2", channel: "C1" } }],+ historyMessages: [{ ts: "9.1", metadata: { event_type: "qm_delivery", event_payload: { idempotency_key: KEY } } }],+ });+ const res = await postWithVerify(h.client, { channel: "C1", text: "Full fallback", blocks }, KEY, {+ verifyFirst: true,+ });+ assert.equal(h.postCalls, 1);+ assert.equal(h.postArgs[0].metadata.event_payload.idempotency_key, `${KEY}#p2`);+ assert.deepEqual(h.postArgs[0].blocks, blocks.slice(50));+ assert.equal(res.ts, "9.1");+ assert.ok(!res.reused);+ assert.deepEqual(+ res.parts?.map((part) => part.reused ?? false),+ [true, false],+ );+});