import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { GRAIN, install, MODERATOR, network, report, SERVICE_DID } from "./network.ts"; process.env.STATE_DIR = mkdtempSync(join(tmpdir(), "agent-child-safety-")); process.env.GRAIN_URL = GRAIN; process.env.GRAIN_MOD_IDENTIFIER = MODERATOR; process.env.GRAIN_MOD_APP_PASSWORD = "app-password"; install(); // Imported after the environment is set: config reads it as it loads. const { childSafety, HUMAN, modelText, withheldFromModels } = await import("../src/child-safety.ts"); const { pollReports } = await import("../src/reports.ts"); const { investigate, investigationPrompt, moderationPrompt } = await import("../src/investigate.ts"); const { act, labelNames, modelRefusal } = await import("../src/moderate.ts"); const { getReviewSubject } = await import("../src/grain.ts"); const store = await import("../src/store.ts"); // Fixtures only: nothing here is a real account, record or report. const SECRET = "what the reporter wrote about a child"; const REPORTER = "did:plc:reporter"; const CSAE_LABEL = "did:plc:bylabel"; const BY_REASON = "did:plc:byreason"; const PRIOR = "did:plc:prior"; const UNREADABLE = "did:plc:unreadable"; const LABELED = "did:plc:labeled"; const CLEAN = "did:plc:clean"; const LATER = "did:plc:later"; const PHOTO = `at://${CSAE_LABEL}/social.grain.photo/3photo`; const GALLERY = `at://${CSAE_LABEL}/social.grain.gallery/3gallery`; const OTHER_GALLERY = `at://${BY_REASON}/social.grain.gallery/3other`; network.profiles.set(CSAE_LABEL, { handle: "flagged.example" }); network.profiles.set(BY_REASON, { handle: "byreason.example" }); network.records.set(PHOTO, { $type: "social.grain.photo", photo: { $type: "blob", ref: { $link: "bafysecretimage" } } }); network.records.set(GALLERY, { $type: "social.grain.gallery", title: "Gallery title" }); network.records.set(`at://${CSAE_LABEL}/social.grain.gallery.item/3item`, { gallery: GALLERY, item: PHOTO }); network.unreadable.add(UNREADABLE); network.csaeLabeled.add(LABELED); const byRef = (ref: string) => [...Array(50).keys()].map((i) => store.get(i + 1)).find((i) => i?.source_ref === ref); test("child safety is known by its label or its reason, as grain's labels::child_safety", () => { assert.ok(childSafety("csae")); assert.ok(childSafety("other", "tools.ozone.report.defs#reasonChildSafetyOther")); assert.ok(childSafety("other", "tools.ozone.report.defs#reasonChildSafetyCSAM")); assert.ok(!childSafety("spam")); assert.ok(!childSafety("spam", "com.atproto.moderation.defs#reasonSpam")); assert.ok(!childSafety("sexual", "com.atproto.moderation.defs#reasonSexual")); assert.ok(!childSafety("spam", "tools.ozone.report.defs#reasonMisleadingSpam")); }); test("reports about the safety of a child, or on an account that is, go to a person, and a model reads only ids, dates and HUMAN", async () => { // An account with an earlier child-safety report, closed long ago. report({ id: 1, subject: PRIOR, subjectDid: PRIOR, label: "csae", reason: SECRET, reportedBy: REPORTER, status: "dismissed" }); assert.equal(await pollReports(), 0); // A csae report by a classifier on a photo, which would carry an image link. report({ id: 2, subject: PHOTO, subjectDid: CSAE_LABEL, label: "csae", reason: `csam: ${SECRET} 0.99`, reportedBy: SERVICE_DID }); // A child-safety reason type under another label. report({ id: 3, subject: OTHER_GALLERY, subjectDid: BY_REASON, label: "other", reasonType: "tools.ozone.report.defs#reasonChildSafetyGroom", reason: SECRET, reportedBy: REPORTER }); // Innocuous reports on an account with a prior child-safety report, with a csae label, and one grain-serve cannot read. report({ id: 4, subject: PRIOR, subjectDid: PRIOR, label: "spam", reason: SECRET, reportedBy: REPORTER }); report({ id: 5, subject: LABELED, subjectDid: LABELED, label: "spam", reason: SECRET, reportedBy: REPORTER }); report({ id: 6, subject: UNREADABLE, subjectDid: UNREADABLE, label: "spam", reason: SECRET, reportedBy: REPORTER }); // And one on a clean account. report({ id: 7, subject: CLEAN, subjectDid: CLEAN, label: "spam", reason: "an ad", reportedBy: REPORTER }); assert.equal(await pollReports(), 6); for (const ref of ["2", "3", "4", "5", "6"]) { const item = byRef(ref)!; assert.equal(item.child_safety, 1, ref); // Straight to a person: never "new" (Clef's queue) or "investigate". assert.equal(item.status, "needs_review", ref); assert.equal(item.route_reason, "child safety, for a person only", ref); assert.equal(await withheldFromModels(item), true, ref); const subject = await getReviewSubject(item.subject_did).catch(() => ({ did: item.subject_did, counts: [], labels: [], reports: [], childSafety: true })); const seen = [item.report, modelText(item), investigationPrompt(item, ["grain"]), moderationPrompt(item, subject)]; for (const text of seen) { assert.ok(text.includes(HUMAN), ref); assert.ok(text.includes(`Report ${ref} on ${item.subject_did}, filed 2026-11-16T00:00:00.000Z.`), ref); for (const leak of [SECRET, "cdn.bsky.app", "bafysecretimage", "/social.grain.", "flagged.example", "byreason.example", "Gallery title", "Reason type", "Subject:"]) { assert.ok(!text.includes(leak), `${ref} shows ${leak}`); } } // Nothing starts: no session, no workspace. let session = ""; await assert.rejects( investigate(item, (id) => (session = id)), (err: Error) => err.message === HUMAN, ); assert.equal(session, ""); } // The person sees it whole on the dashboard. assert.match(byRef("2")!.text, /The csam classifier filed a csae report/); assert.match(byRef("2")!.text, /Image: https:\/\/cdn\.bsky\.app\//); assert.ok(byRef("3")!.text.includes(SECRET)); assert.ok(byRef("4")!.text.includes(SECRET)); const clean = byRef("7")!; assert.equal(clean.child_safety, 0); assert.equal(clean.status, "new"); assert.equal(await withheldFromModels(clean), false); assert.equal(modelText(clean), clean.text); // No model was reached: Clef (Workers AI), OpenRouter, or an image host. assert.deepEqual(network.elsewhere, []); }); test("a brief shows a report on a child-safety account by its ids alone", async () => { const subject = await getReviewSubject(PRIOR); assert.equal(subject.childSafety, true); const brief = moderationPrompt(byRef("7")!, subject); assert.ok(!brief.includes(SECRET)); assert.ok(brief.includes(HUMAN)); }); test("an account that turns child safety after its report came in is withheld from then on", async () => { store.enqueue({ source: "report", source_ref: "later", author: REPORTER, text: `Reason: ${SECRET}`, url: "u", received_at: "2026-11-16T00:00:00.000Z", subject_did: LATER, subject_uri: LATER }); network.csaeLabeled.add(LATER); const item = byRef("later")!; assert.equal(await withheldFromModels(item), true); const after = byRef("later")!; assert.equal(after.child_safety, 1); assert.ok(!after.report.includes(SECRET)); assert.ok(after.report.includes(HUMAN)); }); test("the agent may not give out csae nor act on a child-safety subject; a person may", async () => { assert.ok(!(await labelNames()).includes("csae")); network.calls = []; const clean = byRef("7")!; assert.equal(await modelRefusal(clean, { label: "csae" }), HUMAN); await assert.rejects(act(clean, { action: "label", label: "csae" }, "model"), (err: Error) => err.message === HUMAN); assert.equal(await modelRefusal(clean, { label: "spam" }), null); for (const ref of ["2", "3", "4", "5", "6"]) { const item = byRef(ref)!; assert.equal(await modelRefusal(item, {}), HUMAN, ref); for (const m of [{ action: "dismiss" as const }, { action: "label" as const, label: "spam" }, { action: "takedown" as const }]) { await assert.rejects(act(item, m, "model"), (err: Error) => err.message === HUMAN, `${ref} ${m.action}`); } } // An open child-safety report on the subject refuses it too, whatever the item says. const sneaky = { ...clean, child_safety: 0, subject_uri: PHOTO, subject_did: CLEAN }; await assert.rejects(act(sneaky, { action: "dismiss" }, "model"), (err: Error) => err.message === HUMAN); assert.deepEqual(network.calls, []); // The dashboard's buttons, a person's, still act. await act(byRef("4")!, { action: "dismiss" }, "person"); assert.equal(network.reports.find((r) => r.id === 4)!.status, "dismissed"); assert.equal(byRef("4")!.status, "done"); });