Make routines act on their own and check every bot action

Scheduled runs are pre-approved: they carry out their brief instead of
drafting for confirmation. A run that can't do its brief calls
report_run_failed and is retried 5 and 10 minutes later; only then does
the user get one "failed after N tries" message, and the routine keeps
why in last_error.

Every reply and task now gets ActionCheckNote: look for an existing
record before creating one, and verify an action took effect before
saying it's done. Memory and instructions saves are read back, and
create_bot, create_schedule, start_task and delegate_task refuse
duplicates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 08:45:49 +01:00
co-authored by Claude Opus 5.5
parent 6c57fae444
commit 535adb6e16
25 changed files with 448 additions and 16 deletions
+38
View File
@@ -0,0 +1,38 @@
import { describe, expect, test } from "bun:test";
import type { BUN_SQLITE_COBBLE_CRON_JOBS } from "@/db/types/db";
import findDuplicateCronJob from "./find-duplicate-cron-job";
const digest: BUN_SQLITE_COBBLE_CRON_JOBS = {
id: 2,
title: "Morning digest",
instructions: "Email the news digest.",
schedule: "0 6 * * *",
workspace_id: undefined,
};
const find = (params: Partial<Parameters<typeof findDuplicateCronJob>[0]>) =>
findDuplicateCronJob({
cron_jobs: [digest],
title: "Other",
instructions: "Something else.",
schedule: "0 7 * * *",
...params,
});
describe("findDuplicateCronJob", () => {
test("matches the same title, ignoring case and spacing", () => {
expect(find({ title: " morning DIGEST " })?.id).toBe(2);
});
test("matches the same timing and brief under another title", () => {
expect(find({ schedule: "0 6 * * *", instructions: "Email the news digest." })?.id).toBe(2);
});
test("doesn't match the same brief at another time", () => {
expect(find({ instructions: "Email the news digest." })).toBeNull();
});
test("only matches schedules in the same place", () => {
expect(find({ title: "Morning digest", workspace_id: 4 })).toBeNull();
});
});
+39
View File
@@ -0,0 +1,39 @@
import type { BUN_SQLITE_COBBLE_CRON_JOBS } from "@/db/types/db";
type Params<T extends BUN_SQLITE_COBBLE_CRON_JOBS> = {
/**
* The bot's schedules for this user.
*/
cron_jobs: T[];
title: string;
instructions: string;
schedule: string;
/**
* Where the new one would run: only schedules there can be its twin.
*/
workspace_id?: number | string | null;
};
const normalize = (text?: string | null) => (text || "").trim().replace(/\s+/g, " ").toLowerCase();
/**
* The schedule a new one would duplicate, if any: same place and either
* the same title, or the same timing and brief.
*/
export default function findDuplicateCronJob<T extends BUN_SQLITE_COBBLE_CRON_JOBS>({
cron_jobs,
title,
instructions,
schedule,
workspace_id,
}: Params<T>) {
return (
cron_jobs.find(
(cron_job) =>
Number(cron_job.workspace_id || 0) == Number(workspace_id || 0) &&
(normalize(cron_job.title) == normalize(title) ||
(normalize(cron_job.schedule) == normalize(schedule) &&
normalize(cron_job.instructions) == normalize(instructions))),
) || null
);
}
+18
View File
@@ -0,0 +1,18 @@
import { describe, expect, test } from "bun:test";
import { AppData } from "@/src/data/app-data";
import scheduledRunRetryAt from "./scheduled-run-retry-at";
describe("scheduledRunRetryAt", () => {
test("waits the retry delay after the first failure", () => {
expect(scheduledRunRetryAt({ attempts: 1, now: 0 })).toBe(AppData.ScheduledRunRetryMs);
});
test("doubles after each failure", () => {
expect(scheduledRunRetryAt({ attempts: 2, now: 0 })).toBe(AppData.ScheduledRunRetryMs * 2);
expect(scheduledRunRetryAt({ attempts: 3, now: 0 })).toBe(AppData.ScheduledRunRetryMs * 4);
});
test("treats no attempts as the first", () => {
expect(scheduledRunRetryAt({ attempts: 0, now: 10 })).toBe(10 + AppData.ScheduledRunRetryMs);
});
});
+19
View File
@@ -0,0 +1,19 @@
import { AppData } from "@/src/data/app-data";
type Params = {
/**
* Attempts the job has used so far, the failed one included.
*/
attempts: number;
now?: number;
};
/**
* When a failed scheduled run is tried again: `AppData.ScheduledRunRetryMs`
* after the first failure, doubling after each one after that.
*/
export default function scheduledRunRetryAt({ attempts, now = Date.now() }: Params) {
const step = Math.min(Math.max(attempts - 1, 0), 10);
return now + AppData.ScheduledRunRetryMs * 2 ** step;
}