Public: anyone can read this; the crew can change it.
Two ways to give an app time, and v1's schedule probe sees only one: the morning paper's 07:15 print is invisible to it #13558
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13533-two-ways-to-give-an-app-time-and-v1-s-sc → main 5 commits fork
27 files
changed +484
−593
docs/APPS.md +1
| @@ -1340,6 +1340,7 @@ Declare schedules in `forge.json`: | ||
| 1340 | 1340 | - **Missed.** Timers are rows read at boot, so a release never drops one. A run due while the forge was away fires as soon as it is back if it is inside `catchUp` (default `"1h"`; `"none"`, or `"30m"`, `"2d"` up to 7 d) and the history says it ran late; past it, the run is recorded as missed and skipped. |
| 1341 | 1341 | - **Seen and steered.** The app's page has a Schedules row per schedule: next run, last run and how it went, in the schedule's timezone, with **Run now** (once, apart from the clock) and **Pause**. `GET /api/v1/repos/{owner}/{name}/schedules` (`?runs=N` adds the history: attempts, duration, status, the first 2 KB of the response) and the `app_deploy` MCP tool (`action: "schedules" | "run_schedule" | "pause_schedule" | "resume_schedule"`) answer the same. |
| 1342 | 1342 | - **When it fails.** A failed or missed run asks the app's owner once, with the reason in plain words (and on their phone when a notify URL is set); the ask is the schedule, not the run, so a schedule that keeps failing does not ask again. A schedule that fails five days running pauses itself and says so; Resume waits for the next occurrence. |
| 1343 | - **A feature's tools on a clock.** `feature.schedule` in `forge.json` (`{ "tool": "print_today", "at": "07:15", "tz": "America/Chicago" }`) is sugar for an entry here, named for the tool and posting `/forge/tools/<tool>`; it is the same row, steering, retries, overdue watch and health count, and the forge calls the tool only while an admin has absorbed the app. `docs/features/feature-schedule.md`. | |
| 1343 | 1344 | - **Runtime timers.** An app arms its own with `POST /api/v1/timers` under its own token: `{ "in": "10m" | seconds, or "at": "<ISO date>", "path": "/jobs/x", "key": "optional", "body": <up to 64 KB> }`; arming a key again replaces it; `DELETE /api/v1/timers/<key>` cancels. At most `FORGE_TIMERS_PER_APP` (default 100) pending, a year ahead at most. |
| 1344 | 1345 | |
| 1345 | 1346 | A fired job is an ordinary request to the app's port and counts toward its own limits. How to try it: `docs/features/timers.md`. |
docs/features/feature-schedule.md +14 −12
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Scheduled tools: the forge calls an absorbed app's tool on time |
| 2 | 2 | |
| 3 | An app can ask the forge to call one of its own declared tools on an interval. No model and no crew is involved: the forge makes the same loopback call the `features` MCP tool makes (`callTool`, `POST /forge/tools/<tool>` with `{"args":{}}`) and keeps how it went. | |
| 3 | An app can ask the forge to call one of its own declared tools on an interval or at a clock time. No model and no crew is involved: the forge makes the call (`POST /forge/tools/<tool>` with `{"args":{}}`) as a schedule like any other and keeps how it went. | |
| 4 | 4 | |
| 5 | 5 | ## Declare it |
| 6 | 6 | |
| @@ -15,38 +15,40 @@ In the app's `forge.json`, next to the `tools` the schedule names: | ||
| 15 | 15 | |
| 16 | 16 | |
| 17 | 17 | - At most 4 entries. `every` is `15m`, `1h`, `6h` or `1d`. A wrong entry is refused with the field named (`src/apps/feature.ts`). |
| 18 | - **Clock time:** `{ "tool": "print_today", "at": "07:15", "tz": "America/Chicago" }` instead of `every` (exactly one of the two; `tz` is an IANA name, daylight saving is followed). It fires once per local day; a forge that was away fires it on its next look within a 3 h grace window and records `late`, and past the window skips the day and records `skipped`. A manual run does not move an `at` schedule (it does move an `every` one). A new or changed schedule first fires at its next due time, never on absorb; Run now is for the impatient. Every way of running a scheduled tool (schedule, page, terminal, the `features` tool) is the one recorded run; a tool the schedule does not name is not recorded. | |
| 19 | - Each entry names a tool the feature declares. A tool that is no longer declared is skipped, never called. | |
| 18 | - **Clock time:** `{ "tool": "print_today", "at": "07:15", "tz": "America/Chicago" }` instead of `every` (exactly one of the two; `tz` is an IANA name, daylight saving is followed). It fires once per local day. A manual run does not move the schedule. A new or changed schedule first fires at its next due time; Run now is for the impatient. | |
| 19 | - Each entry names a tool the feature declares. | |
| 20 | 20 | - The tool is called with no arguments, so it should be one that needs none. |
| 21 | 21 | |
| 22 | 22 | ## When it fires |
| 23 | 23 | |
| 24 | `src/apps/feature_schedule.ts` looks once a minute (the timer is off under `bun test`). A tool is due when it has never fired or its interval has passed since its last fire, so two looks in one window fire once. The forge calls it only when: | |
| 24 | A feature's `schedule` is sugar for top-level `schedules` (#13533): `src/apps/feature.ts` `scheduleSpecs` turns each entry into a `schedules` entry named for its tool (`print_today`), whose cron is the clock time in its zone (`15 7 * * *` in `America/Chicago`) or the interval in UTC, whose `path` is `/forge/tools/<tool>` and whose body is `{"args":{}}`. `src/apps/manifest.ts` adds them to the manifest's `schedules`, so there is one parser (`schedule_spec.ts`) and one firing loop (`timer_fire.ts`). So the entry is: | |
| 25 | 25 | |
| 26 | - an admin has absorbed the app (an app that is not absorbed is never called), and | |
| 27 | - its production is running. A stopped or asleep app is skipped and never woken; its tool fires on the first look after it is running. | |
| 26 | - a Schedules row on the app's page, with next and last run, the app's answer, **Run now** and **Pause**; | |
| 27 | - counted in `/api/v1/health` `schedules` (`total`, `ran`, `overdue`), and so in the v1 proof's #11595 probe; | |
| 28 | - fired by waking the app and waiting for it to be ready; the time limit (300 s) covers the call, not the wake; | |
| 29 | - tried again after 30 s, 2 min and 10 min when it fails (`retries` 3), under one idempotency key; and when it still fails the owner is asked once, with the reason (docs/APPS.md, "Declare schedules"). | |
| 28 | 30 | |
| 29 | One app at a time, one tool at a time: a slow tool delays the next, and a tool is never called while its previous call is still running. A failing look at one app is logged and does not stop the others. | |
| 31 | A name used by both a `schedules` entry and a feature tool is refused by the manifest. A run due while the forge was away fires within an hour (`catchUp`) and says it was late. **Only while the app is absorbed:** the forge calls a feature's tool only for an app an admin has absorbed. The timer exists from the commit (so its row shows), but when it comes due for an app that is not absorbed it is dropped unfired, with no run recorded and no call made (`permits` in `timer_fire.ts`, set by `timer_service.ts`); Run now on the features page and the terminal's `r` refuse it in words, arming nothing. Un-absorbing therefore stops the schedule at its next due time; absorbing again starts it again, at the next due time, never at once. | |
| 30 | 32 | |
| 31 | 33 | ## What the forge records |
| 32 | 34 | |
| 33 | One row per (app, tool) in `feature_fire` (`src/core/feature_fires.ts`), replaced on each fire: when it started, whether the app answered `ok`, the error sentence when it did not (`the app answered 500`, a timeout), and how many milliseconds it took. Un-absorbing an app, or deleting it, clears its rows; absorbing again starts from never fired. | |
| 35 | The runs are the timers' (`timer_run`, `src/core/timers.ts`): when each started, how many attempts, the HTTP status, the first 2 KB of the app's answer, the error sentence. A hand's call (the `features` tool, `mcp`) is the one thing still kept in `feature_fire` and `feature_run` (`src/core/feature_fires.ts`); un-absorbing or deleting the app clears those. | |
| 34 | 36 | |
| 35 | 37 | ## Read it |
| 36 | 38 | |
| 37 | 39 | Every door shows one line per schedule entry, worded once by `scheduleMeta` in `src/apps/feature_schedule.ts`: `scan every 1h · last 10:02, ok · 42 ms · next 11:02`. When the last call failed, its error sentence stands in for `ok`. Before the first call the line reads `scan every 1h · never run · next soon`. |
| 38 | 40 | |
| 39 | - **Page:** Settings › Features › the app, under **Scheduled** (a site admin's page, as the rest of it). The last run reads as the page's relative clock (`2m ago`) and the next as its day; both carry the exact instant in their tooltip. A failure is in the danger colour. Each row has **Run now**: the same call, recorded as the tool's fire. A stopped or asleep app is refused with a sentence, never woken; a call already running is refused, never doubled. | |
| 41 | - **Page:** Settings › Features › the app, under **Scheduled** (a site admin's page, as the rest of it). The last run reads as the page's relative clock (`2m ago`) and the next as its day; both carry the exact instant in their tooltip. A failure is in the danger colour. Each row has **Run now**: it arms the tool's timer for now, the one the app page's Run now arms, so the app is woken, the call retried and the run kept like any schedule's. | |
| 40 | 42 | - **API and the `features` tool:** `GET /api/v1/features/:slug` returns `fires`, a list of `{ tool, at, ok, error, durationMs }` (`at` in ms since the epoch), empty until something has fired. It also returns `schedule`, one `{ tool, every, lastAt, ok, error, durationMs, nextAt, line }` per entry while the app is absorbed. `line` is the plain-text sentence above, with clocks in UTC. Readable by whoever may read the feature. |
| 41 | - **Terminal:** Feature feeds › the app: a `Scheduled:` line above the feed items, the entries' `line`s joined by `; `. An admin presses `r` there to run those tools now: the same call, record and refusal sentences as Run now on the page. | |
| 43 | - **Terminal:** Feature feeds › the app: a `Scheduled:` line above the feed items, the entries' `line`s joined by `; `. An admin presses `r` there to run those tools now: the same sentences as Run now on the page. | |
| 42 | 44 | |
| 43 | 45 | ## Not done |
| 44 | 46 | |
| 45 | No arguments per entry, no history beyond the last fire, and no wake-on-schedule for a stopped app. | |
| 47 | No arguments per entry. | |
| 46 | 48 | |
| 47 | 49 | ## Run history |
| 48 | 50 | |
| 49 | Each scheduled tool keeps its last 30 runs, with how each started (`schedule`, `late`, `skipped`, `page`, `terminal`, `mcp`), when, whether it went well and how long it took. | |
| 51 | Each scheduled tool shows its last 30 runs, with how each started (`schedule`, `late`, `skipped` for a missed one, `mcp`), when, whether it went well and how long it took. | |
| 50 | 52 | |
| 51 | 53 | - **Features page:** "Run history", one list per tool, the newest 5 shown and the older ones behind "N older". |
| 52 | 54 | - **Terminal:** open the feature under Feature feeds and press `h` to switch between the feed and the history; the Scheduled lines above (one per tool, with `at`/zone) stay. |
scripts/unread-tables-allow.json +1
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | [ |
| 2 | "feature_schedule_seen", | |
| 2 | 3 | "keeper_beat", |
| 3 | 4 | "proof_run", |
| 4 | 5 | "sentinel_kept", |
src/actions/features.ts +23 −11
| @@ -17,8 +17,8 @@ | ||
| 17 | 17 | * `FORGE_SETTING_*` in a new process, the same way an env var does. |
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | import { scheduleStatus, type ScheduleStatus } from "../apps/feature_schedule.ts"; | |
| 21 | import { lastFires, runHistory, scheduleSeen, type Fired, type Run } from "../core/feature_fires.ts"; | |
| 20 | import { scheduleStatus, timerView, type ScheduleStatus } from "../apps/feature_schedule.ts"; | |
| 21 | import { lastFires, runHistory, type Fired, type Run } from "../core/feature_fires.ts"; | |
| 22 | 22 | import type { App } from "../app.ts"; |
| 23 | 23 | import { readFileAt } from "../apps/checkout.ts"; |
| 24 | 24 | import { checkSetting, featureEnv, parseFeature, settingEnvName, type FeatureSpec } from "../apps/feature.ts"; |
| @@ -80,7 +80,10 @@ export interface FeatureView { | ||
| 80 | 80 | readonly name: string; |
| 81 | 81 | readonly summary: string | null; |
| 82 | 82 | readonly page: string; |
| 83 | /** The name and page the app declares now, when they differ from the absorbed ones: waits for an admin. */ | |
| 83 | /** | |
| 84 | * The name and page the app declares now, when they differ from the absorbed ones: waits for an | |
| 85 | * admin. | |
| 86 | */ | |
| 84 | 87 | readonly pending: { readonly name: string; readonly page: string; readonly summary: string | null } | null; |
| 85 | 88 | readonly absorbed: boolean; |
| 86 | 89 | readonly absorbedBy: string | null; |
| @@ -111,7 +114,10 @@ export interface Provenance { | ||
| 111 | 114 | readonly changedAt: string | null; |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | /** The last change by tip. Bounded on its own, apart from the declarations': cleared whole when full. */ | |
| 117 | /** | |
| 118 | * The last change by tip. Bounded on its own, apart from the declarations': cleared whole when | |
| 119 | * full. | |
| 120 | */ | |
| 115 | 121 | const changed = new Map<string, { by: string; at: string } | null>(); |
| 116 | 122 | const CHANGED_MAX = 500; |
| 117 | 123 | |
| @@ -206,8 +212,9 @@ async function readDeclaration(dir: string, tip: string): Promise<Result<Feature | ||
| 206 | 212 | |
| 207 | 213 | /** |
| 208 | 214 | * Whether the link to an app's feature shows on its page: the app is absorbed |
| 209 | * or its default branch declares a feature, a site admin's only, and read through `declaredFeature`'s cache by | |
| 210 | * commit, so a page view costs a git read only the first time a tip is seen. | |
| 215 | * or its default branch declares a feature, a site admin's only, and read through | |
| 216 | * `declaredFeature`'s cache by commit, so a page view costs a git read only the first time a tip is | |
| 217 | * seen. | |
| 211 | 218 | */ |
| 212 | 219 | export async function declaresFeature(app: App, actor: User | undefined, repo: Repo): Promise<boolean> { |
| 213 | 220 | if (!siteAdmin(actor)) return false; |
| @@ -228,7 +235,9 @@ function viewOf( | ||
| 228 | 235 | const kept = spec === undefined ? featureSettings(app.db, record.id) : openSettings(app.db, app.config.dataDir, record.id, spec); |
| 229 | 236 | const absorbed = getAbsorbed(app.db, record.id); |
| 230 | 237 | const env = spec === undefined ? {} : featureEnv(spec, kept); |
| 231 | const fires = absorbed === undefined ? [] : lastFires(app.db, record.id); | |
| 238 | const handFires = absorbed === undefined ? [] : lastFires(app.db, record.id); | |
| 239 | const timed = absorbed === undefined ? undefined : timerView(app.db, record.id, spec?.schedule ?? []); | |
| 240 | const fires = [...handFires.filter((f) => timed?.fires.every((t) => t.tool !== f.tool) ?? true), ...(timed?.fires ?? [])]; | |
| 232 | 241 | return { |
| 233 | 242 | slug: record.slug, |
| 234 | 243 | repo: `${repo.ownerName}/${repo.name}`, |
| @@ -248,8 +257,8 @@ function viewOf( | ||
| 248 | 257 | provenance, |
| 249 | 258 | tools: spec?.tools ?? [], |
| 250 | 259 | fires, |
| 251 | history: absorbed === undefined ? [] : runHistory(app.db, record.id), | |
| 252 | schedule: absorbed === undefined ? [] : scheduleStatus(spec?.schedule ?? [], fires, Date.now(), scheduleSeen(app.db, record.id)), | |
| 260 | history: absorbed === undefined ? [] : [...runHistory(app.db, record.id), ...(timed?.history ?? [])].sort((a, b) => a.tool.localeCompare(b.tool) || b.at - a.at), | |
| 261 | schedule: absorbed === undefined ? [] : scheduleStatus(spec?.schedule ?? [], fires, Date.now(), timed?.dues), | |
| 253 | 262 | settings: (spec?.settings ?? []).map((s) => { |
| 254 | 263 | const name = settingEnvName(s.key); |
| 255 | 264 | return { |
| @@ -313,7 +322,9 @@ export interface NavFeature { | ||
| 313 | 322 | readonly record: AppRecord; |
| 314 | 323 | } |
| 315 | 324 | |
| 316 | /** Absorbed apps the actor may read, for the forge's nav. Cheap enough for every page: no git reads. */ | |
| 325 | /** | |
| 326 | * Absorbed apps the actor may read, for the forge's nav. Cheap enough for every page: no git reads. | |
| 327 | */ | |
| 317 | 328 | export function absorbedForNav(app: App, actor: User | undefined): NavFeature[] { |
| 318 | 329 | const out: NavFeature[] = []; |
| 319 | 330 | for (const row of listAbsorbed(app.db)) { |
| @@ -450,7 +461,8 @@ export async function changeFeatureSettings( | ||
| 450 | 461 | const before = openSettings(app.db, app.config.dataDir, found.record.id, declared.value); |
| 451 | 462 | const changed = [...changes].filter(([k, v]) => (before.get(k) ?? declared.value!.settings.find((s) => s.key === k)!.default) !== v); |
| 452 | 463 | const isSecret = (k: string): boolean => declared.value!.settings.find((s) => s.key === k)?.type === "secret"; |
| 453 | // A blank secret clears it: the row goes, so `set` says not set rather than a sealed empty string. | |
| 464 | // A blank secret clears it: the row goes, so `set` says not set rather than a sealed empty | |
| 465 | // string. | |
| 454 | 466 | const cleared = [...changes].filter(([k, v]) => v === "" && isSecret(k)).map(([k]) => k); |
| 455 | 467 | const stored = new Map<string, string>(); |
| 456 | 468 | for (const [k, v] of changes) if (!cleared.includes(k)) stored.set(k, isSecret(k) ? sealSetting(app.config.dataDir, found.record.id, k, v) : v); |
src/apps/feature.ts +41 −2
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 26 | 26 | import { isTimezone } from "./cron.ts"; |
| 27 | 27 | import { parseTools, type ToolSpec } from "./feature_tools.ts"; |
| 28 | import { RETRIES_DEFAULT, TIMEOUT_DEFAULT_S, CATCH_UP_DEFAULT_MS, type ScheduleSpec } from "./schedule_spec.ts"; | |
| 28 | 29 | |
| 29 | 30 | export type SettingType = "bool" | "int" | "string" | "choice" | "days" | "list" | "secret"; |
| 30 | 31 | |
| @@ -71,7 +72,10 @@ export interface FeatureSpec { | ||
| 71 | 72 | readonly settings: readonly SettingSpec[]; |
| 72 | 73 | /** MCP tools the app serves through the forge's `features` tool (`feature_tools.ts`). */ |
| 73 | 74 | readonly tools: readonly ToolSpec[]; |
| 74 | /** Tools the app asks the forge to call on a timer; empty when none. Fired by `apps/feature_schedule.ts`. */ | |
| 75 | /** | |
| 76 | * Tools the app asks the forge to call on a timer; empty when none. Fired by | |
| 77 | * `apps/feature_schedule.ts`. | |
| 78 | */ | |
| 75 | 79 | readonly schedule: readonly ScheduleEntry[]; |
| 76 | 80 | } |
| 77 | 81 | |
| @@ -93,7 +97,42 @@ const MINUTE = 60_000; | ||
| 93 | 97 | export const SCHEDULE_EVERY: Readonly<Record<string, number>> = { "15m": 15 * MINUTE, "1h": 60 * MINUTE, "6h": 360 * MINUTE, "1d": 1440 * MINUTE }; |
| 94 | 98 | export const SCHEDULE_MAX = 4; |
| 95 | 99 | |
| 96 | /** The `schedule` of a feature block, checked against the tools the feature declares; an error names the entry. */ | |
| 100 | /** The path the forge calls a feature's tool on: the same one the `features` tool uses. */ | |
| 101 | export const toolPath = (tool: string): string => `/forge/tools/${tool}`; | |
| 102 | ||
| 103 | /** | |
| 104 | * The cron a feature entry means: a clock time is daily in its zone; an interval is aligned to the | |
| 105 | * hour of the day in UTC. | |
| 106 | */ | |
| 107 | function cronOf(e: ScheduleEntry): { cron: string; timezone: string } { | |
| 108 | if (e.at !== undefined && e.tz !== undefined) return { cron: `${Number(e.at.slice(3, 5))} ${Number(e.at.slice(0, 2))} * * *`, timezone: e.tz }; | |
| 109 | const minutes = (e.everyMs ?? 0) / MINUTE; | |
| 110 | const cron = minutes < 60 ? `*/${minutes} * * * *` : minutes < 1440 ? `0 */${minutes / 60} * * *` : "0 0 * * *"; | |
| 111 | return { cron, timezone: "UTC" }; | |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * A feature's scheduled tools as ordinary `schedules` entries (#13533): the one way the forge | |
| 116 | * keeps time. The entry is named for its tool and POSTs the tool's route with empty arguments. | |
| 117 | */ | |
| 118 | export function scheduleSpecs(schedule: readonly ScheduleEntry[]): ScheduleSpec[] { | |
| 119 | return schedule.map((e) => ({ | |
| 120 | name: e.tool, | |
| 121 | ...cronOf(e), | |
| 122 | path: toolPath(e.tool), | |
| 123 | body: '{"args":{}}', | |
| 124 | contentType: "application/json", | |
| 125 | env: "production" as const, | |
| 126 | timeoutMs: TIMEOUT_DEFAULT_S * 1000, | |
| 127 | retries: RETRIES_DEFAULT, | |
| 128 | catchUpMs: CATCH_UP_DEFAULT_MS, | |
| 129 | })); | |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * The `schedule` of a feature block, checked against the tools the feature declares; an error names | |
| 134 | * the entry. | |
| 135 | */ | |
| 97 | 136 | function parseSchedule(raw: unknown, tools: readonly ToolSpec[]): Result<readonly ScheduleEntry[], string> { |
| 98 | 137 | if (raw === undefined) return ok([]); |
| 99 | 138 | if (!Array.isArray(raw)) return bad("schedule must be a list of { tool, every } or { tool, at, tz }"); |
src/apps/feature_run.ts +15 −13
| @@ -1,24 +1,26 @@ | ||
| 1 | 1 | /** |
| 2 | * Run now, in words: one call of a scheduled tool through `fireOne`, and what | |
| 3 | * to tell the person. The page and the terminal both say it through here, so | |
| 4 | * the sentences are one set. | |
| 2 | * Run now, in words: a scheduled tool's own timer armed for this moment (`core/timers.ts` | |
| 3 | * `runNow`, the same one the app page's Run now uses), and what to tell the person. The page and | |
| 4 | * the terminal both say it through here, so the sentences are one set. The timer loop calls the | |
| 5 | * tool within a second, wakes the app if it sleeps, retries a failure, and keeps the run where | |
| 6 | * every schedule's runs are kept. | |
| 5 | 7 | */ |
| 6 | 8 | |
| 7 | 9 | import type { Database } from "bun:sqlite"; |
| 8 | import { lastFires, type How } from "../core/feature_fires.ts"; | |
| 9 | import { fireOne, type Reach } from "./feature_schedule.ts"; | |
| 10 | import { getAbsorbed } from "../core/features.ts"; | |
| 11 | import { runNow as armNow } from "../core/timers.ts"; | |
| 10 | 12 | |
| 11 | 13 | export const RUN_REFUSALS = { |
| 12 | 14 | not_absorbed: "Absorb the app before running its tools.", |
| 13 | 15 | not_scheduled: "That tool is not on the app's schedule.", |
| 14 | asleep: "The app's production is not running; it is not woken for a run. Redeploy it first.", | |
| 15 | in_flight: "That tool is already running.", | |
| 16 | 16 | } as const; |
| 17 | 17 | |
| 18 | /** Call `tool` now and say how it went; `ok` is false for a refusal or a failed call. */ | |
| 19 | export async function runNow(db: Database, appId: string, tool: string, reach: Reach, how: How = "page"): Promise<{ ok: boolean; message: string }> { | |
| 20 | const fired = await fireOne(db, appId, tool, reach, Date.now, how); | |
| 21 | if (!fired.ok) return { ok: false, message: RUN_REFUSALS[fired.why] }; | |
| 22 | const fire = lastFires(db, appId).find((f) => f.tool === tool); | |
| 23 | return fire?.ok === false ? { ok: false, message: `${tool} ran and failed: ${fire.error ?? "failed"}.` } : { ok: true, message: `${tool} ran.` }; | |
| 18 | /** | |
| 19 | * Run `tool` now and say so; `ok` is false for a refusal. How the run went is on the schedule's | |
| 20 | * line once it has. | |
| 21 | */ | |
| 22 | export function runNow(db: Database, appId: string, tool: string, now: number = Date.now()): { ok: boolean; message: string } { | |
| 23 | if (getAbsorbed(db, appId) === undefined) return { ok: false, message: RUN_REFUSALS.not_absorbed }; | |
| 24 | if (!armNow(db, appId, tool, "production", now)) return { ok: false, message: RUN_REFUSALS.not_scheduled }; | |
| 25 | return { ok: true, message: `${tool} is running now; its line shows how it went in a moment.` }; | |
| 24 | 26 | } |
src/apps/feature_schedule.ts +48 −171
| @@ -1,75 +1,27 @@ | ||
| 1 | 1 | /** |
| 2 | * An absorbed app's declared schedule, run: on a tick the forge calls each due | |
| 3 | * tool through the same loopback call the `features` tool uses | |
| 4 | * (`callTool`), with no model and no crew. The last fire of each (app, tool) is | |
| 5 | * kept in `feature_fire`. An app that is not running is skipped, never woken; | |
| 6 | * an app that is not absorbed is never called. | |
| 2 | * A feature's scheduled tools, read (#13533). The forge does not run them: a feature's `schedule` | |
| 3 | * is sugar for `schedules` (`feature.ts` `scheduleSpecs`), so the timer loop (`timer_fire.ts`) | |
| 4 | * fires, retries and records them, and the app's Schedules row, Run now, pause, the overdue watch | |
| 5 | * and the health count are the ones every schedule has. What is here is the status line the | |
| 6 | * features page and the terminal print from those timers' runs, and `runRecorded`, the one way a | |
| 7 | * person's or an agent's own call of a tool (the `features` tool) is made and kept in | |
| 8 | * `feature_fire`. | |
| 7 | 9 | */ |
| 8 | 10 | |
| 9 | 11 | import type { Database } from "bun:sqlite"; |
| 10 | import type { App } from "../app.ts"; | |
| 11 | import { declaredFeature } from "../actions/features.ts"; | |
| 12 | import { getAbsorbed } from "../core/features.ts"; | |
| 13 | import { lastFires, lastScheduled, recordFire, scheduleSince, type Fired, type How, type Run } from "../core/feature_fires.ts"; | |
| 12 | import { recordFire, type Fired, type How, type Run } from "../core/feature_fires.ts"; | |
| 14 | 13 | import { err, type Result } from "../kernel/result.ts"; |
| 15 | import { lastSlot, localLabel, nextSlot } from "./feature_clock.ts"; | |
| 16 | import { listApps, type AppRecord } from "../core/apps.ts"; | |
| 17 | import { getRepoById } from "../core/repos.ts"; | |
| 14 | import { localLabel, nextSlot } from "./feature_clock.ts"; | |
| 15 | import { runsOf, scheduleTimers, type TimerRun } from "../core/timers.ts"; | |
| 18 | 16 | import { callTool } from "./feature_tools.ts"; |
| 19 | 17 | import { SCHEDULE_EVERY, type ScheduleEntry } from "./feature.ts"; |
| 20 | import { appsRuntime } from "./runtime.ts"; | |
| 21 | import { during } from "../perf/jobs.ts"; | |
| 22 | ||
| 23 | /** | |
| 24 | * How long after its clock time a daily `at` may still fire (and say it was late); past it the day | |
| 25 | * is skipped. | |
| 26 | */ | |
| 27 | export const GRACE_MS = 3 * 3_600_000; | |
| 28 | /** | |
| 29 | * A fire this long after its time was not the minute's look but a forge that was away: it is said | |
| 30 | * to be late. | |
| 31 | */ | |
| 32 | export const LATE_MS = 2 * 60_000; | |
| 33 | ||
| 34 | /** | |
| 35 | * What a look decides for one entry: fire it (and how it is told), skip a day it missed, or | |
| 36 | * nothing. | |
| 37 | */ | |
| 38 | export type Verdict = { readonly do: "fire"; readonly how: "schedule" | "late" } | { readonly do: "skip"; readonly slot: number } | undefined; | |
| 39 | ||
| 40 | /** | |
| 41 | * Whether the entry is due. An interval counts from the later of its last fire (any way it was | |
| 42 | * started) and when the schedule began to count; a clock time is due once per local day, counted | |
| 43 | * from the later of the schedule's own last act and when it began, so a manual run does not move it | |
| 44 | * and a new schedule waits for its next time. `lastFired` is the last run of any kind, | |
| 45 | * `lastScheduled` the schedule's own last act. | |
| 46 | */ | |
| 47 | export function due( | |
| 48 | entry: ScheduleEntry, | |
| 49 | seen: { readonly lastFired?: number | undefined; readonly lastScheduled?: number | undefined; readonly since: number }, | |
| 50 | now: number, | |
| 51 | ): Verdict { | |
| 52 | if (entry.at !== undefined && entry.tz !== undefined) { | |
| 53 | const slot = lastSlot(entry.at, entry.tz, now); | |
| 54 | if (slot <= Math.max(seen.lastScheduled ?? 0, seen.since)) return undefined; | |
| 55 | if (now - slot > GRACE_MS) return { do: "skip", slot }; | |
| 56 | return { do: "fire", how: now - slot > LATE_MS ? "late" : "schedule" }; | |
| 57 | } | |
| 58 | const every = entry.everyMs ?? Infinity; | |
| 59 | return now - Math.max(seen.lastFired ?? 0, seen.since) >= every ? { do: "fire", how: "schedule" } : undefined; | |
| 60 | } | |
| 61 | ||
| 62 | /** The entry as the forge compares it from one look to the next. */ | |
| 63 | const specOf = (e: ScheduleEntry): string => (e.at !== undefined ? `at ${e.at} ${e.tz}` : `every ${e.everyMs}`); | |
| 64 | 18 | |
| 65 | 19 | const inFlight = new Set<string>(); |
| 66 | export const isRunning = (appId: string, tool: string): boolean => inFlight.has(`${appId}:${tool}`); | |
| 67 | 20 | |
| 68 | 21 | /** |
| 69 | * The one way a tool is run and recorded: the schedule, Run now on the page, the terminal's `r` and | |
| 70 | * the `features` tool (for a scheduled tool) all come through here, so each is the same row in the | |
| 71 | * tool's last fire and its history. `ran: false` means a call of the same tool is already running; | |
| 72 | * it is never doubled. | |
| 22 | * The one way a tool is called by hand and recorded: the `features` tool comes through here, so it | |
| 23 | * is a row in the tool's last fire and its history. `ran: false` means a call of the same tool is | |
| 24 | * already running; it is never doubled. | |
| 73 | 25 | */ |
| 74 | 26 | export async function runRecorded( |
| 75 | 27 | db: Database, |
| @@ -95,113 +47,37 @@ export async function runRecorded( | ||
| 95 | 47 | } |
| 96 | 48 | } |
| 97 | 49 | |
| 98 | /** What `fireApp` needs from the running forge: the production port when running, and the declared schedule. */ | |
| 99 | export interface Reach { | |
| 100 | port(): number | undefined; | |
| 101 | /** The feature as the app declares it now: its tools and its schedule. */ | |
| 102 | declared(): Promise<{ readonly tools: readonly string[]; readonly schedule: readonly ScheduleEntry[] }>; | |
| 103 | } | |
| 50 | /** | |
| 51 | * A timer run as a tool's run: a missed one is a skipped day, one begun long after its time is | |
| 52 | * late. | |
| 53 | */ | |
| 54 | const asRun = (r: TimerRun): Run => ({ | |
| 55 | tool: r.name, | |
| 56 | at: r.startedAt, | |
| 57 | ok: r.status === "ok", | |
| 58 | error: r.status === "ok" ? null : r.error, | |
| 59 | durationMs: r.durationMs, | |
| 60 | how: r.status === "missed" ? "skipped" : r.lateMs > 2 * 60_000 ? "late" : "schedule", | |
| 61 | }); | |
| 104 | 62 | |
| 105 | 63 | /** |
| 106 | * Look at every scheduled tool of one app if it is absorbed: call the due ones while it runs, note | |
| 107 | * the days missed; the tools called. | |
| 64 | * What the forge's timers hold for an app's scheduled tools: the newest run of each (as its last | |
| 65 | * fire), when each is next due, and every recent run, newest first within each tool. | |
| 108 | 66 | */ |
| 109 | export async function fireApp(db: Database, appId: string, reach: Reach, now: () => number = Date.now): Promise<string[]> { | |
| 110 | if (getAbsorbed(db, appId) === undefined) return []; | |
| 111 | const declared = await reach.declared(); | |
| 112 | const schedule = declared.schedule.filter((e) => declared.tools.includes(e.tool)); | |
| 113 | if (schedule.length === 0) return []; | |
| 114 | const last = new Map(lastFires(db, appId).map((f) => [f.tool, f.at])); | |
| 115 | const called: string[] = []; | |
| 116 | for (const entry of schedule) { | |
| 117 | const at = now(); | |
| 118 | const since = scheduleSince(db, appId, entry.tool, specOf(entry), at); | |
| 119 | const verdict = due(entry, { lastFired: last.get(entry.tool), lastScheduled: lastScheduled(db, appId, entry.tool), since }, at); | |
| 120 | if (verdict === undefined) continue; | |
| 121 | if (verdict.do === "skip") { | |
| 122 | const when = localLabel(verdict.slot, entry.tz!); | |
| 123 | recordFire(db, appId, { tool: entry.tool, at, ok: false, error: `skipped ${when}: the forge came back more than ${GRACE_MS / 3_600_000} h late`, durationMs: 0 }, "skipped"); | |
| 124 | continue; | |
| 125 | } | |
| 126 | const port = reach.port(); | |
| 127 | if (port === undefined) continue; | |
| 128 | const ran = await runRecorded(db, appId, entry.tool, port, {}, verdict.how, now); | |
| 129 | if (ran.ran) called.push(entry.tool); | |
| 67 | export function timerView(db: Database, appId: string, schedule: readonly ScheduleEntry[]): { fires: Fired[]; dues: Map<string, number>; history: Run[] } { | |
| 68 | const fires: Fired[] = []; | |
| 69 | const dues = new Map<string, number>(); | |
| 70 | const history: Run[] = []; | |
| 71 | for (const e of schedule) { | |
| 72 | const timer = scheduleTimers(db, appId).find((t) => t.name === e.tool && t.env === "production"); | |
| 73 | if (timer === undefined) continue; | |
| 74 | if (!timer.paused) dues.set(e.tool, timer.dueAt); | |
| 75 | const runs = runsOf(db, appId, e.tool, 30).filter((r) => r.env === "production" && r.status !== "paused").map(asRun); | |
| 76 | history.push(...runs); | |
| 77 | const last = runs[0]; | |
| 78 | if (last !== undefined) fires.push({ tool: last.tool, at: last.at, ok: last.ok, error: last.error, durationMs: last.durationMs }); | |
| 130 | 79 | } |
| 131 | return called; | |
| 132 | } | |
| 133 | ||
| 134 | /** Why a run now was not made. */ | |
| 135 | export type FireOneRefusal = "not_absorbed" | "not_scheduled" | "asleep" | "in_flight"; | |
| 136 | ||
| 137 | /** | |
| 138 | * Call one scheduled tool now, the way the schedule does, and record it as its | |
| 139 | * fire. A stopped or asleep app is refused, never woken; a call already running | |
| 140 | * for the same (app, tool) is refused, never doubled. | |
| 141 | */ | |
| 142 | export async function fireOne( | |
| 143 | db: Database, | |
| 144 | appId: string, | |
| 145 | tool: string, | |
| 146 | reach: Reach, | |
| 147 | now: () => number = Date.now, | |
| 148 | how: How = "page", | |
| 149 | ): Promise<{ ok: true } | { ok: false; why: FireOneRefusal }> { | |
| 150 | if (getAbsorbed(db, appId) === undefined) return { ok: false, why: "not_absorbed" }; | |
| 151 | if (isRunning(appId, tool)) return { ok: false, why: "in_flight" }; | |
| 152 | const port = reach.port(); | |
| 153 | if (port === undefined) return { ok: false, why: "asleep" }; | |
| 154 | const declared = await reach.declared().catch(() => undefined); | |
| 155 | if (declared === undefined || !declared.tools.includes(tool) || !declared.schedule.some((e) => e.tool === tool)) return { ok: false, why: "not_scheduled" }; | |
| 156 | const ran = await runRecorded(db, appId, tool, port, {}, how, now); | |
| 157 | return ran.ran ? { ok: true } : { ok: false, why: "in_flight" }; | |
| 158 | } | |
| 159 | ||
| 160 | /** What `fireApp` reaches for one app in the real runtime: only a production that is running, never woken. */ | |
| 161 | export function runtimeReach(app: App, record: AppRecord): Reach { | |
| 162 | return { | |
| 163 | port: () => { | |
| 164 | const status = appsRuntime(app)?.envStatus(record, "production"); | |
| 165 | return status?.state === "running" ? status.port : undefined; | |
| 166 | }, | |
| 167 | declared: async () => { | |
| 168 | const repo = getRepoById(app.db, record.repoId); | |
| 169 | if (repo === undefined) return { tools: [], schedule: [] }; | |
| 170 | const declared = await declaredFeature(app, repo); | |
| 171 | const spec = declared.ok ? declared.value : undefined; | |
| 172 | return { tools: (spec?.tools ?? []).map((t) => t.name), schedule: spec?.schedule ?? [] }; | |
| 173 | }, | |
| 174 | }; | |
| 175 | } | |
| 176 | ||
| 177 | /** Every app, one after another: a slow app delays the next, never overlaps itself. `reachOf` is the seam a test replaces. */ | |
| 178 | export async function fireDue( | |
| 179 | app: App, | |
| 180 | now: () => number = Date.now, | |
| 181 | reachOf: (record: AppRecord) => Reach = (record) => runtimeReach(app, record), | |
| 182 | ): Promise<void> { | |
| 183 | for (const record of listApps(app.db)) { | |
| 184 | try { | |
| 185 | await fireApp(app.db, record.id, reachOf(record), now); | |
| 186 | } catch (error) { | |
| 187 | app.log.warn("could not fire a scheduled tool", { app: record.slug, error: String(error) }); | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | const TICK_MS = 60_000; | |
| 193 | ||
| 194 | /** The timer. Off under `bun test` (NODE_ENV=test) so no sweep outlives a test; a test that wants it calls `fireDue`. */ | |
| 195 | export function startFeatureSchedule(app: App, tickMs: number = TICK_MS): { stop(): void } | undefined { | |
| 196 | if (process.env["NODE_ENV"] === "test") return undefined; | |
| 197 | let running = false; | |
| 198 | const tick = setInterval(() => { | |
| 199 | if (running) return; | |
| 200 | running = true; | |
| 201 | void during("feature-schedule", () => fireDue(app), undefined, app).finally(() => (running = false)); | |
| 202 | }, tickMs); | |
| 203 | tick.unref(); | |
| 204 | return { stop: () => clearInterval(tick) }; | |
| 80 | return { fires, dues, history }; | |
| 205 | 81 | } |
| 206 | 82 | |
| 207 | 83 | /** |
| @@ -233,19 +109,17 @@ function everyWord(everyMs: number): string { | ||
| 233 | 109 | } |
| 234 | 110 | |
| 235 | 111 | /** |
| 236 | * One status per schedule entry, in the app's order, joined to the last fire of its tool. `since` | |
| 237 | * is when each began to count. | |
| 112 | * One status per schedule entry, in the app's order, joined to the last run of its tool. `dues` | |
| 113 | * is when the timer of each is next due, where the forge holds one. | |
| 238 | 114 | */ |
| 239 | 115 | export function scheduleStatus( |
| 240 | 116 | schedule: readonly ScheduleEntry[], |
| 241 | 117 | fires: readonly Fired[], |
| 242 | 118 | now: number = Date.now(), |
| 243 | since: ReadonlyMap<string, number> = new Map(), | |
| 119 | dues: ReadonlyMap<string, number> = new Map(), | |
| 244 | 120 | ): ScheduleStatus[] { |
| 245 | 121 | return schedule.map((e) => { |
| 246 | 122 | const fire = fires.find((f) => f.tool === e.tool); |
| 247 | const begun = since.get(e.tool); | |
| 248 | const base = Math.max(fire?.at ?? 0, begun ?? 0); | |
| 249 | 123 | const clock = e.at !== undefined && e.tz !== undefined; |
| 250 | 124 | const status: Omit<ScheduleStatus, "line"> = { |
| 251 | 125 | tool: e.tool, |
| @@ -256,7 +130,7 @@ export function scheduleStatus( | ||
| 256 | 130 | ok: fire?.ok ?? null, |
| 257 | 131 | durationMs: fire?.durationMs ?? null, |
| 258 | 132 | error: fire === undefined || fire.ok ? null : (fire.error ?? "failed"), |
| 259 | nextAt: clock ? nextSlot(e.at!, e.tz!, now) : base === 0 ? null : base + (e.everyMs ?? 0), | |
| 133 | nextAt: dues.get(e.tool) ?? (clock ? nextSlot(e.at!, e.tz!, now) : fire === undefined ? null : fire.at + (e.everyMs ?? 0)), | |
| 260 | 134 | }; |
| 261 | 135 | return { ...status, line: scheduleLine(status) }; |
| 262 | 136 | }); |
| @@ -278,7 +152,10 @@ export const runLine = (r: Run): string => | ||
| 278 | 152 | /** `10:02`: the plain-text clock (UTC) of the line the tool and the terminal print. */ |
| 279 | 153 | export const scheduleClock = (ms: number): string => new Date(ms).toISOString().slice(11, 16); |
| 280 | 154 | |
| 281 | /** A piece of the line after the head: words, the last or next moment (ms) the reader formats, or the sentence of a failure. */ | |
| 155 | /** | |
| 156 | * A piece of the line after the head: words, the last or next moment (ms) the reader formats, or | |
| 157 | * the sentence of a failure. | |
| 158 | */ | |
| 282 | 159 | export type SchedulePiece = string | { readonly last: number } | { readonly next: number } | { readonly error: string }; |
| 283 | 160 | |
| 284 | 161 | /** |
src/apps/manifest.ts +9 −1
| @@ -20,6 +20,7 @@ import { isLaunch, LAUNCHES, type Launch } from "../git/builds.ts"; | ||
| 20 | 20 | import { parseAdapter, type AdapterSpec } from "./adapter.ts"; |
| 21 | 21 | import { parseToolchain, type ToolchainPin } from "./toolchain.ts"; |
| 22 | 22 | import { parseSchedules, type ScheduleSpec } from "./schedule_spec.ts"; |
| 23 | import { parseFeature, scheduleSpecs } from "./feature.ts"; | |
| 23 | 24 | |
| 24 | 25 | export interface AppManifest { |
| 25 | 26 | /** The command that serves on `$PORT`. Run through `sh -c`, `exec`-ed. */ |
| @@ -213,6 +214,13 @@ export function parseManifest( | ||
| 213 | 214 | return invalid(`forge.json: adapter.source.hosts names ${unopened}, which egress does not list`); |
| 214 | 215 | const schedules = parseSchedules(parsed.value["schedules"]); |
| 215 | 216 | if (!schedules.ok) return invalid(schedules.error); |
| 217 | // A feature's scheduled tools are schedules like any other (#13533). A feature block that does | |
| 218 | // not parse is the absorb page's to report, as before: it declares none here. | |
| 219 | const feature = parseFeature(parsed.value); | |
| 220 | const scheduled = feature.ok && feature.value !== undefined ? scheduleSpecs(feature.value.schedule) : []; | |
| 221 | const clash = scheduled.find((f) => schedules.value.some((s) => s.name === f.name)); | |
| 222 | if (clash !== undefined) return invalid(`forge.json: schedules already has "${clash.name}", which feature.schedule also runs`); | |
| 223 | const allSchedules = [...schedules.value, ...scheduled]; | |
| 216 | 224 | const kind = app["kind"]; |
| 217 | 225 | if (kind !== undefined && kind !== "web" && kind !== "service") |
| 218 | 226 | return invalid('forge.json: app.kind must be "web" or "service"'); |
| @@ -249,7 +257,7 @@ export function parseManifest( | ||
| 249 | 257 | ...(needs.value.length === 0 ? {} : { env: needs.value }), |
| 250 | 258 | ...(adapter.value === undefined ? {} : { adapter: adapter.value }), |
| 251 | 259 | ...(toolchain.value === undefined ? {} : { toolchain: toolchain.value }), |
| 252 | ...(schedules.value.length === 0 ? {} : { schedules: schedules.value }), | |
| 260 | ...(allSchedules.length === 0 ? {} : { schedules: allSchedules }), | |
| 253 | 261 | }); |
| 254 | 262 | } |
| 255 | 263 |
src/apps/schedule_spec.ts +7
| @@ -24,6 +24,13 @@ export interface ScheduleSpec { | ||
| 24 | 24 | readonly retries: number; |
| 25 | 25 | /** ms a missed run may still fire late; 0 is "none". */ |
| 26 | 26 | readonly catchUpMs: number; |
| 27 | /** | |
| 28 | * The JSON text each call carries; only a feature's tool call (`feature.ts` `scheduleSpecs`) has | |
| 29 | * one. | |
| 30 | */ | |
| 31 | readonly body?: string | undefined; | |
| 32 | /** The `content-type` the body goes with; only set with a body. */ | |
| 33 | readonly contentType?: string | undefined; | |
| 27 | 34 | } |
| 28 | 35 | |
| 29 | 36 | export const SCHEDULES_MAX = 20; |
src/apps/timer_fire.ts +25 −6
| @@ -33,6 +33,11 @@ export interface Reach { | ||
| 33 | 33 | /** The key the app verifies the forge with. */ |
| 34 | 34 | identityKey(appId: string): string; |
| 35 | 35 | fetch: typeof fetch; |
| 36 | /** | |
| 37 | * Whether the forge may call the app with this timer at all; a timer it may not call is dropped | |
| 38 | * unfired, with no run recorded. Absent, every timer is allowed. | |
| 39 | */ | |
| 40 | permits?(app: AppRecord, t: Timer): boolean; | |
| 36 | 41 | } |
| 37 | 42 | |
| 38 | 43 | export const BACKOFF_MS = [30_000, 120_000, 600_000, 1_800_000, 3_600_000]; |
| @@ -49,7 +54,10 @@ export function idempotencyKey(slug: string, t: Pick<Timer, "name" | "key" | "sc | ||
| 49 | 54 | return t.kind === "once" && t.name === "" ? `${slug}:${t.key}` : `${slug}:${t.name}:${iso(t.scheduledAt)}`; |
| 50 | 55 | } |
| 51 | 56 | |
| 52 | /** The headers of one delivery. The signature covers `forge-timer|<idempotency key>|timer|<expiry ms>`. */ | |
| 57 | /** | |
| 58 | * The headers of one delivery. The signature covers | |
| 59 | * `forge-timer|<idempotency key>|timer|<expiry ms>`. | |
| 60 | */ | |
| 53 | 61 | export function deliveryHeaders(key: string, t: Timer, attempt: number, expires: number, idemKey: string): Record<string, string> { |
| 54 | 62 | const identity = `forge-timer|${idemKey}|timer|${expires}`; |
| 55 | 63 | return { |
| @@ -59,7 +67,7 @@ export function deliveryHeaders(key: string, t: Timer, attempt: number, expires: | ||
| 59 | 67 | "idempotency-key": idemKey, |
| 60 | 68 | "x-forge-identity": identity, |
| 61 | 69 | "x-forge-signature": createHmac("sha256", key).update(identity).digest("base64url"), |
| 62 | ...(t.body === null ? {} : { "content-type": "application/octet-stream" }), | |
| 70 | ...(t.body === null ? {} : { "content-type": t.contentType ?? "application/octet-stream" }), | |
| 63 | 71 | }; |
| 64 | 72 | } |
| 65 | 73 | |
| @@ -73,7 +81,10 @@ function failureText(head: string): string | undefined { | ||
| 73 | 81 | } |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | /** The headers of a request the forge makes of an app outside a timer (listing or steering its jobs): the same signature, an idempotency key of its own. */ | |
| 84 | /** | |
| 85 | * The headers of a request the forge makes of an app outside a timer (listing or steering its | |
| 86 | * jobs): the same signature, an idempotency key of its own. | |
| 87 | */ | |
| 77 | 88 | export function controlHeaders(key: string, now: number): Record<string, string> { |
| 78 | 89 | const idem = `control:${crypto.randomUUID()}`; |
| 79 | 90 | const identity = `forge-timer|${idem}|timer|${now + 60_000}`; |
| @@ -111,7 +122,8 @@ async function attemptOnce(reach: Reach, app: AppRecord, t: Timer, attempt: numb | ||
| 111 | 122 | }); |
| 112 | 123 | const head = await readHead(res); |
| 113 | 124 | if (res.status >= 200 && res.status < 300) return { ok: true, status: res.status, response: head, error: null }; |
| 114 | // An app that says `x-forge-no-retry` has decided: delivering again would only repeat it (a durable job that failed, #11596). | |
| 125 | // An app that says `x-forge-no-retry` has decided: delivering again would only repeat it (a | |
| 126 | // durable job that failed, #11596). | |
| 115 | 127 | if (res.headers.get("x-forge-no-retry") === "1") return { ok: false, final: true, status: res.status, response: head, error: failureText(head) ?? `the app answered ${res.status}` }; |
| 116 | 128 | return { ok: false, status: res.status, response: head, error: `the app answered ${res.status}` }; |
| 117 | 129 | } catch (e) { |
| @@ -129,7 +141,10 @@ function nextOf(t: Timer, after: number): number | undefined { | ||
| 129 | 141 | return c.ok ? nextRun(c.value, t.timezone, after) : undefined; |
| 130 | 142 | } |
| 131 | 143 | |
| 132 | /** Whether the schedule has failed on each of the last five days with no success between (the newest runs first). */ | |
| 144 | /** | |
| 145 | * Whether the schedule has failed on each of the last five days with no success between (the newest | |
| 146 | * runs first). | |
| 147 | */ | |
| 133 | 148 | export function failingDays(runs: readonly TimerRun[]): number { |
| 134 | 149 | const days = new Set<string>(); |
| 135 | 150 | for (const r of runs) { |
| @@ -149,9 +164,13 @@ export async function fireTimer(db: Database, reach: Reach, t: Timer, now: () => | ||
| 149 | 164 | if (app === undefined) return void db.query("DELETE FROM timer WHERE id = ?1").run(t.id); |
| 150 | 165 | const started = now(); |
| 151 | 166 | const lateMs = Math.max(0, started - t.scheduledAt); |
| 167 | // The forge's own consent to call the app has gone (an app un-absorbed): the timer ends, | |
| 168 | // unfired. | |
| 169 | if (reach.permits?.(app, t) === false) return finishTimer(db, t, t.kind === "schedule" ? nextOf(t, Math.max(t.scheduledAt, now())) : undefined); | |
| 152 | 170 | const idem = idempotencyKey(app.slug, t); |
| 153 | 171 | const base = { appId: t.appId, env: t.env, name: t.name === "" ? t.key : t.name, idemKey: idem, scheduledAt: t.scheduledAt }; |
| 154 | // A schedule that was away past its window skips the runs it missed (one record), and keeps the ones inside it. | |
| 172 | // A schedule that was away past its window skips the runs it missed (one record), and keeps the | |
| 173 | // ones inside it. | |
| 155 | 174 | if (t.kind === "schedule" && t.attempt === 0 && lateMs > Math.max(t.catchUpMs, LATE_AFTER_MS)) { |
| 156 | 175 | const window = t.catchUpMs === 0 ? "its catch-up is none" : `it is past its ${Math.round(t.catchUpMs / 60_000)} min catch-up window`; |
| 157 | 176 | recordRun(db, { |
src/apps/timer_service.ts +27 −6
| @@ -7,12 +7,15 @@ | ||
| 7 | 7 | * Off under `bun test` (NODE_ENV=test) so no loop outlives a test. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import type { Database } from "bun:sqlite"; | |
| 10 | 11 | import type { App } from "../app.ts"; |
| 11 | 12 | import { listApps, type AppRecord } from "../core/apps.ts"; |
| 13 | import { getAbsorbed } from "../core/features.ts"; | |
| 12 | 14 | import { getRepoById } from "../core/repos.ts"; |
| 13 | import { dropGoneEnvs, syncSchedules, syncedCommit } from "../core/timers.ts"; | |
| 15 | import { dropGoneEnvs, syncSchedules, syncedCommit, type Timer } from "../core/timers.ts"; | |
| 14 | 16 | import { readFileAt } from "./checkout.ts"; |
| 15 | 17 | import { identityKey } from "./proxy.ts"; |
| 18 | import { toolPath } from "./feature.ts"; | |
| 16 | 19 | import { parseManifest } from "./manifest.ts"; |
| 17 | 20 | import { appsRuntime } from "./runtime.ts"; |
| 18 | 21 | import type { EnvName } from "./data.ts"; |
| @@ -20,6 +23,19 @@ import { fireDueTimers, type Reach } from "./timer_fire.ts"; | ||
| 20 | 23 | import { repoDir } from "../git/repos.ts"; |
| 21 | 24 | import { during } from "../perf/jobs.ts"; |
| 22 | 25 | |
| 26 | /** | |
| 27 | * Bumped when what a commit's manifest means for schedules changes, so every environment is read | |
| 28 | * again (2: a feature's `schedule` is a schedule, #13533). | |
| 29 | */ | |
| 30 | const SYNC_REVISION = "#2"; | |
| 31 | ||
| 32 | /** | |
| 33 | * A feature's tool is called only while an admin has absorbed the app (docs/features/absorb.md). | |
| 34 | */ | |
| 35 | export function mayCall(db: Database, record: AppRecord, t: Pick<Timer, "path">): boolean { | |
| 36 | return !t.path.startsWith(toolPath("")) || getAbsorbed(db, record.id) !== undefined; | |
| 37 | } | |
| 38 | ||
| 23 | 39 | export function runtimeReach(app: App): Reach { |
| 24 | 40 | return { |
| 25 | 41 | async ensure(record: AppRecord, env) { |
| @@ -31,6 +47,7 @@ export function runtimeReach(app: App): Reach { | ||
| 31 | 47 | }, |
| 32 | 48 | identityKey: (appId) => identityKey(app.config.dataDir, appId), |
| 33 | 49 | fetch, |
| 50 | permits: (record, t) => mayCall(app.db, record, t), | |
| 34 | 51 | }; |
| 35 | 52 | } |
| 36 | 53 | |
| @@ -43,13 +60,15 @@ export async function syncAll(app: App, now: () => number = Date.now): Promise<v | ||
| 43 | 60 | for (const st of rt.status(record)) { |
| 44 | 61 | if (st.env !== "production" && !st.env.startsWith("pull-")) continue; |
| 45 | 62 | live.add(st.env); |
| 46 | if (syncedCommit(app.db, record.id, st.env) === st.commitOid) continue; | |
| 63 | const sync = `${st.commitOid}${SYNC_REVISION}`; | |
| 64 | if (syncedCommit(app.db, record.id, st.env) === sync) continue; | |
| 47 | 65 | const repo = getRepoById(app.db, record.repoId); |
| 48 | 66 | if (repo === undefined) continue; |
| 49 | 67 | const text = await readFileAt(repoDir(app.reposRoot, repo.ownerName, repo.name), st.commitOid, "forge.json"); |
| 50 | 68 | const manifest = text === undefined ? undefined : parseManifest(text, undefined); |
| 51 | // A forge.json that does not parse is the deploy's to report; it declares no schedule, so the rows are dropped. | |
| 52 | syncSchedules(app.db, record.id, st.env, manifest?.ok ? (manifest.value.schedules ?? []) : [], st.commitOid, now()); | |
| 69 | // A forge.json that does not parse is the deploy's to report; it declares no schedule, so the | |
| 70 | // rows are dropped. | |
| 71 | syncSchedules(app.db, record.id, st.env, manifest?.ok ? (manifest.value.schedules ?? []) : [], sync, now()); | |
| 53 | 72 | } |
| 54 | 73 | dropGoneEnvs(app.db, record.id, live); |
| 55 | 74 | } |
| @@ -61,9 +80,11 @@ export function startTimers(app: App, tickMs = 1000): { stop(): void } | undefin | ||
| 61 | 80 | let firing = false; |
| 62 | 81 | let syncing = false; |
| 63 | 82 | let last = 0; |
| 64 | // One pass: fire what is due, and once a minute read what the commits declare. A pass still running is not doubled. | |
| 83 | // One pass: fire what is due, and once a minute read what the commits declare. A pass still | |
| 84 | // running is not doubled. | |
| 65 | 85 | const cycle = async (): Promise<void> => { |
| 66 | // Before the apps runtime is up a due timer would only burn an attempt: it waits, a row, for the next tick. | |
| 86 | // Before the apps runtime is up a due timer would only burn an attempt: it waits, a row, for | |
| 87 | // the next tick. | |
| 67 | 88 | if (appsRuntime(app) === undefined) return; |
| 68 | 89 | if (!syncing && Date.now() - last >= 60_000) { |
| 69 | 90 | syncing = true; |
src/core/feature_fires.ts +3 −33
| @@ -14,12 +14,10 @@ export interface Fired { | ||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | * How a run was started: the clock, the clock after its time had passed, a person's hand (`page`, | |
| 18 | * `terminal`, `mcp`), or a day the clock missed. | |
| 17 | * How a run was shown: the clock, the clock after its time had passed, a call by hand through the | |
| 18 | * `features` tool (`mcp`, the only one `feature_fire` records now), or a day the clock missed. | |
| 19 | 19 | */ |
| 20 | export type How = "schedule" | "late" | "page" | "terminal" | "mcp" | "skipped"; | |
| 21 | /** The hows that are the schedule's own: they, and only they, move a clock-time schedule. */ | |
| 22 | export const SCHEDULED_HOWS: readonly How[] = ["schedule", "late", "skipped"]; | |
| 20 | export type How = "schedule" | "late" | "mcp" | "skipped"; | |
| 23 | 21 | |
| 24 | 22 | export interface Run extends Fired { |
| 25 | 23 | readonly how: How; |
| @@ -66,34 +64,6 @@ export function runHistory(db: Database, appId: string): Run[] { | ||
| 66 | 64 | .map((r) => ({ tool: r.tool, at: r.at, ok: r.ok === 1, error: r.error, durationMs: r.duration_ms, how: r.how as How })); |
| 67 | 65 | } |
| 68 | 66 | |
| 69 | /** | |
| 70 | * When the schedule itself last acted on the tool (a fire, a late fire, a skipped day), or | |
| 71 | * undefined. | |
| 72 | */ | |
| 73 | export function lastScheduled(db: Database, appId: string, tool: string): number | undefined { | |
| 74 | const marks = SCHEDULED_HOWS.map(() => "?").join(","); | |
| 75 | return db | |
| 76 | .query<{ at: number | null }, string[]>(`SELECT MAX(at) AS at FROM feature_run WHERE app_id = ?1 AND tool = ?2 AND how IN (${marks})`) | |
| 77 | .get(appId, tool, ...SCHEDULED_HOWS)?.at ?? undefined; | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * When the tool's schedule, as `spec`, began to count: the first look at it, or at its change. A | |
| 82 | * schedule | |
| 83 | * the forge has not seen starts now, so it fires at its next due time and not at once. | |
| 84 | */ | |
| 85 | export function scheduleSince(db: Database, appId: string, tool: string, spec: string, now: number): number { | |
| 86 | const row = db.query<{ spec: string; since: number }, [string, string]>("SELECT spec, since FROM feature_schedule_seen WHERE app_id = ?1 AND tool = ?2").get(appId, tool); | |
| 87 | if (row !== null && row.spec === spec) return row.since; | |
| 88 | db.query("INSERT OR REPLACE INTO feature_schedule_seen (app_id, tool, spec, since) VALUES (?1, ?2, ?3, ?4)").run(appId, tool, spec, now); | |
| 89 | return now; | |
| 90 | } | |
| 91 | ||
| 92 | /** When each of the app's scheduled tools began to count, by tool. */ | |
| 93 | export function scheduleSeen(db: Database, appId: string): Map<string, number> { | |
| 94 | return new Map(db.query<{ tool: string; since: number }, [string]>("SELECT tool, since FROM feature_schedule_seen WHERE app_id = ?1").all(appId).map((r) => [r.tool, r.since])); | |
| 95 | } | |
| 96 | ||
| 97 | 67 | /** Forget the app's fires: it is no longer absorbed, or no longer exists. */ |
| 98 | 68 | export function clearFires(db: Database, appId: string): void { |
| 99 | 69 | for (const t of ["feature_fire", "feature_run", "feature_schedule_seen"]) db.query(`DELETE FROM ${t} WHERE app_id = ?1`).run(appId); |
src/core/migration-pins/20261116090000-timer-content-type.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 82986d03269a46c42adc5f178716d039a957b0b9ebe60211755c43518baadad3 |
src/core/migrations/20261116090000-timer-content-type.ts added +6
| @@ -0,0 +1,6 @@ | ||
| 1 | // A timer's body goes with the content type it was armed for (#13533): a feature's tool call is | |
| 2 | // JSON, an app's own one-shot is octet-stream. Null reads as octet-stream, as every row before it | |
| 3 | // was sent. One nullable column. Additive. | |
| 4 | export default ` | |
| 5 | ALTER TABLE timer ADD COLUMN content_type TEXT; | |
| 6 | `; |
src/core/timers.ts +14 −9
| @@ -24,6 +24,8 @@ export interface Timer { | ||
| 24 | 24 | readonly key: string; |
| 25 | 25 | readonly path: string; |
| 26 | 26 | readonly body: Uint8Array | null; |
| 27 | /** What the body is; null reads as octet-stream. */ | |
| 28 | readonly contentType: string | null; | |
| 27 | 29 | readonly cron: string | null; |
| 28 | 30 | readonly timezone: string | null; |
| 29 | 31 | readonly timeoutMs: number; |
| @@ -68,6 +70,7 @@ const toTimer = (r: Row): Timer => ({ | ||
| 68 | 70 | key: r["key"] as string, |
| 69 | 71 | path: r["path"] as string, |
| 70 | 72 | body: (r["body"] as Uint8Array | null) ?? null, |
| 73 | contentType: (r["content_type"] as string | null) ?? null, | |
| 71 | 74 | cron: (r["cron"] as string | null) ?? null, |
| 72 | 75 | timezone: (r["timezone"] as string | null) ?? null, |
| 73 | 76 | timeoutMs: r["timeout_ms"] as number, |
| @@ -181,17 +184,18 @@ export function syncSchedules(db: Database, appId: string, env: TimerEnv, specs: | ||
| 181 | 184 | const cron = parseCron(s.cron); |
| 182 | 185 | if (!cron.ok) continue; |
| 183 | 186 | const existing = scheduleTimer(db, appId, s.name, env); |
| 187 | const body = s.body === undefined ? null : new TextEncoder().encode(s.body); | |
| 184 | 188 | if (existing !== undefined && existing.cron === s.cron && existing.timezone === s.timezone) { |
| 185 | db.query("UPDATE timer SET path = ?2, timeout_ms = ?3, retries = ?4, catch_up_ms = ?5 WHERE id = ?1").run(existing.id, s.path, s.timeoutMs, s.retries, s.catchUpMs); | |
| 189 | db.query("UPDATE timer SET path = ?2, timeout_ms = ?3, retries = ?4, catch_up_ms = ?5, body = ?6, content_type = ?7 WHERE id = ?1").run(existing.id, s.path, s.timeoutMs, s.retries, s.catchUpMs, body, body === null ? null : (s.contentType ?? null)); | |
| 186 | 190 | continue; |
| 187 | 191 | } |
| 188 | 192 | const next = nextRun(cron.value, s.timezone, now); |
| 189 | 193 | if (next === undefined) continue; |
| 190 | 194 | if (existing !== undefined) db.query("DELETE FROM timer WHERE id = ?1").run(existing.id); |
| 191 | 195 | db.query( |
| 192 | `INSERT INTO timer (app_id, env, kind, name, key, path, cron, timezone, timeout_ms, retries, catch_up_ms, scheduled_at, due_at, paused, created_at) | |
| 193 | VALUES (?1, ?2, 'schedule', ?3, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?10, ?11, ?12)`, | |
| 194 | ).run(appId, env, s.name, s.path, s.cron, s.timezone, s.timeoutMs, s.retries, s.catchUpMs, next, existing?.paused ? 1 : 0, now); | |
| 196 | `INSERT INTO timer (app_id, env, kind, name, key, path, cron, timezone, timeout_ms, retries, catch_up_ms, scheduled_at, due_at, paused, created_at, body, content_type) | |
| 197 | VALUES (?1, ?2, 'schedule', ?3, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?10, ?11, ?12, ?13, ?14)`, | |
| 198 | ).run(appId, env, s.name, s.path, s.cron, s.timezone, s.timeoutMs, s.retries, s.catchUpMs, next, existing?.paused ? 1 : 0, now, body, body === null ? null : (s.contentType ?? null)); | |
| 195 | 199 | } |
| 196 | 200 | db.query("INSERT OR REPLACE INTO timer_sync (app_id, env, commit_oid) VALUES (?1, ?2, ?3)").run(appId, env, commit); |
| 197 | 201 | })(); |
| @@ -207,6 +211,7 @@ export interface Arm { | ||
| 207 | 211 | readonly path: string; |
| 208 | 212 | readonly at: number; |
| 209 | 213 | readonly body?: Uint8Array | undefined; |
| 214 | readonly contentType?: string | undefined; | |
| 210 | 215 | /** A manual run of a schedule carries its name; an app's own one-shot has none. */ |
| 211 | 216 | readonly name?: string | undefined; |
| 212 | 217 | readonly timeoutMs: number; |
| @@ -216,11 +221,11 @@ export interface Arm { | ||
| 216 | 221 | /** Arm a one-shot. Arming a key again replaces the pending timer (one per key). */ |
| 217 | 222 | export function armTimer(db: Database, a: Arm, now: number): void { |
| 218 | 223 | db.query( |
| 219 | `INSERT INTO timer (app_id, env, kind, name, key, path, body, timeout_ms, retries, catch_up_ms, scheduled_at, due_at, created_at) | |
| 220 | VALUES (?1, ?2, 'once', ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?10, ?11) | |
| 221 | ON CONFLICT (app_id, env, kind, name, key) DO UPDATE SET path = excluded.path, body = excluded.body, timeout_ms = excluded.timeout_ms, | |
| 224 | `INSERT INTO timer (app_id, env, kind, name, key, path, body, timeout_ms, retries, catch_up_ms, scheduled_at, due_at, created_at, content_type) | |
| 225 | VALUES (?1, ?2, 'once', ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?10, ?11, ?12) | |
| 226 | ON CONFLICT (app_id, env, kind, name, key) DO UPDATE SET path = excluded.path, body = excluded.body, content_type = excluded.content_type, timeout_ms = excluded.timeout_ms, | |
| 222 | 227 | retries = excluded.retries, scheduled_at = excluded.scheduled_at, due_at = excluded.due_at, attempt = 0`, |
| 223 | ).run(a.appId, a.env, a.name ?? "", a.key, a.path, a.body ?? null, a.timeoutMs, a.retries, 365 * 86_400_000, a.at, now); | |
| 228 | ).run(a.appId, a.env, a.name ?? "", a.key, a.path, a.body ?? null, a.timeoutMs, a.retries, 365 * 86_400_000, a.at, now, a.body === undefined ? null : (a.contentType ?? null)); | |
| 224 | 229 | } |
| 225 | 230 | |
| 226 | 231 | /** Cancel an app's pending one-shot by key; whether there was one. */ |
| @@ -318,6 +323,6 @@ export function dropGoneEnvs(db: Database, appId: string, live: ReadonlySet<stri | ||
| 318 | 323 | export function runNow(db: Database, appId: string, name: string, env: TimerEnv, now: number): boolean { |
| 319 | 324 | const t = scheduleTimer(db, appId, name, env); |
| 320 | 325 | if (t === undefined) return false; |
| 321 | armTimer(db, { appId, env, key: `run-${now}`, name, path: t.path, at: now, timeoutMs: t.timeoutMs, retries: t.retries }, now); | |
| 326 | armTimer(db, { appId, env, key: `run-${now}`, name, path: t.path, body: t.body ?? undefined, contentType: t.contentType ?? undefined, at: now, timeoutMs: t.timeoutMs, retries: t.retries }, now); | |
| 322 | 327 | return true; |
| 323 | 328 | } |
src/http/server.ts −3
| @@ -581,7 +581,6 @@ import { childHost } from "../spawn/door.ts"; | ||
| 581 | 581 | import { during } from "../perf/jobs.ts"; |
| 582 | 582 | import { classOf, countryOf, repoOf, signatureOf } from "../perf/traffic.ts"; |
| 583 | 583 | import { createTrafficWriter } from "../perf/traffic_store.ts"; |
| 584 | import { startFeatureSchedule } from "../apps/feature_schedule.ts"; | |
| 585 | 584 | import { startTimers } from "../apps/timer_service.ts"; |
| 586 | 585 | import { startPullSchedule } from "../apps/pull_schedule.ts"; |
| 587 | 586 | import { startGithubPull } from "../perf/github_traffic.ts"; |
| @@ -2771,7 +2770,6 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2771 | 2770 | // when a token is set. |
| 2772 | 2771 | const viewWriter = createViewWriter(app.db, app.log); |
| 2773 | 2772 | const githubPull = startGithubPull(app.db, app.log); |
| 2774 | const featureSchedule = startFeatureSchedule(app); | |
| 2775 | 2773 | const timers = startTimers(app); |
| 2776 | 2774 | const pullSchedule = startPullSchedule(app); |
| 2777 | 2775 | const readWriter = createReadWriter(app.db, readLedger, app.log); |
| @@ -3149,7 +3147,6 @@ export async function serve(app: App): Promise<Servers> { | ||
| 3149 | 3147 | trafficWriter.stop(); |
| 3150 | 3148 | viewWriter.stop(); |
| 3151 | 3149 | githubPull?.stop(); |
| 3152 | featureSchedule?.stop(); | |
| 3153 | 3150 | timers?.stop(); |
| 3154 | 3151 | pullSchedule?.stop(); |
| 3155 | 3152 | readWriter.stop(); |
src/tui/screens/featurefeed.ts +2 −2
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { runNow } from "../../apps/feature_run.ts"; |
| 10 | import { runLine, runtimeReach } from "../../apps/feature_schedule.ts"; | |
| 10 | import { runLine } from "../../apps/feature_schedule.ts"; | |
| 11 | 11 | import { getAppBySlug } from "../../core/apps.ts"; |
| 12 | 12 | import { absorbedForNav, feedOf, getFeature, type NavFeature } from "../../actions/features.ts"; |
| 13 | 13 | import type { Fired, Run } from "../../core/feature_fires.ts"; |
| @@ -48,7 +48,7 @@ export function featureFeedScreen(f: Session, ctx: Ctx, feature: NavFeature): Sc | ||
| 48 | 48 | running = true; |
| 49 | 49 | try { |
| 50 | 50 | for (const tool of fires.map((x) => x.tool)) { |
| 51 | const said = await runNow(f.app.db, record.id, tool, runtimeReach(f.app, record), "terminal"); | |
| 51 | const said = runNow(f.app.db, record.id, tool); | |
| 52 | 52 | ctx.toast(said.message, said.ok ? "success" : "danger"); |
| 53 | 53 | } |
| 54 | 54 | const found = await getFeature(f.app, f.user, feature.slug); |
src/web/pages/features.ts +9 −6
| @@ -20,9 +20,9 @@ import { | ||
| 20 | 20 | provenanceFacts, |
| 21 | 21 | } from "../../actions/features.ts"; |
| 22 | 22 | import { runNow } from "../../apps/feature_run.ts"; |
| 23 | import { runtimeReach, scheduleMeta, scheduleWhen, type Reach } from "../../apps/feature_schedule.ts"; | |
| 23 | import { scheduleMeta, scheduleWhen } from "../../apps/feature_schedule.ts"; | |
| 24 | 24 | import type { Run } from "../../core/feature_fires.ts"; |
| 25 | import { getAppBySlug, type AppRecord } from "../../core/apps.ts"; | |
| 25 | import { getAppBySlug } from "../../core/apps.ts"; | |
| 26 | 26 | import { DAYS } from "../../apps/feature.ts"; |
| 27 | 27 | import type { FeedItem } from "../../feed/rank.ts"; |
| 28 | 28 | import type { Ctx } from "../../http/context.ts"; |
| @@ -59,7 +59,10 @@ export function switchNote(f: Pick<FeatureView, "name" | "absorbed">): string { | ||
| 59 | 59 | return f.absorbed ? `Un-absorbing takes ${f.name} off every menu; its settings are kept.` : `Absorbing restarts ${f.name} and adds it to the menu.`; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | /** Where the app comes from, on a feature's own page: `dl` rows, the upstream when a fork and who last changed it. */ | |
| 62 | /** | |
| 63 | * Where the app comes from, on a feature's own page: `dl` rows, the upstream when a fork and who | |
| 64 | * last changed it. | |
| 65 | */ | |
| 63 | 66 | export function provenanceRows(p: FeatureView["provenance"]) { |
| 64 | 67 | const facts = provenanceFacts(p); |
| 65 | 68 | if (facts.length === 0) return null; |
| @@ -300,8 +303,8 @@ export async function featureSave(ctx: Ctx): Promise<Response> { | ||
| 300 | 303 | return flashTo(ctx, back, { tone: "danger", message: changed.error.message }); |
| 301 | 304 | } |
| 302 | 305 | |
| 303 | /** POST /settings/features/:slug/run/:tool, with `reachOf` the seam a test replaces. */ | |
| 304 | export function featureRun(reachOf: (app: Ctx["app"], record: AppRecord) => Reach = runtimeReach) { | |
| 306 | /** POST /settings/features/:slug/run/:tool. */ | |
| 307 | export function featureRun() { | |
| 305 | 308 | return async (ctx: Ctx): Promise<Response> => { |
| 306 | 309 | const no = requireAdmin(ctx); |
| 307 | 310 | if (no) return no; |
| @@ -312,7 +315,7 @@ export function featureRun(reachOf: (app: Ctx["app"], record: AppRecord) => Reac | ||
| 312 | 315 | const record = getAppBySlug(ctx.app.db, slug); |
| 313 | 316 | if (record === undefined) return notFoundPage(ctx); |
| 314 | 317 | const back = `/settings/features/${encodeURIComponent(slug)}`; |
| 315 | const said = await runNow(ctx.app.db, record.id, tool, reachOf(ctx.app, record)); | |
| 318 | const said = runNow(ctx.app.db, record.id, tool); | |
| 316 | 319 | return flashTo(ctx, back, said.ok ? { message: said.message } : { tone: "danger", message: said.message }); |
| 317 | 320 | }; |
| 318 | 321 | } |
test/feature-clock-schedule.test.ts +10 −76
| @@ -1,15 +1,13 @@ | ||
| 1 | 1 | /** |
| 2 | * A schedule can fire at a clock time (#12043): `at` in an IANA zone across a daylight-saving | |
| 3 | * change, once per local day, late within the grace window and skipped past it; a manual run does | |
| 4 | * not move it; a new or changed schedule waits for its next time; every run is the one recorded | |
| 5 | * run, with a history of 30. | |
| 2 | * A feature's schedule can name a clock time (#12043): `at` in an IANA zone across a | |
| 3 | * daylight-saving change; a hand's call is the one recorded run, with a history of 30. Firing it is | |
| 4 | * the timers' (#13533). | |
| 6 | 5 | */ |
| 7 | 6 | import { Database } from "bun:sqlite"; |
| 8 | 7 | import { beforeEach, describe, expect, test } from "bun:test"; |
| 9 | import { parseFeature, type ScheduleEntry } from "../src/apps/feature.ts"; | |
| 8 | import { parseFeature } from "../src/apps/feature.ts"; | |
| 10 | 9 | import { lastSlot, nextSlot } from "../src/apps/feature_clock.ts"; |
| 11 | import { runNow } from "../src/apps/feature_run.ts"; | |
| 12 | import { fireApp, GRACE_MS, scheduleStatus, type Reach } from "../src/apps/feature_schedule.ts"; | |
| 10 | import { scheduleStatus } from "../src/apps/feature_schedule.ts"; | |
| 13 | 11 | import { HISTORY_KEEP, lastFires, runHistory, recordFire } from "../src/core/feature_fires.ts"; |
| 14 | 12 | import { absorb } from "../src/core/features.ts"; |
| 15 | 13 | import { migrationFiles } from "../src/core/schema.ts"; |
| @@ -22,14 +20,10 @@ const entry = { tool: "print_today", at: "07:15", tz: TZ }; | ||
| 22 | 20 | const utc = (s: string): number => Date.parse(s); |
| 23 | 21 | |
| 24 | 22 | let db: Database; |
| 25 | let calls: number; | |
| 26 | const server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => (calls++, Response.json({ ok: true })) }); | |
| 27 | const reach = (schedule: ScheduleEntry[] = [entry]): Reach => ({ port: () => server.port, declared: async () => ({ tools: ["print_today"], schedule }) }); | |
| 28 | 23 | beforeEach(() => { |
| 29 | 24 | db = new Database(":memory:"); |
| 30 | 25 | migrate(db, migrationFiles); |
| 31 | 26 | absorb(db, "a1", "admin", "Paper", "/"); |
| 32 | calls = 0; | |
| 33 | 27 | }); |
| 34 | 28 | |
| 35 | 29 | describe("the clock", () => { |
| @@ -49,77 +43,17 @@ describe("the clock", () => { | ||
| 49 | 43 | expect(after - before).toBe(DAY + HOUR); |
| 50 | 44 | }); |
| 51 | 45 | |
| 52 | test("across the spring change (2026-03-08) too, and a daily at fires once on that 23-hour day", async () => { | |
| 46 | test("across the spring change (2026-03-08) too, the 23-hour day has its slot an hour earlier in UTC", () => { | |
| 53 | 47 | expect(lastSlot("07:15", TZ, utc("2026-03-07T20:00:00Z"))).toBe(utc("2026-03-07T13:15:00Z")); |
| 54 | 48 | expect(lastSlot("07:15", TZ, utc("2026-03-08T20:00:00Z"))).toBe(utc("2026-03-08T12:15:00Z")); |
| 55 | await fireApp(db, "a1", reach(), () => utc("2026-03-07T20:00:00Z")); | |
| 56 | let fired = 0; | |
| 57 | for (let t = utc("2026-03-07T20:00:00Z"); t < utc("2026-03-09T20:00:00Z"); t += 60_000) fired += (await fireApp(db, "a1", reach(), () => t)).length; | |
| 58 | expect(fired).toBe(2); | |
| 59 | expect(runHistory(db, "a1").map((r) => r.at)).toEqual([utc("2026-03-09T12:15:00Z"), utc("2026-03-08T12:15:00Z")]); | |
| 60 | }); | |
| 61 | }); | |
| 62 | ||
| 63 | describe("a clock-time schedule", () => { | |
| 64 | test("a new schedule fires at its next time, not at once; once per local day, never twice", async () => { | |
| 65 | const t0 = utc("2026-11-13T05:30:00Z"); // the moment the app was absorbed | |
| 66 | expect(await fireApp(db, "a1", reach(), () => t0)).toEqual([]); | |
| 67 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-13T13:14:00Z"))).toEqual([]); | |
| 68 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-13T13:15:00Z"))).toEqual(["print_today"]); | |
| 69 | for (const t of ["13:16", "15:00", "23:59"]) expect(await fireApp(db, "a1", reach(), () => utc(`2026-11-13T${t}:00Z`))).toEqual([]); | |
| 70 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-14T13:15:30Z"))).toEqual(["print_today"]); | |
| 71 | expect(calls).toBe(2); | |
| 72 | expect(runHistory(db, "a1").map((r) => r.how)).toEqual(["schedule", "schedule"]); | |
| 73 | }); | |
| 74 | ||
| 75 | test("a forge that was away at that minute fires on its next look inside the grace window and says it was late", async () => { | |
| 76 | await fireApp(db, "a1", reach(), () => utc("2026-11-13T05:30:00Z")); | |
| 77 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-13T13:15:00Z") + GRACE_MS)).toEqual(["print_today"]); | |
| 78 | expect(runHistory(db, "a1")[0]?.how).toBe("late"); | |
| 79 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-13T17:00:00Z"))).toEqual([]); | |
| 80 | }); | |
| 81 | ||
| 82 | test("past the grace window the day is skipped, says so, and tomorrow still fires", async () => { | |
| 83 | await fireApp(db, "a1", reach(), () => utc("2026-11-13T05:30:00Z")); | |
| 84 | const back = utc("2026-11-13T13:15:00Z") + GRACE_MS + 60_000; | |
| 85 | expect(await fireApp(db, "a1", reach(), () => back)).toEqual([]); | |
| 86 | expect(calls).toBe(0); | |
| 87 | const [skip] = runHistory(db, "a1"); | |
| 88 | expect(skip).toMatchObject({ how: "skipped", ok: false }); | |
| 89 | expect(skip?.error).toContain("skipped Fri 07:15"); | |
| 90 | expect(lastFires(db, "a1")).toEqual([]); // a missed day is not a fire | |
| 91 | expect(await fireApp(db, "a1", reach(), () => back + 60_000)).toEqual([]); | |
| 92 | expect(runHistory(db, "a1")).toHaveLength(1); | |
| 93 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-14T13:15:00Z"))).toEqual(["print_today"]); | |
| 94 | }); | |
| 95 | ||
| 96 | test("a manual run does not move an at schedule; for every it does", async () => { | |
| 97 | await fireApp(db, "a1", reach(), () => utc("2026-11-13T05:30:00Z")); | |
| 98 | expect(await runNow(db, "a1", "print_today", reach(), "terminal")).toEqual({ ok: true, message: "print_today ran." }); | |
| 99 | expect(await fireApp(db, "a1", reach(), () => utc("2026-11-13T13:15:00Z"))).toEqual(["print_today"]); | |
| 100 | const every = [{ tool: "print_today", everyMs: DAY }]; | |
| 101 | await fireApp(db, "a1", reach(every), () => utc("2026-11-20T00:00:00Z")); | |
| 102 | await runNow(db, "a1", "print_today", reach(every), "page"); | |
| 103 | const ran = lastFires(db, "a1")[0]!.at; | |
| 104 | expect(await fireApp(db, "a1", reach(every), () => ran + DAY - 1)).toEqual([]); | |
| 105 | }); | |
| 106 | ||
| 107 | test("changing the schedule starts it counting again", async () => { | |
| 108 | await fireApp(db, "a1", reach(), () => utc("2026-11-13T05:30:00Z")); | |
| 109 | const moved = [{ ...entry, at: "09:00" }]; | |
| 110 | // At 13:20Z (07:20 local) a schedule changed to 07:30 has not missed today's 07:15, and does | |
| 111 | // not fire. | |
| 112 | expect(await fireApp(db, "a1", reach([{ ...entry, at: "07:30" }]), () => utc("2026-11-13T13:20:00Z"))).toEqual([]); | |
| 113 | expect(await fireApp(db, "a1", reach(moved), () => utc("2026-11-14T14:00:00Z"))).toEqual([]); // seen: counting starts here | |
| 114 | expect(await fireApp(db, "a1", reach(moved), () => utc("2026-11-14T15:00:30Z"))).toEqual(["print_today"]); | |
| 115 | 49 | }); |
| 116 | 50 | }); |
| 117 | 51 | |
| 118 | 52 | describe("one recorded run", () => { |
| 119 | test("every way of starting a run is a row in the last fire and the history, with how", async () => { | |
| 120 | await runNow(db, "a1", "print_today", reach(), "page"); | |
| 121 | await runNow(db, "a1", "print_today", reach(), "terminal"); | |
| 122 | expect(runHistory(db, "a1").map((r) => r.how)).toEqual(["terminal", "page"]); | |
| 53 | test("every way of calling a tool by hand is a row in the last fire and the history, with how", () => { | |
| 54 | recordFire(db, "a1", { tool: "print_today", at: 1, ok: true, error: null, durationMs: 1 }, "mcp"); | |
| 55 | recordFire(db, "a1", { tool: "print_today", at: 2, ok: true, error: null, durationMs: 1 }, "mcp"); | |
| 56 | expect(runHistory(db, "a1").map((r) => r.how)).toEqual(["mcp", "mcp"]); | |
| 123 | 57 | expect(lastFires(db, "a1")).toHaveLength(1); |
| 124 | 58 | }); |
| 125 | 59 |
test/feature-kept.test.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * What a feature's record keeps (#13533: its schedule is fired by the timers, see | |
| 3 | * `test/feature-schedule-one.test.ts`): un-absorbing or deleting the app forgets its hand-made | |
| 4 | * runs. | |
| 5 | */ | |
| 6 | ||
| 7 | import { Database } from "bun:sqlite"; | |
| 8 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | |
| 9 | import { deleteApp } from "../src/core/apps.ts"; | |
| 10 | import { lastFires, recordFire } from "../src/core/feature_fires.ts"; | |
| 11 | import { absorb, unabsorb } from "../src/core/features.ts"; | |
| 12 | import { migrationFiles } from "../src/core/schema.ts"; | |
| 13 | import { migrate } from "../src/kernel/migrate.ts"; | |
| 14 | ||
| 15 | let db: Database; | |
| 16 | beforeEach(() => { | |
| 17 | db = new Database(":memory:"); | |
| 18 | migrate(db, migrationFiles); | |
| 19 | absorb(db, "a1", "admin", "Radar", "/"); | |
| 20 | }); | |
| 21 | afterEach(() => db.close()); | |
| 22 | ||
| 23 | const addApp = (id: string, slug: string): void => { | |
| 24 | db.run("PRAGMA foreign_keys = OFF"); | |
| 25 | db.run("INSERT INTO app (id, repo_id, slug, access) VALUES (?1, ?2, ?3, 'private')", [id, `repo-${id}`, slug]); | |
| 26 | }; | |
| 27 | ||
| 28 | describe("what is kept", () => { | |
| 29 | const fire = { tool: "scan", at: 1, ok: true, error: null, durationMs: 2 }; | |
| 30 | ||
| 31 | test("un-absorbing forgets the fires", () => { | |
| 32 | recordFire(db, "a1", fire); | |
| 33 | recordFire(db, "other", fire); | |
| 34 | expect(unabsorb(db, "a1")).toBe(true); | |
| 35 | expect(lastFires(db, "a1")).toEqual([]); | |
| 36 | expect(lastFires(db, "other")).toHaveLength(1); | |
| 37 | }); | |
| 38 | ||
| 39 | test("deleting the app forgets the fires", () => { | |
| 40 | addApp("a1", "radar"); | |
| 41 | recordFire(db, "a1", fire); | |
| 42 | expect(deleteApp(db, "a1")).toBe(true); | |
| 43 | expect(lastFires(db, "a1")).toEqual([]); | |
| 44 | }); | |
| 45 | }); | |
| 46 |
test/feature-run-now.test.ts +35 −92
| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | * Run now (#10108): an admin runs one scheduled tool from Settings › Features, | |
| 3 | * through the schedule's own call, recorded as its fire. Tried with a fake | |
| 4 | * reach: a good call, a failing one, an asleep app (never reached), a call | |
| 5 | * already running (never doubled); and on a forge with no app runtime, where a | |
| 6 | * non-admin gets the 404 and an admin is told the app is asleep. | |
| 2 | * Run now (#10108, #13533): an admin runs one scheduled tool from Settings › Features by arming | |
| 3 | * the tool's own timer for this moment, the one the app page's Run now arms; the timer loop calls | |
| 4 | * it, wakes the app, retries and records it. Tried on the words, and on the route: a non-admin | |
| 5 | * gets the 403, an admin is sent back with a sentence. | |
| 7 | 6 | */ |
| 8 | 7 | import { Database } from "bun:sqlite"; |
| 9 | 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| @@ -13,103 +12,40 @@ import { join } from "node:path"; | ||
| 13 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 14 | 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 15 | 14 | import { RUN_REFUSALS, runNow } from "../src/apps/feature_run.ts"; |
| 16 | import { fireOne, type Reach } from "../src/apps/feature_schedule.ts"; | |
| 15 | import { scheduleSpecs } from "../src/apps/feature.ts"; | |
| 17 | 16 | import { createApp as insertApp } from "../src/core/apps.ts"; |
| 18 | import { lastFires, recordFire } from "../src/core/feature_fires.ts"; | |
| 19 | import { absorb } from "../src/core/features.ts"; | |
| 17 | import { absorb, unabsorb } from "../src/core/features.ts"; | |
| 20 | 18 | import { migrationFiles } from "../src/core/schema.ts"; |
| 19 | import { dueTimers, syncSchedules } from "../src/core/timers.ts"; | |
| 21 | 20 | import { createUser } from "../src/core/users.ts"; |
| 22 | 21 | import { migrate } from "../src/kernel/migrate.ts"; |
| 23 | 22 | import { serve, type Servers } from "../src/http/server.ts"; |
| 24 | 23 | import { promote } from "./helpers/members.ts"; |
| 25 | 24 | |
| 26 | const HOUR = 3_600_000; | |
| 27 | const schedule = [{ tool: "scan", everyMs: HOUR }]; | |
| 28 | let seen: string[] = []; | |
| 29 | let answer: () => Response = () => Response.json({ ok: true }); | |
| 30 | let hold: Promise<void> | undefined; | |
| 31 | const stub = Bun.serve({ | |
| 32 | port: 0, | |
| 33 | hostname: "127.0.0.1", | |
| 34 | fetch: async (req) => { | |
| 35 | seen.push(new URL(req.url).pathname); | |
| 36 | if (hold) await hold; | |
| 37 | return answer(); | |
| 38 | }, | |
| 39 | }); | |
| 40 | const reach = (over: Partial<Reach> = {}): Reach => ({ port: () => stub.port, declared: async () => ({ tools: ["scan"], schedule }), ...over }); | |
| 41 | afterAll(() => stub.stop(true)); | |
| 42 | ||
| 43 | describe("fireOne", () => { | |
| 44 | let db: Database; | |
| 45 | const fresh = () => { | |
| 46 | db = new Database(":memory:"); | |
| 47 | migrate(db, migrationFiles); | |
| 48 | absorb(db, "a1", "admin", "Radar", "/"); | |
| 49 | seen = []; | |
| 50 | answer = () => Response.json({ ok: true }); | |
| 51 | hold = undefined; | |
| 52 | }; | |
| 53 | ||
| 54 | test("a good call is made once and recorded as the fire, even inside the interval", async () => { | |
| 55 | fresh(); | |
| 56 | recordFire(db, "a1", { tool: "scan", at: 1, ok: false, error: "old", durationMs: 1 }); | |
| 57 | expect(await fireOne(db, "a1", "scan", reach(), () => 10)).toEqual({ ok: true }); | |
| 58 | expect(seen).toEqual(["/forge/tools/scan"]); | |
| 59 | expect(lastFires(db, "a1")).toEqual([{ tool: "scan", at: 10, ok: true, error: null, durationMs: 0 }]); | |
| 60 | }); | |
| 61 | ||
| 62 | test("a failing call is recorded with its sentence", async () => { | |
| 63 | fresh(); | |
| 64 | answer = () => new Response("no", { status: 500 }); | |
| 65 | expect(await fireOne(db, "a1", "scan", reach(), () => 5)).toEqual({ ok: true }); | |
| 66 | expect(lastFires(db, "a1")[0]).toMatchObject({ ok: false, error: "the app answered 500" }); | |
| 67 | }); | |
| 68 | ||
| 69 | test("an asleep app is refused and not called", async () => { | |
| 70 | fresh(); | |
| 71 | expect(await fireOne(db, "a1", "scan", reach({ port: () => undefined }))).toEqual({ ok: false, why: "asleep" }); | |
| 72 | expect(seen).toEqual([]); | |
| 73 | expect(lastFires(db, "a1")).toEqual([]); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a call already running is refused, not doubled", async () => { | |
| 77 | fresh(); | |
| 78 | let release!: () => void; | |
| 79 | hold = new Promise((r) => (release = r)); | |
| 80 | const first = fireOne(db, "a1", "scan", reach()); | |
| 81 | while (seen.length === 0) await Bun.sleep(1); | |
| 82 | expect(await fireOne(db, "a1", "scan", reach())).toEqual({ ok: false, why: "in_flight" }); | |
| 83 | release(); | |
| 84 | expect(await first).toEqual({ ok: true }); | |
| 85 | expect(seen.length).toBe(1); | |
| 86 | }); | |
| 87 | ||
| 88 | test("a tool not on the schedule, or an app not absorbed, is refused", async () => { | |
| 89 | fresh(); | |
| 90 | expect(await fireOne(db, "a1", "other", reach())).toEqual({ ok: false, why: "not_scheduled" }); | |
| 91 | expect(await fireOne(db, "nope", "scan", reach())).toEqual({ ok: false, why: "not_absorbed" }); | |
| 92 | expect(seen).toEqual([]); | |
| 93 | }); | |
| 94 | }); | |
| 95 | ||
| 96 | 25 | describe("what is said", () => { |
| 97 | 26 | const db = new Database(":memory:"); |
| 98 | 27 | migrate(db, migrationFiles); |
| 99 | 28 | absorb(db, "a1", "admin", "Radar", "/"); |
| 100 | 29 | |
| 101 | test("a good call says ran; a failing one says ran and failed with why; a refusal has its sentence", async () => { | |
| 102 | answer = () => Response.json({ ok: true }); | |
| 103 | expect(await runNow(db, "a1", "scan", reach())).toEqual({ ok: true, message: "scan ran." }); | |
| 104 | answer = () => new Response("no", { status: 500 }); | |
| 105 | expect(await runNow(db, "a1", "scan", reach())).toEqual({ ok: false, message: "scan ran and failed: the app answered 500." }); | |
| 106 | expect(await runNow(db, "a1", "scan", reach({ port: () => undefined }))).toEqual({ ok: false, message: RUN_REFUSALS.asleep }); | |
| 30 | test("a scheduled tool is armed for now, once more each time it is asked, and says so", () => { | |
| 31 | syncSchedules(db, "a1", "production", scheduleSpecs([{ tool: "scan", everyMs: 3_600_000 }]), "c1", 1_000); | |
| 32 | expect(runNow(db, "a1", "scan", 5_000)).toEqual({ ok: true, message: "scan is running now; its line shows how it went in a moment." }); | |
| 33 | const due = dueTimers(db, 5_000); | |
| 34 | expect(due.map((t) => [t.name, t.path, t.kind])).toEqual([["scan", "/forge/tools/scan", "once"]]); | |
| 35 | expect(new TextDecoder().decode(due[0]!.body!)).toBe('{"args":{}}'); | |
| 107 | 36 | }); |
| 108 | 37 | |
| 109 | test("a reach that throws is a refusal, and a dead port a recorded failure, never a throw", async () => { | |
| 110 | const boom = reach({ declared: async () => { throw new Error("x"); } }); | |
| 111 | expect(await runNow(db, "a1", "scan", boom)).toEqual({ ok: false, message: RUN_REFUSALS.not_scheduled }); | |
| 112 | expect((await runNow(db, "a1", "scan", reach({ port: () => 1 }))).message).toBe("scan ran and failed: the app did not answer."); | |
| 38 | test("a tool not on the schedule, or an app not absorbed, is refused with its sentence", () => { | |
| 39 | expect(runNow(db, "a1", "other")).toEqual({ ok: false, message: RUN_REFUSALS.not_scheduled }); | |
| 40 | expect(runNow(db, "nope", "scan")).toEqual({ ok: false, message: RUN_REFUSALS.not_absorbed }); | |
| 41 | }); | |
| 42 | ||
| 43 | test("an app that is not absorbed arms nothing, for the page and the terminal alike (both say it through runNow)", () => { | |
| 44 | const bare = new Database(":memory:"); | |
| 45 | migrate(bare, migrationFiles); | |
| 46 | syncSchedules(bare, "a2", "production", scheduleSpecs([{ tool: "scan", everyMs: 3_600_000 }]), "c1", 1_000); | |
| 47 | expect(runNow(bare, "a2", "scan", 5_000)).toEqual({ ok: false, message: RUN_REFUSALS.not_absorbed }); | |
| 48 | expect(dueTimers(bare, 5_000)).toEqual([]); | |
| 113 | 49 | }); |
| 114 | 50 | }); |
| 115 | 51 | |
| @@ -151,7 +87,7 @@ describe("the route", () => { | ||
| 151 | 87 | if (!row.ok) throw new Error(row.error); |
| 152 | 88 | appId = row.value.id; |
| 153 | 89 | absorb(app.db, appId, "admin", "Radar", "/"); |
| 154 | recordFire(app.db, appId, { tool: "scan", at: Date.now(), ok: true, error: null, durationMs: 3 }); | |
| 90 | syncSchedules(app.db, appId, "production", scheduleSpecs([{ tool: "scan", everyMs: 3_600_000 }]), "c1", Date.now()); | |
| 155 | 91 | adaCookie = await signIn("ada"); |
| 156 | 92 | cyCookie = await signIn("cy"); |
| 157 | 93 | }, 60_000); |
| @@ -172,11 +108,18 @@ describe("the route", () => { | ||
| 172 | 108 | expect((await web(cyCookie, "/settings/features/radar/run/scan", true)).status).toBe(403); |
| 173 | 109 | }); |
| 174 | 110 | |
| 175 | test("an admin on an asleep app is sent back with a sentence, and nothing is recorded", async () => { | |
| 176 | const before = lastFires(app.db, appId); | |
| 111 | test("an admin is sent back, and the tool's timer is armed for now", async () => { | |
| 177 | 112 | const res = await web(adaCookie, "/settings/features/radar/run/scan", true); |
| 178 | 113 | expect(res.status).toBe(303); |
| 179 | 114 | expect(res.headers.get("location")).toBe("/settings/features/radar"); |
| 180 | expect(lastFires(app.db, appId)).toEqual(before); | |
| 115 | expect(dueTimers(app.db, Date.now() + 1000).some((t) => t.appId === appId && t.name === "scan" && t.kind === "once")).toBe(true); | |
| 116 | }); | |
| 117 | ||
| 118 | test("once un-absorbed, Run now arms nothing", async () => { | |
| 119 | app.db.query("DELETE FROM timer WHERE kind = 'once'").run(); | |
| 120 | unabsorb(app.db, appId); | |
| 121 | const res = await web(adaCookie, "/settings/features/radar/run/scan", true); | |
| 122 | expect(res.status).toBe(303); | |
| 123 | expect(dueTimers(app.db, Date.now() + 1000).some((t) => t.appId === appId && t.kind === "once")).toBe(false); | |
| 181 | 124 | }); |
| 182 | 125 | }); |
test/feature-schedule-fire.test.ts deleted −140
| @@ -1,140 +0,0 @@ | ||
| 1 | /** | |
| 2 | * The forge calls an absorbed app's scheduled tool when it is due, once per | |
| 3 | * window, only while production runs, and remembers how it went. The clock is | |
| 4 | * injected; nothing waits on wall time. | |
| 5 | */ | |
| 6 | ||
| 7 | import { Database } from "bun:sqlite"; | |
| 8 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | |
| 9 | import { due, fireApp, fireDue, startFeatureSchedule, type Reach } from "../src/apps/feature_schedule.ts"; | |
| 10 | import { deleteApp } from "../src/core/apps.ts"; | |
| 11 | import { lastFires, recordFire } from "../src/core/feature_fires.ts"; | |
| 12 | import { absorb, unabsorb } from "../src/core/features.ts"; | |
| 13 | import { migrationFiles } from "../src/core/schema.ts"; | |
| 14 | import { migrate } from "../src/kernel/migrate.ts"; | |
| 15 | ||
| 16 | const HOUR = 3_600_000; | |
| 17 | const schedule = [{ tool: "scan", everyMs: HOUR }]; | |
| 18 | let db: Database; | |
| 19 | let seen: string[]; | |
| 20 | let answer: () => Response; | |
| 21 | const server = Bun.serve({ | |
| 22 | port: 0, | |
| 23 | hostname: "127.0.0.1", | |
| 24 | fetch: (req) => { | |
| 25 | seen.push(new URL(req.url).pathname); | |
| 26 | return answer(); | |
| 27 | }, | |
| 28 | }); | |
| 29 | const reach = (over: Partial<Reach> = {}): Reach => ({ port: () => server.port, declared: async () => ({ tools: ["scan"], schedule }), ...over }); | |
| 30 | ||
| 31 | beforeEach(() => { | |
| 32 | db = new Database(":memory:"); | |
| 33 | migrate(db, migrationFiles); | |
| 34 | absorb(db, "a1", "admin", "Radar", "/"); | |
| 35 | seen = []; | |
| 36 | answer = () => Response.json({ ok: true }); | |
| 37 | }); | |
| 38 | afterEach(() => db.close()); | |
| 39 | ||
| 40 | describe("due", () => { | |
| 41 | const every = schedule[0]!; | |
| 42 | test("an interval counts from the later of its last fire and when the schedule began; at it, it is due", () => { | |
| 43 | expect(due(every, { since: 5 }, 5 + HOUR - 1)).toBeUndefined(); | |
| 44 | expect(due(every, { since: 5 }, 5 + HOUR)).toEqual({ do: "fire", how: "schedule" }); | |
| 45 | expect(due(every, { since: 5, lastFired: 1000 }, 1000 + HOUR - 1)).toBeUndefined(); | |
| 46 | expect(due(every, { since: 5, lastFired: 1000 }, 1000 + HOUR)).toEqual({ do: "fire", how: "schedule" }); | |
| 47 | }); | |
| 48 | }); | |
| 49 | ||
| 50 | describe("fireApp", () => { | |
| 51 | test("calls a due tool through /forge/tools and keeps how it went; two ticks in a window fire once", async () => { | |
| 52 | expect(await fireApp(db, "a1", reach(), () => 10_000)).toEqual([]); // the first look only begins to count | |
| 53 | expect(seen).toEqual([]); | |
| 54 | expect(await fireApp(db, "a1", reach(), () => 10_000 + HOUR - 1)).toEqual([]); | |
| 55 | expect(await fireApp(db, "a1", reach(), () => 10_000 + HOUR)).toEqual(["scan"]); | |
| 56 | expect(seen).toEqual(["/forge/tools/scan"]); | |
| 57 | expect(await fireApp(db, "a1", reach(), () => 10_000 + 2 * HOUR - 1)).toEqual([]); | |
| 58 | expect(seen.length).toBe(1); | |
| 59 | expect(lastFires(db, "a1")).toEqual([{ tool: "scan", at: 10_000 + HOUR, ok: true, error: null, durationMs: 0 }]); | |
| 60 | expect(await fireApp(db, "a1", reach(), () => 10_000 + 2 * HOUR)).toEqual(["scan"]); | |
| 61 | expect(seen.length).toBe(2); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a failing tool is recorded with its sentence", async () => { | |
| 65 | answer = () => new Response("no", { status: 500 }); | |
| 66 | await fireApp(db, "a1", reach(), () => 5); | |
| 67 | await fireApp(db, "a1", reach(), () => 5 + HOUR); | |
| 68 | const [fire] = lastFires(db, "a1"); | |
| 69 | expect(fire?.ok).toBe(false); | |
| 70 | expect(fire?.error).toBe("the app answered 500"); | |
| 71 | }); | |
| 72 | ||
| 73 | test("an asleep or stopped app is skipped and not called", async () => { | |
| 74 | expect(await fireApp(db, "a1", reach({ port: () => undefined }), () => 5)).toEqual([]); | |
| 75 | expect(seen).toEqual([]); | |
| 76 | expect(lastFires(db, "a1")).toEqual([]); | |
| 77 | }); | |
| 78 | ||
| 79 | test("an un-absorbed app is never called", async () => { | |
| 80 | expect(await fireApp(db, "other", reach(), () => 5)).toEqual([]); | |
| 81 | expect(seen).toEqual([]); | |
| 82 | }); | |
| 83 | ||
| 84 | test("a scheduled tool the app no longer declares is not called", async () => { | |
| 85 | const stale = reach({ declared: async () => ({ tools: ["other"], schedule }) }); | |
| 86 | expect(await fireApp(db, "a1", stale, () => 5)).toEqual([]); | |
| 87 | expect(seen).toEqual([]); | |
| 88 | expect(lastFires(db, "a1")).toEqual([]); | |
| 89 | }); | |
| 90 | }); | |
| 91 | ||
| 92 | const addApp = (id: string, slug: string): void => { | |
| 93 | db.run("PRAGMA foreign_keys = OFF"); | |
| 94 | db.run("INSERT INTO app (id, repo_id, slug, access) VALUES (?1, ?2, ?3, 'private')", [id, `repo-${id}`, slug]); | |
| 95 | }; | |
| 96 | ||
| 97 | describe("what is kept", () => { | |
| 98 | const fire = { tool: "scan", at: 1, ok: true, error: null, durationMs: 2 }; | |
| 99 | ||
| 100 | test("un-absorbing forgets the fires", () => { | |
| 101 | recordFire(db, "a1", fire); | |
| 102 | recordFire(db, "other", fire); | |
| 103 | expect(unabsorb(db, "a1")).toBe(true); | |
| 104 | expect(lastFires(db, "a1")).toEqual([]); | |
| 105 | expect(lastFires(db, "other")).toHaveLength(1); | |
| 106 | }); | |
| 107 | ||
| 108 | test("deleting the app forgets the fires", () => { | |
| 109 | addApp("a1", "radar"); | |
| 110 | recordFire(db, "a1", fire); | |
| 111 | expect(deleteApp(db, "a1")).toBe(true); | |
| 112 | expect(lastFires(db, "a1")).toEqual([]); | |
| 113 | }); | |
| 114 | }); | |
| 115 | ||
| 116 | describe("fireDue", () => { | |
| 117 | test("sweeps every app with the injected clock, once per window, and survives one that throws", async () => { | |
| 118 | addApp("a1", "radar"); | |
| 119 | addApp("a2", "broken"); | |
| 120 | absorb(db, "a2", "admin", "Broken", "/"); | |
| 121 | const warned: string[] = []; | |
| 122 | const forge = { db, log: { warn: (m: string) => warned.push(m) } } as never; | |
| 123 | const reachOf = (r: { id: string }): Reach => | |
| 124 | r.id === "a2" ? reach({ declared: async () => Promise.reject(new Error("boom")) }) : reach(); | |
| 125 | await fireDue(forge, () => 10_000, reachOf); | |
| 126 | expect(warned).toEqual(["could not fire a scheduled tool"]); | |
| 127 | await fireDue(forge, () => 10_000 + HOUR, reachOf); | |
| 128 | expect(seen).toEqual(["/forge/tools/scan"]); | |
| 129 | await fireDue(forge, () => 10_000 + 2 * HOUR - 1, reachOf); | |
| 130 | expect(seen.length).toBe(1); | |
| 131 | await fireDue(forge, () => 10_000 + 2 * HOUR, reachOf); | |
| 132 | expect(seen.length).toBe(2); | |
| 133 | expect(lastFires(db, "a1")[0]?.at).toBe(10_000 + 2 * HOUR); | |
| 134 | }); | |
| 135 | }); | |
| 136 | ||
| 137 | test("the timer does not start under the test harness", () => { | |
| 138 | expect(startFeatureSchedule({} as never)).toBeUndefined(); | |
| 139 | server.stop(true); | |
| 140 | }); |
test/feature-schedule-one.test.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | /** | |
| 2 | * One way to give an app time (#13533): a feature's `schedule` is read as `schedules` entries by | |
| 3 | * the one parser, so the morning paper's 07:15 print is a timer like any other: counted in the | |
| 4 | * health answer, woken for and waited on, tried again and recorded with the app's answer. The | |
| 5 | * forge calls it only while the app is absorbed. | |
| 6 | */ | |
| 7 | import { Database } from "bun:sqlite"; | |
| 8 | import { describe, expect, test } from "bun:test"; | |
| 9 | import { parseManifest } from "../src/apps/manifest.ts"; | |
| 10 | import { fireDueTimers, type Reach } from "../src/apps/timer_fire.ts"; | |
| 11 | import { createApp } from "../src/core/apps.ts"; | |
| 12 | import { absorb, unabsorb } from "../src/core/features.ts"; | |
| 13 | import { mayCall } from "../src/apps/timer_service.ts"; | |
| 14 | import { migrationFiles } from "../src/core/schema.ts"; | |
| 15 | import { migrate } from "../src/kernel/migrate.ts"; | |
| 16 | import { overdueSchedules, runsOf, scheduleTimer, syncSchedules } from "../src/core/timers.ts"; | |
| 17 | ||
| 18 | const at = (s: string): number => Date.parse(s); | |
| 19 | const paper = (extra: Record<string, unknown> = {}) => | |
| 20 | JSON.stringify({ | |
| 21 | app: { run: "bun run src/server.ts" }, | |
| 22 | feature: { name: "Paper", tools: { print_today: { description: "Print" } }, schedule: [{ tool: "print_today", at: "07:15", tz: "America/Chicago" }] }, | |
| 23 | ...extra, | |
| 24 | }); | |
| 25 | const specsOf = (json: string) => { | |
| 26 | const m = parseManifest(json, undefined); | |
| 27 | if (!m.ok) throw new Error(JSON.stringify(m.error)); | |
| 28 | return m.value.schedules ?? []; | |
| 29 | }; | |
| 30 | ||
| 31 | describe("the manifest", () => { | |
| 32 | test("a feature's clock time is a schedules entry: its tool's route, daily in its zone", () => { | |
| 33 | const [s, ...rest] = specsOf(paper()); | |
| 34 | expect(rest).toEqual([]); | |
| 35 | expect(s).toMatchObject({ name: "print_today", cron: "15 7 * * *", timezone: "America/Chicago", path: "/forge/tools/print_today", env: "production", retries: 3 }); | |
| 36 | }); | |
| 37 | ||
| 38 | test("an interval becomes a cron in UTC", () => { | |
| 39 | const json = JSON.stringify({ app: { run: "x" }, feature: { name: "R", tools: { scan: { description: "s" } }, schedule: [{ tool: "scan", every: "6h" }] } }); | |
| 40 | expect(specsOf(json)[0]).toMatchObject({ cron: "0 */6 * * *", timezone: "UTC" }); | |
| 41 | }); | |
| 42 | ||
| 43 | test("both ways side by side are both schedules; a name taken twice is refused", () => { | |
| 44 | const both = specsOf(paper({ schedules: [{ name: "tidy", cron: "0 3 * * *", path: "/jobs/tidy" }] })); | |
| 45 | expect(both.map((s) => s.name)).toEqual(["tidy", "print_today"]); | |
| 46 | const clash = parseManifest(paper({ schedules: [{ name: "print_today", cron: "0 3 * * *", path: "/jobs/x" }] }), undefined); | |
| 47 | expect(!clash.ok && "message" in clash.error && clash.error.message).toContain("print_today"); | |
| 48 | }); | |
| 49 | }); | |
| 50 | ||
| 51 | function world(absorbed = true) { | |
| 52 | const db = new Database(":memory:"); | |
| 53 | migrate(db, migrationFiles); | |
| 54 | db.run("PRAGMA foreign_keys = OFF"); | |
| 55 | const made = createApp(db, { repoId: "r1", slug: "paper", access: "private" }); | |
| 56 | if (!made.ok) throw new Error(made.error); | |
| 57 | const id = made.value.id; | |
| 58 | if (absorbed) absorb(db, id, "admin", "Paper", "/"); | |
| 59 | syncSchedules(db, id, "production", specsOf(paper()), "c1", at("2026-11-14T20:00:00Z")); | |
| 60 | const w = { asleep: true, answers: [] as (Response | "down")[], calls: [] as { url: string; type: string | undefined; body: unknown }[] }; | |
| 61 | const reach: Reach = { | |
| 62 | async ensure() { | |
| 63 | w.asleep = false; // a wake that waits until the app is ready | |
| 64 | return 4321; | |
| 65 | }, | |
| 66 | identityKey: () => "k", | |
| 67 | permits: (record, t) => mayCall(db, record, t), | |
| 68 | fetch: (async (url: string, init: RequestInit) => { | |
| 69 | w.calls.push({ url: String(url), type: (init.headers as Record<string, string>)["content-type"], body: init.body }); | |
| 70 | const next = w.answers.shift() ?? Response.json({ printed: 3 }); | |
| 71 | if (next === "down") throw new Error("down"); | |
| 72 | return next; | |
| 73 | }) as unknown as typeof fetch, | |
| 74 | }; | |
| 75 | return { db, id, w, reach }; | |
| 76 | } | |
| 77 | ||
| 78 | describe("the morning paper's print", () => { | |
| 79 | const due = at("2026-11-15T13:15:00Z"); | |
| 80 | ||
| 81 | test("is armed for 07:15 Chicago and counted in the health answer before it has run", () => { | |
| 82 | const { db, id } = world(); | |
| 83 | expect(scheduleTimer(db, id, "print_today", "production")?.dueAt).toBe(due); | |
| 84 | expect(overdueSchedules(db, at("2026-11-15T00:00:00Z"))).toEqual({ overdue: 0, oldestSeconds: 0, total: 1, ran: 0 }); | |
| 85 | }); | |
| 86 | ||
| 87 | test("an app that is not absorbed is never called: its timer moves on with no call and no run", async () => { | |
| 88 | const { db, id, w, reach } = world(false); | |
| 89 | expect(await fireDueTimers(db, reach, () => due + 1000)).toBe(1); | |
| 90 | expect(w.calls).toEqual([]); | |
| 91 | expect(runsOf(db, id, "print_today")).toEqual([]); | |
| 92 | expect(scheduleTimer(db, id, "print_today", "production")?.dueAt).toBe(at("2026-11-16T13:15:00Z")); | |
| 93 | }); | |
| 94 | ||
| 95 | test("an app un-absorbed after it was absorbed stops being called", async () => { | |
| 96 | const { db, id, w, reach } = world(); | |
| 97 | await fireDueTimers(db, reach, () => due + 1000); | |
| 98 | expect(w.calls.length).toBe(1); | |
| 99 | unabsorb(db, id); | |
| 100 | await fireDueTimers(db, reach, () => at("2026-11-16T13:15:01Z")); | |
| 101 | expect(w.calls.length).toBe(1); | |
| 102 | }); | |
| 103 | ||
| 104 | test("fires once at its time: wakes the app, posts the tool's JSON, records the answer", async () => { | |
| 105 | const { db, id, w, reach } = world(); | |
| 106 | expect(await fireDueTimers(db, reach, () => due + 1000)).toBe(1); | |
| 107 | expect(w.calls).toEqual([{ url: "http://127.0.0.1:4321/forge/tools/print_today", type: "application/json", body: new TextEncoder().encode('{"args":{}}') }]); | |
| 108 | const [run] = runsOf(db, id, "print_today"); | |
| 109 | expect(run).toMatchObject({ status: "ok", httpStatus: 200, attempts: 1 }); | |
| 110 | expect(run?.response).toContain("printed"); | |
| 111 | expect(scheduleTimer(db, id, "print_today", "production")?.dueAt).toBe(at("2026-11-16T13:15:00Z")); | |
| 112 | expect(overdueSchedules(db, due + 60_000)).toMatchObject({ total: 1, ran: 1 }); | |
| 113 | }); | |
| 114 | ||
| 115 | test("a failed call is tried again, and the run is one success after the retry", async () => { | |
| 116 | const { db, id, w, reach } = world(); | |
| 117 | w.answers.push("down"); | |
| 118 | await fireDueTimers(db, reach, () => due); | |
| 119 | expect(runsOf(db, id, "print_today")).toEqual([]); | |
| 120 | const retry = scheduleTimer(db, id, "print_today", "production")!; | |
| 121 | expect(retry.attempt).toBe(1); | |
| 122 | await fireDueTimers(db, reach, () => retry.dueAt + 1); | |
| 123 | expect(runsOf(db, id, "print_today").map((r) => [r.status, r.attempts])).toEqual([["ok", 2]]); | |
| 124 | }); | |
| 125 | ||
| 126 | test("failing past its retries is one failed run ", async () => { | |
| 127 | const { db, id, w, reach } = world(); | |
| 128 | w.answers.push(...Array.from({ length: 4 }, () => "down" as const)); | |
| 129 | let now = due; | |
| 130 | for (let i = 0; i < 4; i++) { | |
| 131 | await fireDueTimers(db, reach, () => now); | |
| 132 | now = scheduleTimer(db, id, "print_today", "production")!.dueAt + 1; | |
| 133 | } | |
| 134 | const runs = runsOf(db, id, "print_today"); | |
| 135 | expect(runs.map((r) => r.status)).toEqual(["failed"]); | |
| 136 | expect(runs[0]?.error).toContain("the app did not answer"); | |
| 137 | expect(overdueSchedules(db, now)).toMatchObject({ total: 1, ran: 1 }); | |
| 138 | }); | |
| 139 | }); |
test/features-tui.test.ts +6 −6
| @@ -170,7 +170,7 @@ describe("features in the terminal", () => { | ||
| 170 | 170 | test("the feed screen words a clock-time schedule in its zone, and h shows the run history", async () => { |
| 171 | 171 | const clock = JSON.stringify({ ...JSON.parse(FORGE_JSON), feature: { ...JSON.parse(FORGE_JSON).feature, schedule: [{ tool: "scan", at: "07:15", tz: "America/Chicago" }] } }); |
| 172 | 172 | await commitToBranch(forge, ada, getRepoById(forge.db, repoId)!, { message: "clock", files: [{ path: "forge.json", content: clock }] }); |
| 173 | recordFire(forge.db, appId, { tool: "scan", at: Date.now() - 120_000, ok: true, error: null, durationMs: 3 }, "terminal"); | |
| 173 | recordFire(forge.db, appId, { tool: "scan", at: Date.now() - 120_000, ok: true, error: null, durationMs: 3 }, "mcp"); | |
| 174 | 174 | const t = boot(ada); |
| 175 | 175 | await t.palette("Feature feeds"); |
| 176 | 176 | await t.press("enter"); |
| @@ -180,29 +180,29 @@ describe("features in the terminal", () => { | ||
| 180 | 180 | expect(t.text()).toMatch(/Scheduled: scan at 07:15 America\/Chicago · last /); |
| 181 | 181 | expect(t.text()).not.toContain("every 1h"); |
| 182 | 182 | await t.press("h"); |
| 183 | expect(t.text()).toMatch(/scan · terminal · \d{4}-\d\d-\d\d \d\d:\d\d · ok · 3 ms/); | |
| 183 | expect(t.text()).toMatch(/scan · mcp · \d{4}-\d\d-\d\d \d\d:\d\d · ok · 3 ms/); | |
| 184 | 184 | await t.press("h"); |
| 185 | expect(t.text()).not.toContain("scan · terminal"); | |
| 185 | expect(t.text()).not.toContain("scan · mcp"); | |
| 186 | 186 | t.stop(); |
| 187 | 187 | clearFires(forge.db, appId); |
| 188 | 188 | await commitToBranch(forge, ada, getRepoById(forge.db, repoId)!, { message: "back", files: [{ path: "forge.json", content: FORGE_JSON }] }); |
| 189 | 189 | }); |
| 190 | 190 | |
| 191 | test("an admin presses r on the feed screen to run the scheduled tool; the refusal is the page's sentence", async () => { | |
| 191 | test("an admin presses r on the feed screen to run the scheduled tool; with no timer the refusal is the page's sentence", async () => { | |
| 192 | 192 | recordFire(forge.db, appId, { tool: "scan", at: Date.now() - 120_000, ok: true, error: null, durationMs: 3 }); |
| 193 | 193 | const t = boot(ada); |
| 194 | 194 | await t.palette("Feature feeds"); |
| 195 | 195 | await t.press("enter"); |
| 196 | 196 | await t.press("enter"); |
| 197 | 197 | await t.press("r"); |
| 198 | expect(t.text()).toContain(RUN_REFUSALS.asleep); | |
| 198 | expect(t.text()).toContain(RUN_REFUSALS.not_scheduled); | |
| 199 | 199 | t.stop(); |
| 200 | 200 | const other = boot(eve); |
| 201 | 201 | await other.palette("Feature feeds"); |
| 202 | 202 | await other.press("enter"); |
| 203 | 203 | await other.press("enter"); |
| 204 | 204 | await other.press("r"); |
| 205 | expect(other.text()).not.toContain(RUN_REFUSALS.asleep); | |
| 205 | expect(other.text()).not.toContain(RUN_REFUSALS.not_scheduled); | |
| 206 | 206 | other.stop(); |
| 207 | 207 | clearFires(forge.db, appId); |
| 208 | 208 | }); |
test/features.test.ts +1 −1
| @@ -365,7 +365,7 @@ describe("absorbing an app", () => { | ||
| 365 | 365 | test("the page words a clock-time schedule in its zone, and groups the run history per tool, the older ones behind a disclosure", async () => { |
| 366 | 366 | const clock = JSON.stringify({ app: { run: "x" }, feature: { ...FEATURE, schedule: [{ tool: "scan", at: "07:15", tz: "America/Chicago" }] } }); |
| 367 | 367 | await commitToBranch(app, cy, repo, { message: "clock", files: [{ path: "forge.json", content: clock }] }); |
| 368 | for (let i = 0; i < 7; i++) recordFire(app.db, appId, { tool: "scan", at: Date.now() - (10 - i) * 60_000, ok: i !== 6, error: i === 6 ? "<i>boom</i>" : null, durationMs: 5 }, "page"); | |
| 368 | for (let i = 0; i < 7; i++) recordFire(app.db, appId, { tool: "scan", at: Date.now() - (10 - i) * 60_000, ok: i !== 6, error: i === 6 ? "<i>boom</i>" : null, durationMs: 5 }, "mcp"); | |
| 369 | 369 | const page = await (await web(adaCookie, "/settings/features/radar")).text(); |
| 370 | 370 | expect(page).toMatch(/at 07:15 America\/Chicago/); |
| 371 | 371 | expect(page).not.toMatch(/every 1h/); |
test/job-slices.test.ts −2
| @@ -64,7 +64,6 @@ import { readLandings } from "../src/release/landings.ts"; | ||
| 64 | 64 | import { sweepStanding as sweepCallers } from "../src/http/caller.ts"; |
| 65 | 65 | import { nightlyDue } from "../src/checks/scan_store.ts"; |
| 66 | 66 | import { startNotifier } from "../src/crew/notify.ts"; |
| 67 | import { fireDue } from "../src/apps/feature_schedule.ts"; | |
| 68 | 67 | import { pullDue } from "../src/apps/pull_schedule.ts"; |
| 69 | 68 | import { startMaintain } from "../src/maintain/runner.ts"; |
| 70 | 69 | import { retentionJobs } from "../src/system/retention.ts"; |
| @@ -243,7 +242,6 @@ function jobs(): Record<string, () => unknown> { | ||
| 243 | 242 | }, |
| 244 | 243 | "standing-sweep": () => sweepCallers(app.db, () => {}), |
| 245 | 244 | "nightly-scan": () => nightlyDue(app.db, new Date(Date.now() - 24 * 3_600_000).toISOString(), 25), |
| 246 | "feature-schedule": () => fireDue(app), | |
| 247 | 245 | "pull-schedule": () => pullDue(app), |
| 248 | 246 | maintain: () => maintainer.tick(new Date(Date.now() + 1e9)), |
| 249 | 247 | // The retention slices, each as its timer calls it: bounded rows, paced by their own cost |
test/timers-sync.test.ts +1 −1
| @@ -60,7 +60,7 @@ test("the schedules a production commit declares become rows once per commit; a | ||
| 60 | 60 | await syncAll(app); |
| 61 | 61 | const rows = scheduleTimers(app.db, record.id); |
| 62 | 62 | expect(rows.map((t) => [t.name, t.env, t.cron, t.timezone, t.path])).toEqual([["print", "production", "30 6 * * *", "America/Chicago", "/jobs/print"]]); |
| 63 | expect(syncedCommit(app.db, record.id, "production")).toBe(tip); | |
| 63 | expect(syncedCommit(app.db, record.id, "production")?.startsWith(tip)).toBe(true); // the commit, then the sync revision (#13533) | |
| 64 | 64 | // Read once per commit: a pause survives the next pass. |
| 65 | 65 | app.db.run("UPDATE timer SET paused = 1"); |
| 66 | 66 | await syncAll(app); |
new above
offline