Cron: route reminders by session namespace

This commit is contained in:
Vignesh Natarajan
2026-02-16 14:29:21 -08:00
committed by Peter Steinberger
parent f452a7a60b
commit f988abf202
19 changed files with 530 additions and 32 deletions

View File

@@ -144,6 +144,46 @@ describe("cron tool", () => {
expect(call?.params?.agentId).toBeNull();
});
it("stamps cron.add with caller sessionKey when missing", async () => {
callGatewayMock.mockResolvedValueOnce({ ok: true });
const callerSessionKey = "agent:main:discord:channel:ops";
const tool = createCronTool({ agentSessionKey: callerSessionKey });
await tool.execute("call-session-key", {
action: "add",
job: {
name: "wake-up",
schedule: { at: new Date(123).toISOString() },
payload: { kind: "systemEvent", text: "hello" },
},
});
const call = callGatewayMock.mock.calls[0]?.[0] as {
params?: { sessionKey?: string };
};
expect(call?.params?.sessionKey).toBe(callerSessionKey);
});
it("preserves explicit job.sessionKey on add", async () => {
callGatewayMock.mockResolvedValueOnce({ ok: true });
const tool = createCronTool({ agentSessionKey: "agent:main:discord:channel:ops" });
await tool.execute("call-explicit-session-key", {
action: "add",
job: {
name: "wake-up",
schedule: { at: new Date(123).toISOString() },
sessionKey: "agent:main:telegram:group:-100123:topic:99",
payload: { kind: "systemEvent", text: "hello" },
},
});
const call = callGatewayMock.mock.calls[0]?.[0] as {
params?: { sessionKey?: string };
};
expect(call?.params?.sessionKey).toBe("agent:main:telegram:group:-100123:topic:99");
});
it("adds recent context for systemEvent reminders when contextMessages > 0", async () => {
callGatewayMock
.mockResolvedValueOnce({

View File

@@ -332,13 +332,22 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
throw new Error("job required");
}
const job = normalizeCronJobCreate(params.job) ?? params.job;
if (job && typeof job === "object" && !("agentId" in job)) {
if (job && typeof job === "object") {
const cfg = loadConfig();
const agentId = opts?.agentSessionKey
? resolveSessionAgentId({ sessionKey: opts.agentSessionKey, config: cfg })
const { mainKey, alias } = resolveMainSessionAlias(cfg);
const resolvedSessionKey = opts?.agentSessionKey
? resolveInternalSessionKey({ key: opts.agentSessionKey, alias, mainKey })
: undefined;
if (agentId) {
(job as { agentId?: string }).agentId = agentId;
if (!("agentId" in job)) {
const agentId = opts?.agentSessionKey
? resolveSessionAgentId({ sessionKey: opts.agentSessionKey, config: cfg })
: undefined;
if (agentId) {
(job as { agentId?: string }).agentId = agentId;
}
}
if (!("sessionKey" in job) && resolvedSessionKey) {
(job as { sessionKey?: string }).sessionKey = resolvedSessionKey;
}
}