/** * The memory rules: the briefing compiler, the owner's order, slug minting, * the secret and technical marks, title similarity and repo-URL * normalisation are cotton-rules' (crates/cotton-rules/src/{memory_briefing, * memory_order,memory_technical}.rs), asked through the rules module. What * stays here is the Worker's own: the shapes it hands over, the refusals in * its lines, a slug's form and a byte order for its sorts. */ import { rule } from "../../../rules/index.ts"; import { say } from "./words.ts"; export interface BriefingEntry { slug: string; title: string; /** Pinned IN THIS PROJECT — the link's pin. */ pinned: boolean; /** The link's position in this project. */ position: number; machine: string | null; created_at: string; /** AUDIT C 4: an assistant wrote it and the owner has not reviewed it (the brief marks it; the Rust never sees this). */ unreviewed?: boolean; } export interface BriefingProcedure { project: string; id: string; title: string; trigger: string; } export interface BriefingInput { generatedAt: string; machine: string | null; projects: { slug: string; name: string; entries: BriefingEntry[] }[]; /** The Owner section's entries, or none. */ owner: BriefingEntry[] | null; procedures: BriefingProcedure[]; budgetTokens: number | null; } export interface Dropped { entries: number; procedures: number; proceduresCut: number; } export interface BriefingResult { markdown: string; tokensEstimate: number; overBudget: boolean; dropped: Dropped; } interface Limits { budget: number; owner: number; positionStep: number; similarity: number; } let held: Limits | undefined; /** The briefing's token budget, the Owner section's share, a position step and the similarity threshold, as the rules say them. */ export function limits(): Limits { held ??= rule("briefing.limits", null); return held; } /** An entry as the Rust reads one (`createdAt`). */ const entryIn = (e: BriefingEntry): Record => ({ slug: e.slug, title: e.title, pinned: e.pinned, position: e.position, machine: e.machine, createdAt: e.created_at }); interface ResultOut { markdown: string; tokensEstimate: number; overBudget: boolean; dropped: { entries: number; procedures: number; procedures_cut: number }; } /** * Where this object's briefing sends a reader for what it left out: tools it * serves (the phone names `get_project`, which the object has not). */ const POINTERS = { findProject: "Call `list_projects`, then `brief { project }` for the one this work belongs to.", everyTrigger: '`get_record { kind: "note", id }` reads each whole.', everyProcedure: '`search_records { q: "Trigger:", table: "note" }` finds every procedure note.', }; function compile(input: BriefingInput, whole: boolean): BriefingResult { const out = rule("briefing.compile", { whole, input: { ownerName: null, generatedAt: input.generatedAt, machine: input.machine, projects: input.projects.map((p) => ({ slug: p.slug, name: p.name, entries: p.entries.map(entryIn) })), owner: input.owner?.map(entryIn) ?? null, procedures: input.procedures, budgetTokens: input.budgetTokens, pointers: POINTERS, }, }); return { markdown: out.markdown, tokensEstimate: out.tokensEstimate, overBudget: out.overBudget, dropped: { entries: out.dropped.entries, procedures: out.dropped.procedures, proceduresCut: out.dropped.procedures_cut } }; } /** The briefing within its budget. */ export function compileBriefing(input: BriefingInput): BriefingResult { return compile(input, false); } /** The briefing with every entry whole, however long. */ export function compileBriefingWhole(input: BriefingInput): BriefingResult { return compile(input, true); } /** chars/4 over UTF-16 units, rounded up. */ export function estimateTokens(text: string): number { return rule("briefing.tokens", text); } /** The Owner section's tokens for these entries. */ export function ownerSectionTokens(entries: BriefingEntry[]): number { return rule("briefing.owner_tokens", entries.map(entryIn)); } /** A procedure's trigger line, or null. */ export function procedureTrigger(body: string): string | null { return rule("briefing.trigger", body); } /** A trigger's first sentence. */ export function firstSentence(trigger: string): string { return rule("briefing.first_sentence", trigger); } /** Rust's `str` order: byte order of UTF-8, which is code point order. */ export function byteOrder(a: string, b: string): number { if (a === b) return 0; let i = 0; let j = 0; while (i < a.length && j < b.length) { const x = a.codePointAt(i)!; const y = b.codePointAt(j)!; if (x !== y) return x < y ? -1 : 1; i += x > 0xffff ? 2 : 1; j += y > 0xffff ? 2 : 1; } return i < a.length ? 1 : j < b.length ? -1 : 0; } // ── slugs ──────────────────────────────────────────────────────────────── export function isMemorySlug(value: string): boolean { return /^[a-z0-9][a-z0-9-]{0,63}$/.test(value); } /** mint_memory_slug: kebab of the NFKD title, ≤64 units, `-2`, `-3`… past the taken ones. */ export function mintMemorySlug(title: string, taken: Iterable): string { return rule("briefing.mint_slug", { title, taken: [...taken] }); } // ── secrets and technical marks ────────────────────────────────────────── /** The kind of credential a sentence appears to hold, or null. */ export function looksLikeSecret(text: string): string | null { return rule("briefing.secret", text); } export function secretRefusal(kind: string): string { return say("memory.secret.refusal", { kind: say(`memory.secret.${kind}`) }); } export function technicalMark(title: string, machineScoped: boolean): string | null { return rule("briefing.technical", { title, machineScoped }); } export function technicalRefusal(mark: string): string { return say("memory.technical.refusal", { mark: say(`memory.technical.${mark}`) }); } // ── similarity ─────────────────────────────────────────────────────────── /** Dice coefficient over the sets of character bigrams; 0 when either is empty. */ export function titleSimilarity(a: string, b: string): number { return rule("briefing.similarity", [a, b]); } /** Words worth matching on: distinct lowercase ASCII-alphanumeric runs of four or more. */ export function significantTokens(title: string): string[] { const seen: string[] = []; for (const token of title.toLowerCase().split(/[^a-z0-9]/)) if (token.length >= 4 && !seen.includes(token)) seen.push(token); return seen; } // ── repo URLs ──────────────────────────────────────────────────────────── /** One spelling for a remote: `https://github.com/a/b.git`, `git@github.com:a/b.git` → `github.com/a/b`. */ export function normalizeRepoUrl(raw: string): string { return rule("briefing.repo", raw); }