mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 22:04:30 +00:00
refactor(agent): dedupe harness and command workflows
This commit is contained in:
@@ -21,118 +21,131 @@ async function makeStorePath() {
|
||||
};
|
||||
}
|
||||
|
||||
describe("CronService delivery plan consistency", () => {
|
||||
it("does not post isolated summary when legacy deliver=false", async () => {
|
||||
const store = await makeStorePath();
|
||||
const enqueueSystemEvent = vi.fn();
|
||||
const cron = new CronService({
|
||||
cronEnabled: true,
|
||||
storePath: store.storePath,
|
||||
log: noopLogger,
|
||||
enqueueSystemEvent,
|
||||
requestHeartbeatNow: vi.fn(),
|
||||
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok", summary: "done" })),
|
||||
});
|
||||
await cron.start();
|
||||
const job = await cron.add({
|
||||
name: "legacy-off",
|
||||
schedule: { kind: "every", everyMs: 60_000, anchorMs: Date.now() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: {
|
||||
kind: "agentTurn",
|
||||
message: "hello",
|
||||
deliver: false,
|
||||
},
|
||||
});
|
||||
type DeliveryMode = "none" | "announce";
|
||||
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).not.toHaveBeenCalled();
|
||||
type DeliveryOverride = {
|
||||
mode: DeliveryMode;
|
||||
channel?: string;
|
||||
to?: string;
|
||||
};
|
||||
|
||||
async function withCronService(
|
||||
params: {
|
||||
runIsolatedAgentJob?: () => Promise<{ status: "ok"; summary: string; delivered?: boolean }>;
|
||||
},
|
||||
run: (context: {
|
||||
cron: CronService;
|
||||
enqueueSystemEvent: ReturnType<typeof vi.fn>;
|
||||
requestHeartbeatNow: ReturnType<typeof vi.fn>;
|
||||
}) => Promise<void>,
|
||||
) {
|
||||
const store = await makeStorePath();
|
||||
const enqueueSystemEvent = vi.fn();
|
||||
const requestHeartbeatNow = vi.fn();
|
||||
const cron = new CronService({
|
||||
cronEnabled: true,
|
||||
storePath: store.storePath,
|
||||
log: noopLogger,
|
||||
enqueueSystemEvent,
|
||||
requestHeartbeatNow,
|
||||
runIsolatedAgentJob:
|
||||
params.runIsolatedAgentJob ??
|
||||
(vi.fn(async () => ({ status: "ok", summary: "done" })) as never),
|
||||
});
|
||||
|
||||
await cron.start();
|
||||
try {
|
||||
await run({ cron, enqueueSystemEvent, requestHeartbeatNow });
|
||||
} finally {
|
||||
cron.stop();
|
||||
await store.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
async function addIsolatedAgentTurnJob(
|
||||
cron: CronService,
|
||||
params: {
|
||||
name: string;
|
||||
wakeMode: "next-heartbeat" | "now";
|
||||
payload?: { deliver?: boolean };
|
||||
delivery?: DeliveryOverride;
|
||||
},
|
||||
) {
|
||||
return cron.add({
|
||||
name: params.name,
|
||||
schedule: { kind: "every", everyMs: 60_000, anchorMs: Date.now() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: params.wakeMode,
|
||||
payload: {
|
||||
kind: "agentTurn",
|
||||
message: "hello",
|
||||
...params.payload,
|
||||
},
|
||||
...(params.delivery
|
||||
? {
|
||||
delivery: params.delivery as unknown as {
|
||||
mode: DeliveryMode;
|
||||
channel?: string;
|
||||
to?: string;
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
}
|
||||
|
||||
describe("CronService delivery plan consistency", () => {
|
||||
it("does not post isolated summary when legacy deliver=false", async () => {
|
||||
await withCronService({}, async ({ cron, enqueueSystemEvent }) => {
|
||||
const job = await addIsolatedAgentTurnJob(cron, {
|
||||
name: "legacy-off",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { deliver: false },
|
||||
});
|
||||
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("treats delivery object without mode as announce", async () => {
|
||||
const store = await makeStorePath();
|
||||
const enqueueSystemEvent = vi.fn();
|
||||
const cron = new CronService({
|
||||
cronEnabled: true,
|
||||
storePath: store.storePath,
|
||||
log: noopLogger,
|
||||
enqueueSystemEvent,
|
||||
requestHeartbeatNow: vi.fn(),
|
||||
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok", summary: "done" })),
|
||||
});
|
||||
await cron.start();
|
||||
const job = await cron.add({
|
||||
name: "partial-delivery",
|
||||
schedule: { kind: "every", everyMs: 60_000, anchorMs: Date.now() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: {
|
||||
kind: "agentTurn",
|
||||
message: "hello",
|
||||
},
|
||||
delivery: { channel: "telegram", to: "123" } as unknown as {
|
||||
mode: "none" | "announce";
|
||||
channel?: string;
|
||||
to?: string;
|
||||
},
|
||||
});
|
||||
await withCronService({}, async ({ cron, enqueueSystemEvent }) => {
|
||||
const job = await addIsolatedAgentTurnJob(cron, {
|
||||
name: "partial-delivery",
|
||||
wakeMode: "next-heartbeat",
|
||||
delivery: { channel: "telegram", to: "123" } as DeliveryOverride,
|
||||
});
|
||||
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).toHaveBeenCalledWith(
|
||||
"Cron: done",
|
||||
expect.objectContaining({ agentId: undefined }),
|
||||
);
|
||||
|
||||
cron.stop();
|
||||
await store.cleanup();
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).toHaveBeenCalledWith(
|
||||
"Cron: done",
|
||||
expect.objectContaining({ agentId: undefined }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("does not enqueue duplicate relay when isolated run marks delivery handled", async () => {
|
||||
const store = await makeStorePath();
|
||||
const enqueueSystemEvent = vi.fn();
|
||||
const requestHeartbeatNow = vi.fn();
|
||||
const runIsolatedAgentJob = vi.fn(async () => ({
|
||||
status: "ok" as const,
|
||||
summary: "done",
|
||||
delivered: true,
|
||||
}));
|
||||
const cron = new CronService({
|
||||
cronEnabled: true,
|
||||
storePath: store.storePath,
|
||||
log: noopLogger,
|
||||
enqueueSystemEvent,
|
||||
requestHeartbeatNow,
|
||||
runIsolatedAgentJob,
|
||||
});
|
||||
await cron.start();
|
||||
const job = await cron.add({
|
||||
name: "announce-delivered",
|
||||
schedule: { kind: "every", everyMs: 60_000, anchorMs: Date.now() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "now",
|
||||
payload: {
|
||||
kind: "agentTurn",
|
||||
message: "hello",
|
||||
await withCronService(
|
||||
{
|
||||
runIsolatedAgentJob: vi.fn(async () => ({
|
||||
status: "ok",
|
||||
summary: "done",
|
||||
delivered: true,
|
||||
})),
|
||||
},
|
||||
delivery: { channel: "telegram", to: "123" } as unknown as {
|
||||
mode: "none" | "announce";
|
||||
channel?: string;
|
||||
to?: string;
|
||||
async ({ cron, enqueueSystemEvent, requestHeartbeatNow }) => {
|
||||
const job = await addIsolatedAgentTurnJob(cron, {
|
||||
name: "announce-delivered",
|
||||
wakeMode: "now",
|
||||
delivery: { channel: "telegram", to: "123" } as DeliveryOverride,
|
||||
});
|
||||
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).not.toHaveBeenCalled();
|
||||
expect(requestHeartbeatNow).not.toHaveBeenCalled();
|
||||
},
|
||||
});
|
||||
|
||||
const result = await cron.run(job.id, "force");
|
||||
expect(result).toEqual({ ok: true, ran: true });
|
||||
expect(enqueueSystemEvent).not.toHaveBeenCalled();
|
||||
expect(requestHeartbeatNow).not.toHaveBeenCalled();
|
||||
|
||||
cron.stop();
|
||||
await store.cleanup();
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user