Files
openbot/scripts/workflow-schedule-e2e.ts
NB 553e88e62a Add paused workflows and openbot.workflow files (#14875)
* Add typed workflow fields: output templates, trigger fields and field chips

This combines the reviewed PRs #14732, #14738, #14745, #14789 and #14795
into one change on main:

- Output message, and {{name}} in output title, labels and person (format 2).
- Built-in typed trigger fields (format 3).
- The editor completes field names after {{.
- An "Insert field" button, and list rows that show the source step.
- Chips for {{name}} in the editor (the saved text does not change), and
  the answer fields rows with inline values.

* Fix open review findings of the combined workflow PRs

- Duplicate keeps a saved step value that the host does not know: create
  compares it with the saved nodes that have the same ID (#14716).
- The format 3 rule reads names with workflowNodeAnswerNames and checks
  them against the built-in sources on the path to each step (#14738).
- An output step shows its message when the message has text, so a hidden
  message cannot set the GitHub item number (#14732).
- The field list follows the caret on Left, Right, Home and End (#14745).
- The composer badge keeps its old font size (#14789).
- isWorkflowFormat moves to ipc-workflows.ts to prevent an import cycle.

* Add paused workflows (#14701)

* Add the openbot.workflow file codec (#14750)

* Export and import workflows as openbot.workflow files (#14764)

* Use the workflow file codec for paste and transfer (#14772)

* Add workflow format and round-trip E2E (#14777)

* Fix open review findings of the combined workflow file PRs

- Check the size of the filled workflow file, so an exported file always decodes.
- Use one helper to fill the host export on the desktop and in the web client.
- Use a plain json filter in the save dialog.
- Tell about locked notes only when a transfer made notes.
- Check the posts of the output fixtures in the format E2E.
- Connect the trigger to the model step in the codec E2E, because the format 3 rule now reads the path.

* Remove the unused workflows-format-v1 capability

No client reads the capability. Each workflow detail has format and
hostFormat, and a client gives format 1 when a host does not send them.

* Share the placeholder logic of newer workflow steps

workflowGraphPlaceholders in contracts changes an unknown step kind to a
locked note, drops edges of an unknown type or branch and edges to a
placeholder, and tells if the graph has newer settings. The team client
decoder and the instance transfer now use it.

The decoder now also drops an edge of an unknown branch. Before, such an
edge made the whole workflow detail fail to decode on an older client.

* Fix exactOptionalPropertyTypes errors in the workflow import dialog

The candidate title can be undefined, and the select gets null when no
option is chosen.

* Name the unknown steps when a workflow file import is refused

A file from a newer OpenBot gives the steps that this version does not
know. The refusal text now lists up to six of them, as the changelog
says. New key workflow.file.import.newerSteps in 15 languages.

* Wait for the run to end before the typed answer test reads the retried gate

After a failed step, the run writes a skipped gate row. The test could read
that row before the retry added a new gate attempt.

* Give the workflow Team API test its own fake Codex

The media account step needs a Codex that starts. A CI runner has no Codex,
so the step failed with "not downloaded" before the account check. Main CI
did not run this script; this branch adds it to CI.
2026-11-23 00:18:14 +00:00

470 lines
18 KiB
TypeScript

import assert from "node:assert/strict";
import { mkdirSync, writeFileSync } from "node:fs";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import type { RoutineSchedule, WorkflowGraph, WorkflowNode } from "@openbot/contracts/ipc";
import { Effect } from "effect";
import { runCauseEffect } from "../src/backend/effect-boundary";
import { OpenBotDatabase } from "../src/backend/openbot-database";
import { RoutineTimer } from "../src/backend/routine-timer";
import { WorkflowGitHubStore } from "../src/backend/workflows/workflow-github-store";
import { WorkflowRunStore } from "../src/backend/workflows/workflow-run-store";
import { WorkflowStore } from "../src/backend/workflows/workflow-store";
import { WorkflowTriggers, workflowScheduleDueAt } from "../src/backend/workflows/workflow-triggers";
import { createWorkflows, type WorkflowsHandle } from "../src/backend/workflows/workflows";
const OUT = resolve(import.meta.dirname, "../.openbot-build/workflow-schedule-e2e");
const ZONE = "UTC";
const START = "2026-10-24T09:00:00.000Z";
const EVERY_15: RoutineSchedule = { kind: "interval", amount: 15, unit: "minutes", anchorAt: START };
interface Report {
checks: string[];
triggerOutputs?: string[];
modelPrompt?: string;
runs?: { workflow: string; number: number; source: string; scheduledFor: string | null; status: string }[];
passed: boolean;
failure?: string;
}
const report: Report = { checks: [], passed: false };
const check = (name: string, ok: boolean, detail?: unknown) => {
assert.ok(ok, `${name}${detail === undefined ? "" : `: ${JSON.stringify(detail)}`}`);
report.checks.push(name);
};
const trigger = (schedule: RoutineSchedule | undefined): WorkflowNode => ({
id: "trigger",
kind: "trigger",
at: { x: 0, y: 0 },
fields: { trigger: "schedule" },
...(schedule ? { schedule } : {}),
});
const modelPrompts: string[] = [];
const changeListeners = new Set<() => void>();
const nextChange = () =>
new Promise<void>((resolve) => {
const listener = () => {
changeListeners.delete(listener);
resolve();
};
changeListeners.add(listener);
});
async function until(test: () => Promise<boolean>, what: string) {
const deadline = Date.now() + 10_000;
while (Date.now() < deadline) {
const change = nextChange();
if (await test()) return;
let timer: ReturnType<typeof setTimeout> | undefined;
await Promise.race([change, new Promise((resolve) => (timer = setTimeout(resolve, deadline - Date.now())))]);
clearTimeout(timer);
}
assert.fail(`timed out: ${what}`);
}
const modelGraph = (schedule: RoutineSchedule, timezone: string): WorkflowGraph => ({
nodes: [
{ ...trigger(schedule), fields: { trigger: "schedule", timezone } },
{ id: "model", kind: "llm", at: { x: 300, y: 0 }, fields: { prompt: "Summarize the last 24 hours." } },
],
edges: [{ id: "edge", from: "trigger", to: "model", type: "text" }],
strokes: [],
});
const graphOf = (schedule: RoutineSchedule | undefined, step: "notify" | "approval"): WorkflowGraph => ({
nodes: [
trigger(schedule),
step === "notify"
? { id: "step", kind: "output", at: { x: 300, y: 0 }, fields: { target: "notify" } }
: { id: "step", kind: "approval", at: { x: 300, y: 0 }, fields: { question: "Go on?" } },
],
edges: [{ id: "edge", from: "trigger", to: "step", type: "text" }],
strokes: [],
});
const directory = await mkdtemp(join(tmpdir(), "openbot-workflow-schedule-"));
let clock = new Date(START);
let database = new OpenBotDatabase(directory);
let service: WorkflowsHandle | null = null;
async function open() {
await Effect.runPromise(database.initialize());
const store = new WorkflowStore({ database, nextDueAt: workflowScheduleDueAt(() => ZONE) });
const workflows = await Effect.runPromise(
createWorkflows({
store,
runs: new WorkflowRunStore({ database }),
github: new WorkflowGitHubStore({ database }),
githubApp: () => null,
delivery: () => null,
turnAnswer: () => null,
sendHandoff: () => Effect.fail({ cause: new Error("No agent step runs in this test.") }),
stopDelivery: () => Effect.void,
generateText: (request) =>
Effect.sync(() => {
modelPrompts.push(request.prompt);
return "Summary.";
}),
notify: () => {},
connectionOn: () => false,
post: () => Effect.die(new Error("No output posts in this test.")),
changed: () => {
for (const listener of [...changeListeners]) listener();
},
rearm: () => {},
timezone: () => ZONE,
now: () => clock,
}),
);
service = workflows;
const triggers = new WorkflowTriggers({
store,
timezone: () => ZONE,
start: (start) => workflows.startScheduled(start),
changed: () => {},
});
const timer = new RoutineTimer(
() => [triggers],
() => true,
() => {},
);
const fire = () => runCauseEffect(triggers.processDue(clock, () => true));
const nextDueAt = async (workflowId: string) =>
(await runCauseEffect(workflows.get(workflowId))).triggers.find((entry) => entry.nodeId === "trigger")?.nextDueAt ??
null;
const runsOf = async (workflowId: string) => {
const occurrence = database.connection.prepare("SELECT scheduled_for FROM workflow_runs WHERE run_id = ?");
return (await runCauseEffect(workflows.runs({ workflowId, limit: 50 }))).runs.map(({ run }) => {
const row = occurrence.get(run.id);
const scheduledFor = row && typeof row.scheduled_for === "string" ? row.scheduled_for : null;
return { ...run, scheduledFor };
});
};
const triggerOutput = async (runId: string) =>
(await runCauseEffect(workflows.run(runId))).steps.find((step) => step.nodeId === "trigger")?.output ?? null;
return { workflows, timer, fire, nextDueAt, runsOf, triggerOutput };
}
async function close() {
if (service) await Effect.runPromise(service.close());
service = null;
database.close();
}
try {
let app = await open();
const quarter = await runCauseEffect(app.workflows.create({ title: "Every quarter hour" }));
const quarterId = quarter.summary.id;
const saved = await runCauseEffect(
app.workflows.saveGraph({
workflowId: quarterId,
baseRevision: quarter.revision,
graph: graphOf(EVERY_15, "notify"),
}),
);
assert.ok(saved.ok);
check("save plans the next occurrence", (await app.nextDueAt(quarterId)) === "2026-10-24T09:15:00.000Z");
check("the timer wakes at that occurrence", app.timer.nextDueAt() === "2026-10-24T09:15:00.000Z");
clock = new Date("2026-10-24T09:14:59.000Z");
await app.fire();
check("nothing starts before the occurrence", (await app.runsOf(quarterId)).length === 0);
clock = new Date("2026-10-24T09:15:01.000Z");
await app.fire();
await app.fire();
let runs = await app.runsOf(quarterId);
check("a due occurrence starts one run, also when the timer fires twice", runs.length === 1, runs.length);
check(
"the run is a schedule run of that occurrence",
runs[0]?.source === "schedule" && runs[0].scheduledFor === "2026-10-24T09:15:00.000Z",
runs[0],
);
await runCauseEffect(app.workflows.sweep());
runs = await app.runsOf(quarterId);
check("the run ends", runs[0]?.status === "done", runs[0]?.status);
const quarterOutput = runs[0] ? await app.triggerOutput(runs[0].id) : null;
check(
"the schedule trigger gives the time of its occurrence to the next step",
quarterOutput?.startsWith(
"Run time: 2026-10-24T09:15:00.000Z\nLocal time of this run: Saturday, 2026-10-24 09:15 in UTC (GMT",
) === true,
quarterOutput,
);
check("the trigger moves to the next occurrence", (await app.nextDueAt(quarterId)) === "2026-10-24T09:30:00.000Z");
clock = new Date("2026-10-24T10:20:00.000Z");
await app.fire();
runs = await app.runsOf(quarterId);
check("missed occurrences become one run", runs.length === 2, runs.length);
check("that run is the last missed occurrence", runs[0]?.scheduledFor === "2026-10-24T10:15:00.000Z", runs[0]);
check("the next occurrence is in the future", (await app.nextDueAt(quarterId)) === "2026-10-24T10:30:00.000Z");
const unchanged = await runCauseEffect(app.workflows.get(quarterId));
const moved = { ...graphOf(EVERY_15, "notify") };
moved.nodes = moved.nodes.map((node) => (node.id === "step" ? { ...node, at: { x: 500, y: 40 } } : node));
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({ workflowId: quarterId, baseRevision: unchanged.revision, graph: moved }),
)
).ok,
);
check(
"a save that keeps the schedule keeps the plan",
(await app.nextDueAt(quarterId)) === "2026-10-24T10:30:00.000Z",
);
const hourly: RoutineSchedule = { kind: "hourly", minute: 5 };
const before = await runCauseEffect(app.workflows.get(quarterId));
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({
workflowId: quarterId,
baseRevision: before.revision,
graph: graphOf(hourly, "notify"),
}),
)
).ok,
);
check(
"a save that changes the schedule plans it again",
(await app.nextDueAt(quarterId)) === "2026-10-24T11:05:00.000Z",
);
const tooOften: RoutineSchedule = { kind: "interval", amount: 1, unit: "minutes", anchorAt: START };
const latest = await runCauseEffect(app.workflows.get(quarterId));
const refused = await runCauseEffect(
app.workflows.saveGraph({
workflowId: quarterId,
baseRevision: latest.revision,
graph: graphOf(tooOften, "notify"),
}),
).then(
() => null,
(error: unknown) => (error instanceof Error ? error.message : String(error)),
);
check("a schedule that is too frequent is refused", refused !== null && refused.length > 0, refused);
check("the refused save keeps the plan", (await app.nextDueAt(quarterId)) === "2026-10-24T11:05:00.000Z");
const gated = await runCauseEffect(app.workflows.create({ title: "Waits for a person" }));
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({
workflowId: gated.summary.id,
baseRevision: gated.revision,
graph: graphOf(EVERY_15, "approval"),
}),
)
).ok,
);
check("the second workflow is planned", (await app.nextDueAt(gated.summary.id)) === "2026-10-24T10:30:00.000Z");
clock = new Date("2026-10-24T10:30:00.000Z");
await app.fire();
await runCauseEffect(app.workflows.sweep());
let gatedRuns = await app.runsOf(gated.summary.id);
check(
"its first occurrence starts a run that waits",
gatedRuns.length === 1 && gatedRuns[0]?.status === "approval",
gatedRuns,
);
clock = new Date("2026-10-24T10:45:00.000Z");
await app.fire();
gatedRuns = await app.runsOf(gated.summary.id);
check("an occurrence while the run waits starts nothing", gatedRuns.length === 1, gatedRuns.length);
check(
"the waiting occurrence keeps its plan",
(await app.nextDueAt(gated.summary.id)) === "2026-10-24T10:45:00.000Z",
);
check(
"the timer does not wake for a workflow that runs",
app.timer.nextDueAt() === "2026-10-24T11:05:00.000Z",
app.timer.nextDueAt(),
);
const approveNewest = async () => {
const newest = (await app.runsOf(gated.summary.id))[0];
assert.ok(newest, "the gated workflow has a run");
const { steps } = await runCauseEffect(app.workflows.run(newest.id));
const waiting = steps.find((step) => step.status === "approval");
assert.ok(waiting, "the run waits for a decision");
await runCauseEffect(app.workflows.decide({ stepId: waiting.id, approved: true }));
};
clock = new Date("2026-10-24T10:50:00.000Z");
await approveNewest();
gatedRuns = await app.runsOf(gated.summary.id);
check("the approved run ends", gatedRuns[0]?.status === "done", gatedRuns[0]);
await app.fire();
gatedRuns = await app.runsOf(gated.summary.id);
check(
"the occurrence that waited starts when the run ends",
gatedRuns.length === 2 && gatedRuns[0]?.scheduledFor === "2026-10-24T10:45:00.000Z",
gatedRuns,
);
check(
"then the plan moves to the next occurrence",
(await app.nextDueAt(gated.summary.id)) === "2026-10-24T11:00:00.000Z",
);
await approveNewest();
const once = await runCauseEffect(app.workflows.create({ title: "Once" }));
const onceSchedule: RoutineSchedule = { kind: "once", date: "2026-10-24", time: "12:00" };
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({
workflowId: once.summary.id,
baseRevision: once.revision,
graph: graphOf(onceSchedule, "notify"),
}),
)
).ok,
);
check("a one-time schedule is planned", (await app.nextDueAt(once.summary.id)) === "2026-10-24T12:00:00.000Z");
await close();
database = new OpenBotDatabase(directory);
app = await open();
check("a restart keeps the plans", (await app.nextDueAt(once.summary.id)) === "2026-10-24T12:00:00.000Z");
check("a restart keeps the earliest wake", app.timer.nextDueAt() === "2026-10-24T11:00:00.000Z");
clock = new Date("2026-10-24T12:00:30.000Z");
await app.fire();
const onceRuns = await app.runsOf(once.summary.id);
check(
"the one-time occurrence starts one run",
onceRuns.length === 1 && onceRuns[0]?.scheduledFor === "2026-10-24T12:00:00.000Z",
onceRuns,
);
check("a one-time schedule ends after its run", (await app.nextDueAt(once.summary.id)) === null);
const model = await runCauseEffect(app.workflows.create({ title: "Daily summary" }));
const modelSchedule: RoutineSchedule = { kind: "once", date: "2026-10-24", time: "14:30" };
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({
workflowId: model.summary.id,
baseRevision: model.revision,
graph: modelGraph(modelSchedule, "Europe/Warsaw"),
}),
)
).ok,
);
check(
"a schedule in the trigger's time zone is planned",
(await app.nextDueAt(model.summary.id)) === "2026-10-24T12:30:00.000Z",
);
clock = new Date("2026-10-24T12:30:00.000Z");
await app.fire();
await until(async () => (await app.runsOf(model.summary.id))[0]?.status === "done", "the model step run ends");
const modelRuns = await app.runsOf(model.summary.id);
check("the model step run ends", modelRuns.length === 1 && modelRuns[0]?.status === "done", modelRuns);
const prompt = modelPrompts.at(-1) ?? "";
report.modelPrompt = prompt;
check(
"the model step reads the run time in the trigger's time zone",
modelPrompts.length === 1 &&
prompt.includes("Run time: 2026-10-24T12:30:00.000Z") &&
prompt.includes("Local time of this run: Saturday, 2026-10-24 14:30 in Europe/Warsaw (GMT+02:00)."),
prompt,
);
const manual = await runCauseEffect(app.workflows.startRun({ workflowId: once.summary.id, requestId: "manual-1" }));
const manualOutput = await app.triggerOutput(manual.run.id);
check(
"a manual run of a schedule trigger gives the time it started",
manualOutput?.startsWith(`Run time: ${manual.run.startedAt}\n`) === true,
manualOutput,
);
report.triggerOutputs = [quarterOutput ?? "", manualOutput ?? ""];
const held = await runCauseEffect(app.workflows.create({ title: "Paused copy", paused: true }));
check("a workflow can be made paused", held.summary.paused === true, held.summary);
assert.ok(
(
await runCauseEffect(
app.workflows.saveGraph({
workflowId: held.summary.id,
baseRevision: held.revision,
graph: graphOf(EVERY_15, "notify"),
}),
)
).ok,
);
check(
"a save keeps the workflow paused",
(await runCauseEffect(app.workflows.get(held.summary.id))).summary.paused === true,
);
check(
"the list shows the paused workflow",
(await runCauseEffect(app.workflows.list())).some((entry) => entry.id === held.summary.id && entry.paused === true),
);
check("a paused schedule keeps its plan", (await app.nextDueAt(held.summary.id)) === "2026-10-24T12:45:00.000Z");
clock = new Date("2026-10-24T12:45:00.000Z");
await app.fire();
check("a paused schedule starts no run", (await app.runsOf(held.summary.id)).length === 0);
check(
"a paused schedule moves past the skipped occurrence",
(await app.nextDueAt(held.summary.id)) === "2026-10-24T13:00:00.000Z",
);
const heldManual = await runCauseEffect(
app.workflows.startRun({ workflowId: held.summary.id, requestId: "manual-paused" }),
);
await runCauseEffect(app.workflows.sweep());
check(
"a paused workflow still runs by hand",
(await app.runsOf(held.summary.id)).some((run) => run.id === heldManual.run.id && run.status === "done"),
);
const resumed = await runCauseEffect(app.workflows.setPaused({ workflowId: held.summary.id, paused: false }));
check("resume clears the paused state", resumed.paused === undefined, resumed);
clock = new Date("2026-10-24T13:00:00.000Z");
await app.fire();
const heldRuns = await app.runsOf(held.summary.id);
check(
"a resumed schedule starts the next occurrence and not the skipped one",
heldRuns.length === 2 &&
heldRuns[0]?.source === "schedule" &&
heldRuns[0].scheduledFor === "2026-10-24T13:00:00.000Z",
heldRuns,
);
const repaused = await runCauseEffect(app.workflows.setPaused({ workflowId: held.summary.id, paused: true }));
check("pause sets the paused state", repaused.paused === true, repaused);
const missingPause = await runCauseEffect(app.workflows.setPaused({ workflowId: "missing", paused: true })).then(
() => null,
(error: unknown) => (error instanceof Error ? error.message : String(error)),
);
check("pause of a missing workflow fails", missingPause !== null, missingPause);
report.runs = [];
for (const [workflow, id] of [
["quarter", quarterId],
["gated", gated.summary.id],
["once", once.summary.id],
] as const) {
for (const run of await app.runsOf(id))
report.runs.push({
workflow,
number: run.number,
source: run.source,
scheduledFor: run.scheduledFor,
status: run.status,
});
}
report.passed = true;
} catch (error) {
report.failure = error instanceof Error ? error.message : String(error);
process.exitCode = 1;
} finally {
try {
await close();
} finally {
await rm(directory, { recursive: true, force: true });
}
mkdirSync(OUT, { recursive: true });
const artifact = join(OUT, "report.json");
writeFileSync(artifact, `${JSON.stringify(report, null, 2)}\n`);
process.stdout.write(`${artifact}\n`);
if (report.failure) process.stderr.write(`${report.failure}\n`);
}