Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Support threads in person DMs (#643)

Joshua France72a5ed26e624
diff --git a/README.md b/README.md
index da02d70d9..145a890e4 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ A multiplayer agent harness for work. In Slack and on the web.
## Setup
-Tell your coding agent of choice `Let's deploy https://github.com/yc-software/qm`. From here, it should follow the deployment guide in this repo.
+Tell your coding agent of choice `Let's deploy https://github.com/yc-software/qm`. From here, it should follow the deployment guide in this repo.
## What is QM?
diff --git a/src/api/agent-api-catalog.ts b/src/api/agent-api-catalog.ts
index 2a5388dc4..d4db19a51 100644
--- a/src/api/agent-api-catalog.ts
+++ b/src/api/agent-api-catalog.ts
@@ -235,7 +235,7 @@ const FAMILIES: AgentApiFamily[] = [
method: "POST",
path: "/v1/reach",
summary:
- "send a teammate a DM, post to a channel, or post to a group DM RIGHT NOW — `text` plus `recipient`, `channel`, or `participants` (the group DM's other members — the group is opened for you if it doesn't exist yet, so never ask someone to create one), optionally with `files` (workspace-relative paths, attached to the message all-or-nothing). EXTREMELY IMPORTANT: a `channel` post broadcasts to everyone there — pick the narrowest audience that can act; a question or errand for one person goes to their DM (`recipient`), NEVER a public channel, unless the person you're helping explicitly named that channel as the destination or the message genuinely concerns the whole room; pass `threadTs` (the parent message's ts) with a `channel`/`participants` post to reply inside that thread instead of top-level; or react to a message instead of posting with `react:{ts,emoji}` plus a `channel`/`participants`; or retract one of your own messages with `delete:{ts}` (no target = this conversation, or name a `channel`/`participants` to delete elsewhere) — find a message's `ts` via /v1/surface-context; pass `unfurlLinks:false` to suppress Slack previews (no schedule; for later/recurring use /v1/crons)",
+ "send a teammate a DM, post to a channel, or post to a group DM RIGHT NOW — `text` plus `recipient`, `channel`, or `participants` (the group DM's other members — the group is opened for you if it doesn't exist yet, so never ask someone to create one), optionally with `files` (workspace-relative paths, attached to the message all-or-nothing). EXTREMELY IMPORTANT: a `channel` post broadcasts to everyone there — pick the narrowest audience that can act; a question or errand for one person goes to their DM (`recipient`), NEVER a public channel, unless the person you're helping explicitly named that channel as the destination or the message genuinely concerns the whole room; pass `threadTs` (the parent message's ts) with a `recipient`/`channel`/`participants` post to reply inside that thread instead of top-level; or react to a message instead of posting with `react:{ts,emoji}` plus a `channel`/`participants`; or retract one of your own messages with `delete:{ts}` (no target = this conversation, or name a `channel`/`participants` to delete elsewhere) — find a message's `ts` via /v1/surface-context; pass `unfurlLinks:false` to suppress Slack previews (no schedule; for later/recurring use /v1/crons)",
},
],
},
diff --git a/src/api/app-messaging.ts b/src/api/app-messaging.ts
index 5cbf95742..55717005a 100644
--- a/src/api/app-messaging.ts
+++ b/src/api/app-messaging.ts
@@ -467,12 +467,16 @@ export function createMessagingMethods(
if (r.group) extra.group = r.group;
}
if (input.threadTs) {
- if (baseDestination.type !== "slack" && baseDestination.type !== "group") {
+ if (
+ baseDestination.type !== "slack" &&
+ baseDestination.type !== "group" &&
+ baseDestination.type !== "principal"
+ ) {
return {
ok: false,
status: 400,
error: "bad_request",
- message: "threadTs threads a channel or group DM post — a DM to a person has no threads",
+ message: "threadTs requires a Slack channel, group DM, or person DM",
};
}
baseDestination = withThread(baseDestination, input.threadTs);
diff --git a/src/api/routes/reach.ts b/src/api/routes/reach.ts
index d56a740e7..70488b4fb 100644
--- a/src/api/routes/reach.ts
+++ b/src/api/routes/reach.ts
@@ -80,12 +80,6 @@ async function reachNow(ctx: ApiCtx): Promise<void> {
message: "threadTs threads a text post — react/delete already name their target message ts",
});
}
- if (typeof b.recipient === "string") {
- return sendJson(res, 400, {
- error: "bad_request",
- message: "threadTs applies to a channel or group DM post — a DM to a person has no threads",
- });
- }
}
const hasNamedTarget =
typeof b.recipient === "string" || typeof b.channel === "string" || Array.isArray(b.participants);
diff --git a/src/reach/reach.ts b/src/reach/reach.ts
index 49e776caf..32f54fe51 100644
--- a/src/reach/reach.ts
+++ b/src/reach/reach.ts
@@ -288,7 +288,10 @@ export function withDelete(destination: Destination, del: { messageTs: string }
}
export function withThread(destination: Destination, threadTs: string | undefined): Destination {
- return threadTs ? { ...destination, target: `${destination.target}:${threadTs}` } : destination;
+ if (!threadTs) return destination;
+ return destination.type === "principal"
+ ? { ...destination, threadTs }
+ : { ...destination, target: `${destination.target}:${threadTs}` };
}
export function withEdit(destination: Destination, editRef: string | undefined): Destination {
diff --git a/src/slack/deliveries.ts b/src/slack/deliveries.ts
index c331b8bf2..5052788f3 100644
--- a/src/slack/deliveries.ts
+++ b/src/slack/deliveries.ts
@@ -233,18 +233,19 @@ export function createDeliveryPoller(deps: {
const text = toSlackMrkdwn(stripReactionDirectives(d.text));
if (!text.trim() && !d.attachments?.length) return undefined;
const channel = await openConversationFor(client, [d.destination.target]);
+ const threadTs = d.destination.threadTs;
if (text.trim()) {
const posted = await client.chat.postMessage(
- slackReplyArgs(channel, text, undefined, { unfurlLinks: d.destination.unfurlLinks }),
+ slackReplyArgs(channel, text, threadTs, { unfurlLinks: d.destination.unfurlLinks }),
);
- mirrorSelfPost(channel, posted?.ts, text, { kind: "dm" });
+ mirrorSelfPost(channel, posted?.ts, text, { kind: "dm", sub: threadTs });
}
if (d.attachments?.length) {
try {
await uploadAttachments(
client,
channel,
- undefined,
+ threadTs,
d.attachments,
fetchBlobFromCore,
fetchFileArtifactFromCore,
@@ -255,11 +256,11 @@ export function createDeliveryPoller(deps: {
(err as Error).message,
);
await client.chat
- .postMessage(slackReplyArgs(channel, uploadFailureNote(err), undefined))
+ .postMessage(slackReplyArgs(channel, uploadFailureNote(err), threadTs))
.catch(swallowAs("slack: post principal upload-failure note", undefined));
}
}
- return { recipientThreadRef: dmThreadRef(channel) };
+ return { recipientThreadRef: dmThreadRef(channel, threadTs) };
} finally {
slackApiMs = Math.round(performance.now() - tPost);
}
diff --git a/src/types.ts b/src/types.ts
index e75edd052..244641a33 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -166,6 +166,7 @@ export interface Destination {
target: string;
audienceScopeId?: ScopeId;
onBehalfOf?: string;
+ threadTs?: string;
editRef?: string;
taskList?: Array<{
id: string;
diff --git a/test/reach-thread.test.ts b/test/reach-thread.test.ts
index 7dc175058..b30849f55 100644
--- a/test/reach-thread.test.ts
+++ b/test/reach-thread.test.ts
@@ -62,11 +62,14 @@ describe("POST /v1/reach with threadTs", () => {
assert.equal(d!.destination.target, "C-eng");
});
- it("rejects threadTs on a DM to a person", async () => {
- const res = await post({ recipient: "Alice", text: "hi", threadTs: TS });
- assert.equal(res.status, 400);
- const body = (await res.json()) as { message: string };
- assert.match(body.message, /has no threads/);
+ it("threads a DM to a person without changing its principal target", async () => {
+ const res = await post({ recipient: "Alice", text: "in the DM thread", threadTs: TS });
+ assert.equal(res.status, 200);
+ const body = (await res.json()) as { deliveryId: string };
+ const d = (await built.app.pendingDeliveries("principal")).find((x) => x.id === body.deliveryId);
+ assert.ok(d, "delivery enqueued for the principal surface");
+ assert.equal(d!.destination.target, "U-alice");
+ assert.equal(d!.destination.threadTs, TS);
});
it("rejects a malformed threadTs", async () => {
diff --git a/test/slack-principal-thread-delivery.test.ts b/test/slack-principal-thread-delivery.test.ts
new file mode 100644
index 000000000..2256b6f40
--- /dev/null
+++ b/test/slack-principal-thread-delivery.test.ts
@@ -0,0 +1,55 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import { createDeliveryPoller } from "../src/slack/deliveries.ts";
+
+const TS = "1723497600.123456";
+
+test("a threaded principal delivery posts and records the DM thread", async () => {
+ const posts: Array<Record<string, unknown>> = [];
+ const acks: Array<{ id: string; body: unknown }> = [];
+ let claimed = false;
+ const delivery = {
+ id: "delivery-1",
+ destination: { type: "principal", target: "U-alice", threadTs: TS },
+ text: "in the DM thread",
+ idempotencyKey: "reach:1",
+ createdAt: Date.now(),
+ deliveredAt: null,
+ };
+ const client = {
+ conversations: { open: async () => ({ channel: { id: "D-alice" } }) },
+ chat: {
+ postMessage: async (args: Record<string, unknown>) => {
+ posts.push(args);
+ return { ts: "1723497601.000001" };
+ },
+ },
+ };
+ const poller = createDeliveryPoller({
+ core: {
+ claimDeliveries: async (type: string) => {
+ if (type !== "principal" || claimed) return [];
+ claimed = true;
+ return [delivery];
+ },
+ ackDelivery: async (id: string, body: unknown) => void acks.push({ id, body }),
+ } as any,
+ bridge: {
+ inFlightRuns: new Set(),
+ fetchBlobFromCore: async () => new Uint8Array(),
+ fetchFileArtifactFromCore: async () => new Uint8Array(),
+ } as any,
+ mirror: { mirrorSelfPost: () => undefined } as any,
+ threads: { mark: () => undefined } as any,
+ clientForIdentity: () => client,
+ });
+
+ await poller.pollDeliveries(client);
+
+ assert.equal(posts.length, 1);
+ assert.equal(posts[0]!.channel, "D-alice");
+ assert.equal(posts[0]!.thread_ts, TS);
+ assert.equal(acks.length, 1);
+ assert.equal(acks[0]!.id, "delivery-1");
+ assert.equal((acks[0]!.body as { recipientThreadRef?: string }).recipientThreadRef, `dm:D-alice:${TS}`);
+});