Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 7474d0567ba9c5274122c08706cc207454b67dc2
parent 14e66c7c4cc70706b4effcee350d2264e008ad95
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 11 Nov 2026 09:27:14 -0500

local media: "all new" over 1000 recordings takes the first 1000 and says how many remain

Review fix 7. `pnpm ops local-media-import {"allNew": true}` over a library
with more than 1000 new recordings sent them all and got the job's generic
"Select between 1 and 1000" refusal. selectAllNew() takes the first 1000 new
candidates in the preview's order (the scan's sorted names, so a re-run takes
the next ones) and counts the rest: the ops answer carries `selected` and
`remaining`, and the job's log says "<n> more new recording(s) were not
selected ..." at its start and its end. Usage and COMMANDS.md updated. Tests:
1003 candidates (one an issue) -> the first 1000 new in order and 2 left; the
job log line; the answer's fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Diffstat:
MCOMMANDS.md | 2+-
Mcommon/controller/localMediaImport.test.ts | 28+++++++++++++++++++++++++++-
Mcommon/controller/localMediaImportJob.ts | 29+++++++++++++++++++++++++++--
Meditor/app/api/ops/local-media-import/route.test.ts | 3+++
Meditor/app/api/ops/local-media-import/route.ts | 28++++++++++++++++++----------
Mscripts/archilyzer-ops.mjs | 4+++-
6 files changed, 79 insertions(+), 15 deletions(-)

