Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const beginThread = mock(
const finishThread = mock(async (_args: unknown) => {});
const setQuiet = mock(async (_args: unknown) => {});
const followUpsEnabled = mock(async (_teamId: string) => true);
const requestStop = mock(async (_key: unknown, _ts: string) => true);
const stopRequested = mock(async (_id: string, _ts: string) => false);
const takeQueued = mock(
async (
_id: string,
Expand All @@ -107,6 +109,8 @@ mock.module("../utils/thread-sessions", () => ({
finishThreadRun: finishThread,
setThreadQuiet: setQuiet,
threadFollowUpsEnabled: followUpsEnabled,
requestThreadStop: requestStop,
threadStopRequested: stopRequested,
takeQueuedEvents: takeQueued,
completeHandBack: completeHandoff,
abandonHandBack: abandonHandoff,
Expand All @@ -117,6 +121,7 @@ mock.module("../utils/thread-sessions", () => ({
.toLowerCase();
if (t.startsWith("!mute")) return "mute";
if (t.startsWith("!unmute")) return "unmute";
if (t.startsWith("!stop")) return "stop";
return null;
},
renderThreadMemory: (entities: { label: string }[]) =>
Expand Down Expand Up @@ -185,6 +190,8 @@ beforeEach(() => {
beginThread.mockClear();
finishThread.mockClear();
setQuiet.mockClear();
requestStop.mockClear();
stopRequested.mockClear();
followUpsEnabled.mockReset();
followUpsEnabled.mockImplementation(async () => true);
release.mockClear();
Expand Down Expand Up @@ -222,6 +229,62 @@ test("with the flag off, !mute is an ordinary message", async () => {
expect(runAgent).toHaveBeenCalledTimes(1);
});

test("!stop asks the running turn to stop and confirms", async () => {
await processAgentMessage({
...params,
event: { ...params.event, text: "<@UBOT> !stop" },
});
expect(requestStop).toHaveBeenCalledWith(
expect.objectContaining({ threadTs: "1.0" }),
"10.0",
);
expect(runAgent).not.toHaveBeenCalled();
expect(postMessage.mock.calls[0]?.[0].text).toBe("Stopping.");
requestStop.mockImplementationOnce(async () => false);
await processAgentMessage({
...params,
event: { ...params.event, text: "<@UBOT> !stop" },
});
expect(postMessage.mock.calls.at(-1)?.[0].text).toBe(
"Nothing is running in this thread.",
);
});

test("!stop works in a DM; !mute there stays ordinary text", async () => {
await processAgentMessage({
...params,
event: {
...params.event,
type: "message",
channel_type: "im",
text: "!stop",
},
});
expect(requestStop).toHaveBeenCalledTimes(1);
expect(runAgent).not.toHaveBeenCalled();
await processAgentMessage({
...params,
event: {
...params.event,
type: "message",
channel_type: "im",
text: "!mute",
},
});
expect(setQuiet).not.toHaveBeenCalled();
expect(runAgent).toHaveBeenCalledTimes(1);
});

test("the agent is given a way to check for a stop request", async () => {
stopRequested.mockImplementationOnce(async () => true);
await processAgentMessage(params);
const args = runAgent.mock.calls[0]?.[0] as {
shouldStop: () => Promise<boolean>;
};
expect(await args.shouldStop()).toBe(true);
expect(stopRequested).toHaveBeenCalledWith("thread-session", "10.0");
});

test("!unmute reopens the thread without running the agent", async () => {
await processAgentMessage({
...params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import {
finishThreadRun,
parseThreadCommand,
renderThreadMemory,
requestThreadStop,
setThreadQuiet,
takeQueuedEvents,
threadFollowUpsEnabled,
threadStopRequested,
} from "../utils/thread-sessions";

import { splitMarkdown } from "./utils/split-markdown";
Expand All @@ -58,6 +60,8 @@ const LOST_TRACK_TEXT =
const QUIETED_TEXT =
"Got it. I'll stay out of this thread unless someone mentions me.";
const UNQUIETED_TEXT = "Got it. I'll answer replies in this thread again.";
const STOPPING_TEXT = "Stopping.";
const NOTHING_RUNNING_TEXT = "Nothing is running in this thread.";
const JOB_URLS = {
mention: `${env.NEXT_PUBLIC_API_URL}/api/integrations/slack/jobs/process-mention`,
assistant: `${env.NEXT_PUBLIC_API_URL}/api/integrations/slack/jobs/process-assistant-message`,
Expand Down Expand Up @@ -252,9 +256,10 @@ export async function processAgentMessage({
// closed since, and the unflagged path never runs the agent for one.
const isFollowUp = event.type === "message" && !isDm;
if (isFollowUp && !sessions) return;
// Every DM already reaches the agent, so quieting means nothing there.
const command =
sessions && !isDm ? parseThreadCommand(event.text ?? "") : null;
// Every DM already reaches the agent, so quieting means nothing there;
// stopping a turn does.
const parsed = sessions ? parseThreadCommand(event.text ?? "") : null;
const command = isDm && parsed !== "stop" ? null : parsed;
// assistant.threads.setStatus only works in assistant (DM) threads; Slack
// answers method_not_supported_for_channel_type anywhere else. Channels get
// a placeholder message that carries progress and is removed once the final
Expand Down Expand Up @@ -344,11 +349,19 @@ export async function processAgentMessage({
if (command) {
let applied = false;
try {
await setThreadQuiet({ ...threadKey, quiet: command === "mute" });
let text: string;
if (command === "stop") {
text = (await requestThreadStop(threadKey, event.ts))
? STOPPING_TEXT
: NOTHING_RUNNING_TEXT;
} else {
await setThreadQuiet({ ...threadKey, quiet: command === "mute" });
text = command === "mute" ? QUIETED_TEXT : UNQUIETED_TEXT;
}
await reply.chat.postMessage({
channel: event.channel,
thread_ts: threadTs,
text: command === "mute" ? QUIETED_TEXT : UNQUIETED_TEXT,
text,
});
applied = true;
} finally {
Expand Down Expand Up @@ -431,6 +444,7 @@ export async function processAgentMessage({
? {
threadMemory: renderThreadMemory(threadSession.entityLog),
lastContextTs: threadSession.lastContextTs ?? undefined,
shouldStop: () => threadStopRequested(threadSession.id, event.ts),
...(isDm
? {}
: {
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/app/api/integrations/slack/events/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ mock.module("./utils/thread-sessions", () => ({
setThreadQuiet: async () => {},
threadFollowUpsEnabled: async () => true,
parseThreadCommand: () => null,
requestThreadStop: async () => false,
threadStopRequested: async () => false,
takeQueuedEvents: async () => [],
completeHandBack: async () => {},
abandonHandBack: async () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,69 @@ describe("agent loop", () => {
expect(rateLimitWaitMs(headers(null), now)).toBe(2_000);
});

test("a stop request ends the turn with the stopped copy and keeps completed actions", async () => {
create.mockImplementation(async () => toolResponse());
callTool.mockImplementation(async ({ name }) =>
name === "tasks_create"
? {
structuredContent: {
task: { id: "task", title: "Task", slug: "SUP-1" },
},
}
: {},
);
// First check (before the first request) passes; the stop lands before
// the second request, after one tool call has completed.
let checks = 0;
const result = await runSlackAgent({
...params,
shouldStop: async () => ++checks > 2,
});
expect(result.text).toContain("Stopped");
expect(result.actions).toHaveLength(1);
expect(
create.mock.calls.filter(([req]) => "tools" in (req as object)),
).toHaveLength(1);
});

test("a stop that lands mid-batch prevents the next tool call", async () => {
create.mockImplementationOnce(async () => ({
stop_reason: "tool_use",
content: [
{
type: "tool_use",
id: "a",
name: "superset_tasks_create",
input: { title: "one" },
},
{
type: "tool_use",
id: "b",
name: "superset_tasks_create",
input: { title: "two" },
},
],
}));
// The context prefetch also calls tools; only the task call flips the flag.
let stop = false;
callTool.mockImplementation(async ({ name }) => {
if (name !== "tasks_create") return {};
stop = true;
return {
structuredContent: { task: { id: "t", title: "one", slug: "S-1" } },
};
});
const result = await runSlackAgent({
...params,
shouldStop: async () => stop,
});
expect(result.text).toContain("Stopped");
expect(
callTool.mock.calls.filter(([a]) => a.name === "tasks_create"),
).toHaveLength(1);
expect(result.actions).toHaveLength(1);
});

test("text split around citations comes back as one paragraph", async () => {
create.mockImplementationOnce(async () => ({
stop_reason: "end_turn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ interface RunSlackAgentParams {
/** The newest thread message the agent had read before this turn. */
lastContextTs?: string;
onProgress?: (status: string) => void | Promise<void>;
/** Checked between steps; true ends the turn with the stopped copy. */
shouldStop?: () => Promise<boolean>;
}

/** Everything after the handler's claim shares one budget (job maxDuration is 300s). */
Expand All @@ -183,6 +185,7 @@ const AGENT_COPY = {
truncated:
"My reply was cut off before it finished. Ask me to continue, or narrow the request. Anything I completed is listed below.",
refusal: "I can't help with that request.",
stopped: "Stopped. Anything I completed before that is listed below.",
empty: "I finished without an answer to show. Ask again with more detail.",
} as const;

Expand Down Expand Up @@ -661,7 +664,13 @@ ${agentContext}`;
},
];

const request = () => {
const stopIfRequested = async () => {
if (await params.shouldStop?.()) {
throw new SlackAgentError(AGENT_COPY.stopped);
}
};
const request = async () => {
await stopIfRequested();
const remaining = deadline - Date.now();
if (remaining <= 0) throw new SlackAgentError(AGENT_COPY.timeLimit);
return anthropic.messages.create(
Expand Down Expand Up @@ -750,6 +759,9 @@ ${agentContext}`;
const toolResults: Anthropic.ToolResultBlockParam[] = [];

for (const toolUse of toolUseBlocks) {
// Per tool, not per batch: a stop that lands during one tool call
// must not let the next one create a task or launch an agent.
await stopIfRequested();
if (Date.now() >= deadline)
throw new SlackAgentError(AGENT_COPY.timeLimit);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export {
finishThreadRun,
parseThreadCommand,
renderThreadMemory,
requestThreadStop,
resetThreadFollowUpFlagCache,
setThreadQuiet,
type ThreadCommand,
type ThreadRunClaim,
takeQueuedEvents,
threadFollowUpsEnabled,
threadFollowUpTarget,
threadStopRequested,
} from "./thread-sessions";
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ describe("parseThreadCommand", () => {
expect(parseThreadCommand("<@UBOT> !unmute")).toBe("unmute");
expect(parseThreadCommand("<@UBOT|superset> !unmute")).toBe("unmute");
expect(parseThreadCommand(" !Quiet please")).toBe("mute");
expect(parseThreadCommand("<@UBOT> !stop")).toBe("stop");
expect(parseThreadCommand("!cancel that")).toBe("stop");
});
test("leaves prose to the model", () => {
expect(parseThreadCommand("only respond when I mention you")).toBeNull();
expect(
parseThreadCommand("build a bot that should only respond when mentioned"),
).toBeNull();
expect(parseThreadCommand("mute the alerts channel")).toBeNull();
expect(parseThreadCommand("stop the deploy automation")).toBeNull();
});
});

Expand Down
Loading
Loading