~ knotless Search/
↑↓ move↵ open⇧↵ all results

grain.social/grain-support-agent

grain's support agent: triages mentions and reports, investigates bugs, proposes fixes

1 collaborator
0
0
main
124 lines · 5.4 KB Raw History
import { getReviewSubject, type Report } from "./grain.ts";
import { update, type Item } from "./store.ts";

// What a report or label about the safety of a child is about is never shown
// to a model, and acting on it is for a person, as grain's own MCP server
// (grain/crates/grain-mcp) holds it. A report is child safety by its label or
// its reason type (childSafety). An account is child safety when grain-serve
// says so: its getReviewSubject answers `childSafety`, true when a
// child-safety report was ever made against the account or one of its
// records, in any state, or a csae label ever given one of them, negated or
// not. Every report on such an account is treated as child safety too. When
// grain-serve cannot say, the answer is that it is.
//
// Such a report is kept for a person, whole, on the dashboard. What any model
// can read of it (the queue's brief, prompts, the support tools) is its ids,
// its date and HUMAN; it never goes to Clef, to an investigation or to a fix,
// and the agent's moderation refuses to act on its subject.

export const CSAE = "csae";

/** What the service says to a model in place of what concerns the safety of a child. */
export const HUMAN =
  "This concerns the safety of a child. What it is about is never shown to a model, and acting on it is for a person: an admin handles it with the grain command line, `grain admin reports --label csae` and `grain admin account <did>`.";

/**
 * Ozone's reason types. grain's labels::label_for files every one of them
 * starting `ChildSafety` under csae, and no other reason type.
 */
const OZONE_CHILD_SAFETY = "tools.ozone.report.defs#reasonChildSafety";

/**
 * Whether a report or label of `label`, or made for the reason type `reason`,
 * is about the safety of children: grain's labels::child_safety.
 */
export function childSafety(label: string, reason?: string | null): boolean {
  return label === CSAE || Boolean(reason?.startsWith(OZONE_CHILD_SAFETY));
}

export const childSafetyReport = (r: Pick<Report, "label" | "reasonType">) => childSafety(r.label, r.reasonType);

// An account found child safety stays so: grain-serve's answer counts reports
// and labels ever made, whatever became of them. One found clear is asked
// again after a short while, and a failed lookup is never kept.
const CLEAR_MS = 30_000;
const childSafetyAccounts = new Set<string>();
const clearUntil = new Map<string, number>();

/** Records that the account `did` is child safety, as a report on it just showed. */
function remember(did: string): void {
  childSafetyAccounts.add(did);
  clearUntil.delete(did);
}

/** Whether the account `did` is child safety, by grain-serve's word; true when it cannot say. */
export async function childSafetyAccount(did: string): Promise<boolean> {
  if (!did || childSafetyAccounts.has(did)) return true;
  if ((clearUntil.get(did) ?? 0) > Date.now()) return false;
  let clear = false;
  try {
    clear = (await getReviewSubject(did)).childSafety === false;
  } catch (err) {
    console.error(`child safety: could not read ${did}, treated as child safety:`, err instanceof Error ? err.message : err);
    return true;
  }
  if (clear) clearUntil.set(did, Date.now() + CLEAR_MS);
  else remember(did);
  return !clear;
}

/** Whether a report is child safety, or on an account that is. */
export async function childSafetyScoped(r: Report): Promise<boolean> {
  if (childSafetyReport(r)) {
    remember(r.subjectDid);
    return true;
  }
  return childSafetyAccount(r.subjectDid);
}

/** What a model may read of a child-safety report: its ids, its date, and HUMAN. */
export function withheld(r: { id: number | string; subjectDid: string; createdAt: string }): string {
  return `Report ${r.id} on ${r.subjectDid || "an unknown account"}, filed ${r.createdAt}.\n\n${HUMAN}`;
}

/** The withheld text for an item, from what the queue holds of its report. */
export const withheldItem = (item: Item) =>
  withheld({ id: item.source_ref, subjectDid: item.subject_did, createdAt: item.received_at });

/** What a model may read of an item's own text: all of it, or for a child-safety one, its ids, date and HUMAN. */
export const modelText = (item: Item) => (item.child_safety ? withheldItem(item) : item.text);

function mark(item: Item): void {
  update(item.id, { child_safety: 1, report: withheldItem(item) });
  item.child_safety = 1;
}

/** Marks the item as child safety, as grain-serve just said its account is. */
export function withhold(item: Item): void {
  if (item.subject_did) remember(item.subject_did);
  mark(item);
}

/**
 * Whether a model must not see or act on the item: it is a child-safety
 * report, or a report on an account that is (now) child safety, or one whose
 * account is unknown or cannot be read. Once it is, the queue keeps it so.
 */
export async function withheldFromModels(item: Item): Promise<boolean> {
  if (item.child_safety) return true;
  if (item.source === "bluesky") return false;
  if (!(await childSafetyAccount(item.subject_did))) return false;
  mark(item);
  return true;
}

/**
 * A report as a model may see it: whole, or for one that is child safety or
 * on such an account, its id, its account and its dates. Not its record's
 * URI, which would lead to the record, nor its label or reason.
 */
export function shownReport(r: Report, onChildSafety: boolean): Partial<Report> & { withheld?: string } {
  if (!onChildSafety && !childSafetyReport(r)) return r;
  const { id, subjectDid, resolvedAt, createdAt } = r;
  return { id, subjectDid, createdAt, resolvedAt, withheld: HUMAN };
}