caelum0x 5ccd834941 feat(watch): uptime checks with a public status page per app and owner email through Rally Send (PY2.2)
RYP-E4, dispatch 4.2 PY2.2 (Railyard side):
- lib/watch: checks (public https app addresses only; IPs, private names and
  our own hosts refused), 20 per org, a status page slug each. The box 1
  agent reads GET /api/v1/watch/agent/checks (ids and URLs only, bearer
  WATCH_AGENT_TOKEN) and posts results to /api/v1/watch/agent/results.
  Two failures in a row turn a check red; each change emails the owner once
  through Rally Send (product railyard, from status@, idempotency key per
  change; a failed send is retried on the next result). Older, unknown and
  future results change nothing.
- /status/<slug>: live results only (state, share of the last checks, bars),
  the check's name and never its address or any host or vendor name.
- The Watch screen: add by address, pause or resume, open the status page.
- Bridge route uptime (contract cases, uptime_check_keys): a check for the
  given address, or the app's latest live deploy; the first result is the
  slot's proof (check_result).
- Gated on RAILYARD_WATCH_LIVE (denylist rule railyard-slot-watch): off, no
  check runs, the pages 404, the nav has no Watch item and the slot answers
  arriving. The copy test lints each gated surface with the flag and proves it
  checks the flag first.
- Migration 0006 (watch_checks, watch_status; https only, history capped at
  90, cascades with the org).

Tests: watch 10/10, bridge 25/25, contract 3/3, migration 7/7 (0006 twice on a
scratch cluster), watch copy 7/7, console 108/108 with the bundle build,
tsc clean, SDK 7/7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:24:06 +03:00

56 lines
2.9 KiB
TypeScript

// HTTP handlers for Railyard Watch (Wave P PY2.2), framework-free.
import type { Repo } from "../repo.ts";
import { agentAuthorized, agentChecks, ARRIVING, createCheck, listWithState, recordResults, WatchError, type WatchDeps } from "./service.ts";
const reply = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json", "cache-control": "no-store" } });
/** GET /api/v1/watch/agent/checks: the box 1 agent's list (bearer WATCH_AGENT_TOKEN). */
export async function agentList(deps: WatchDeps, req: Request, now: Date): Promise<Response> {
if (!deps.agentToken) return reply({ error: "watch_agent_not_configured" }, 503);
if (!agentAuthorized(deps, req.headers.get("authorization"))) return reply({ error: "unauthorized" }, 401);
return reply({ checks: await agentChecks(deps, now) });
}
/** POST /api/v1/watch/agent/results: `{results:[{id, ok, at, ms}]}` from the agent. */
export async function agentResults(deps: WatchDeps, req: Request, now: Date): Promise<Response> {
if (!deps.agentToken) return reply({ error: "watch_agent_not_configured" }, 503);
if (!agentAuthorized(deps, req.headers.get("authorization"))) return reply({ error: "unauthorized" }, 401);
const body = await req.json().catch(() => null);
try {
return reply(await recordResults(deps, body, now));
} catch (e) {
if (e instanceof WatchError) return reply({ error: e.message }, e.status);
throw e;
}
}
/**
* POST /api/watch (signed-in console user, same origin):
* `{app, url, name?}` adds a check for one of the org's apps;
* `{id, paused}` pauses or resumes one of the org's checks.
*/
export async function consoleWatch(
deps: WatchDeps,
repo: Repo,
session: { org: string; email: string } | null,
body: unknown,
): Promise<{ status: number; body: Record<string, unknown> }> {
if (!session) return { status: 401, body: { error: "Sign in again." } };
if (!deps.live) return { status: 503, body: { error: ARRIVING } };
const b = (body ?? {}) as Record<string, unknown>;
try {
if (typeof b.id === "string" && typeof b.paused === "boolean") {
const c = await deps.store.setPaused(session.org, b.id, b.paused);
return c ? { status: 200, body: { check: { id: c.id, paused: c.paused } } } : { status: 404, body: { error: "There is no such check in your organization." } };
}
if (typeof b.app !== "string" || !(await repo.findAppByName(session.org, b.app))) return { status: 404, body: { error: "Pick one of your apps." } };
const email = /@/.test(session.email) && !session.email.endsWith(".invalid") ? session.email : null;
const c = await createCheck(deps, { orgId: session.org, app: b.app, name: b.name, url: b.url, ownerEmail: email });
return { status: 200, body: { checks: await listWithState(deps, session.org, c.app) } };
} catch (e) {
if (e instanceof WatchError) return { status: e.status, body: { error: e.message } };
throw e;
}
}