diff --git a/telemetry-worker/src/report-email.ts b/telemetry-worker/src/report-email.ts index 78ae25be..8f47bad5 100644 --- a/telemetry-worker/src/report-email.ts +++ b/telemetry-worker/src/report-email.ts @@ -80,19 +80,41 @@ export function subjectOf(r: Report): string { const p0 = r.items.filter((i) => i.p === 0); if (r.kind === "alert" && p0[0]) return `${test}[P0] ${p0[0].app}: ${p0[0].title}${p0.length > 1 ? ` (+${p0.length - 1} more)` : ""}`.slice(0, 200); const c = counts(counted(r)); - const name = r.apps.length === 1 ? r.apps[0]!.name : `${r.apps.length} apps`; const adj = ADJ[r.every] || "daily"; + if (!r.apps.length) return `${test}GenClass ${adj} report: add your first app`; + const name = r.apps.length === 1 ? r.apps[0]!.name : `${r.apps.length} apps`; const lead = `${test}GenClass ${adj} report, ${name}`; if (c[0] || (c[1] && measured(r).length)) return `${lead}: ${[c[0] ? `${c[0]} critical` : "", c[1] ? `${c[1]} important` : ""].filter(Boolean).join(", ")}`.slice(0, 200); if (!measured(r).length) return `${lead}: finish setting up`; return `${lead}: all clear`; } +const hiddenProblems = (r: Report): number => r.problemsHidden.reduce((n, x) => n + x.counts[0] + x.counts[1] + x.counts[2], 0); +/** What Free's half of this report leaves out, as short phrases (empty when nothing is left out). */ +function leftOut(r: Report): string[] { + const hp = hiddenProblems(r); + return [ + hp ? `${plural(hp, "more problem", "more problems")} with ${hp === 1 ? "its" : "their"} pages and fix prompts` : "", + r.actionsHidden ? `${plural(r.actionsHidden, "next step", "next steps")} from the summary, each with a prompt` : "", + r.suggestionsHidden ? plural(r.suggestionsHidden, "more suggestion", "more suggestions") : "", + r.designsHidden ? plural(r.designsHidden, "more website design", "more website designs") : "", + r.apps.some((x) => x.visitorsLimited && x.visitors) ? "where your visitors come from and what they read" : "", + ].filter(Boolean); +} +/** "1 critical, 2 important" for counts by priority. */ +const byP = (c: Record): string => ([0, 1, 2] as Priority[]).filter((k) => c[k]).map((k) => `${fmt(c[k])} ${PRIORITY_WORDS[k].toLowerCase()}`).join(", "); +/** One quiet line that says something is Pro's, with the upgrade link. */ +function lockedLine(r: Report, text: string): string { + const u = r.upgrade; + return `

🔒  ${text}${u ? `  ${a(u.url, `Unlock with ${u.plan} →`)}` : ""}

`; +} + /** The headline at the top of the report: what matters most, in a few words. */ function headlineOf(r: Report): { text: string; tone: Priority | null } { const c = counts(counted(r)); if (r.kind === "alert") return { text: r.items.length === 1 ? "Something critical needs you" : `${fmt(r.items.length)} critical problems need you`, tone: 0 }; if (c[0]) return { text: `${plural(c[0], "critical problem", "critical problems")}${c[1] ? ` and ${plural(c[1], "important one", "important ones")}` : ""}`, tone: 0 }; + if (!r.apps.length) return { text: "Add your first app to GenClass", tone: 1 }; if (!measured(r).length) return { text: notSending(r), tone: 1 }; if (c[1]) return { text: `${plural(c[1], "important thing", "important things")} to look at`, tone: 1 }; return { text: "All clear on your apps", tone: null }; @@ -214,10 +236,24 @@ function hero(r: Report): string { : ""; return card(`

${esc(kicker)}

● ${esc(h.text)}

-

${esc(plural(r.apps.length, "app", "apps"))} · ${esc(period)}

+

${r.apps.length ? `${esc(plural(r.apps.length, "app", "apps"))} · ${esc(period)}` : "Your GenClass account"}

${r.kind === "alert" ? "" : `${legend}
`}${tileRow}`); } +/** For an account with a Free app: this report is half, what the other half holds, and the trial. */ +function halfCard(r: Report): string { + const u = r.upgrade; + if (!u || !r.apps.length) return ""; + const out = leftOut(r); + const list = out.length ? `

${esc(u.plan)} shows the other half: ${esc(out.join(", "))}.

` : `

${esc(u.plan)} shows everything: every problem with its pages and fix prompts, the summary's next steps, every suggestion and design, and your visitors.

`; + return card( + `

You're seeing half of this report on Free

${list}${button(u.url, u.trialDays ? `Start the ${u.trialDays}-day free trial` : `Upgrade to ${u.plan}`)}

${esc(u.price)}${u.trialDays ? `, ${u.trialDays} days free to start` : ""}. Cancel any time.

`, + "", + "", + `border:1px solid ${C.green};background:${C.greenBg}`, + ); +} + function summaryCard(r: Report): string { const s = r.summary; if (!s) return ""; @@ -232,7 +268,7 @@ function summaryCard(r: Report): string { return card( `

GenClass summary

${esc(s.headline)}

-

${esc(s.text)}

${acts}${note("Written by a model from the numbers below; the priorities are GenClass's own rules.")}`, +

${esc(s.text)}

${acts}${r.actionsHidden ? lockedLine(r, `${plural(r.actionsHidden, "next step", "next steps")}, each with a prompt for your coding agent, ${r.actionsHidden === 1 ? "is" : "are"} in the other half.`) : ""}${note("Written by a model from the numbers below; the priorities are GenClass's own rules.")}`, "", "", `border-left:4px solid ${C.green}`, @@ -242,18 +278,17 @@ function summaryCard(r: Report): string { function needsCard(r: Report): string { const now = problemItems(r).filter((i) => i.p <= 1); const sec = r.items.filter((i) => i.section === "security" && i.p <= 1).length; - if (now.length) return card(itemRows(r, now, true), now.some((i) => i.p === 0) ? "Fix these first" : "Worth fixing", plural(now.length, "item", "items")); + // Free's half: the problems it leaves out, counted per app, never named + const locked = r.problemsHidden.map((x) => lockedLine(r, `${plural(x.counts[0] + x.counts[1] + x.counts[2], "more problem", "more problems")} on ${esc(x.app)} (${esc(byP(x.counts))}), with ${x.counts[0] + x.counts[1] + x.counts[2] === 1 ? "its" : "their"} pages and fix prompts.`)).join(""); + if (now.length) return card(itemRows(r, now, true) + locked, now.some((i) => i.p === 0) ? "Fix these first" : "Worth fixing", plural(now.length, "item", "items")); + if (locked) return card(p("The problems in the free half of this report are below.", "margin:8px 0 0") + locked, "Problems"); if (!measured(r).length) return ""; return card(p(`Nothing critical or important on your apps.${sec ? ` ${plural(sec, "item needs", "items need")} a look under Security.` : ""}`, "margin:0"), "Needs you"); } -function freeCard(r: Report): string { - const free = r.apps.filter((x) => !x.pro && x.totals.problems + x.totals.checks > 0); - if (!free.length || !r.upgrade) return ""; - return card(p(`${free.map((x) => `${esc(x.name)} ${esc(plural(x.totals.problems + x.totals.checks, "problem", "problems"))}`).join("  ·  ")}`, "margin:0") + note(`Free shows the totals. ${esc(r.upgrade.plan)} lists each one with its pages and a fix prompt.`, "margin-top:4px"), "Found on Free apps"); -} function appsCard(r: Report): string { + if (!r.apps.length) return ""; const hourly = r.every === "hourly"; const fixed = r.apps.some((x) => !x.demo && x.totals.fixed > 0); const th = (t: string, right = true) => `${esc(t)}`; @@ -320,7 +355,7 @@ function visitorsCard(r: Report): string { const list = (l: { key: string; count: number }[]) => l.map((y) => `${esc(y.key)} ${fmt(y.count)}`).join("  ·  "); const facts = [plural(v.visits, "visit", "visits"), plural(v.views, "page view", "page views"), v.seconds !== null ? `${fmt(v.seconds)} s a page` : "", v.rage ? plural(v.rage, "rage click", "rage clicks") : "", v.dead ? plural(v.dead, "dead click", "dead clicks") : ""].filter(Boolean); const line = (k: string, l: { key: string; count: number }[]) => (l.length ? `

${k}  ${list(l)}

` : ""); - return `${withV.length > 1 || r.apps.length > 1 ? title(x.name) : ""}

${esc(facts.join(" · "))}

${line("Top pages", v.pages)}${line("From", v.sources)}${line("Countries", v.countries)}${line("Devices", v.devices)}`; + return `${withV.length > 1 || r.apps.length > 1 ? title(x.name) : ""}

${esc(facts.join(" · "))}

${line("Top pages", v.pages)}${line("From", v.sources)}${line("Countries", v.countries)}${line("Devices", v.devices)}${x.visitorsLimited ? lockedLine(r, "The pages they read, where they came from, their countries and devices are in the other half.") : ""}`; }), ), "Visitors", @@ -353,7 +388,7 @@ function suggestionsCard(r: Report): string { (s, k) => `

${k + 1}.  ${esc(s.title)}

${s.why ? detail(s.why) : ""}${s.prompt ? promptAction(r, s.prompt, "Copy the prompt", true) : ""}`, ), ); - const more = r.suggestionsHidden && r.upgrade ? p(`${plural(r.suggestionsHidden, "more suggestion", "more suggestions")} with ${esc(r.upgrade.plan)}.`, "margin:12px 0 0") : ""; + const more = r.suggestionsHidden && r.upgrade ? lockedLine(r, `${plural(r.suggestionsHidden, "more suggestion", "more suggestions")}, each with a prompt, ${r.suggestionsHidden === 1 ? "is" : "are"} in the other half.`) : ""; return card(`${list}${more}${note("From your last genclass suggest: measured checks ordered by a model, and the model's own ideas (not measured).")}`, "Suggestions for your app", plural(r.suggestions.length, "idea", "ideas")); } @@ -377,9 +412,10 @@ function designCard(r: Report): string { ); } else if (r.designsHidden && r.upgrade) { out.push(p(`${plural(r.designsHidden, "design", "designs")} picked for your company, each with a full redesign prompt, ${r.designsHidden === 1 ? "comes" : "come"} with ${esc(r.upgrade.plan)}.`, "margin:12px 0 0")); - } else { + } else if (!r.designs.length) { out.push(p(`For designs picked for your company, run ${code("npx genclass suggest")} once: it reads what your site is about and finds the best sites in your field.`, "margin:12px 0 0")); } + if (r.designs.length && r.designsHidden) out.push(lockedLine(r, `${plural(r.designsHidden, "more design", "more designs")} picked for your company, each with a redesign prompt, ${r.designsHidden === 1 ? "is" : "are"} in the other half.`)); const name = r.apps[0]?.name ?? "my app"; out.push(`

Sites to learn from today

`); out.push( @@ -398,6 +434,7 @@ function designCard(r: Report): string { * measured app, no genclass suggest answer): what the next reports hold for them, by name, and how to get ideas now. */ function nextCard(r: Report): string { + if (!r.apps.length) return ""; const name = appsName(r); return card( `${p(`Once ${esc(name)} ${r.apps.length === 1 ? "sends its" : "send their"} first page load, this report lists what breaks there with the pages it happens on, ${r.apps.some((x) => x.analytics) ? "who visits and what they click, " : ""}and contrast and layout problems measured on your visitors' screens.`, "margin:8px 0 0")}${p(`For ideas picked for ${esc(name)} today, run ${code("npx genclass suggest")} in ${r.apps.length === 1 ? "its" : "an app's"} folder: GenClass checks the repository and the live site, then suggests what to fix and website designs from sites in your field.`, "margin:8px 0 0")}`, @@ -408,10 +445,11 @@ function nextCard(r: Report): string { function upgradeCard(r: Report): string { const u = r.upgrade; if (!u) return ""; - const hidden = r.apps.filter((x) => !x.pro).reduce((s, x) => s + x.totals.problems + x.totals.checks, 0); + if (!r.apps.length) return ""; + const out = leftOut(r); return card( - `

See everything in this report with ${esc(u.plan)}

-

${hidden ? `${plural(hidden, "problem", "problems")} on your Free ${r.apps.filter((x) => !x.pro).length === 1 ? "app" : "apps"} with their pages and fix prompts, ` : "Every problem with its pages and fix prompts, "}the GenClass summary, visitors, and website designs picked for your company. ${esc(u.price)}${u.trialDays ? `, ${u.trialDays} days free to start` : ""}.

+ `

Upgrade to see the whole report

+

${out.length ? `The other half: ${esc(out.join(", "))}. ` : ""}${esc(u.plan)} also watches every app every day with every problem's pages and fix prompts. ${esc(u.price)}${u.trialDays ? `, ${u.trialDays} days free to start` : ""}.

${button(u.url, u.trialDays ? `Start the ${u.trialDays}-day free trial` : `Get ${u.plan}`)}`, "", "", @@ -463,9 +501,9 @@ export function htmlOf(r: Report): string { ? [hero(r), card(itemRows(r, r.items, true), "What GenClass saw")].join("") : [ hero(r), + halfCard(r), summaryCard(r), needsCard(r), - freeCard(r), setupFirst ? setupCard(r) : "", appsCard(r), prsCard(r), @@ -514,6 +552,7 @@ export function textOf(r: Report): string { } const now = problemItems(r).filter((i) => i.p <= 1); out.push("WHAT NEEDS DOING", ...(now.length ? now.flatMap(textItem) : ["Nothing critical or important in this period.", ""])); + for (const x of r.problemsHidden) out.push(`Locked: ${plural(x.counts[0] + x.counts[1] + x.counts[2], "more problem", "more problems")} on ${x.app} (${byP(x.counts)}): in the other half, with ${r.upgrade?.plan ?? "Pro"}.`, ""); if (r.kind !== "alert") { out.push("YOUR APPS"); for (const x of r.apps) { @@ -542,7 +581,10 @@ export function textOf(r: Report): string { out.push("WHAT THIS REPORT WILL SHOW", `Once ${appsName(r)} sends its first page load: what breaks there and on which pages, and contrast and layout problems on your visitors' screens.`, "For ideas now, run npx genclass suggest in the app's folder."); } out.push(""); - if (r.upgrade) out.push(`${r.upgrade.plan}: everything in this report, ${r.upgrade.price}${r.upgrade.trialDays ? `, ${r.upgrade.trialDays} days free to start` : ""}: ${r.upgrade.url}`, ""); + if (r.upgrade && r.apps.length) { + const lo = leftOut(r); + out.push(`You're seeing half of this report on Free.${lo.length ? ` ${r.upgrade.plan} shows the other half: ${lo.join(", ")}.` : ""} ${r.upgrade.price}${r.upgrade.trialDays ? `, ${r.upgrade.trialDays} days free to start` : ""}: ${r.upgrade.url}`, ""); + } } out.push("--", `Sent to ${r.account.email} for your GenClass account.`, r.links.settings ? `Change how often, or stop these emails: ${r.links.settings}` : `Change how often: ${r.links.account}`, "Reply to reach a person at GenClass."); return out.join("\n"); diff --git a/telemetry-worker/src/report.ts b/telemetry-worker/src/report.ts index e5ba2661..7f7ec5a7 100644 --- a/telemetry-worker/src/report.ts +++ b/telemetry-worker/src/report.ts @@ -189,6 +189,8 @@ export interface AppReport { checks: { kind: string; name: string; count: number }[]; pages: { page: string; count: number }[]; visitors: Visitors | null; + /** A Free app's visitors: the numbers only, its lists (pages, sources, countries, devices) are Pro's. */ + visitorsLimited: boolean; prs: PullRequest[]; } @@ -231,6 +233,10 @@ export interface Report { items: Item[]; /** The model's summary, for an account with a Pro app; null without one. */ summary: Summary | null; + /** The summary's next steps a Free account does not see (Free sees the headline and the text). */ + actionsHidden: number; + /** Per Free app, the problems its half leaves out, counted by priority (Free sees the first half, most urgent first). */ + problemsHidden: { appId: string; app: string; counts: Record }[]; /** From the account's last `genclass suggest`: the plan's items and the model's ideas, and its website designs. */ suggestions: { title: string; why: string | null; prompt: string | null }[]; suggestionsHidden: number; @@ -262,6 +268,8 @@ const pairs = (list: { key: string; count: number }[] | undefined): { a: string; }); const plural = (k: number, one: string, many: string): string => `${Math.round(k).toLocaleString("en-US")} ${Math.round(k) === 1 ? one : many}`; const cleanPage = (p: string): string => line(p, 120) ?? "(no page)"; +/** Free's half of a list: the first half, rounded up (1 of 1, 1 of 2, 2 of 3). */ +export const halfOf = (n: number): number => Math.ceil(n / 2); /** The pages a kind was seen on, most first (detect_issue, possible_issue and check_issue are "kind TAB page"). */ function pagesOf(s: Stats, metric: string, kind: string): string[] { @@ -637,10 +645,13 @@ export function summaryFromAnswer(text: unknown): Summary | null { } } -/** What the model is given: per app the dashboard digest (Pro apps only) and the report's items. Never an email, a key or an id. */ +/** + * What the model is given: per app the digest of what its dashboard shows under its plan (a Free app's totals only) + * and the items the report lists (a Free app's visible half). Never an email, a key or an id. + */ export function summaryDigest(apps: { app: AppReport; stats: Stats }[], items: Item[]): Record { return { - apps: apps.filter((a) => a.app.pro && !a.app.demo).map((a) => appDigest(a.stats, a.app.analytics)), + apps: apps.filter((a) => !a.app.demo).map((a) => appDigest(a.stats, a.app.analytics)), items: items.filter((i) => i.section !== "security" || i.appId).slice(0, 30).map((i) => ({ priority: i.p, app: i.app, title: i.title, detail: i.detail, pages: i.pages })), account_security: items.filter((i) => i.section === "security" && !i.appId).map((i) => i.title), }; @@ -723,6 +734,12 @@ export interface AccountRow { } export const ACCOUNT_REPORT_COLUMNS = "id, email, email_verified, report_every, report_next, report_alerts, report_model, report_sent"; +/** A Free app's half of the visitors: the numbers, when there are any, and none of the lists. */ +function freeVisitors(s: Stats): Visitors | null { + const v = visitorsOf(s); + return v.visits + v.views > 0 ? { ...v, pages: [], sources: [], countries: [], devices: [] } : null; +} + function visitorsOf(s: Stats): Visitors { const t = s.totals, b = s.breakdowns; const views = n(t, "ix_views"); @@ -774,7 +791,8 @@ export function appReport(p: Project, access: Access, s: Stats, origin: string, kinds: (b.detect_diagnosis ?? []).filter((x) => x.count > 0).slice(0, 12).map((x) => ({ kind: x.key, name: kindName(x.key), count: x.count })), checks: (b.check_kind ?? []).filter((x) => x.count > 0).slice(0, 12).map((x) => ({ kind: x.key, name: checkName(x.key), count: x.count })), pages: (b.page_route ?? []).slice(0, 5).map((x) => ({ page: cleanPage(x.key), count: x.count })), - visitors: hasAnalytics(access) && pro && !s.demo ? visitorsOf(s) : null, + visitors: s.demo ? null : hasAnalytics(access) && pro ? visitorsOf(s) : freeVisitors(s), + visitorsLimited: !s.demo && !(hasAnalytics(access) && pro), prs: [], }; } @@ -799,44 +817,68 @@ export async function buildReport(env: Env, a: AccountRow, o: BuildOptions): Pro const every = everyOf(a.report_every); const now = o.now; const access = (await accountProjectsAccess(db, a.id, env, now)).slice(0, MAX_APPS); - if (!access.length) return null; const ids = access.map((x) => x.row.id); - const projects = await rowsOf(db, `SELECT ${PROJECT_COLUMNS} FROM projects WHERE id IN (${ids.map(() => "?").join(", ")})`, ...ids); + const projects = ids.length ? await rowsOf(db, `SELECT ${PROJECT_COLUMNS} FROM projects WHERE id IN (${ids.map(() => "?").join(", ")})`, ...ids) : []; const byId = new Map(projects.map((p) => [p.id, p])); - const apps: { app: AppReport; stats: Stats; s24: Stats | null; access: Access }[] = []; + const apps: { app: AppReport; stats: Stats; full: Stats; s24: Stats | null; access: Access }[] = []; for (const { row, access: acc } of access) { const p = byId.get(row.id); if (!p) continue; const range = pickRange(RANGE_OF[every === "off" ? DEFAULT_EVERY : every], viewRanges(acc.plan)); - // what the app's own dashboard shows under its plan, never more (src/collector.ts applies the same two cuts) - let s = await stats(db, p, range, now); + // `s` is what the app's own dashboard shows under its plan (src/collector.ts applies the same two cuts): the + // numbers and the model's digest come from it. A Free app's items come from everything measured (`full`) and are + // halved below: Free sees half of the report (the owner's call, 2026-11-14) + const full = await stats(db, p, range, now); + let s = full; if (!hasAnalytics(acc)) s = stripAnalytics(s, { plan: firstPlanWith("analytics")?.name ?? null, upgrade_url: `${origin}/account/billing` }); if (!hasProblems(acc)) s = stripProblems(s, problemsLock(`${origin}/account/billing`)); // the hour rules read 24 hours; a longer report reads them once more, for apps that sent something today const s24 = range !== "24h" && p.last_event && now.getTime() - Date.parse(p.last_event) < 86_400_000 ? await stats(db, p, "24h", now) : null; - apps.push({ app: appReport(p, acc, s, origin, every), stats: s, s24, access: acc }); + apps.push({ app: appReport(p, acc, hasProblems(acc) ? s : full, origin, every), stats: s, full, s24, access: acc }); } - if (!apps.length) return null; - const from = apps.reduce((m, x) => (x.stats.from < m ? x.stats.from : m), apps[0]!.stats.from); - const prs = await pullRequests(db, apps.map((x) => x.app.id), from); + const from = apps.length ? apps.reduce((m, x) => (x.stats.from < m ? x.stats.from : m), apps[0]!.stats.from) : new Date(now.getTime() - 86_400_000).toISOString(); + const prs = apps.length ? await pullRequests(db, apps.map((x) => x.app.id), from) : new Map(); for (const x of apps) x.app.prs = prs.get(x.app.id) ?? []; let items: Item[] = []; - for (const x of apps) items.push(...appItems(x.app, x.stats, x.s24, origin, now)); + for (const x of apps) items.push(...appItems(x.app, x.app.pro ? x.stats : x.full, x.s24, origin, now)); + // an account with no app yet is told how to add one: everyone with an email gets the report + if (!apps.length) { + items.push({ p: 1, rule: "setup:first-app", appId: "", app: "Your GenClass account", title: "Add your first app", detail: "Once your web app is added, GenClass watches it for you: what breaks for your visitors, on which pages, and a fix prompt for each. It takes a minute on genclass.dev.", pages: [], prompt: null, link: { label: "Add your first app", href: `${origin}/account` }, section: "setup" }); + } items.push(...(await accountEvents(db, a.id, from, origin))); if (!(await rowsOf(db, "SELECT 1 AS x FROM github_installations WHERE owner = ? LIMIT 1", a.id)).length && env.GITHUB_APP_ID) { items.push({ p: 2, rule: "setup:github", appId: "", app: "Your GenClass account", title: "Connect GitHub so GenClass reviews your pull requests", detail: "The GitHub App reads your test runs' race files and comments on the pull request that adds a problem, before it ships. Free on public repositories.", pages: [], prompt: null, link: { label: "Add the GitHub App", href: `${origin}/github` }, section: "setup" }); } items = byPriority(items); + // Free sees half (the owner's call, 2026-11-14): of each Free app's problems the first half, most urgent first; the + // rest is counted, with Pro's upgrade, never listed + const problemsHidden: Report["problemsHidden"] = []; + const left = new Set(); + for (const x of apps) { + if (x.app.pro) continue; + const mine = items.filter((i) => i.section === "problems" && i.appId === x.app.id); + const rest = mine.slice(halfOf(mine.length)); + if (!rest.length) continue; + for (const i of rest) left.add(i); + problemsHidden.push({ appId: x.app.id, app: x.app.name, counts: { 0: rest.filter((i) => i.p === 0).length, 1: rest.filter((i) => i.p === 1).length, 2: rest.filter((i) => i.p === 2).length } }); + } + items = items.filter((i) => !left.has(i)); + const anyPro = apps.some((x) => x.app.pro); const sugg = await suggestAnswers(db, a.id); - const shownSugg = anyPro ? sugg.items.slice(0, 5) : sugg.items.slice(0, 1); + const suggAll = sugg.items.slice(0, 5), designsAll = sugg.designs.slice(0, 3); + const shownSugg = anyPro ? suggAll : suggAll.slice(0, halfOf(suggAll.length)); + const shownDesigns = anyPro ? designsAll : designsAll.slice(0, halfOf(designsAll.length)); const lock = firstPaidWith("problems"); const freeApp = apps.find((x) => !x.app.pro); - const summary = anyPro && o.model ? await askSummary(env, o.fetch, summaryDigest(apps, items)).catch(() => null) : null; + // every account with a measured or connected app gets the summary; on Free its next steps are counted, not shown + let summary = o.model && apps.some((x) => !x.app.demo) ? await askSummary(env, o.fetch, summaryDigest(apps, items)).catch(() => null) : null; + const actionsHidden = summary && !anyPro ? summary.actions.length : 0; + if (summary && !anyPro) summary = { ...summary, actions: [] }; const settingsToken = await accountToken(env, a.id); const q = settingsToken ? `a=${encodeURIComponent(a.id)}&t=${encodeURIComponent(settingsToken)}` : null; @@ -848,10 +890,12 @@ export async function buildReport(env: Env, a: AccountRow, o: BuildOptions): Pro apps: apps.map((x) => x.app), items, summary, + actionsHidden, + problemsHidden, suggestions: shownSugg, - suggestionsHidden: Math.max(0, Math.min(sugg.items.length, 5) - shownSugg.length), - designs: anyPro ? sugg.designs.slice(0, 3) : [], - designsHidden: anyPro ? 0 : Math.min(3, sugg.designs.length), + suggestionsHidden: suggAll.length - shownSugg.length, + designs: shownDesigns, + designsHidden: designsAll.length - shownDesigns.length, showcase: showcaseFor(now), upgrade: freeApp && lock ? { plan: lock.name, trialDays: lock.trial_days ?? null, price: priceText(lock), url: `${origin}/account/billing?project=${encodeURIComponent(freeApp.app.id)}` } : null, links: { @@ -870,7 +914,7 @@ export async function buildReport(env: Env, a: AccountRow, o: BuildOptions): Pro for (const s of report.summary?.actions ?? []) if (s.prompt) prompts.add(s.prompt); for (const s of report.suggestions) if (s.prompt) prompts.add(s.prompt); for (const d of report.designs) prompts.add(d.prompt); - const name = apps[0]!.app.name; + const name = apps[0]?.app.name ?? "my app"; for (const s of report.showcase) prompts.add(showcasePrompt(s, name)); for (const p of [...prompts].slice(0, 60)) { const sig = await promptSignature(env, p); diff --git a/telemetry-worker/test/report.test.ts b/telemetry-worker/test/report.test.ts index 88a3e27a..1897f205 100644 --- a/telemetry-worker/test/report.test.ts +++ b/telemetry-worker/test/report.test.ts @@ -304,19 +304,66 @@ describe("reports: one account's report", () => { expect(text).toContain(long.trim().slice(0, 200)); }); - it("a Free app's report has the totals and Pro's box, never its pages or prompts", async () => { + it("a Free account sees half: the first half of each app's problems, the rest counted with Pro's upgrade (owner, 2026-11-14)", async () => { const { env, sql } = world(); const { cookie, account } = await signedIn(env); const app = await createApp(env, cookie, "Blog"); - await sendBatch(env, app.token, [page("/secret-admin"), ...detection("stale", "/secret-admin")]); + // two problems: a failing endpoint (P0) on /api/pay, stale responses (P1) on /secret-admin + await sendBatch(env, app.token, [page("/secret-admin"), ...detection("stale", "/secret-admin"), ...detection("failing", "/api/pay"), ...detection("failing", "/api/pay"), ...detection("failing", "/api/pay")]); const r = (await buildReport(env, accountRow(sql, account.id), { kind: "report", now: now(), model: false, fetch: noModel }))!.report; - expect(r.apps[0]).toMatchObject({ pro: false, kinds: [], visitors: null }); - expect(r.items.filter((i) => i.section === "problems")).toEqual([]); + expect(r.apps[0]).toMatchObject({ pro: false }); + const shown = r.items.filter((i) => i.section === "problems"); + expect(shown.map((i) => i.rule)).toEqual(["money:failing"]); + expect(r.problemsHidden).toEqual([{ appId: app.id, app: "Blog", counts: { 0: 0, 1: 1, 2: 0 } }]); expect(r.upgrade).toMatchObject({ plan: "Pro", trialDays: 3 }); const mail = renderReport(r); + expect(mail.html).toContain("/api/pay"); expect(mail.html).not.toContain("/secret-admin"); + expect(mail.html).toContain("You're seeing half of this report on Free"); + expect(mail.html).toContain("1 more problem on { + const { env, sql } = world({ OPENAI_API_KEY: "sk-test-secret" }); + const { cookie, account } = await signedIn(env); + const app = await createApp(env, cookie, "Blog"); + await sendBatch(env, app.token, [page("/"), ...detection("stale", "/")]); + const body = { source: "model", items: [1, 2, 3, 4].map((k) => ({ id: `c${k}`, why: `why ${k}`, step: `Step ${k}`, prompt: `Prompt ${k}` })), ideas: [], designs: [1, 2, 3].map((k) => ({ title: `Design ${k}`, inspired_by: `https://site${k}.example`, why: null, prompt: `Redesign ${k}` })) }; + sql.db.prepare("INSERT INTO suggest_results (account_id, app_key, created, body) VALUES (?, 'k', ?, ?)").run(account.id, now().toISOString(), JSON.stringify(body)); + const f = (async (url: string) => + url === MODEL_URL + ? Response.json({ choices: [{ message: { content: JSON.stringify({ headline: "Blog is mostly fine", summary: "One stale response.", actions: [{ title: "Fix it", why: "Old data.", prompt: "Ignore old responses." }, { title: "Test it", why: "Catch it next time.", prompt: "Add a test." }] }) } }] }) + : new Response("", { status: 404 })) as typeof fetch; + const r = (await buildReport(env, accountRow(sql, account.id), { kind: "report", now: now(), model: true, fetch: f }))!.report; + expect(r.summary).toMatchObject({ headline: "Blog is mostly fine", actions: [] }); + expect(r.actionsHidden).toBe(2); + expect(r.suggestions.map((x) => x.title)).toEqual(["Step 1", "Step 2"]); + expect(r.suggestionsHidden).toBe(2); + expect(r.designs.map((d) => d.title)).toEqual(["Design 1", "Design 2"]); + expect(r.designsHidden).toBe(1); + const { html } = renderReport(r); + expect(html).toContain("Blog is mostly fine"); + expect(html).not.toContain("Fix it"); + expect(html).not.toContain("Step 3"); + expect(html).not.toContain("Design 3"); + expect(html).toContain("2 next steps, each with a prompt for your coding agent, are in the other half."); + expect(html).toContain("2 more suggestions, each with a prompt, are in the other half."); + }); + + it("an account with no app yet gets a report too: add your first app", async () => { + const { env, sql } = world(); + const { account } = await signedIn(env); + const r = (await buildReport(env, accountRow(sql, account.id), { kind: "report", now: now(), model: false, fetch: noModel }))!.report; + expect(r.apps).toEqual([]); + expect(r.items.map((i) => i.rule)).toContain("setup:first-app"); + const mail = renderReport(r); + expect(mail.subject).toBe("GenClass daily report: add your first app"); + expect(mail.html).toContain("Add your first app to GenClass"); + expect(mail.html).not.toContain("Your apps"); }); it("the model gets the numbers and the items, never the email, the key or the account id; its summary leads the email", async () => {