// RESEARCH ACQUISITIONS — what a research run (release 27 S3b/S11) may ask // the editor to fetch, and the one pure decision every ask goes through // before anything is enqueued. // // An acquisition is a PROPOSAL: an existing editor action and its arguments, // with the run's reason and evidence links. `decideAcquisition` answers // enqueue, needs approval, or refuse (with the reason), from the run's // AUTONOMY and LIMITS and what the run has already spent: // // propose every acquisition waits for the operator's yes or no. // limits an acquisition into an EXISTING channel runs on its own while it // is under the caps and its size is known; a new channel and a post // walk wait while the limits say they need approval (the default), // a priority bump always waits, and so does anything over a cap or // of unknown size. // auto everything runs on its own, through the editor: the approval // flags are lifted (ruled 2026-10-13), but the caps stay hard // ceilings — past one (or of unknown size), it waits for approval. // // THE STANDING RULES are checked first and refuse in every mode — they are // not the run's to relax: // - a new channel is created on NO site (create-channel never carries // `sites`); // - an X channel is never put on a site; // - no Twitch VOD URL: an expired VOD is never fetched again; // - nothing bypasses the editor's queues: no `queueKey` override, and only // the actions below, each an existing editor action. export type Autonomy = "propose" | "limits" | "auto"; export const AUTONOMIES: readonly Autonomy[] = ["propose", "limits", "auto"]; export type ResearchLimits = { maxItemsPerRun: number; maxGbPerRun: number; maxTranscribeHours: number; newChannelsNeedApproval: boolean; postWalksNeedApproval: boolean; }; export const DEFAULT_RESEARCH_LIMITS: ResearchLimits = { maxItemsPerRun: 20, maxGbPerRun: 5, maxTranscribeHours: 6, newChannelsNeedApproval: true, postWalksNeedApproval: true, }; // The editor actions a research run may ask for, by the name its tool and its // proposal carry. Each maps to ONE existing editor action (`priority-bump` is // `channel-priority` with tier "normal"). export const ACQUISITION_ACTIONS = [ "import-archive-org", "import-video", "create-channel", "fetch-posts", "capture-posts", "import-post", "transcribe-one", "priority-bump", "persist-videos", ] as const; export type AcquisitionAction = (typeof ACQUISITION_ACTIONS)[number]; export const PROPOSAL_STATUSES = ["proposed", "approved", "rejected", "enqueued", "done", "failed"] as const; export type ProposalStatus = (typeof PROPOSAL_STATUSES)[number]; // What the run expects an acquisition to cost, as far as it knows. export type AcquisitionEstimate = { items?: number; gb?: number; transcribeHours?: number; }; export type Proposal = { id: string; action: AcquisitionAction; args: Record; reason: string; evidence: string[]; estimate?: AcquisitionEstimate; status: ProposalStatus; jobId?: string; // Why it waits, was refused or failed (the decision's or the editor's words). note?: string; // The channel a create-channel made (the editor's answer), for the scope // a continuation widens to. channel?: string; // When its job (all of its jobs) ended done. doneAt?: string; }; export type Spent = { items: number; gb: number; transcribeHours: number }; export const NOTHING_SPENT: Spent = { items: 0, gb: 0, transcribeHours: 0 }; export type AcquisitionDecision = | { decision: "enqueue" } | { decision: "approval"; reason: string } | { decision: "refuse"; reason: string }; // ─── the standing rules ─── const TWITCH_VOD = /^(?:https?:\/\/)?(?:[a-z0-9-]+\.)*twitch\.tv\/(?:videos\/\d+|[^/?#]+\/(?:v|video|videos)\/\d+)/i; const X_HOST = /^(?:https?:\/\/)?(?:[a-z0-9-]+\.)*(?:x\.com|twitter\.com)(?:[/:?#]|$)/i; // Every string anywhere in the args, depth-first. function strings(v: unknown, out: string[] = []): string[] { if (typeof v === "string") out.push(v); else if (Array.isArray(v)) for (const x of v) strings(x, out); else if (v && typeof v === "object") for (const x of Object.values(v)) strings(x, out); return out; } export const isTwitchVodUrl = (s: string) => TWITCH_VOD.test(s.trim()); export const isXUrl = (s: string) => X_HOST.test(s.trim()); function createChannelIsX(args: Record): boolean { const f = (args.fields ?? {}) as Record; const url = typeof f.url === "string" ? f.url : ""; const platform = typeof f.platform === "string" ? f.platform.toLowerCase() : ""; const fetcher = typeof f.postFetcher === "string" ? f.postFetcher.toLowerCase() : ""; return isXUrl(url) || platform === "x" || platform === "twitter" || fetcher.startsWith("x-"); } // The standing rule a proposal breaks, or null. export function standingRuleRefusal(p: Pick): string | null { if (!(ACQUISITION_ACTIONS as readonly string[]).includes(p.action)) { return `"${p.action}" is not an acquisition a research run may ask for (${ACQUISITION_ACTIONS.join(", ")})`; } if ("queueKey" in p.args) return "nothing bypasses the editor's queues: a queueKey override is not allowed"; const vod = strings(p.args).find(isTwitchVodUrl); if (vod) return `a Twitch VOD is never fetched again (expired VODs are gone; look for the stream on archive.org or elsewhere): ${vod}`; if (p.action === "create-channel") { const sites = p.args.sites; if (sites !== undefined && !(Array.isArray(sites) && sites.length === 0)) { if (createChannelIsX(p.args)) return "an X channel is never put on a site"; return "a channel a research run creates goes on NO site; the operator adds it to one"; } } return null; } // ─── the decision ─── // A walk over a social account's history (a full re-walk or the older walk). export const isPostWalk = (p: Pick) => p.action === "fetch-posts" && (p.args.full === true || p.args.older === true); // What an acquisition costs against the caps; unknown where it downloads and // the run did not say. A channel created, a post fetch, a capture, a bump // cost no item; one post is one item. function cost(p: Pick): { items: number; gb: number | null; hours: number | null } { const e = p.estimate ?? {}; const listed = (k: string) => (Array.isArray(p.args[k]) ? (p.args[k] as unknown[]).length : undefined); switch (p.action) { case "import-archive-org": case "import-video": case "persist-videos": return { items: e.items ?? listed("items") ?? 1, gb: e.gb ?? (p.args.dryRun === true ? 0 : null), // An import into a transcribing channel queues its transcription: // what the run says it will take counts against the cap. hours: e.transcribeHours ?? 0, }; case "import-post": return { items: e.items ?? 1, gb: e.gb ?? 0, hours: 0 }; case "transcribe-one": return { items: e.items ?? 0, gb: 0, hours: e.transcribeHours ?? null }; default: return { items: e.items ?? 0, gb: e.gb ?? 0, hours: e.transcribeHours ?? 0 }; } } function overCap(p: Pick, limits: ResearchLimits, spent: Spent): string | null { const c = cost(p); if (spent.items + c.items > limits.maxItemsPerRun) { return `over the run's cap of ${limits.maxItemsPerRun} item(s) (${spent.items} so far, this is ${c.items})`; } if (c.gb === null) return "its size is not known"; if (spent.gb + c.gb > limits.maxGbPerRun) { return `over the run's cap of ${limits.maxGbPerRun} GB (${spent.gb.toFixed(1)} so far, this is ${c.gb.toFixed(1)})`; } if (c.hours === null) return "its transcription length is not known"; if (spent.transcribeHours + c.hours > limits.maxTranscribeHours) { return `over the run's cap of ${limits.maxTranscribeHours} h of transcription (${spent.transcribeHours.toFixed(1)} so far, this is ${c.hours.toFixed(1)})`; } return null; } export function decideAcquisition( proposal: Pick, autonomy: Autonomy, limits: ResearchLimits, spentSoFar: Spent = NOTHING_SPENT, ): AcquisitionDecision { const broken = standingRuleRefusal(proposal); if (broken) return { decision: "refuse", reason: broken }; if (!(AUTONOMIES as readonly string[]).includes(autonomy)) { return { decision: "refuse", reason: `unknown autonomy "${String(autonomy)}"` }; } if (autonomy === "propose") return { decision: "approval", reason: "this run proposes; the operator approves each acquisition" }; // Under limits the approval flags hold; under auto they are lifted. if (autonomy === "limits") { if (proposal.action === "create-channel" && limits.newChannelsNeedApproval) { return { decision: "approval", reason: "a new channel needs the operator's approval" }; } if (isPostWalk(proposal) && limits.postWalksNeedApproval) { return { decision: "approval", reason: "a walk over an account's post history needs the operator's approval" }; } if (proposal.action === "priority-bump") { return { decision: "approval", reason: "a priority change is the operator's under limits" }; } } const over = overCap(proposal, limits, spentSoFar); if (over) return { decision: "approval", reason: over }; return { decision: "enqueue" }; } // What an enqueued proposal adds to the run's spend. export function spendOf(p: Pick): Spent { const c = cost(p); return { items: c.items, gb: c.gb ?? 0, transcribeHours: c.hours ?? 0 }; } export function addSpent(a: Spent, b: Spent): Spent { return { items: a.items + b.items, gb: a.gb + b.gb, transcribeHours: a.transcribeHours + b.transcribeHours }; }