Skip to content

Commit e38a645

Browse files
authored
fix(cron): honor explicit delivery agent ownership (#124631)
* fix(cron): honor explicit delivery agent ownership * test(gateway): honor explicit ownership in send mock
1 parent 8d86fe7 commit e38a645

3 files changed

Lines changed: 34 additions & 15 deletions

File tree

‎src/gateway/server-methods/send.test.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,24 @@ vi.mock("../../channels/plugins/message-action-dispatch.js", () => ({
9494
const TEST_AGENT_WORKSPACE = "/tmp/openclaw-test-workspace";
9595
let sendHandlers: typeof import("./send.js").sendHandlers;
9696

97-
function resolveAgentIdFromSessionKeyForTests(params: { sessionKey?: string }): string {
97+
function resolveAgentIdFromSessionKeyForTests(params: {
98+
sessionKey?: string;
99+
agentId?: string;
100+
}): string {
101+
const explicitAgentId = params.agentId?.trim().toLowerCase();
98102
if (typeof params.sessionKey === "string") {
99103
const match = params.sessionKey.match(/^agent:([^:]+)/i);
100104
if (match?.[1]) {
101-
return match[1];
105+
const sessionAgentId = match[1].toLowerCase();
106+
if (explicitAgentId && explicitAgentId !== sessionAgentId) {
107+
throw new Error(
108+
`agent "${explicitAgentId}" does not match session key agent "${sessionAgentId}"`,
109+
);
110+
}
111+
return sessionAgentId;
102112
}
103113
}
104-
return "main";
114+
return explicitAgentId ?? "main";
105115
}
106116

107117
function messageActionContextFromSessionKeyForTests(sessionKey: string): {
@@ -139,11 +149,12 @@ vi.mock("../../agents/agent-scope.js", async (importOriginal) => ({
139149
...(await importOriginal<typeof import("../../agents/agent-scope.js")>()),
140150
resolveSessionAgentId: ({
141151
sessionKey,
152+
agentId,
142153
}: {
143154
sessionKey?: string;
144155
config?: unknown;
145156
agentId?: string;
146-
}) => resolveAgentIdFromSessionKeyForTests({ sessionKey }),
157+
}) => resolveAgentIdFromSessionKeyForTests({ sessionKey, agentId }),
147158
resolveAgentConfig: () => undefined,
148159
resolveDefaultAgentId: () => "main",
149160
resolveAgentWorkspaceDir: () => TEST_AGENT_WORKSPACE,

‎src/infra/outbound/session-context.test.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,31 @@ describe("buildOutboundSessionContext", () => {
6060
expect(resolveSessionAgentIdMock).toHaveBeenCalledWith({
6161
sessionKey: "session:main:123",
6262
config: { agents: {} },
63+
agentId: undefined,
6364
});
6465
});
6566

66-
it("prefers an explicit trimmed agent id over the derived one", () => {
67-
resolveSessionAgentIdMock.mockReturnValueOnce("derived-agent");
67+
it("passes explicit ownership when resolving an unscoped session key", () => {
68+
resolveSessionAgentIdMock.mockImplementationOnce(({ agentId }: { agentId?: string }) => {
69+
if (!agentId) {
70+
throw new Error("missing explicit agent ownership");
71+
}
72+
return agentId;
73+
});
6874

6975
expect(
7076
buildOutboundSessionContext({
71-
cfg: {} as never,
72-
sessionKey: "session:main:123",
77+
cfg: { agents: {} } as never,
78+
sessionKey: "cron:job-123:failure",
7379
agentId: " explicit-agent ",
7480
}),
7581
).toEqual({
76-
key: "session:main:123",
82+
key: "cron:job-123:failure",
83+
agentId: "explicit-agent",
84+
});
85+
expect(resolveSessionAgentIdMock).toHaveBeenCalledWith({
86+
sessionKey: "cron:job-123:failure",
87+
config: { agents: {} },
7788
agentId: "explicit-agent",
7889
});
7990
});

‎src/infra/outbound/session-context.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@ export function buildOutboundSessionContext(params: {
103103
const requesterSenderName = normalizeOptionalString(params.requesterSenderName);
104104
const requesterSenderUsername = normalizeOptionalString(params.requesterSenderUsername);
105105
const requesterSenderE164 = normalizeOptionalString(params.requesterSenderE164);
106-
const derivedAgentId = key
107-
? resolveSessionAgentId({ sessionKey: key, config: params.cfg })
108-
: undefined;
109-
// Prefer explicit caller ownership, but derive from the canonical session key
110-
// so redirected deliveries still get workspace-scoped media policy.
111-
const agentId = explicitAgentId ?? derivedAgentId;
106+
const agentId = key
107+
? resolveSessionAgentId({ sessionKey: key, config: params.cfg, agentId: explicitAgentId })
108+
: explicitAgentId;
112109
if (
113110
!key &&
114111
!policyKey &&

0 commit comments

Comments
 (0)