Files
ipal-kit/src/modules/doctor/rules/live.ts
T
Radosław SmolińskiandClaude Opus 5.5 a4e8cfeae8 ipal-doctor: one command that checks a site against the team rules
bin ipal-doctor + @intecion/ipal-kit/doctor: static rules (versions, Node 22, AGENT.md §3
layout and naming, database names, env rules, headers, X-Powered-By, CSP, 2FA, cookie
scanner, adminMeta, AGENTS.md, skills, seeds, block library via blockLibraryManifest,
ipal-audit --quick), registry rules (latest plugin, pnpm audit --prod), live rules (--url,
ipal-seo-check), deep rules (--deep via payload run; GET /api/ipal/doctor, off until
IPAL_DOCTOR_TOKEN). Human / JSON output, --strict, exit codes 0/1/2/3, --sites drift table,
ipal-doctor.json with reasons, until, files, seen.

Fixes found on the way: sitemap leaves out the System pages 404 page; ipal-audit finds
array labels after fields, any form seed, runs grep in UTF-8; ipal-seo-check counts
duplicate titles per language. Reference site at zero findings (static), CI steps on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 16:03:00 +01:00

95 lines
4.2 KiB
TypeScript

import { existsSync } from 'node:fs'
import { join } from 'node:path'
import type { DoctorLive, DoctorRule, LiveResponse, RuleFinding } from '../types.js'
import { run } from '../util.js'
import { noindex } from './security.js'
import { parseToolOutput, titleSlug } from './tooling.js'
/** GET-only fetcher with a cache (each path is requested once per run) and a doctor user agent. */
export function createLiveFetcher(url: string, options: { adminRoute?: string; staging?: boolean } = {}): DoctorLive {
const base = url.replace(/\/+$/, '')
const cache = new Map<string, Promise<LiveResponse>>()
const fetchOnce = (path: string, init: { redirect?: 'follow' | 'manual' } = {}) => {
const key = `${init.redirect ?? 'follow'} ${path}`
if (!cache.has(key)) {
cache.set(
key,
(async () => {
try {
const res = await fetch(base + path, {
headers: { 'user-agent': 'ipal-doctor (+https://intecion.com; read-only)' },
redirect: init.redirect ?? 'follow',
signal: AbortSignal.timeout(20_000),
})
return { headers: res.headers, status: res.status, text: await res.text() }
} catch (err) {
const e = err as { cause?: { code?: string }; message?: string }
return { error: e.cause?.code ?? e.message ?? 'unreachable', headers: new Headers(), status: 0, text: '' }
}
})(),
)
}
return cache.get(key)!
}
return { adminRoute: options.adminRoute ?? '/admin', fetch: fetchOnce, staging: options.staging ?? false, url: base }
}
export const liveRules: DoctorRule[] = [
{
id: 'live-reachable',
check: async (ctx) => {
const home = await ctx.live!.fetch('/', { redirect: 'manual' })
return home.status ? [] : [{ message: `${ctx.live!.url} is unreachable (${home.error})`, severity: 'error' }]
},
group: 'live',
source: 'live',
},
{
id: 'live-health',
check: async (ctx) => {
const res = await ctx.live!.fetch('/api/health')
return res.status === 200 ? [] : [{ fix: 'src/app/api/health/route.ts → healthRoute (Coolify health check)', message: `/api/health → ${res.status || res.error}`, severity: 'error' }]
},
docs: 'docs/deployment.md',
group: 'live',
source: 'live',
},
{
id: 'live-noindex',
check: async (ctx) => {
const home = await ctx.live!.fetch('/')
if (!home.status) {return []}
const isNoindex = noindex(home.text, home.headers.get('x-robots-tag'))
if (ctx.live!.staging) {return isNoindex ? [] : [{ fix: 'IPAL_NOINDEX=1 on the staging app', message: 'the staging site is indexable', severity: 'error' }]}
return isNoindex ? [{ fix: 'unset IPAL_NOINDEX on production and redeploy', message: 'the production home page is noindex', severity: 'error' }] : []
},
docs: 'docs/seo.md',
group: 'live',
source: 'live',
},
{
id: 'live-seo',
check: async (ctx) => {
// ipal-seo-check of the doctor's own version: robots, sitemap, canonical, hreflang, noindex, JSON-LD, llms, MCP.
const script = join(ctx.packageRoot, 'docs/agent/skills/ipal-seo-check/scripts/seo-check.mjs')
if (!existsSync(script)) {return [{ message: 'ipal-seo-check not found', severity: 'warning' }]}
const home = await ctx.live!.fetch('/', { redirect: 'manual' })
if (!home.status) {return []}
const res = run(process.execPath, [script, ctx.live!.url, '--max', '10'], ctx.root, 300_000)
const out: RuleFinding[] = []
// INFO lines are notes (e.g. "no verification meta" with DNS verification) — not findings.
for (const line of parseToolOutput(res.stdout).filter((l) => l.level !== 'info')) {
const items = line.items.length ? `: ${line.items.slice(0, 3).join('; ')}${line.items.length > 3 ? ` … +${line.items.length - 3}` : ''}` : ''
out.push({ fix: line.rule, message: `${line.title}${items}`, rule: `live-seo:${titleSlug(line.title.replace(/[\d/]+/g, ' '))}`, severity: line.level })
}
if (res.code === 2) {out.push({ message: `ipal-seo-check: ${res.stderr.trim()}`, severity: 'error' })}
return out
},
docs: 'docs/agent/skills/ipal-seo-check/SKILL.md',
group: 'live',
source: 'live',
},
]