mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 20:11:23 +00:00
Agents: fix subagent completion thread routing
This commit is contained in:
@@ -212,7 +212,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
expect(send?.sessionKey).toBe("agent:main:main");
|
expect(send?.sessionKey).toBe("agent:main:main");
|
||||||
expect(send?.channel).toBe("whatsapp");
|
expect(send?.channel).toBe("whatsapp");
|
||||||
expect(send?.to).toBe("+123");
|
expect(send?.to).toBe("+123");
|
||||||
expect(send?.message).toBe("done");
|
expect(send?.message).toBe("✅ Subagent main finished\n\ndone");
|
||||||
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -297,7 +297,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
||||||
expect(send?.channel).toBe("discord");
|
expect(send?.channel).toBe("discord");
|
||||||
expect(send?.to).toBe("discord:dm:u123");
|
expect(send?.to).toBe("discord:dm:u123");
|
||||||
expect(send?.message).toContain("completed successfully");
|
expect(send?.message).toBe("✅ Subagent main finished");
|
||||||
|
|
||||||
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
});
|
});
|
||||||
@@ -364,7 +364,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
||||||
expect(send?.channel).toBe("discord");
|
expect(send?.channel).toBe("discord");
|
||||||
expect(send?.to).toBe("discord:dm:u123");
|
expect(send?.to).toBe("discord:dm:u123");
|
||||||
expect(send?.message).toBe("done");
|
expect(send?.message).toBe("✅ Subagent main finished\n\ndone");
|
||||||
|
|
||||||
// Session should be deleted
|
// Session should be deleted
|
||||||
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ describe("openclaw-tools: subagents (sessions_spawn model + thinking)", () => {
|
|||||||
});
|
});
|
||||||
expect(result.details).toMatchObject({
|
expect(result.details).toMatchObject({
|
||||||
status: "accepted",
|
status: "accepted",
|
||||||
note: "auto-announces on completion, do not poll",
|
note: "auto-announces on completion, do not poll/sleep. The response will be sent back as an agent message.",
|
||||||
modelApplied: true,
|
modelApplied: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { beforeEach, describe, expect, it } from "vitest";
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
|
||||||
import "./test-helpers/fast-core-tools.js";
|
import "./test-helpers/fast-core-tools.js";
|
||||||
import {
|
import {
|
||||||
callGatewayMock,
|
getCallGatewayMock,
|
||||||
setSubagentsConfigOverride,
|
getSessionsSpawnTool,
|
||||||
} from "./openclaw-tools.subagents.test-harness.js";
|
setSessionsSpawnConfigOverride,
|
||||||
|
} from "./openclaw-tools.subagents.sessions-spawn.test-harness.js";
|
||||||
import {
|
import {
|
||||||
listSubagentRunsForRequester,
|
listSubagentRunsForRequester,
|
||||||
resetSubagentRegistryForTests,
|
resetSubagentRegistryForTests,
|
||||||
@@ -12,9 +12,10 @@ import {
|
|||||||
|
|
||||||
describe("sessions_spawn requesterOrigin threading", () => {
|
describe("sessions_spawn requesterOrigin threading", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
const callGatewayMock = getCallGatewayMock();
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
setSubagentsConfigOverride({
|
setSessionsSpawnConfigOverride({
|
||||||
session: {
|
session: {
|
||||||
mainKey: "main",
|
mainKey: "main",
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
@@ -35,20 +36,18 @@ describe("sessions_spawn requesterOrigin threading", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("captures threadId in requesterOrigin", async () => {
|
it("captures threadId in requesterOrigin", async () => {
|
||||||
const tool = createOpenClawTools({
|
const tool = await getSessionsSpawnTool({
|
||||||
agentSessionKey: "main",
|
agentSessionKey: "main",
|
||||||
agentChannel: "telegram",
|
agentChannel: "telegram",
|
||||||
agentTo: "telegram:123",
|
agentTo: "telegram:123",
|
||||||
agentThreadId: 42,
|
agentThreadId: 42,
|
||||||
}).find((candidate) => candidate.name === "sessions_spawn");
|
});
|
||||||
if (!tool) {
|
|
||||||
throw new Error("missing sessions_spawn tool");
|
|
||||||
}
|
|
||||||
|
|
||||||
await tool.execute("call", {
|
const result = await tool.execute("call", {
|
||||||
task: "do thing",
|
task: "do thing",
|
||||||
runTimeoutSeconds: 1,
|
runTimeoutSeconds: 1,
|
||||||
});
|
});
|
||||||
|
expect(result.details).toMatchObject({ status: "accepted", runId: "run-1" });
|
||||||
|
|
||||||
const runs = listSubagentRunsForRequester("main");
|
const runs = listSubagentRunsForRequester("main");
|
||||||
expect(runs).toHaveLength(1);
|
expect(runs).toHaveLength(1);
|
||||||
@@ -60,19 +59,17 @@ describe("sessions_spawn requesterOrigin threading", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("stores requesterOrigin without threadId when none is provided", async () => {
|
it("stores requesterOrigin without threadId when none is provided", async () => {
|
||||||
const tool = createOpenClawTools({
|
const tool = await getSessionsSpawnTool({
|
||||||
agentSessionKey: "main",
|
agentSessionKey: "main",
|
||||||
agentChannel: "telegram",
|
agentChannel: "telegram",
|
||||||
agentTo: "telegram:123",
|
agentTo: "telegram:123",
|
||||||
}).find((candidate) => candidate.name === "sessions_spawn");
|
});
|
||||||
if (!tool) {
|
|
||||||
throw new Error("missing sessions_spawn tool");
|
|
||||||
}
|
|
||||||
|
|
||||||
await tool.execute("call", {
|
const result = await tool.execute("call", {
|
||||||
task: "do thing",
|
task: "do thing",
|
||||||
runTimeoutSeconds: 1,
|
runTimeoutSeconds: 1,
|
||||||
});
|
});
|
||||||
|
expect(result.details).toMatchObject({ status: "accepted", runId: "run-1" });
|
||||||
|
|
||||||
const runs = listSubagentRunsForRequester("main");
|
const runs = listSubagentRunsForRequester("main");
|
||||||
expect(runs).toHaveLength(1);
|
expect(runs).toHaveLength(1);
|
||||||
|
|||||||
@@ -372,11 +372,8 @@ describe("subagent announce formatting", () => {
|
|||||||
expect(call?.params?.channel).toBe("discord");
|
expect(call?.params?.channel).toBe("discord");
|
||||||
expect(call?.params?.to).toBe("channel:12345");
|
expect(call?.params?.to).toBe("channel:12345");
|
||||||
expect(call?.params?.sessionKey).toBe("agent:main:main");
|
expect(call?.params?.sessionKey).toBe("agent:main:main");
|
||||||
expect(msg).toContain("[System Message]");
|
expect(msg).toContain("✅ Subagent main finished");
|
||||||
expect(msg).toContain('subagent task "do thing"');
|
|
||||||
expect(msg).toContain("Result:");
|
|
||||||
expect(msg).toContain("final answer: 2");
|
expect(msg).toContain("final answer: 2");
|
||||||
expect(msg).toContain("Stats:");
|
|
||||||
expect(msg).not.toContain("Convert the result above into your normal assistant voice");
|
expect(msg).not.toContain("Convert the result above into your normal assistant voice");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -413,6 +410,45 @@ describe("subagent announce formatting", () => {
|
|||||||
const call = sendSpy.mock.calls[0]?.[0] as { params?: Record<string, unknown> };
|
const call = sendSpy.mock.calls[0]?.[0] as { params?: Record<string, unknown> };
|
||||||
expect(call?.params?.channel).toBe("discord");
|
expect(call?.params?.channel).toBe("discord");
|
||||||
expect(call?.params?.to).toBe("channel:12345");
|
expect(call?.params?.to).toBe("channel:12345");
|
||||||
|
expect(call?.params?.threadId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes requesterOrigin.threadId for manual completion direct-send", async () => {
|
||||||
|
const { runSubagentAnnounceFlow } = await import("./subagent-announce.js");
|
||||||
|
sessionStore = {
|
||||||
|
"agent:main:subagent:test": {
|
||||||
|
sessionId: "child-session-direct-thread-pass",
|
||||||
|
},
|
||||||
|
"agent:main:main": {
|
||||||
|
sessionId: "requester-session-thread-pass",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
chatHistoryMock.mockResolvedValueOnce({
|
||||||
|
messages: [{ role: "assistant", content: [{ type: "text", text: "done" }] }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const didAnnounce = await runSubagentAnnounceFlow({
|
||||||
|
childSessionKey: "agent:main:subagent:test",
|
||||||
|
childRunId: "run-direct-thread-pass",
|
||||||
|
requesterSessionKey: "agent:main:main",
|
||||||
|
requesterDisplayKey: "main",
|
||||||
|
requesterOrigin: {
|
||||||
|
channel: "discord",
|
||||||
|
to: "channel:12345",
|
||||||
|
accountId: "acct-1",
|
||||||
|
threadId: 99,
|
||||||
|
},
|
||||||
|
...defaultOutcomeAnnounce,
|
||||||
|
expectsCompletionMessage: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(didAnnounce).toBe(true);
|
||||||
|
expect(sendSpy).toHaveBeenCalledTimes(1);
|
||||||
|
expect(agentSpy).not.toHaveBeenCalled();
|
||||||
|
const call = sendSpy.mock.calls[0]?.[0] as { params?: Record<string, unknown> };
|
||||||
|
expect(call?.params?.channel).toBe("discord");
|
||||||
|
expect(call?.params?.to).toBe("channel:12345");
|
||||||
|
expect(call?.params?.threadId).toBe("99");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("steers announcements into an active run when queue mode is steer", async () => {
|
it("steers announcements into an active run when queue mode is steer", async () => {
|
||||||
|
|||||||
@@ -463,12 +463,17 @@ async function sendSubagentAnnounceDirectly(params: {
|
|||||||
hasCompletionDirectTarget &&
|
hasCompletionDirectTarget &&
|
||||||
params.completionMessage?.trim()
|
params.completionMessage?.trim()
|
||||||
) {
|
) {
|
||||||
|
const completionThreadId =
|
||||||
|
completionDirectOrigin?.threadId != null && completionDirectOrigin.threadId !== ""
|
||||||
|
? String(completionDirectOrigin.threadId)
|
||||||
|
: undefined;
|
||||||
await callGateway({
|
await callGateway({
|
||||||
method: "send",
|
method: "send",
|
||||||
params: {
|
params: {
|
||||||
channel: completionChannel,
|
channel: completionChannel,
|
||||||
to: completionTo,
|
to: completionTo,
|
||||||
accountId: completionDirectOrigin?.accountId,
|
accountId: completionDirectOrigin?.accountId,
|
||||||
|
threadId: completionThreadId,
|
||||||
sessionKey: canonicalRequesterSessionKey,
|
sessionKey: canonicalRequesterSessionKey,
|
||||||
message: params.completionMessage,
|
message: params.completionMessage,
|
||||||
idempotencyKey: params.directIdempotencyKey,
|
idempotencyKey: params.directIdempotencyKey,
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ export const SendParamsSchema = Type.Object(
|
|||||||
gifPlayback: Type.Optional(Type.Boolean()),
|
gifPlayback: Type.Optional(Type.Boolean()),
|
||||||
channel: Type.Optional(Type.String()),
|
channel: Type.Optional(Type.String()),
|
||||||
accountId: Type.Optional(Type.String()),
|
accountId: Type.Optional(Type.String()),
|
||||||
|
/** Thread id (channel-specific meaning, e.g. Telegram forum topic id). */
|
||||||
|
threadId: Type.Optional(Type.String()),
|
||||||
/** Optional session key for mirroring delivered output back into the transcript. */
|
/** Optional session key for mirroring delivered output back into the transcript. */
|
||||||
sessionKey: Type.Optional(Type.String()),
|
sessionKey: Type.Optional(Type.String()),
|
||||||
idempotencyKey: NonEmptyString,
|
idempotencyKey: NonEmptyString,
|
||||||
|
|||||||
@@ -235,4 +235,22 @@ describe("gateway send mirroring", () => {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("forwards threadId to outbound delivery when provided", async () => {
|
||||||
|
mocks.deliverOutboundPayloads.mockResolvedValue([{ messageId: "m-thread", channel: "slack" }]);
|
||||||
|
|
||||||
|
await runSend({
|
||||||
|
to: "channel:C1",
|
||||||
|
message: "hi",
|
||||||
|
channel: "slack",
|
||||||
|
threadId: "1710000000.9999",
|
||||||
|
idempotencyKey: "idem-thread",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
threadId: "1710000000.9999",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ export const sendHandlers: GatewayRequestHandlers = {
|
|||||||
gifPlayback?: boolean;
|
gifPlayback?: boolean;
|
||||||
channel?: string;
|
channel?: string;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
|
threadId?: string;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
idempotencyKey: string;
|
idempotencyKey: string;
|
||||||
};
|
};
|
||||||
@@ -130,6 +131,10 @@ export const sendHandlers: GatewayRequestHandlers = {
|
|||||||
typeof request.accountId === "string" && request.accountId.trim().length
|
typeof request.accountId === "string" && request.accountId.trim().length
|
||||||
? request.accountId.trim()
|
? request.accountId.trim()
|
||||||
: undefined;
|
: undefined;
|
||||||
|
const threadId =
|
||||||
|
typeof request.threadId === "string" && request.threadId.trim().length
|
||||||
|
? request.threadId.trim()
|
||||||
|
: undefined;
|
||||||
const outboundChannel = channel;
|
const outboundChannel = channel;
|
||||||
const plugin = getChannelPlugin(channel);
|
const plugin = getChannelPlugin(channel);
|
||||||
if (!plugin) {
|
if (!plugin) {
|
||||||
@@ -182,6 +187,7 @@ export const sendHandlers: GatewayRequestHandlers = {
|
|||||||
agentId: derivedAgentId,
|
agentId: derivedAgentId,
|
||||||
accountId,
|
accountId,
|
||||||
target: resolved.to,
|
target: resolved.to,
|
||||||
|
threadId,
|
||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
if (derivedRoute) {
|
if (derivedRoute) {
|
||||||
@@ -203,6 +209,7 @@ export const sendHandlers: GatewayRequestHandlers = {
|
|||||||
? resolveSessionAgentId({ sessionKey: providedSessionKey, config: cfg })
|
? resolveSessionAgentId({ sessionKey: providedSessionKey, config: cfg })
|
||||||
: derivedAgentId,
|
: derivedAgentId,
|
||||||
gifPlayback: request.gifPlayback,
|
gifPlayback: request.gifPlayback,
|
||||||
|
threadId: threadId ?? null,
|
||||||
deps: outboundDeps,
|
deps: outboundDeps,
|
||||||
mirror: providedSessionKey
|
mirror: providedSessionKey
|
||||||
? {
|
? {
|
||||||
|
|||||||
Reference in New Issue
Block a user