diff --git a/COMMANDS.md b/COMMANDS.md @@ -146,7 +146,7 @@ Drives a running editor over HTTP (`/api/ops/*`, the same actions its pages run) | `local-media-configure` | local-media-configure gives a video channel a LOCAL MEDIA LIBRARY: {"slug", "sourceId", "directory"} — a stable lowercase id and an absolute directory on the editor server whose recordings stay where they are (never copied, moved or deleted). Written through the channel config; a channel that already has one is refused, and so is queued or running channel work. Playback is not set. Synchronous. | `pnpm ops local-media-configure --json '{"slug":"demo-local","sourceId":"library-1","directory":"/mnt/recordings"}'` | | `local-media-preview` | local-media-preview scans the library and writes nothing: {"slug"} -&gt; {preview: {sourceToken, counts, candidates}}. Each .mp4 directly in the directory (not its subdirectories) with its same-named .info.json and .vtt (or .srt) is "new", "existing" (its record is held) or "issue" (with issues). The sourceToken and a candidate's fingerprint (or preparationFingerprint, when it has no .info.json) are what the other local-media actions take. An unreachable library is an error, never an empty preview. | `pnpm ops local-media-preview --json '{"slug":"demo-local"}'` | | `local-media-prepare` | local-media-prepare reviews details for a recording with no .info.json and writes nothing: {"slug", "sourceToken", "media", "sourceFingerprint" (the candidate's preparationFingerprint), "details": {"mode": "inspect" \| "manual", "title", "recordedDate"? (YYYY-MM-DD), "durationSeconds"? (manual)}} -&gt; {review: {metadata, fingerprint}}. "inspect" measures the duration with ffprobe. Import it with local-media-import, the review's fingerprint and "preparation": {"sourceFingerprint", "details"}. | `pnpm ops local-media-prepare --json '{"slug":"demo-local","sourceToken":"<token>","media":"talk.mp4","sourceFingerprint":"<fp>","details":{"mode":"inspect","title":"A talk"}}'` | -| `local-media-import` | local-media-import imports library recordings as one job on the channel's queue: {"slug", "sourceToken", "selected": \[{"media", "fingerprint", "preparation"?}\]} from a preview, or {"slug", "allNew": true} for every "new" candidate of a fresh preview. Each record gets metadata.info.json, transcript.en.vtt and external-media.json; the video stays in the library. Every selection is re-checked first (a changed file is stale; a held record is left unchanged). Drainable between recordings; searchable after the next index build. | `pnpm ops local-media-import --json '{"slug":"demo-local","allNew":true}' --wait` | +| `local-media-import` | local-media-import imports library recordings as one job on the channel's queue: {"slug", "sourceToken", "selected": \[{"media", "fingerprint", "preparation"?}\]} from a preview, or {"slug", "allNew": true} for every "new" candidate of a fresh preview (the first 1000 in the scan's order, with "remaining" in the answer and the log when there are more). Each record gets metadata.info.json, transcript.en.vtt and external-media.json; the video stays in the library. Every selection is re-checked first (a changed file is stale; a held record is left unchanged). Drainable between recordings; searchable after the next index build. | `pnpm ops local-media-import --json '{"slug":"demo-local","allNew":true}' --wait` | | `local-media-link` | local-media-link links a record the channel already holds to its library original: {"slug", "sourceToken", "media", "fingerprint", "dryRun"?} for an "existing" candidate. Only when the library's .info.json and transcript match the record's byte for byte is external-media.json written ("attached"; "already-linked"; "conflict" keeps a different one). Nothing else changes, and no playback is granted. "dryRun" answers "ready" without writing. | `pnpm ops local-media-link --json '{"slug":"demo-local","sourceToken":"<token>","media":"talk.mp4","fingerprint":"<fp>","dryRun":true}'` | | `live-preclip` | live-preclip transcribes and mines a recording WHILE it records: {"slug", "id", "action"?: "start" (default) \| "stop" \| "status" \| "finalize"}, and for start "everyMinutes"? (10), "maxChunkMinutes"? (20), "overlapSeconds"? (15), "scan"? (true: the laugh scan), "mine"? (true), "project"? (the umtool project, &lt;id&gt;-live). One job per stream beside the recorder's: each tick reads only the new span of the growing file (never writing it), transcribes it on a worker lease (one at a time — a busy worker coalesces ticks), splices it into data/&lt;id&gt;/live-transcript.json (provisional) and grows the umtool project. Skipped and said: under 30 s new, MemAvailable under HEAVY\_MIN\_FREE\_MB (6000), the store's disk under 2 GB. When the stream lands: the clock check (flagged when the landed video's start differs), then the final transcript supersedes the live one. stop cancels the job, never the recording; status prints the state; finalize does the supersede by hand. No network request. | `pnpm ops live-preclip --json '{"slug":"demo-yt","id":"<videoId>","action":"status"}'` | | `live-clip` | live-clip makes a shareable clip of a stream still live: {"slug", "id", "start", "end"} (seconds of the stream, at most 2:20) or "clip" (a clip id of the project), "project"? (&lt;id&gt;-live), "replace"?. umtool cuts the window from the recording now (the editor's /api/media/live-window: the part being written, or the saved video once landed; no network), adds the clip, and makes its short with the live transcript's captions. With --wait the result (the short's URL) is stdout. | `pnpm ops live-clip --json '{"slug":"demo-yt","id":"<videoId>","start":3605,"end":3660}' --wait` | diff --git a/common/controller/localMediaImport.test.ts b/common/controller/localMediaImport.test.ts @@ -116,7 +116,7 @@ test("missing source fails instead of reporting an empty successful preview", as import type { Paths } from "../lib/paths"; import type { JobRunContext } from "../jobs/streamCommand"; -import { localMediaJobOptions } from "./localMediaImportJob"; +import { localMediaJobOptions, selectAllNew, LOCAL_MEDIA_IMPORT_MAX } from "./localMediaImportJob"; import { getJobKind, isIngestKind } from "../jobs/jobKinds"; function jobContext(drain = new AbortController()): JobRunContext { @@ -190,3 +190,29 @@ test("pre-start drain and cancellation write nothing; empty job request is rejec assert.deepEqual(await readdir(f.channel), ["config.json"]); assert.throws(() => localMediaJobOptions(f.paths as Paths, "sample", {sourceToken: p.sourceToken, selected: []}), /Select between/); }); + +// Review fix 7: over 1000 new recordings, "all new" takes the first 1000 in the +// preview's (sorted) order and says how many are left, in the job's log too. +test("selectAllNew takes the first 1000 new in order and counts the rest; the job log says so", async (t) => { + const candidates = Array.from({ length: 1003 }, (_, i) => ({ + media: `r${String(i).padStart(4, "0")}.mp4`, fingerprint: `fp${i}`, status: i === 5 ? "issue" as const : "new" as const, + metadata: "", transcript: "", issues: [], warnings: [], + })); + const { request, remaining } = selectAllNew({ sourceToken: "t", candidates, counts: { discovered: 1003, new: 1002, existing: 0, issues: 1 } }); + assert.equal(LOCAL_MEDIA_IMPORT_MAX, 1000); + assert.equal(request.selected.length, 1000); + assert.equal(request.selected[0].media, "r0000.mp4"); + assert.ok(!request.selected.some((s) => s.media === "r0005.mp4")); + assert.equal(request.selected[999].media, "r1000.mp4"); + assert.equal(remaining, 2); + assert.equal(request.remaining, 2); + assert.equal(selectAllNew({ sourceToken: "t", candidates: candidates.slice(0, 3), counts: { discovered: 3, new: 3, existing: 0, issues: 0 } }).request.remaining, undefined); + + const f = await fixture(t); await f.add("first"); + const p = await previewLocalMedia(f.paths, "sample"); + const job = localMediaJobOptions(f.paths as Paths, "sample", { ...selection(p), remaining: 2 }); + const log: string[] = []; + await job.fn(line => log.push(line), new AbortController().signal, () => {}, jobContext()); + const said = log.filter(line => line.startsWith("2 more new recording(s) were not selected (one import takes at most 1000)")); + assert.equal(said.length, 2, log.join("\n")); +}); diff --git a/common/controller/localMediaImportJob.ts b/common/controller/localMediaImportJob.ts @@ -1,20 +1,38 @@ import { parseLocalRecordingDetails } from "../lib/localRecordingDetails"; import type { Paths } from "../lib/paths"; import { channelQueueKey } from "../lib/queueKeys"; -import { importLocalMedia, type LocalMediaSelection } from "./localMediaImport"; +import { importLocalMedia, type LocalMediaPreview, type LocalMediaSelection } from "./localMediaImport"; import { runManagedFunction, type RunManagedFunctionOpts } from "../jobs/streamCommand"; export type LocalMediaJobRequest = { sourceToken: string; selected: readonly LocalMediaSelection[]; + // New recordings left for a later run (selectAllNew): said in the job's log. + remaining?: number; }; +// One job imports at most this many recordings. +export const LOCAL_MEDIA_IMPORT_MAX = 1000; + +// EVERY NEW RECORDING of a fresh preview, at most LOCAL_MEDIA_IMPORT_MAX of them: +// the first in the preview's order (the scan's sorted file names, so a re-run +// after an import takes the next ones), and how many new ones are left over. +// Review fix 7 (lane/local-media): over the cap it was a generic refusal. +export function selectAllNew(preview: LocalMediaPreview): { request: LocalMediaJobRequest; remaining: number } { + const all = preview.candidates + .filter((c) => c.status === "new" && c.fingerprint) + .map((c) => ({ media: c.media, fingerprint: c.fingerprint! })); + const selected = all.slice(0, LOCAL_MEDIA_IMPORT_MAX); + const remaining = all.length - selected.length; + return { request: { sourceToken: preview.sourceToken, selected, ...(remaining > 0 ? { remaining } : {}) }, remaining }; +} + // Keep the managed job's body testable with synthetic sources, without starting // the application's registry or snapshot timers. The production entry point // below always uses runManagedFunction, including its enqueue/start guards. export function localMediaJobOptions(paths: Paths, slug: string, request: LocalMediaJobRequest): RunManagedFunctionOpts { if (!request || typeof request.sourceToken !== "string" || !Array.isArray(request.selected) - || request.selected.length === 0 || request.selected.length > 1000 + || request.selected.length === 0 || request.selected.length > LOCAL_MEDIA_IMPORT_MAX || request.selected.some(s => !s || typeof s.media !== "string" || typeof s.fingerprint !== "string") || new Set(request.selected.map(s => s.media)).size !== request.selected.length) { throw new Error("Select between 1 and 1000 distinct recordings from a fresh preview"); @@ -30,6 +48,11 @@ export function localMediaJobOptions(paths: Paths, slug: string, request: LocalM }} : {})}; }); const sourceToken = request.sourceToken; + const remaining = typeof request.remaining === "number" && Number.isSafeInteger(request.remaining) && request.remaining > 0 + ? request.remaining : 0; + const leftOverLine = remaining + ? `${remaining} more new recording(s) were not selected (one import takes at most ${LOCAL_MEDIA_IMPORT_MAX}); import again after this one to take the next ${Math.min(remaining, LOCAL_MEDIA_IMPORT_MAX)}.` + : null; return { kind: "import-local-media", queueKey: channelQueueKey(slug), @@ -39,6 +62,7 @@ export function localMediaJobOptions(paths: Paths, slug: string, request: LocalM // The operator takes a new preview to retry or continue a drained batch. fn: async (onLog, signal, setProgress, ctx) => { setProgress({ metric: "entries", initial: 0, target: selected.length, current: 0 }); + if (leftOverLine) onLog(leftOverLine); onLog("Rechecking selected external recordings before import..."); const result = await importLocalMedia(paths, slug, { sourceToken, selected, signal, drainSignal: ctx.drainSignal, @@ -53,6 +77,7 @@ export function localMediaJobOptions(paths: Paths, slug: string, request: LocalM const occupied = result.records.filter(r => r.status === "occupied").length; const rejected = result.records.length - imported - occupied; onLog(`Imported ${imported}; already occupied ${occupied}; rejected ${rejected}; unprocessed ${selected.length - result.records.length}.`); + if (leftOverLine) onLog(leftOverLine); // What works today: the records are searchable after the next index build. if (result.needsIndexUpdate) onLog("Import complete. The imported recordings are searchable after the next index build (`pnpm ops build-index`, or a site build)."); // The existing managed-job completion hook requests a channel snapshot, diff --git a/editor/app/api/ops/local-media-import/route.test.ts b/editor/app/api/ops/local-media-import/route.test.ts @@ -128,6 +128,9 @@ test("configure, preview, prepare, import and link, the library untouched", asyn ], }); assert.equal(started.status, 200, JSON.stringify(started.json)); + // Review fix 7: the answer says how many were taken and how many remain. + assert.equal(started.json.selected, 2); + assert.equal(started.json.remaining, 0); assert.equal(await waitJob(started.json.jobId), "done"); const data = (await readdir(path.join(CH, "lib", "data"))).sort(); assert.equal(data.length, 3); diff --git a/editor/app/api/ops/local-media-import/route.ts b/editor/app/api/ops/local-media-import/route.ts @@ -1,13 +1,14 @@ import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { previewLocalMedia } from "yt-dlp-transcript-common/controller/localMediaImport"; -import { startLocalMediaImportJob, type LocalMediaJobRequest } from "yt-dlp-transcript-common/controller/localMediaImportJob"; +import { NextResponse } from "next/server"; +import { selectAllNew, startLocalMediaImportJob, type LocalMediaJobRequest } from "yt-dlp-transcript-common/controller/localMediaImportJob"; import { OpsInputError, jobResponse, ops, optBool, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, sourceToken, selected: [{ media, fingerprint, preparation? }] } // | { slug, allNew: true } -// -> { ok: true, jobId } +// -> { ok: true, jobId, selected, remaining } // // LOCAL LIBRARY RECORDINGS IMPORTED (jobs/localMediaImportJob.ts; the Local // media page's Import selected): one `import-local-media` job on the channel's @@ -17,7 +18,9 @@ export const dynamic = "force-dynamic"; // local-media-preview answered (its `sourceToken`, each candidate's `media` // and `fingerprint`; a prepared one adds `preparation: {sourceFingerprint, // details}` with the prepare's fingerprint). `allNew: true` takes a fresh -// preview here and selects every "new" candidate in it. Every selection is +// preview here and selects its "new" candidates — the first 1000 in the scan's +// order when there are more, with `remaining` the count left for the next run +// (also said in the job's log). Every selection is // re-checked before its record is written: a changed file is "stale", an // occupied record is left unchanged. Drainable between recordings. The // imported records are searchable after the next index build. @@ -26,26 +29,31 @@ export async function POST(request: Request) { const slug = reqSlug(body, "slug"); const paths = getPaths(); let req: LocalMediaJobRequest; + let remaining = 0; if (optBool(body, "allNew") === true) { if (body.sourceToken !== undefined || body.selected !== undefined) { throw new OpsInputError('"allNew" takes no "sourceToken" or "selected"'); } - const preview = await previewLocalMedia(paths, slug); - const selected = preview.candidates - .filter((c) => c.status === "new" && c.fingerprint) - .map((c) => ({ media: c.media, fingerprint: c.fingerprint! })); - if (selected.length === 0) throw new OpsInputError("the library has no new recording to import"); - req = { sourceToken: preview.sourceToken, selected }; + const all = selectAllNew(await previewLocalMedia(paths, slug)); + if (all.request.selected.length === 0) throw new OpsInputError("the library has no new recording to import"); + req = all.request; + remaining = all.remaining; } else { if (typeof body.sourceToken !== "string" || !Array.isArray(body.selected)) { throw new OpsInputError('"sourceToken" and "selected" (from local-media-preview) are required, or "allNew": true'); } req = { sourceToken: body.sourceToken, selected: body.selected as LocalMediaJobRequest["selected"] }; } + let started; try { - return jobResponse(await startLocalMediaImportJob(paths, slug, req)); + started = await startLocalMediaImportJob(paths, slug, req); } catch (e) { throw new OpsInputError((e as Error).message); } + if (!started.ok) return jobResponse(started); + void started.stream.cancel(); + // `selected` and `remaining`: over LOCAL_MEDIA_IMPORT_MAX new recordings, + // allNew takes the first ones and says how many are left for the next run. + return NextResponse.json({ ok: true, jobId: started.jobId, selected: req.selected.length, remaining }); }); } diff --git a/scripts/archilyzer-ops.mjs b/scripts/archilyzer-ops.mjs @@ -1783,7 +1783,9 @@ export function usage() { "local-media-import imports library recordings as one job on the channel's", " queue: {\"slug\", \"sourceToken\", \"selected\": [{\"media\", \"fingerprint\",", " \"preparation\"?}]} from a preview, or {\"slug\", \"allNew\": true} for every", - " \"new\" candidate of a fresh preview. Each record gets metadata.info.json,", + " \"new\" candidate of a fresh preview (the first 1000 in the scan's order,", + " with \"remaining\" in the answer and the log when there are more). Each", + " record gets metadata.info.json,", " transcript.en.vtt and external-media.json; the video stays in the", " library. Every selection is re-checked first (a changed file is stale; a", " held record is left unchanged). Drainable between recordings; searchable",