Skip to content

Commit 455807c

Browse files
committed
fix(ui): use active scheduler trigger capability
Control UI automation trigger authoring now reflects the running scheduler across unsaved and saved-but-unapplied config edits and reconnects.
1 parent 91b8a03 commit 455807c

16 files changed

Lines changed: 267 additions & 33 deletions

‎scripts/control-ui-mock-cron.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export function buildCronMocks(baseTime: number) {
182182
}));
183183
const status: CronStatus = {
184184
enabled: true,
185+
triggersEnabled: true,
185186
jobs: jobs.length,
186187
nextWakeAtMs: overdueJob.state?.nextRunAtMs,
187188
};

‎src/cron/service.stream-validation.test.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ function streamJob(overrides: Partial<CronJobCreate> = {}): CronJobCreate {
2121
};
2222
}
2323

24-
async function createCron(triggersEnabled: boolean) {
24+
async function createCron(triggersEnabled: boolean | undefined, cronEnabled = true) {
2525
const { storePath } = await makeStorePath();
2626
const cron = new CronService({
2727
storePath,
28-
cronEnabled: true,
29-
cronConfig: { triggers: { enabled: triggersEnabled } },
28+
cronEnabled,
29+
...(triggersEnabled === undefined
30+
? {}
31+
: { cronConfig: { triggers: { enabled: triggersEnabled } } }),
3032
log: logger,
3133
enqueueSystemEvent: vi.fn(),
3234
requestHeartbeat: vi.fn(),
@@ -37,6 +39,27 @@ async function createCron(triggersEnabled: boolean) {
3739
}
3840

3941
describe("cron stream schedule validation", () => {
42+
it.each([
43+
{ cronEnabled: true, configured: undefined, triggersEnabled: true },
44+
{ cronEnabled: true, configured: true, triggersEnabled: true },
45+
{ cronEnabled: true, configured: false, triggersEnabled: false },
46+
{ cronEnabled: false, configured: true, triggersEnabled: true },
47+
{ cronEnabled: false, configured: false, triggersEnabled: false },
48+
])(
49+
"reports active trigger capability independently of scheduler enablement ($cronEnabled/$configured)",
50+
async ({ cronEnabled, configured, triggersEnabled }) => {
51+
const cron = await createCron(configured, cronEnabled);
52+
try {
53+
await expect(cron.status()).resolves.toMatchObject({
54+
enabled: cronEnabled,
55+
triggersEnabled,
56+
});
57+
} finally {
58+
cron.stop();
59+
}
60+
},
61+
);
62+
4063
it("rejects creation while cron triggers are disabled", async () => {
4164
const cron = await createCron(false);
4265
try {

‎src/cron/service/ops-read.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export async function status(state: CronServiceState) {
4242
const sqlitePath = resolveOpenClawStateSqlitePath();
4343
return {
4444
enabled: state.deps.cronEnabled,
45+
triggersEnabled: state.deps.cronConfig?.triggers?.enabled !== false,
4546
storePath: sqlitePath,
4647
storage: "sqlite" as const,
4748
sqlitePath,

‎src/cron/service/state.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ export type CronWakeMode = "now" | "next-heartbeat";
372372
/** Lightweight service status returned to gateway/control surfaces. */
373373
export type CronStatusSummary = {
374374
enabled: boolean;
375+
triggersEnabled: boolean;
375376
/** @deprecated Alias for `sqlitePath`. */
376377
storePath: string;
377378
/** Storage backend identifier. */

‎src/gateway/server.cron.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@ describe("gateway server cron", () => {
701701
const cronState = await createDirectCronState();
702702

703703
try {
704+
await expect(directCronReq(cronState, "cron.status", {})).resolves.toMatchObject({
705+
ok: true,
706+
payload: { enabled: false, triggersEnabled: false },
707+
});
704708
const response = await directCronReq(cronState, "cron.add", {
705709
name: "disabled watcher",
706710
enabled: true,

‎src/plugins/contracts/scheduled-turns.contract.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function createMockCronService(): CronServiceContract {
9393
stop: vi.fn(),
9494
status: vi.fn(async () => ({
9595
enabled: true,
96+
triggersEnabled: true,
9697
storePath: "/tmp/openclaw-test-cron.json",
9798
storage: "sqlite" as const,
9899
sqlitePath: "/tmp/openclaw-test-state/state/openclaw.sqlite",

‎ui/src/api/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ export type CronPayload = ProtocolCronJob["payload"];
565565

566566
export type CronStatus = {
567567
enabled: boolean;
568+
triggersEnabled: boolean;
568569
jobs: number;
569570
nextWakeAtMs?: number | null;
570571
};

‎ui/src/e2e/cron-trigger-authoring.e2e.test.ts‎

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mkdir } from "node:fs/promises";
33
import path from "node:path";
44
import type { Page } from "playwright";
55
import { expect, it } from "vitest";
6+
import type { ApplicationContext } from "../app/context.ts";
67
import { installMockGateway } from "../test-helpers/control-ui-e2e.ts";
78
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
89

@@ -14,6 +15,7 @@ const suite = createControlUiE2eSuite({
1415

1516
const proofDirectory = process.env.OPENCLAW_TRIGGER_UI_PROOF_DIR;
1617
const proofStage = process.env.OPENCLAW_TRIGGER_UI_PROOF_STAGE ?? "after";
18+
type CronTriggerTestApp = HTMLElement & { runtime?: { context: ApplicationContext } };
1719

1820
const scriptJob = {
1921
id: "existing-script-automation",
@@ -52,6 +54,14 @@ async function captureProof(page: Page, name: string) {
5254
});
5355
}
5456

57+
async function captureTriggerCapabilityProof(page: Page, name: string) {
58+
await page
59+
.locator(".settings-row__title")
60+
.filter({ hasText: "Condition trigger" })
61+
.evaluate((element) => element.scrollIntoView({ block: "center" }));
62+
await captureProof(page, name);
63+
}
64+
5565
async function selectSeconds(page: Page) {
5666
const unit = page.locator("wa-select").filter({
5767
has: page.locator('[slot="label"]', { hasText: "Unit" }),
@@ -82,7 +92,7 @@ suite.define(() => {
8292
hasMore: false,
8393
nextOffset: null,
8494
},
85-
"cron.status": { enabled: true, jobs: 1, nextWakeAtMs: null },
95+
"cron.status": { enabled: true, triggersEnabled: true, jobs: 1, nextWakeAtMs: null },
8696
},
8797
});
8898

@@ -162,4 +172,117 @@ suite.define(() => {
162172
},
163173
);
164174
});
175+
176+
it("keeps saved and unsaved trigger drafts separate from reconnect-refreshed scheduler capability", async () => {
177+
await suite.withPage(
178+
{ locale: "en-US", serviceWorkers: "block", viewport: { height: 1_050, width: 1_440 } },
179+
async ({ page }) => {
180+
const initialConfig = { cron: { triggers: { enabled: true } } };
181+
const gateway = await installMockGateway(page, {
182+
methodResponses: {
183+
"config.get": {
184+
appliedConfigHash: "trigger-config-1",
185+
config: initialConfig,
186+
configRevisionHash: "trigger-config-1",
187+
hash: "trigger-config-1",
188+
issues: [],
189+
raw: JSON.stringify(initialConfig),
190+
valid: true,
191+
},
192+
"cron.list": listResponse([]),
193+
"cron.runs": { entries: [], total: 0, offset: 0, limit: 50, hasMore: false },
194+
"cron.status": { enabled: true, triggersEnabled: true, jobs: 0, nextWakeAtMs: null },
195+
},
196+
});
197+
198+
await page.goto(`${suite.server.baseUrl}cron`);
199+
await page.locator('[data-test-id="cron-new-task"]').click();
200+
await page.locator("details.cron-advanced > summary").click();
201+
const triggerToggle = page
202+
.locator(".settings-row--toggle")
203+
.filter({ hasText: "Condition trigger" });
204+
await expect.poll(() => triggerToggle.count()).toBe(1);
205+
206+
const unsaved = await page.evaluate(async () => {
207+
const config = (document.querySelector("openclaw-app") as CronTriggerTestApp).runtime
208+
?.context.runtimeConfig;
209+
if (!config) {
210+
throw new Error("Runtime config capability is unavailable");
211+
}
212+
await config.ensureLoaded();
213+
config.setWritesSuspended(true);
214+
config.patchForm(["cron", "triggers", "enabled"], false);
215+
return { dirty: config.state.configFormDirty, needsApply: config.state.configNeedsApply };
216+
});
217+
expect(unsaved).toEqual({ dirty: true, needsApply: false });
218+
expect(await gateway.getRequests("config.set")).toHaveLength(0);
219+
await expect.poll(() => triggerToggle.count()).toBe(1);
220+
await captureTriggerCapabilityProof(page, "05-unsaved-disable-keeps-active-trigger");
221+
222+
const saveResult = await page.evaluate(async () => {
223+
const config = (document.querySelector("openclaw-app") as CronTriggerTestApp).runtime
224+
?.context.runtimeConfig;
225+
if (!config) {
226+
throw new Error("Runtime config capability is unavailable");
227+
}
228+
config.setWritesSuspended(false);
229+
const saved = await config.save();
230+
return {
231+
dirty: config.state.configFormDirty,
232+
needsApply: config.state.configNeedsApply,
233+
saved,
234+
};
235+
});
236+
expect(saveResult).toEqual({ dirty: false, needsApply: true, saved: true });
237+
const savedRequest = await gateway.waitForRequest("config.set");
238+
expect(JSON.parse(String((savedRequest.params as { raw?: string }).raw))).toEqual({
239+
cron: { triggers: { enabled: false } },
240+
});
241+
expect(await gateway.getRequests("config.apply")).toHaveLength(0);
242+
await expect.poll(() => triggerToggle.count()).toBe(1);
243+
await captureTriggerCapabilityProof(page, "06-saved-unapplied-keeps-active-trigger");
244+
245+
const previousStatuses = (await gateway.getRequests("cron.status")).length;
246+
await gateway.setMethodResponse("cron.status", {
247+
enabled: true,
248+
triggersEnabled: false,
249+
jobs: 0,
250+
nextWakeAtMs: null,
251+
});
252+
await gateway.closeLatest(1012, "refresh effective trigger capability");
253+
await expect
254+
.poll(async () => (await gateway.getRequests("cron.status")).length)
255+
.toBeGreaterThan(previousStatuses);
256+
await page.locator('[data-test-id="cron-new-task"]').click();
257+
await page.locator("details.cron-advanced > summary").click();
258+
await expect.poll(() => triggerToggle.count()).toBe(0);
259+
await page.getByText("Condition triggers are disabled by cron.triggers.enabled.").waitFor();
260+
await captureTriggerCapabilityProof(page, "07-reconnect-refreshes-disabled-trigger");
261+
262+
const oppositeDraft = await page.evaluate(async () => {
263+
const config = (document.querySelector("openclaw-app") as CronTriggerTestApp).runtime
264+
?.context.runtimeConfig;
265+
if (!config) {
266+
throw new Error("Runtime config capability is unavailable");
267+
}
268+
await config.ensureLoaded();
269+
config.setWritesSuspended(true);
270+
config.patchForm(["cron", "triggers", "enabled"], true);
271+
return config.state.configFormDirty;
272+
});
273+
expect(oppositeDraft).toBe(true);
274+
await expect.poll(() => triggerToggle.count()).toBe(0);
275+
await captureTriggerCapabilityProof(
276+
page,
277+
"08-unsaved-enable-cannot-author-disabled-trigger",
278+
);
279+
await page.evaluate(async () => {
280+
const config = (document.querySelector("openclaw-app") as CronTriggerTestApp).runtime
281+
?.context.runtimeConfig;
282+
await config?.discardDraft();
283+
config?.setWritesSuspended(false);
284+
});
285+
},
286+
);
287+
});
165288
});

‎ui/src/e2e/cron-trigger-filter.e2e.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ suite.define(() => {
6161
],
6262
},
6363
"cron.runs": { entries: [], total: 0, offset: 0, limit: 50, hasMore: false },
64-
"cron.status": { enabled: true, jobs: 2, nextWakeAtMs: null },
64+
"cron.status": { enabled: true, triggersEnabled: false, jobs: 2, nextWakeAtMs: null },
6565
},
6666
});
6767

‎ui/src/pages/agents/view.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ describe("renderAgents", () => {
145145
const job = createCronJob("implicit-default-job", {
146146
name: "Implicit default-agent reminder",
147147
});
148-
const globalNextWakeAtMs = Date.now() + 60_000;
149-
const scopedNextWakeAtMs = globalNextWakeAtMs + 3_600_000;
148+
const nextWakeAtMs = Date.now() + 60_000;
149+
const scopedNextWakeAtMs = nextWakeAtMs + 3_600_000;
150150
const container = document.createElement("div");
151151
render(
152152
renderAgents(
153153
createProps({
154154
activePanel: "cron",
155155
selectedAgentId: "alpha",
156156
cron: {
157-
status: { enabled: true, jobs: 51, nextWakeAtMs: globalNextWakeAtMs },
157+
status: { enabled: true, triggersEnabled: true, jobs: 51, nextWakeAtMs },
158158
jobs: [job],
159159
jobsTotal: 1,
160160
jobsHasMore: false,
@@ -188,7 +188,7 @@ describe("renderAgents", () => {
188188
expect(nextWakeRow?.querySelector(".settings-row__control")?.textContent?.trim()).toBe(
189189
formatNextRun(scopedNextWakeAtMs),
190190
);
191-
expect(nextWakeRow?.textContent).not.toContain(formatNextRun(globalNextWakeAtMs));
191+
expect(nextWakeRow?.textContent).not.toContain(formatNextRun(nextWakeAtMs));
192192
});
193193

194194
it("loads and renders the selected agent's 51st cron job when Load more is clicked", async () => {
@@ -227,7 +227,7 @@ describe("renderAgents", () => {
227227
activePanel: "cron",
228228
selectedAgentId: "alpha",
229229
cron: {
230-
status: { enabled: true, jobs: 80, nextWakeAtMs: null },
230+
status: { enabled: true, triggersEnabled: true, jobs: 80, nextWakeAtMs: null },
231231
jobs: cronState.cronJobs,
232232
jobsTotal: cronState.cronJobsTotal,
233233
jobsHasMore: cronState.cronJobsHasMore,

0 commit comments

Comments
 (0)