/** * 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";exportinterfaceBriefingEntry { 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;}exportinterfaceBriefingProcedure { project: string; id: string; title: string; trigger: string;}exportinterfaceBriefingInput { 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;}exportinterfaceDropped { entries: number; procedures: number; proceduresCut: number;}exportinterfaceBriefingResult { markdown: string; tokensEstimate: number; overBudget: boolean; dropped: Dropped;}interfaceLimits { 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. */exportfunction limits(): Limits { held ??= rule<Limits>("briefing.limits", null);return held;}/** An entry as the Rust reads one (`createdAt`). */const entryIn = (e: BriefingEntry): Record<string, unknown> => ({ slug: e.slug, title: e.title, pinned: e.pinned, position: e.position, machine: e.machine, createdAt: e.created_at });interfaceResultOut { 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<ResultOut>("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. */exportfunction compileBriefing(input: BriefingInput): BriefingResult {return compile(input, false);}/** The briefing with every entry whole, however long. */exportfunction compileBriefingWhole(input: BriefingInput): BriefingResult {return compile(input, true);}/** chars/4 over UTF-16 units, rounded up. */exportfunction estimateTokens(text: string): number {return rule<number>("briefing.tokens", text);}/** The Owner section's tokens for these entries. */exportfunction ownerSectionTokens(entries: BriefingEntry[]): number {return rule<number>("briefing.owner_tokens", entries.map(entryIn));}/** A procedure's trigger line, or null. */exportfunction procedureTrigger(body: string): string | null {return rule<string | null>("briefing.trigger", body);}/** A trigger's first sentence. */exportfunction firstSentence(trigger: string): string {return rule<string>("briefing.first_sentence", trigger);}/** Rust's `str` order: byte order of UTF-8, which is code point order. */exportfunction byteOrder(a: string, b: string): number {if (a === b) return0;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 ────────────────────────────────────────────────────────────────exportfunction 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. */exportfunction mintMemorySlug(title: string, taken: Iterable<string>): string {return rule<string>("briefing.mint_slug", { title, taken: [...taken] });}// ── secrets and technical marks ──────────────────────────────────────────/** The kind of credential a sentence appears to hold, or null. */exportfunction looksLikeSecret(text: string): string | null {return rule<string | null>("briefing.secret", text);}exportfunction secretRefusal(kind: string): string {return say("memory.secret.refusal", { kind: say(`memory.secret.${kind}`) });}exportfunction technicalMark(title: string, machineScoped: boolean): string | null {return rule<string | null>("briefing.technical", { title, machineScoped });}exportfunction 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. */exportfunction titleSimilarity(a: string, b: string): number {return rule<number>("briefing.similarity", [a, b]);}/** Words worth matching on: distinct lowercase ASCII-alphanumeric runs of four or more. */exportfunction 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`. */exportfunction normalizeRepoUrl(raw: string): string {return rule<string>("briefing.repo", raw);}