Multiplayer agent harness for work.
Acknowledge unmentioned thread followups QM decides to answer (#1651)
Regan Bell6e8d7a8060b7
diff --git a/src/api/slack-core-client.ts b/src/api/slack-core-client.tsindex bde4fa44a..f67f53549 100644--- a/src/api/slack-core-client.ts+++ b/src/api/slack-core-client.ts@@ -43,6 +43,7 @@ import type { ConversationEvent } from "../loops/sources/adapter.ts";import { slackConversationRef } from "../loops/sources/slack.ts";interface SlackRunHooks {+ onReplying?(): void;onFirstBlock?(text: string): void;onSurfacePosted?(): void;onTasks?(tasks: Array<{ id: string; title: string; status: TaskStatus }>): void | Promise<void>;@@ -329,8 +330,14 @@ export function createSlackCoreClient(deps: SlackCoreClientDeps): SlackCoreClien},async waitRun(runId, hooks = {}) {+ let replyingSignaled = false;let firstBlockSignaled = false;let surfaceSignaled = false;+ const signalReplying = (): void => {+ if (replyingSignaled || !deps.turnStream.replying(runId)) return;+ replyingSignaled = true;+ hooks.onReplying?.();+ };const signalFirstBlock = (text: string): void => {if (firstBlockSignaled || !text.trim()) return;firstBlockSignaled = true;@@ -392,6 +399,7 @@ export function createSlackCoreClient(deps: SlackCoreClientDeps): SlackCoreClienif (view?.surfacePosted) signalSurface();return (view?.result as TurnResult | null | undefined) ?? null;}+ signalReplying();await emitTasks();await emitGoal().catch(swallowAs("slack-core-client: goal refresh", undefined));const fb = deps.turnStream.firstBlock(runId);diff --git a/src/slack/turn-flow.ts b/src/slack/turn-flow.tsindex 9283b9989..c76d5b599 100644--- a/src/slack/turn-flow.ts+++ b/src/slack/turn-flow.ts@@ -24,6 +24,7 @@ interface TurnHooks {deferDeliveryAck?: boolean;onQueued?: (runId: string) => void | Promise<void>;onSteered?: (runId: string) => void | Promise<void>;+ onReplying?: () => void;onFirstBlock?: (text: string) => void;onSurfacePosted?: () => void;onTasks?: (tasks: RunTaskView[]) => void;@@ -110,6 +111,7 @@ export function createTurnFlow(core: SlackCoreClient): TurnFlow {let result: TurnResult | null;try {result = await core.waitRun(runId, {+ ...(hooks.onReplying ? { onReplying: hooks.onReplying } : {}),...(hooks.onFirstBlock ? { onFirstBlock: hooks.onFirstBlock } : {}),...(hooks.onSurfacePosted ? { onSurfacePosted: hooks.onSurfacePosted } : {}),...(hooks.onTasks ? { onTasks: hooks.onTasks } : {}),diff --git a/src/slack/turn-handler.ts b/src/slack/turn-handler.tsindex 485d58a29..d9f2d2f3b 100644--- a/src/slack/turn-handler.ts+++ b/src/slack/turn-handler.ts@@ -65,6 +65,7 @@ import {} from "./lib.ts";import type { GatewayContext, TurnResult } from "../types.ts";import type { AckGate } from "./deferred-ack.ts";+import type { AckPresenter } from "./presenters.ts";import type { SlackCoreClient } from "../api/slack-core-client.ts";import type { CoreTurnBody, TurnFlow } from "./turn-flow.ts";import type { BotIdentity, Directory } from "./directory.ts";@@ -389,32 +390,34 @@ export function createTurnHandler(deps: {if (intercepted) return;}- const ack = inc.unprompted- ? undefined- : createAckPresenter({- taskManaged,- postAck: async (text) => {- const rendered = toSlackMrkdwn(text);- if (await taskList?.addLead(rendered)) return;- const ts = await postReply(rendered);- if (ts) {- await taskList?.attach(ts, rendered);- }- },- addReaction: (name) => client.reactions.add({ channel: inc.channel, timestamp: inc.ts, name }).then(() => {}),- removeReaction: (name) =>- client.reactions.remove({ channel: inc.channel, timestamp: inc.ts, name }).then(() => {}),- emojiCandidates: (() => {- const override = deps.ackEmojiCandidates?.();- return override?.length ? [...override] : [...DEFAULT_ACK_REACTIONS];- })(),- emojiPick: taskManaged- ? undefined- : ackEmoji.requestAckEmoji(text, ackEmoji.ackPickCandidates(client), {- channel: inc.channel,- ts: inc.ts,- }),- });+ let ack: AckPresenter | undefined;+ const startAck = (): AckPresenter =>+ (ack ??= createAckPresenter({+ taskManaged,+ postAck: async (text) => {+ const rendered = toSlackMrkdwn(text);+ if (await taskList?.addLead(rendered)) return;+ const ts = await postReply(rendered);+ if (ts) {+ await taskList?.attach(ts, rendered);+ }+ },+ addReaction: (name) => client.reactions.add({ channel: inc.channel, timestamp: inc.ts, name }).then(() => {}),+ removeReaction: (name) =>+ client.reactions.remove({ channel: inc.channel, timestamp: inc.ts, name }).then(() => {}),+ emojiCandidates: (() => {+ const override = deps.ackEmojiCandidates?.();+ return override?.length ? [...override] : [...DEFAULT_ACK_REACTIONS];+ })(),+ emojiPick: taskManaged+ ? undefined+ : ackEmoji.requestAckEmoji(text, ackEmoji.ackPickCandidates(client), {+ channel: inc.channel,+ ts: inc.ts,+ }),+ }));+ const acknowledges = !inc.unprompted || !inc.synthetic;+ if (!inc.unprompted) startAck();if (!inc.unprompted) {taskList = createTaskListPresenter({post: (text, blocks) => postReply(text, blocks),@@ -598,12 +601,13 @@ export function createTurnHandler(deps: {inc.ackGate?.persisted();await moveTaskAck(runId, inc.ts);},- ...(ack+ ...(acknowledges? {+ onReplying: () => void startAck(),onFirstBlock: (blockText: string) => {- ack.onFirstBlock(extractPrivateContinuation(cleanAgentReplyForSlack(blockText).text).text);+ startAck().onFirstBlock(extractPrivateContinuation(cleanAgentReplyForSlack(blockText).text).text);},- onSurfacePosted: () => ack.onSurfacePosted(),+ onSurfacePosted: () => startAck().onSurfacePosted(),}: {}),...(taskList@@ -741,6 +745,7 @@ export function createTurnHandler(deps: {await deliverReply();}} catch (err) {+ await settleAck();if (queuedRunId) {console.error(`[slack-plugin] reply post failed after run ${queuedRunId} finished (ch=${inc.channel} ts=${inc.ts}): ${(err as Error).message} — leaving delivery run:${queuedRunId} for the recovery poller`,diff --git a/test/slack-unprompted-ack.test.ts b/test/slack-unprompted-ack.test.tsnew file mode 100644index 000000000..0118dd617--- /dev/null+++ b/test/slack-unprompted-ack.test.ts@@ -0,0 +1,169 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { createTurnHandler } from "../src/slack/turn-handler.ts";+import { createConversationSerializer } from "../src/slack/conversation-view.ts";+import { createThreadTracker, createDeduper } from "../src/slack/lib.ts";+import type { BotIdentity, Directory } from "../src/slack/directory.ts";++const ids = { botUserId: "UBOT", ownBotId: "BBOT" } as BotIdentity;+const actor = { externalId: "U1", displayName: "Teammate" };+const directory = {+ classifyUserCached: async () => ({ actor }),+} as unknown as Directory;++function harness(run: (hooks: any) => Promise<unknown>) {+ const reactions: string[] = [];+ const posts: string[] = [];+ const turns: any[] = [];+ const client = {+ reactions: {+ add: async ({ name, timestamp }: { name: string; timestamp: string }) => {+ reactions.push(`+${name}@${timestamp}`);+ },+ remove: async ({ name, timestamp }: { name: string; timestamp: string }) => {+ reactions.push(`-${name}@${timestamp}`);+ },+ },+ chat: {+ postMessage: async ({ text }: { text: string }) => {+ posts.push(text);+ return { ts: `9${posts.length}.0` };+ },+ },+ };+ const handler = createTurnHandler({+ core: { stageBlob: async () => ({ blobId: "b", sizeBytes: 0 }) },+ flow: {+ inFlightRuns: { add() {}, delete() {}, has: () => false },+ inFlightRunByThread: { get() {}, set() {}, clear() {} },+ ackRunDelivery() {},+ callCore: async (turn: any, hooks: any) => {+ turns.push(turn);+ return run(hooks);+ },+ },+ directory,+ mirror: { mirrorMessageEvent: async () => {} },+ serializer: createConversationSerializer({+ ids,+ directory,+ externalParticipantsEnabled: async () => false,+ readHistory: async () => {+ throw new Error("history unavailable");+ },+ }),+ ackEmoji: {+ refreshAckEmoji() {},+ ackPickCandidates: () => [],+ requestAckEmoji: async () => undefined,+ },+ ackEmojiCandidates: () => ["eyes"],+ ids,+ threads: createThreadTracker(),+ deduper: createDeduper(),+ externalParticipantsEnabled: async () => false,+ } as unknown as Parameters<typeof createTurnHandler>[0]);+ const followup = {+ kind: "channel" as const,+ channel: "C1",+ userId: "U1",+ rawText: "can you also check staging?",+ files: [],+ threadTs: "1.000",+ ts: "2.000",+ unprompted: true,+ prefetched: { actor, info: { id: "C1", name: "eng" } as any, audience: [actor] },+ };+ return { handler, client, followup, reactions, posts, turns };+}++test("an unmentioned thread followup QM answers gets the ack reaction added then cleared", async () => {+ const h = harness(async (hooks) => {+ hooks.onReplying?.();+ await new Promise((r) => setTimeout(r, 2_200));+ return { status: "ok", reply: "Staging is green." };+ });+ await h.handler.handleIncoming(h.followup, h.client);+ assert.equal(h.turns[0].unprompted, true);+ assert.deepEqual(h.reactions, ["[email protected]", "[email protected]"]);+ assert.deepEqual(h.posts, ["Staging is green."]);+});++test("an answered followup's ack clears as soon as QM posts into the thread", async () => {+ const h = harness(async (hooks) => {+ hooks.onReplying?.();+ await new Promise((r) => setTimeout(r, 2_200));+ hooks.onSurfacePosted?.();+ await new Promise((r) => setTimeout(r, 20));+ assert.deepEqual(h.reactions, ["[email protected]", "[email protected]"]);+ return { status: "ok", reply: "" };+ });+ await h.handler.handleIncoming(h.followup, h.client);+ assert.deepEqual(h.reactions, ["[email protected]", "[email protected]"]);+});++test("an answered followup whose reply post fails still clears its ack", async () => {+ const h = harness(async (hooks) => {+ await hooks.onQueued?.("r1");+ hooks.onReplying?.();+ await new Promise((r) => setTimeout(r, 2_200));+ return { status: "ok", reply: "Staging is green." };+ });+ h.client.chat.postMessage = async () => {+ throw new Error("slack 500");+ };+ await h.handler.handleIncoming(h.followup, h.client);+ assert.deepEqual(h.reactions, ["[email protected]", "[email protected]"]);+});++test("synthetic reaction turns never receive ack hooks", async () => {+ let hooksSeen: string[] = [];+ const h = harness(async (hooks) => {+ hooksSeen = Object.keys(hooks).filter((k) => k.startsWith("on") && typeof hooks[k] === "function");+ return { status: "silent" };+ });+ await h.handler.handleIncoming({ ...h.followup, synthetic: true }, h.client);+ assert.ok(!hooksSeen.includes("onReplying"));+ assert.ok(!hooksSeen.includes("onFirstBlock"));+ assert.deepEqual(h.reactions, []);+});++for (const outcome of [{ status: "silent" }, { status: "react", reactions: ["thumbsup"] }] as const) {+ test(`an unmentioned followup QM declines (${outcome.status}) gets no ack reaction`, async () => {+ const h = harness(async () => {+ await new Promise((r) => setTimeout(r, 2_200));+ return outcome;+ });+ await h.handler.handleIncoming(h.followup, h.client);+ assert.deepEqual(h.reactions, outcome.status === "react" ? ["[email protected]"] : []);+ assert.deepEqual(h.posts, []);+ });+}++test("waitRun signals replying only once the run has passed detection and begun its reply", async () => {+ const { createSlackCoreClient } = await import("../src/api/slack-core-client.ts");+ const { createTurnStream } = await import("../src/runs/turn-stream.ts");+ const turnStream = createTurnStream();+ let polls = 0;+ let replying = 0;+ const client = createSlackCoreClient({+ turnStream,+ runs: {+ onTerminal() {},+ get: async () => {+ polls++;+ if (polls === 2) turnStream.begin("r1");+ return { id: "r1", status: polls >= 4 ? "done" : "running", attempts: 1 };+ },+ },+ tasks: { list: async () => [] },+ app: { getRun: async () => ({ result: { status: "ok", reply: "done" } }) },+ } as any);+ await client.waitRun("r1", {+ onReplying: () => {+ assert.ok(polls >= 2, "not before the reply begins");+ replying++;+ },+ });+ assert.equal(replying, 1);+});