Skip to content

Commit cc9be84

Browse files
committed
refactor(runtime): split runtime builders and stabilize cron tool seam
1 parent e1e91bd commit cc9be84

4 files changed

Lines changed: 274 additions & 207 deletions

File tree

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22

3-
const { callGatewayMock } = vi.hoisted(() => ({
4-
callGatewayMock: vi.fn(),
5-
}));
6-
7-
vi.mock("../../gateway/call.js", () => ({
8-
callGateway: (opts: unknown) => callGatewayMock(opts),
3+
const { callGatewayToolMock } = vi.hoisted(() => ({
4+
callGatewayToolMock: vi.fn(),
95
}));
106

117
vi.mock("../agent-scope.js", () => ({
@@ -16,24 +12,28 @@ import { createCronTool } from "./cron-tool.js";
1612

1713
describe("cron tool flat-params", () => {
1814
beforeEach(() => {
19-
callGatewayMock.mockReset();
20-
callGatewayMock.mockResolvedValue({ ok: true });
15+
callGatewayToolMock.mockReset();
16+
callGatewayToolMock.mockResolvedValue({ ok: true });
2117
});
2218

2319
it("preserves explicit top-level sessionKey during flat-params recovery", async () => {
24-
const tool = createCronTool({ agentSessionKey: "agent:main:discord:channel:ops" });
20+
const tool = createCronTool(
21+
{ agentSessionKey: "agent:main:discord:channel:ops" },
22+
{ callGatewayTool: callGatewayToolMock },
23+
);
2524
await tool.execute("call-flat-session-key", {
2625
action: "add",
2726
sessionKey: "agent:main:telegram:group:-100123:topic:99",
2827
schedule: { kind: "at", at: new Date(123).toISOString() },
2928
message: "do stuff",
3029
});
3130

32-
const call = callGatewayMock.mock.calls[0]?.[0] as {
33-
method?: string;
34-
params?: { sessionKey?: string };
35-
};
36-
expect(call.method).toBe("cron.add");
37-
expect(call.params?.sessionKey).toBe("agent:main:telegram:group:-100123:topic:99");
31+
const [method, _gatewayOpts, params] = callGatewayToolMock.mock.calls[0] as [
32+
string,
33+
unknown,
34+
{ sessionKey?: string },
35+
];
36+
expect(method).toBe("cron.add");
37+
expect(params.sessionKey).toBe("agent:main:telegram:group:-100123:topic:99");
3838
});
3939
});

‎src/agents/tools/cron-tool.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Type } from "@sinclair/typebox";
2+
import type { CronDelivery, CronMessageChannel } from "../../cron/types.js";
23
import { loadConfig } from "../../config/config.js";
34
import { normalizeCronJobCreate, normalizeCronJobPatch } from "../../cron/normalize.js";
4-
import type { CronDelivery, CronMessageChannel } from "../../cron/types.js";
55
import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js";
66
import { parseAgentSessionKey } from "../../sessions/session-key-utils.js";
77
import { extractTextFromChatContent } from "../../shared/chat-content.js";
@@ -50,6 +50,12 @@ type CronToolOptions = {
5050
agentSessionKey?: string;
5151
};
5252

53+
type GatewayToolCaller = typeof callGatewayTool;
54+
55+
type CronToolDeps = {
56+
callGatewayTool?: GatewayToolCaller;
57+
};
58+
5359
type ChatMessage = {
5460
role?: unknown;
5561
content?: unknown;
@@ -84,6 +90,7 @@ async function buildReminderContextLines(params: {
8490
agentSessionKey?: string;
8591
gatewayOpts: GatewayCallOptions;
8692
contextMessages: number;
93+
callGatewayTool: GatewayToolCaller;
8794
}) {
8895
const maxMessages = Math.min(
8996
REMINDER_CONTEXT_MESSAGES_MAX,
@@ -100,7 +107,7 @@ async function buildReminderContextLines(params: {
100107
const { mainKey, alias } = resolveMainSessionAlias(cfg);
101108
const resolvedKey = resolveInternalSessionKey({ key: sessionKey, alias, mainKey });
102109
try {
103-
const res = await callGatewayTool<{ messages: Array<unknown> }>(
110+
const res = await params.callGatewayTool<{ messages: Array<unknown> }>(
104111
"chat.history",
105112
params.gatewayOpts,
106113
{
@@ -197,7 +204,8 @@ function inferDeliveryFromSessionKey(agentSessionKey?: string): CronDelivery | n
197204
return delivery;
198205
}
199206

200-
export function createCronTool(opts?: CronToolOptions): AnyAgentTool {
207+
export function createCronTool(opts?: CronToolOptions, deps?: CronToolDeps): AnyAgentTool {
208+
const callGateway = deps?.callGatewayTool ?? callGatewayTool;
201209
return {
202210
label: "Cron",
203211
name: "cron",
@@ -272,10 +280,10 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
272280

273281
switch (action) {
274282
case "status":
275-
return jsonResult(await callGatewayTool("cron.status", gatewayOpts, {}));
283+
return jsonResult(await callGateway("cron.status", gatewayOpts, {}));
276284
case "list":
277285
return jsonResult(
278-
await callGatewayTool("cron.list", gatewayOpts, {
286+
await callGateway("cron.list", gatewayOpts, {
279287
includeDisabled: Boolean(params.includeDisabled),
280288
}),
281289
);
@@ -412,14 +420,15 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
412420
agentSessionKey: opts?.agentSessionKey,
413421
gatewayOpts,
414422
contextMessages,
423+
callGatewayTool: callGateway,
415424
});
416425
if (contextLines.length > 0) {
417426
const baseText = stripExistingContext(payload.text);
418427
payload.text = `${baseText}${REMINDER_CONTEXT_MARKER}${contextLines.join("\n")}`;
419428
}
420429
}
421430
}
422-
return jsonResult(await callGatewayTool("cron.add", gatewayOpts, job));
431+
return jsonResult(await callGateway("cron.add", gatewayOpts, job));
423432
}
424433
case "update": {
425434
const id = readStringParam(params, "jobId") ?? readStringParam(params, "id");
@@ -431,7 +440,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
431440
}
432441
const patch = normalizeCronJobPatch(params.patch) ?? params.patch;
433442
return jsonResult(
434-
await callGatewayTool("cron.update", gatewayOpts, {
443+
await callGateway("cron.update", gatewayOpts, {
435444
id,
436445
patch,
437446
}),
@@ -442,7 +451,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
442451
if (!id) {
443452
throw new Error("jobId required (id accepted for backward compatibility)");
444453
}
445-
return jsonResult(await callGatewayTool("cron.remove", gatewayOpts, { id }));
454+
return jsonResult(await callGateway("cron.remove", gatewayOpts, { id }));
446455
}
447456
case "run": {
448457
const id = readStringParam(params, "jobId") ?? readStringParam(params, "id");
@@ -451,14 +460,14 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
451460
}
452461
const runMode =
453462
params.runMode === "due" || params.runMode === "force" ? params.runMode : "force";
454-
return jsonResult(await callGatewayTool("cron.run", gatewayOpts, { id, mode: runMode }));
463+
return jsonResult(await callGateway("cron.run", gatewayOpts, { id, mode: runMode }));
455464
}
456465
case "runs": {
457466
const id = readStringParam(params, "jobId") ?? readStringParam(params, "id");
458467
if (!id) {
459468
throw new Error("jobId required (id accepted for backward compatibility)");
460469
}
461-
return jsonResult(await callGatewayTool("cron.runs", gatewayOpts, { id }));
470+
return jsonResult(await callGateway("cron.runs", gatewayOpts, { id }));
462471
}
463472
case "wake": {
464473
const text = readStringParam(params, "text", { required: true });
@@ -467,7 +476,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
467476
? params.mode
468477
: "next-heartbeat";
469478
return jsonResult(
470-
await callGatewayTool("wake", gatewayOpts, { mode, text }, { expectFinal: false }),
479+
await callGateway("wake", gatewayOpts, { mode, text }, { expectFinal: false }),
471480
);
472481
}
473482
default:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from "vitest";
2+
import { FIELD_HELP } from "./schema.help.js";
3+
import { FIELD_LABELS } from "./schema.labels.js";
4+
import { OpenClawSchema } from "./zod-schema.js";
5+
6+
function hasLegacyPluginsRuntimeKeys(keys: string[]): boolean {
7+
return keys.some((key) => key === "plugins.runtime" || key.startsWith("plugins.runtime."));
8+
}
9+
10+
describe("plugins runtime boundary config", () => {
11+
it("omits legacy plugins.runtime keys from schema metadata", () => {
12+
expect(hasLegacyPluginsRuntimeKeys(Object.keys(FIELD_HELP))).toBe(false);
13+
expect(hasLegacyPluginsRuntimeKeys(Object.keys(FIELD_LABELS))).toBe(false);
14+
});
15+
16+
it("omits plugins.runtime from the generated config schema", () => {
17+
const schema = OpenClawSchema.toJSONSchema({
18+
target: "draft-7",
19+
io: "input",
20+
reused: "ref",
21+
}) as {
22+
properties?: Record<string, { properties?: Record<string, unknown> }>;
23+
};
24+
const pluginsProperties = schema.properties?.plugins?.properties ?? {};
25+
expect("runtime" in pluginsProperties).toBe(false);
26+
});
27+
28+
it("rejects legacy plugins.runtime config entries", () => {
29+
const result = OpenClawSchema.safeParse({
30+
plugins: {
31+
runtime: {
32+
allowLegacyExec: true,
33+
},
34+
},
35+
});
36+
expect(result.success).toBe(false);
37+
});
38+
});

0 commit comments

Comments
 (0)