Public: anyone can read this; the crew can change it.
Settings › Features shows each scheduled tool's last run, how it went and its next run #10206
scout/forge:scout/10200-settings-features-shows-each-scheduled-t → main 2 commits fork
6 files
changed +86
−9
docs/features/absorb.md +1 −1
| @@ -82,7 +82,7 @@ Settings stay behind on a fork. A fork of another forge's app carries its `featu | ||
| 82 | 82 | |
| 83 | 83 | ## Schedule |
| 84 | 84 | |
| 85 | A feature may declare `"schedule": [{ "tool": "scan", "every": "1h" }]`: at most 4 entries, each naming one of the feature's own `tools` and an `every` of `15m`, `1h`, `6h` or `1d`. Anything else is refused with the field and the entry named. The forge keeps it as `schedule` (`tool`, `everyMs`) on the parsed feature. The forge calls it on that interval while the app is absorbed and running, and remembers how it went: see [](feature-schedule.md). | |
| 85 | A feature may declare `"schedule": [{ "tool": "scan", "every": "1h" }]`: at most 4 entries, each naming one of the feature's own `tools` and an `every` of `15m`, `1h`, `6h` or `1d`. Anything else is refused with the field and the entry named. The forge keeps it as `schedule` (`tool`, `everyMs`) on the parsed feature. The forge calls it on that interval while the app is absorbed and running, and remembers how it went: see [](feature-schedule.md). Settings › Features › the app shows one line per entry under **Scheduled** (`scan every 1h · last 10:02, ok · next 11:02`, the error's sentence when it failed, `never run` before the first fire); `scheduleText` in `src/apps/feature_schedule.ts` is the same line as plain text. | |
| 86 | 86 | |
| 87 | 87 | ## Not done yet |
| 88 | 88 |
src/actions/features.ts +4 −1
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | import { lastFires, type Fired } from "../core/feature_fires.ts"; |
| 21 | 21 | import type { App } from "../app.ts"; |
| 22 | 22 | import { readFileAt } from "../apps/checkout.ts"; |
| 23 | import { checkSetting, featureEnv, parseFeature, settingEnvName, type FeatureSpec } from "../apps/feature.ts"; | |
| 23 | import { checkSetting, featureEnv, parseFeature, settingEnvName, type FeatureSpec, type ScheduleEntry } from "../apps/feature.ts"; | |
| 24 | 24 | import { keptSettings as openSettings, sealSetting } from "../apps/feature_env.ts"; |
| 25 | 25 | import type { ToolSpec } from "../apps/feature_tools.ts"; |
| 26 | 26 | import { appsRuntime } from "../apps/runtime.ts"; |
| @@ -91,6 +91,8 @@ export interface FeatureView { | ||
| 91 | 91 | readonly tools: readonly ToolSpec[]; |
| 92 | 92 | /** How the forge's last call of each scheduled tool went; empty when none has fired. */ |
| 93 | 93 | readonly fires: readonly Fired[]; |
| 94 | /** The scheduled tools the app declares now (only those it still declares), absorbed or not. */ | |
| 95 | readonly schedule: readonly ScheduleEntry[]; | |
| 94 | 96 | /** Where the app comes from: the upstream only when the reader may read it. */ |
| 95 | 97 | readonly provenance: Provenance; |
| 96 | 98 | } |
| @@ -242,6 +244,7 @@ function viewOf( | ||
| 242 | 244 | provenance, |
| 243 | 245 | tools: spec?.tools ?? [], |
| 244 | 246 | fires: absorbed === undefined ? [] : lastFires(app.db, record.id), |
| 247 | schedule: absorbed === undefined || spec === undefined ? [] : spec.schedule.filter((e) => spec.tools.some((t) => t.name === e.tool)), | |
| 245 | 248 | settings: (spec?.settings ?? []).map((s) => { |
| 246 | 249 | const name = settingEnvName(s.key); |
| 247 | 250 | return { |
src/apps/feature_schedule.ts +43 −1
| @@ -14,7 +14,8 @@ import { lastFires, recordFire } from "../core/feature_fires.ts"; | ||
| 14 | 14 | import { listApps, type AppRecord } from "../core/apps.ts"; |
| 15 | 15 | import { getRepoById } from "../core/repos.ts"; |
| 16 | 16 | import { callTool } from "./feature_tools.ts"; |
| 17 | import type { ScheduleEntry } from "./feature.ts"; | |
| 17 | import { SCHEDULE_EVERY, type ScheduleEntry } from "./feature.ts"; | |
| 18 | import type { Fired } from "../core/feature_fires.ts"; | |
| 18 | 19 | import { appsRuntime } from "./runtime.ts"; |
| 19 | 20 | |
| 20 | 21 | /** The entries whose interval has passed since they last fired (never fired: due). `lastFired` is ms by tool. */ |
| @@ -25,6 +26,47 @@ export function due(schedule: readonly ScheduleEntry[], lastFired: ReadonlyMap<s | ||
| 25 | 26 | }); |
| 26 | 27 | } |
| 27 | 28 | |
| 29 | /** One scheduled tool as a person or an agent reads it: plain data, no markup. */ | |
| 30 | export interface ScheduleLine { | |
| 31 | readonly tool: string; | |
| 32 | /** `15m`, `1h`, `6h` or `1d`. */ | |
| 33 | readonly every: string; | |
| 34 | /** When it last ran (ms), or undefined when it never has. */ | |
| 35 | readonly lastAt: number | undefined; | |
| 36 | readonly ok: boolean; | |
| 37 | /** The error's sentence when the last run failed. */ | |
| 38 | readonly error: string | null; | |
| 39 | /** When it is next due (ms), or undefined when it is due at the next look. */ | |
| 40 | readonly nextAt: number | undefined; | |
| 41 | } | |
| 42 | ||
| 43 | /** Each schedule entry beside its last fire: the one source of the line the page and the MCP tool both tell. */ | |
| 44 | export function scheduleLines(schedule: readonly ScheduleEntry[], fires: readonly Fired[], now: number): ScheduleLine[] { | |
| 45 | return schedule.map((e) => { | |
| 46 | const fire = fires.find((f) => f.tool === e.tool); | |
| 47 | const every = Object.entries(SCHEDULE_EVERY).find(([, ms]) => ms === e.everyMs)?.[0] ?? `${Math.round(e.everyMs / 60_000)}m`; | |
| 48 | const next = fire === undefined ? undefined : fire.at + e.everyMs; | |
| 49 | return { | |
| 50 | tool: e.tool, | |
| 51 | every, | |
| 52 | lastAt: fire?.at, | |
| 53 | ok: fire?.ok ?? false, | |
| 54 | error: fire === undefined || fire.ok ? null : (fire.error ?? "failed"), | |
| 55 | nextAt: next === undefined || next <= now ? undefined : next, | |
| 56 | }; | |
| 57 | }); | |
| 58 | } | |
| 59 | ||
| 60 | export const clockOf = (ms: number): string => new Date(ms).toISOString().slice(11, 16); | |
| 61 | ||
| 62 | /** `scan every 1h · last 10:02, ok · next 11:02` (UTC); a failure says its sentence in place of `ok`, a tool never run says so. */ | |
| 63 | export function scheduleText(line: ScheduleLine): string { | |
| 64 | const head = `${line.tool} every ${line.every}`; | |
| 65 | const next = line.nextAt === undefined ? "next at the next look" : `next ${clockOf(line.nextAt)}`; | |
| 66 | if (line.lastAt === undefined) return `${head} · never run · ${next}`; | |
| 67 | return `${head} · last ${clockOf(line.lastAt)}, ${line.error ?? "ok"} · ${next}`; | |
| 68 | } | |
| 69 | ||
| 28 | 70 | const inFlight = new Set<string>(); |
| 29 | 71 | |
| 30 | 72 | /** What `fireApp` needs from the running forge: the production port when running, and the declared schedule. */ |
src/web/pages/features.ts +5 −5
| @@ -19,7 +19,7 @@ import { | ||
| 19 | 19 | provenanceFacts, |
| 20 | 20 | } from "../../actions/features.ts"; |
| 21 | 21 | import { runNow } from "../../apps/feature_run.ts"; |
| 22 | import { runtimeReach, type Reach } from "../../apps/feature_schedule.ts"; | |
| 22 | import { clockOf, runtimeReach, scheduleLines, type Reach } from "../../apps/feature_schedule.ts"; | |
| 23 | 23 | import { getAppBySlug, type AppRecord } from "../../core/apps.ts"; |
| 24 | 24 | import { DAYS } from "../../apps/feature.ts"; |
| 25 | 25 | import type { FeedItem } from "../../feed/rank.ts"; |
| @@ -224,11 +224,11 @@ function featureView(ctx: Ctx, f: FeatureView, refused?: Refused, items: readonl | ||
| 224 | 224 | meta: html`${i.actor ?? i.app} · ${time(i.at)}`, |
| 225 | 225 | }))}</ul>`} |
| 226 | 226 | </section> |
| 227 | ${when(f.fires.length > 0, () => html`<section class="settings-block"> | |
| 227 | ${when(f.schedule.length > 0, () => html`<section class="settings-block"> | |
| 228 | 228 | <h3>Scheduled</h3> |
| 229 | <ul class="list">${f.fires.map((x) => row({ | |
| 230 | title: html`<span class="mono">${x.tool}</span>`, | |
| 231 | meta: html`${x.ok ? "ok" : html`<span class="danger">${x.error ?? "failed"}</span>`} · ${time(new Date(x.at).toISOString())} · ${x.durationMs} ms`, | |
| 229 | <ul class="list">${scheduleLines(f.schedule, f.fires, Date.now()).map((x) => row({ | |
| 230 | title: html`<span class="mono">${x.tool}</span> <span class="muted small">every ${x.every}</span>`, | |
| 231 | meta: html`${x.lastAt === undefined ? "never run" : html`last ${time(new Date(x.lastAt).toISOString())}, ${x.error === null ? "ok" : html`<span class="danger">${x.error}</span>`}`} · ${x.nextAt === undefined ? "next at the next look" : `next ${clockOf(x.nextAt)}`}`, | |
| 232 | 232 | trail: html`<form method="post" action="${base}/run/${encodeURIComponent(x.tool)}">${btn("Run now", { label: `Run ${x.tool} now` })}</form>`, |
| 233 | 233 | }))}</ul> |
| 234 | 234 | </section>`)} |
test/feature-schedule-line.test.ts added +28
| @@ -0,0 +1,28 @@ | ||
| 1 | /** The line Settings › Features and the MCP tell for a scheduled tool: ok, failed, never run. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { scheduleLines, scheduleText } from "../src/apps/feature_schedule.ts"; | |
| 4 | ||
| 5 | const HOUR = 3_600_000; | |
| 6 | const at = Date.parse("2026-11-09T10:02:00Z"); | |
| 7 | const entry = [{ tool: "scan", everyMs: HOUR }]; | |
| 8 | const text = (fires: Parameters<typeof scheduleLines>[1], now = at + 60_000) => scheduleLines(entry, fires, now).map(scheduleText); | |
| 9 | ||
| 10 | test("ok", () => { | |
| 11 | expect(text([{ tool: "scan", at, ok: true, error: null, durationMs: 5 }])).toEqual(["scan every 1h · last 10:02, ok · next 11:02"]); | |
| 12 | }); | |
| 13 | ||
| 14 | test("failed says the error's sentence", () => { | |
| 15 | expect(text([{ tool: "scan", at, ok: false, error: "the app answered 500", durationMs: 5 }])).toEqual([ | |
| 16 | "scan every 1h · last 10:02, the app answered 500 · next 11:02", | |
| 17 | ]); | |
| 18 | }); | |
| 19 | ||
| 20 | test("never run", () => { | |
| 21 | expect(text([])).toEqual(["scan every 1h · never run · next at the next look"]); | |
| 22 | }); | |
| 23 | ||
| 24 | test("overdue says next at the next look", () => { | |
| 25 | expect(text([{ tool: "scan", at, ok: true, error: null, durationMs: 5 }], at + 2 * HOUR)).toEqual([ | |
| 26 | "scan every 1h · last 10:02, ok · next at the next look", | |
| 27 | ]); | |
| 28 | }); |
test/features.test.ts +5 −1
| @@ -54,6 +54,7 @@ const FEATURE = { | ||
| 54 | 54 | scout: { type: "bool", default: true, label: "Search the web for ideas" }, |
| 55 | 55 | }, |
| 56 | 56 | tools: { scan: { description: "Run a scan now" }, latest: { description: "The last findings", writes: false } }, |
| 57 | schedule: [{ tool: "scan", every: "1h" }], | |
| 57 | 58 | }; |
| 58 | 59 | const FORGE_JSON = JSON.stringify({ app: { run: "bun run src/server.ts" }, feature: FEATURE }); |
| 59 | 60 | |
| @@ -350,10 +351,13 @@ describe("absorbing an app", () => { | ||
| 350 | 351 | const page = await (await web(adaCookie, "/settings/features/radar")).text(); |
| 351 | 352 | expect(page).toContain("<h3>Scheduled</h3>"); |
| 352 | 353 | expect(page).toContain("<b>the app answered 500</b>"); |
| 354 | expect(page).toContain("every 1h"); | |
| 353 | 355 | expect(page).not.toContain("<b>the app answered 500"); |
| 354 | 356 | expect(((await (await api("GET", adaToken, "/radar")).json()) as { fires: unknown[] }).fires).toHaveLength(1); |
| 355 | 357 | clearFires(app.db, appId); |
| 356 | expect(await (await web(adaCookie, "/settings/features/radar")).text()).not.toContain("<h3>Scheduled</h3>"); | |
| 358 | const never = await (await web(adaCookie, "/settings/features/radar")).text(); | |
| 359 | expect(never).toContain("never run"); | |
| 360 | expect(never).toContain("Run scan now"); | |
| 357 | 361 | }); |
| 358 | 362 | |
| 359 | 363 | test("the facts are plain and escaped", () => { |
new above
offline