Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Drop the Slack state pre-check before repeating cut reaction, pin and delete deliveries (#2061)

Regan Bell12f1e556a20e
diff --git a/src/slack/deliveries.ts b/src/slack/deliveries.ts
index d3891b57d..e154ae98d 100644
--- a/src/slack/deliveries.ts
+++ b/src/slack/deliveries.ts
@@ -15,10 +15,7 @@ import {
dmThreadRef,
openConversationFor,
findPostedByKey,
- messageExists,
- messagePinned,
parseDeliveryTarget,
- reactionPresent,
statusPlaceholderKey,
postWithVerify,
recoveryVerifyOldest,
@@ -49,6 +46,8 @@ const PERMANENT_POST_ERRORS = new Set([
"message_not_found",
]);
+const SETTLED_PIN_ERRORS = new Set(["already_pinned", "no_pin", "not_pinned"]);
+
const DELIVERY_CLAIM_MARGIN_MS = 2_000;
const RUN_RECOVERY_GRACE_MS = 15_000;
@@ -242,10 +241,8 @@ export function createDeliveryPoller(deps: {
await deliverKeychainCard(core, client, d, channel, threadTs, deps.webUiPublicUrl);
return undefined;
}
- const recovered = recoveredRow(d);
if (d.destination.react) {
const { messageTs, emoji } = d.destination.react;
- if (recovered && (await reactionPresent(client, channel, messageTs, emoji))) return undefined;
const { failed } = await applyReactions(client, channel, messageTs, [emoji]);
if (failed.length)
console.error(
@@ -255,13 +252,12 @@ export function createDeliveryPoller(deps: {
}
if (d.destination.pin) {
const { messageTs, remove } = d.destination.pin;
- if (recovered && (await messagePinned(client, channel, messageTs)) === !remove) return undefined;
try {
if (remove) await client.pins.remove({ channel, timestamp: messageTs });
else await client.pins.add({ channel, timestamp: messageTs });
} catch (err) {
- const code = (err as { data?: { error?: string } })?.data?.error;
- if (code !== "already_pinned" && code !== "no_pin" && code !== "not_pinned")
+ const code = slackErrorCode(err);
+ if (!code || !SETTLED_PIN_ERRORS.has(code))
console.error(
`[slack-plugin] delivery ${d.id} native ${remove ? "unpin" : "pin"} failed: ${code ?? (err as Error).message}`,
);
@@ -270,13 +266,14 @@ export function createDeliveryPoller(deps: {
}
if (d.destination.delete) {
const { messageTs } = d.destination.delete;
- if (recovered && (await messageExists(client, channel, messageTs)) === false) return undefined;
try {
await client.chat.delete({ channel, ts: messageTs });
} catch (err) {
- console.error(
- `[slack-plugin] delivery ${d.id} delete failed: ${slackErrorCode(err) ?? (err as Error).message} (own messages only)`,
- );
+ const code = slackErrorCode(err);
+ if (code !== "message_not_found")
+ console.error(
+ `[slack-plugin] delivery ${d.id} delete failed: ${code ?? (err as Error).message} (own messages only)`,
+ );
}
return undefined;
}
diff --git a/src/slack/delivery.ts b/src/slack/delivery.ts
index 7263a47d3..928e3f094 100644
--- a/src/slack/delivery.ts
+++ b/src/slack/delivery.ts
@@ -474,70 +474,6 @@ export function statusPlaceholderKey(runId: string): string {
return `status:run:${runId}`;
}
-interface SlackStateClient {
- reactions: { get(args: { channel: string; timestamp: string; full: boolean }): Promise<unknown> };
- pins: { list(args: { channel: string }): Promise<unknown> };
- conversations: {
- replies(args: {
- channel: string;
- ts: string;
- oldest: string;
- latest: string;
- inclusive: boolean;
- limit: number;
- }): Promise<unknown>;
- };
-}
-
-export async function reactionPresent(
- client: Pick<SlackStateClient, "reactions">,
- channel: string,
- timestamp: string,
- name: string,
-): Promise<boolean | undefined> {
- try {
- const res = (await client.reactions.get({ channel, timestamp, full: true })) as {
- message?: { reactions?: Array<{ name?: string; count?: number }> };
- };
- return (res.message?.reactions ?? []).some((r) => r.name === name && (r.count ?? 0) > 0);
- } catch {
- return undefined;
- }
-}
-
-export async function messagePinned(
- client: Pick<SlackStateClient, "pins">,
- channel: string,
- timestamp: string,
-): Promise<boolean | undefined> {
- try {
- const res = (await client.pins.list({ channel })) as { items?: Array<{ message?: { ts?: string } }> };
- return (res.items ?? []).some((item) => item.message?.ts === timestamp);
- } catch {
- return undefined;
- }
-}
-
-export async function messageExists(
- client: Pick<SlackStateClient, "conversations">,
- channel: string,
- timestamp: string,
-): Promise<boolean | undefined> {
- try {
- const res = (await client.conversations.replies({
- channel,
- ts: timestamp,
- oldest: timestamp,
- latest: timestamp,
- inclusive: true,
- limit: 1,
- })) as { messages?: Array<{ ts?: string }> };
- return (res.messages ?? []).some((m) => m.ts === timestamp);
- } catch (err) {
- return (err as { data?: { error?: string } }).data?.error === "thread_not_found" ? false : undefined;
- }
-}
-
export function recoveryVerifyOldest(createdAt: number | undefined, editRef: string | undefined): string | undefined {
const bounds: number[] = [];
if (typeof createdAt === "number") bounds.push(createdAt / 1000 - 60);
diff --git a/src/slack/lib.ts b/src/slack/lib.ts
index 815814be6..861b49586 100644
--- a/src/slack/lib.ts
+++ b/src/slack/lib.ts
@@ -141,9 +141,6 @@ export {
findPostedByKey,
recoveryVerifyOldest,
statusPlaceholderKey,
- reactionPresent,
- messagePinned,
- messageExists,
deliveryMetadata,
type DeliveryMetadata,
type PostedPart,
diff --git a/test/slack-delivery-recovery.test.ts b/test/slack-delivery-recovery.test.ts
index 91c26d01b..4c917cbde 100644
--- a/test/slack-delivery-recovery.test.ts
+++ b/test/slack-delivery-recovery.test.ts
@@ -5,20 +5,14 @@ import { statusPlaceholderKey } from "../src/slack/lib.ts";
const RECOVERED_AGE_MS = 60_000;
-function harness(opts: {
- thread?: Array<Record<string, unknown>>;
- reactions?: Array<{ name: string; count: number }>;
- pinned?: string[];
- repliesError?: string;
-}) {
+function harness(opts: { thread?: Array<Record<string, unknown>>; errors?: Record<string, string> }) {
const calls: Array<{ method: string; args: Record<string, unknown> }> = [];
const record =
(method: string, result: unknown = {}) =>
async (args: Record<string, unknown>) => {
calls.push({ method, args });
- if (method === "conversations.replies" && opts.repliesError) {
- throw Object.assign(new Error(opts.repliesError), { data: { error: opts.repliesError } });
- }
+ const error = opts.errors?.[method];
+ if (error) throw Object.assign(new Error(error), { data: { error } });
return typeof result === "function" ? result(args) : result;
};
const client = {
@@ -26,15 +20,8 @@ function harness(opts: {
replies: record("conversations.replies", () => ({ messages: opts.thread ?? [] })),
history: record("conversations.history", () => ({ messages: opts.thread ?? [] })),
},
- reactions: {
- get: record("reactions.get", { message: { reactions: opts.reactions ?? [] } }),
- add: record("reactions.add"),
- },
- pins: {
- list: record("pins.list", { items: (opts.pinned ?? []).map((ts) => ({ message: { ts } })) }),
- add: record("pins.add"),
- remove: record("pins.remove"),
- },
+ reactions: { add: record("reactions.add") },
+ pins: { add: record("pins.add"), remove: record("pins.remove") },
chat: {
postMessage: record("chat.postMessage", { ts: "900.100" }),
update: record("chat.update"),
@@ -141,64 +128,63 @@ test("a recorded editRef skips the placeholder probe", async () => {
assert.deepEqual(h.methods(), ["chat.update"]);
});
-test("a recovered reaction row is a no-op when Slack already shows the reaction", async () => {
- const h = harness({ reactions: [{ name: "eyes", count: 1 }] });
- await h.deliver({ destination: { react: { messageTs: "100.200", emoji: "eyes" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(h.methods(), ["reactions.get"]);
- assert.deepEqual(h.acknowledgements, ["D1"]);
-});
-
-test("a recovered reaction row still reacts when Slack lacks the reaction", async () => {
- const h = harness({ reactions: [{ name: "tada", count: 1 }] });
- await h.deliver({ destination: { react: { messageTs: "100.200", emoji: "eyes" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(h.methods(), ["reactions.get", "reactions.add"]);
-});
-
-test("a fresh reaction row reacts without probing", async () => {
- const h = harness({ reactions: [{ name: "eyes", count: 1 }] });
- await h.deliver({ destination: { react: { messageTs: "100.200", emoji: "eyes" } } });
- assert.deepEqual(h.methods(), ["reactions.add"]);
-});
-
-test("recovered pin and unpin rows are no-ops when the message is already in the requested state", async () => {
- const pinned = harness({ pinned: ["100.200"] });
- await pinned.deliver({ destination: { pin: { messageTs: "100.200" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(pinned.methods(), ["pins.list"]);
- assert.deepEqual(pinned.acknowledgements, ["D1"]);
-
- const unpinned = harness({ pinned: [] });
- await unpinned.deliver({ destination: { pin: { messageTs: "100.200", remove: true } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(unpinned.methods(), ["pins.list"]);
-});
-
-test("recovered pin and unpin rows act when Slack disagrees with the requested state", async () => {
- const pin = harness({ pinned: [] });
- await pin.deliver({ destination: { pin: { messageTs: "100.200" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(pin.methods(), ["pins.list", "pins.add"]);
-
- const unpin = harness({ pinned: ["100.200"] });
- await unpin.deliver({ destination: { pin: { messageTs: "100.200", remove: true } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(unpin.methods(), ["pins.list", "pins.remove"]);
-});
-
-test("a recovered delete row is a no-op once the message is gone", async () => {
- const gone = harness({ thread: [] });
- await gone.deliver({ destination: { delete: { messageTs: "100.500" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(gone.methods(), ["conversations.replies"]);
- assert.deepEqual(gone.acknowledgements, ["D1"]);
+for (const [label, destination, method, error] of [
+ ["reaction", { react: { messageTs: "100.200", emoji: "eyes" } }, "reactions.add", "already_reacted"],
+ ["pin", { pin: { messageTs: "100.200" } }, "pins.add", "already_pinned"],
+ ["unpin", { pin: { messageTs: "100.200", remove: true } }, "pins.remove", "no_pin"],
+ ["unpin", { pin: { messageTs: "100.200", remove: true } }, "pins.remove", "not_pinned"],
+ ["delete", { delete: { messageTs: "100.500" } }, "chat.delete", "message_not_found"],
+] as const) {
+ test(`a recovered ${label} row acts without probing and acks when Slack answers ${error}`, async () => {
+ const errors: string[] = [];
+ const original = console.error;
+ console.error = (...args: unknown[]) => {
+ errors.push(args.join(" "));
+ };
+ try {
+ const h = harness({ errors: { [method]: error } });
+ await h.deliver({ destination, ageMs: RECOVERED_AGE_MS });
+ assert.deepEqual(h.methods(), [method]);
+ assert.deepEqual(h.acknowledgements, ["D1"]);
+ assert.deepEqual(errors, []);
+ } finally {
+ console.error = original;
+ }
+ });
+}
- const missingThread = harness({ repliesError: "thread_not_found" });
- await missingThread.deliver({ destination: { delete: { messageTs: "100.500" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(missingThread.methods(), ["conversations.replies"]);
+test("a delete refused for a reason other than a missing message still acks but is logged", async () => {
+ const errors: string[] = [];
+ const original = console.error;
+ console.error = (...args: unknown[]) => {
+ errors.push(args.join(" "));
+ };
+ try {
+ const h = harness({ errors: { "chat.delete": "cant_delete_message" } });
+ await h.deliver({ destination: { delete: { messageTs: "100.500" } }, ageMs: RECOVERED_AGE_MS });
+ assert.deepEqual(h.acknowledgements, ["D1"]);
+ assert.equal(errors.length, 1);
+ assert.match(errors[0]!, /cant_delete_message/);
+ } finally {
+ console.error = original;
+ }
});
-test("a recovered delete row deletes when the message still exists, and a fresh row deletes without probing", async () => {
- const present = harness({ thread: [{ ts: "100.500", text: "to remove" }] });
- await present.deliver({ destination: { delete: { messageTs: "100.500" } }, ageMs: RECOVERED_AGE_MS });
- assert.deepEqual(present.methods(), ["conversations.replies", "chat.delete"]);
- assert.equal(present.calls[0]!.args.ts, "100.500");
-
- const fresh = harness({ thread: [] });
- await fresh.deliver({ destination: { delete: { messageTs: "100.500" } } });
- assert.deepEqual(fresh.methods(), ["chat.delete"]);
+test("fresh and recovered reaction, pin and delete rows act directly", async () => {
+ for (const ageMs of [0, RECOVERED_AGE_MS]) {
+ const react = harness({});
+ await react.deliver({ destination: { react: { messageTs: "100.200", emoji: "eyes" } }, ageMs });
+ assert.deepEqual(react.methods(), ["reactions.add"]);
+ assert.deepEqual(react.acknowledgements, ["D1"]);
+
+ const pin = harness({});
+ await pin.deliver({ destination: { pin: { messageTs: "100.200" } }, ageMs });
+ assert.deepEqual(pin.methods(), ["pins.add"]);
+
+ const remove = harness({});
+ await remove.deliver({ destination: { delete: { messageTs: "100.500" } }, ageMs });
+ assert.deepEqual(remove.methods(), ["chat.delete"]);
+ assert.equal(remove.calls[0]!.args.ts, "100.500");
+ assert.deepEqual(remove.acknowledgements, ["D1"]);
+ }
});