Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Add cron title and settings link to Slack deliveries (#950)

Regan Bell229e1d067c22
diff --git a/docs/screenshots/cron-settings-footer.png b/docs/screenshots/cron-settings-footer.png
new file mode 100644
index 000000000..a28b67779
Binary files /dev/null and b/docs/screenshots/cron-settings-footer.png differ
diff --git a/src/cron/scheduler.ts b/src/cron/scheduler.ts
index d74c3b243..99c377c15 100644
--- a/src/cron/scheduler.ts
+++ b/src/cron/scheduler.ts
@@ -135,6 +135,7 @@ export function createScheduler(deps: SchedulerDeps): Scheduler {
fireKey,
threadRef,
surface: "cron",
+ ...(cron.title ? { title: cron.title } : {}),
...(cron.message !== undefined ? { message: cron.message } : {}),
...(cron.destination ? { destination: cron.destination } : {}),
...(cron.runAs ? { runAs: cron.runAs } : {}),
diff --git a/src/slack/deliveries.ts b/src/slack/deliveries.ts
index 387557532..9cae27cc5 100644
--- a/src/slack/deliveries.ts
+++ b/src/slack/deliveries.ts
@@ -23,6 +23,7 @@ import type { Delivery } from "../types.ts";
import type { CoreBridge } from "./core-bridge.ts";
import type { Mirror } from "./mirror.ts";
import { cleanAgentReplyForSlack, stripSlackDirectives } from "./messaging.ts";
+import { cronIdOf } from "../sessions/session-store.ts";
const DELIVERY_CLAIM_MS = 15_000;
@@ -37,6 +38,7 @@ function mergeSlackApiMs(body: unknown, slackApiMs: number | undefined): unknown
export function createDeliveryPoller(deps: {
core: SlackCoreClient;
+ webUiPublicUrl?: string;
bridge: CoreBridge;
mirror: Mirror;
threads: ReturnType<typeof createThreadTracker>;
@@ -57,6 +59,17 @@ 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>> {
+ const base = deps.webUiPublicUrl?.trim().replace(/\/+$/, "");
+ const id = d.provenance?.trigger === "cron" ? cronIdOf(d.provenance.sourceThreadRef) : null;
+ if (!base || !id) return [];
+ const title = (d.provenance?.sourceTitle?.trim() || "Cron")
+ .replaceAll("&", "&amp;")
+ .replaceAll("<", "&lt;")
+ .replaceAll(">", "&gt;");
+ return [{ type: "mrkdwn", text: `${title} · <${base}/crons/${encodeURIComponent(id)}|Settings>`, verbatim: true }];
+ }
+
const deliveryTracker = createDeliveryTracker();
const logDeliveryError =
@@ -121,17 +134,21 @@ export function createDeliveryPoller(deps: {
.catch(swallowAs("slack: post upload-failure note", undefined));
}
};
+ const settingsFooter = cronFooter(d);
+ const footer = [
+ ...settingsFooter,
+ ...(d.destination.debugFooter ? [{ type: "mrkdwn", text: d.destination.debugFooter }] : []),
+ ];
const taskList = d.destination.taskList?.length ? renderTaskList(d.destination.taskList) : undefined;
- const taskListBlocks = taskList
- ? [
- ...slackSectionBlocks(text),
- { type: "section", text: { type: "mrkdwn", text: taskList } },
- ...(d.destination.debugFooter
- ? [{ type: "context", elements: [{ type: "mrkdwn", text: d.destination.debugFooter }] }]
- : []),
- ]
- : undefined;
- if (!text.trim()) {
+ const footerBlocks =
+ taskList || footer.length
+ ? [
+ ...(text.trim() ? slackSectionBlocks(text) : []),
+ ...(taskList ? [{ type: "section", text: { type: "mrkdwn", text: taskList } }] : []),
+ ...(footer.length ? [{ type: "context", elements: footer }] : []),
+ ]
+ : undefined;
+ if (!text.trim() && !(settingsFooter.length && d.attachments?.length)) {
if (taskList) {
let preserved = false;
if (d.destination.editRef) {
@@ -172,7 +189,7 @@ export function createDeliveryPoller(deps: {
channel,
ts: d.destination.editRef,
text,
- ...(taskListBlocks ? { blocks: taskListBlocks } : {}),
+ ...(footerBlocks ? { blocks: footerBlocks } : {}),
...botIdentityArgs(),
...(unfurlLinks !== undefined ? { unfurl_links: unfurlLinks, unfurl_media: unfurlLinks } : {}),
});
@@ -184,14 +201,6 @@ export function createDeliveryPoller(deps: {
swallow("slack: finalize recovered reply in place", e);
}
}
- const footerBlocks =
- taskListBlocks ??
- (d.destination.debugFooter && text.length <= 2900
- ? [
- { type: "section", text: { type: "mrkdwn", text } },
- { type: "context", elements: [{ type: "mrkdwn", text: d.destination.debugFooter }] },
- ]
- : undefined);
let composedUploadError: unknown;
const canComposeUpload =
d.attachments?.length &&
@@ -269,9 +278,13 @@ 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 blocks = footer.length
+ ? [...(text.trim() ? slackSectionBlocks(text) : []), { type: "context", elements: footer }]
+ : undefined;
let composedUpload = false;
let uploadError: unknown;
- if (text.trim() && d.attachments?.length && d.destination.unfurlLinks === undefined) {
+ if (text.trim() && d.attachments?.length && !blocks && d.destination.unfurlLinks === undefined) {
try {
const uploaded = await uploadAttachments(
client,
@@ -292,10 +305,11 @@ export function createDeliveryPoller(deps: {
}
}
if (!composedUpload) {
- if (text.trim()) {
- const posted = await client.chat.postMessage(
- slackReplyArgs(channel, text, threadTs, { unfurlLinks: d.destination.unfurlLinks }),
- );
+ if (text.trim() || blocks) {
+ const posted = await client.chat.postMessage({
+ ...slackReplyArgs(channel, text, threadTs, { unfurlLinks: d.destination.unfurlLinks }),
+ ...(blocks ? { blocks } : {}),
+ });
mirrorSelfPost(channel, posted?.ts, text, { kind: "dm", sub: threadTs });
}
if (d.attachments?.length && !uploadError) {
diff --git a/src/slack/index.ts b/src/slack/index.ts
index d86eebb19..f34e51fbd 100644
--- a/src/slack/index.ts
+++ b/src/slack/index.ts
@@ -224,7 +224,14 @@ export async function startSlackPlugin(
...(cfg.userToken ? { userToken: cfg.userToken } : {}),
clientOptions: CLIENT_OPTIONS,
});
- const deliveries = createDeliveryPoller({ core, bridge, mirror, threads, clientForIdentity });
+ const deliveries = createDeliveryPoller({
+ core,
+ bridge,
+ mirror,
+ threads,
+ clientForIdentity,
+ webUiPublicUrl: cfg.webUiPublicUrl,
+ });
let auth: any;
try {
diff --git a/src/triggers/run-trigger.ts b/src/triggers/run-trigger.ts
index fe5ee59e6..00b87f2ff 100644
--- a/src/triggers/run-trigger.ts
+++ b/src/triggers/run-trigger.ts
@@ -37,6 +37,7 @@ export interface TriggerDeps {
}
export interface TriggerSpec {
+ title?: string;
owner: string;
ownerScopeId: ScopeId;
input: string;
@@ -94,6 +95,7 @@ function deliveryProvenance(spec: TriggerSpec, threadRef: string, res?: TurnResu
fireKey: spec.fireKey,
sourceScopeId: spec.ownerScopeId,
sourceThreadRef: threadRef,
+ ...(spec.title ? { sourceTitle: spec.title } : {}),
...(res?.sessionId ? { sourceSessionId: res.sessionId } : {}),
...(res?.sourceUserSeq !== undefined ? { sourceUserSeq: res.sourceUserSeq } : {}),
...(res?.sourceAssistantEntrySeq !== undefined ? { sourceAssistantEntrySeq: res.sourceAssistantEntrySeq } : {}),
diff --git a/src/types.ts b/src/types.ts
index 5fe0d0329..fc8e3726c 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -194,6 +194,7 @@ export interface CandidateDestination extends Destination {
export type BackgroundWakeTrigger = "cron" | "webhook" | "monitor" | (string & {});
export interface DeliveryProvenance {
+ sourceTitle?: string;
trigger: BackgroundWakeTrigger;
surface: string;
fireKey: string;
diff --git a/test/cron-scheduler.test.ts b/test/cron-scheduler.test.ts
index 511ef5dc8..206348993 100644
--- a/test/cron-scheduler.test.ts
+++ b/test/cron-scheduler.test.ts
@@ -71,6 +71,7 @@ test("a channel cron runs in the channel scope and delivers its real output to t
const cron = await crons.create({
schedule: { everyMs: 1000 },
action: "post the standup",
+ title: "Daily standup",
owner: "U1",
createdBy: "U1",
ownerScopeId: scopeId("channel", "C1"),
@@ -82,6 +83,7 @@ test("a channel cron runs in the channel scope and delivers its real output to t
const pending = await deliveries.pending("slack");
assert.equal(pending.length, 1);
assert.equal(pending[0]?.text, "CRON-OUTPUT-XYZ");
+ assert.equal(pending[0]?.provenance?.sourceTitle, "Daily standup");
});
test("a group-DM cron runs in the group scope and delivers its real output to the group", async () => {
diff --git a/test/slack-deliveries.test.ts b/test/slack-deliveries.test.ts
index 287330d96..3c2909a12 100644
--- a/test/slack-deliveries.test.ts
+++ b/test/slack-deliveries.test.ts
@@ -8,15 +8,23 @@ const mixedFiles = [
{ name: "notes.pdf", mimetype: "application/pdf", sizeBytes: 2, blobId: "B3" },
];
-async function deliver(destination: Record<string, unknown> = {}) {
+async function deliver(
+ destination: Record<string, unknown> = {},
+ sourceThreadRef?: string,
+ webUiPublicUrl?: string,
+ text = "two screenshots and the notes",
+) {
const delivery = {
id: "D1",
- text: "two screenshots and the notes",
+ text,
+ ...(sourceThreadRef
+ ? { provenance: { trigger: "cron", sourceThreadRef, sourceTitle: "Weekly <project> & check-in" } }
+ : {}),
destination: { type: "slack", target: "C1:100.200", ...destination },
attachments: mixedFiles,
createdAt: 1,
};
- const queues = new Map<string, unknown[]>([["slack", [delivery]]]);
+ const queues = new Map<string, unknown[]>([[String(delivery.destination.type), [delivery]]]);
const acknowledgements: string[] = [];
const uploads: Record<string, unknown>[] = [];
const posts: Record<string, unknown>[] = [];
@@ -27,6 +35,7 @@ async function deliver(destination: Record<string, unknown> = {}) {
ackDelivery: async (id: string) => void acknowledgements.push(id),
};
const client = {
+ conversations: { open: async () => ({ channel: { id: "C1" } }) },
files: {
uploadV2: async (args: Record<string, unknown>) => {
uploads.push(args);
@@ -43,6 +52,7 @@ async function deliver(destination: Record<string, unknown> = {}) {
};
const poller = createDeliveryPoller({
core: core as never,
+ webUiPublicUrl,
bridge: {
inFlightRuns: new Set<string>(),
fetchBlobFromCore: async (id: string) => Buffer.from(id),
@@ -86,3 +96,67 @@ test("Slack delivery keeps the separate-comment fallback when upload comments ca
assert.equal(uploads[0]!.initial_comment, undefined);
assert.equal((uploads[0]!.file_uploads as unknown[]).length, 3);
});
+
+for (const type of ["slack", "group", "principal"]) {
+ test(`${type} cron deliveries include settings below the complete message and preserve attachments`, async () => {
+ const text = "Scheduled update. ".repeat(250);
+ const { posts, uploads, acknowledgements } = await deliver(
+ { type },
+ "cron:morning report:fire:123",
+ "https://agent.example/web-ui/",
+ text,
+ );
+ assert.equal(posts.length, 1);
+ const blocks = posts[0]!.blocks as Array<{ type: string; text?: { text: string }; elements?: unknown[] }>;
+ assert.equal(
+ blocks
+ .filter((b) => b.type === "section")
+ .map((b) => b.text!.text)
+ .join(""),
+ text,
+ );
+ assert.deepEqual(blocks.at(-1), {
+ type: "context",
+ elements: [
+ {
+ type: "mrkdwn",
+ text: "Weekly &lt;project&gt; &amp; check-in · <https://agent.example/web-ui/crons/morning%20report|Settings>",
+ verbatim: true,
+ },
+ ],
+ });
+ assert.equal(uploads.length, 1);
+ assert.equal(uploads[0]!.initial_comment, undefined);
+ assert.deepEqual(acknowledgements, ["D1"]);
+ });
+}
+
+test("cron deliveries omit settings when the web UI is unavailable", async () => {
+ const { posts, uploads } = await deliver({}, "cron:c1:fire:123");
+ assert.equal(posts.length, 0);
+ assert.equal(uploads[0]!.initial_comment, "two screenshots and the notes");
+});
+
+test("ordinary deliveries omit cron settings even with a configured web UI", async () => {
+ const { posts } = await deliver({ unfurlLinks: false }, "dm:D1", "https://agent.example/web-ui");
+ assert.equal(posts[0]!.blocks, undefined);
+});
+
+for (const type of ["slack", "group", "principal"]) {
+ test(`${type} attachment-only cron deliveries use a valid footer without an empty message section`, async () => {
+ const { posts, uploads, acknowledgements } = await deliver(
+ { type },
+ "cron:c1:fire:123",
+ "https://agent.example",
+ "",
+ );
+ assert.equal(posts.length, 1);
+ assert.deepEqual(
+ (posts[0]!.blocks as Array<{ type: string }>).map((block) => block.type),
+ ["context"],
+ );
+ assert.equal(uploads.length, 1);
+ assert.equal((uploads[0]!.file_uploads as unknown[]).length, 3);
+ assert.deepEqual(acknowledgements, ["D1"]);
+ });
+}