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

Parked opened this
Closed without merging

scout/forge:scout/10200-settings-features-shows-each-scheduled-t → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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
8282
8383 ## Schedule
8484
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](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](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.
8686
8787 ## Not done yet
8888
src/actions/features.ts +4 −1
@@ -20,7 +20,7 @@
2020 import { lastFires, type Fired } from "../core/feature_fires.ts";
2121 import type { App } from "../app.ts";
2222 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";
2424 import { keptSettings as openSettings, sealSetting } from "../apps/feature_env.ts";
2525 import type { ToolSpec } from "../apps/feature_tools.ts";
2626 import { appsRuntime } from "../apps/runtime.ts";
@@ -91,6 +91,8 @@ export interface FeatureView {
9191 readonly tools: readonly ToolSpec[];
9292 /** How the forge's last call of each scheduled tool went; empty when none has fired. */
9393 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[];
9496 /** Where the app comes from: the upstream only when the reader may read it. */
9597 readonly provenance: Provenance;
9698 }
@@ -242,6 +244,7 @@ function viewOf(
242244 provenance,
243245 tools: spec?.tools ?? [],
244246 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)),
245248 settings: (spec?.settings ?? []).map((s) => {
246249 const name = settingEnvName(s.key);
247250 return {
src/apps/feature_schedule.ts +43 −1
@@ -14,7 +14,8 @@ import { lastFires, recordFire } from "../core/feature_fires.ts";
1414 import { listApps, type AppRecord } from "../core/apps.ts";
1515 import { getRepoById } from "../core/repos.ts";
1616 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";
1819 import { appsRuntime } from "./runtime.ts";
1920
2021 /** 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
2526 });
2627 }
2728
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
2870 const inFlight = new Set<string>();
2971
3072 /** 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 {
1919 provenanceFacts,
2020 } from "../../actions/features.ts";
2121 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";
2323 import { getAppBySlug, type AppRecord } from "../../core/apps.ts";
2424 import { DAYS } from "../../apps/feature.ts";
2525 import type { FeedItem } from "../../feed/rank.ts";
@@ -224,11 +224,11 @@ function featureView(ctx: Ctx, f: FeatureView, refused?: Refused, items: readonl
224224 meta: html`${i.actor ?? i.app} · ${time(i.at)}`,
225225 }))}</ul>`}
226226 </section>
227 ${when(f.fires.length > 0, () => html`<section class="settings-block">
227 ${when(f.schedule.length > 0, () => html`<section class="settings-block">
228228 <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)}`}`,
232232 trail: html`<form method="post" action="${base}/run/${encodeURIComponent(x.tool)}">${btn("Run now", { label: `Run ${x.tool} now` })}</form>`,
233233 }))}</ul>
234234 </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 = {
5454 scout: { type: "bool", default: true, label: "Search the web for ideas" },
5555 },
5656 tools: { scan: { description: "Run a scan now" }, latest: { description: "The last findings", writes: false } },
57 schedule: [{ tool: "scan", every: "1h" }],
5758 };
5859 const FORGE_JSON = JSON.stringify({ app: { run: "bun run src/server.ts" }, feature: FEATURE });
5960
@@ -350,10 +351,13 @@ describe("absorbing an app", () => {
350351 const page = await (await web(adaCookie, "/settings/features/radar")).text();
351352 expect(page).toContain("<h3>Scheduled</h3>");
352353 expect(page).toContain("&lt;b&gt;the app answered 500&lt;/b&gt;");
354 expect(page).toContain("every 1h");
353355 expect(page).not.toContain("<b>the app answered 500");
354356 expect(((await (await api("GET", adaToken, "/radar")).json()) as { fires: unknown[] }).fires).toHaveLength(1);
355357 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");
357361 });
358362
359363 test("the facts are plain and escaped", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs