Skip to content

Fresh candidate recalculation

再計算 rebuilds replaceable generated candidate rows from the current persisted subtitle segments and video. It does not fetch media, regenerate subtitles, change subtitle offsets, or reset watch history.

再計算 is also the only thing that rebuilds candidates. Nothing on the 字幕同期 screen recalculates as a side effect:

  • Saving a subtitle offset (PUT /v1/media/{id}/sub_offset) writes the offset and queues nothing. The clips it makes stale are reported by clips_stale on the source row, and re-cut by the source’s ↻「依存関係を直す」 (POST /v1/fix). Since #163 that report covers the other movers too — an accepted alignment, its revert, a subtitle replacement — via media.timeline_rev, which is what changes when cue times move without the offset moving.
  • Switching a subtitle source (POST /v1/media/{id}/subtitle_source) replaces the timeline and answers with stale — how many mined rows the switch left behind — with mine: null. subtracks.activate_source(..., remine=True) is the opt-in for a caller that wants both halves.
  • Adopting or uploading a subtitle file (POST /v1/media/{id}/subtitle_file[/upload]) replaces the segments and answers with candidates_stale and job: null. {"recalc": true} (or ?recalc=true on the upload) composes the old move.

The rule is the one REV 0.70 started from: a 字幕変更 badge is an offer, not a trigger. Issue #142 extended it from the queue rows to every door that can change what the subtitles ARE.

Both the fresh mine job and recalculation call mine.mine_and_enqueue: mine_media applies all current mining policy, then schedules enrich. There is no recalculation-specific settings registry or regex implementation. A future setting belongs in the normal miner/enrichment pipeline and must not be duplicated in mining/recalculate.py.

All derived candidate fields come from new rows, including target, score, context, grouping and status. Old translations, dictionary fields and asset paths are not copied onto replacement rows. The ordinary asynchronous enrich job fills them again, subject to its normal limits/dependency availability. Enrichment failure does not roll back a successfully published mine; it is retried through the existing job workflow. Old physical asset files are not deleted by this operation.

Replaceable rows are automatic cards that are not in Anki, not being sent and not deleted, whatever their status or decision: a 承認 or 却下 alone is overwritten. This includes live-linked rows and orphans, so tighter filters can remove old candidates and looser filters can bring automatically rejected/omitted candidates back.

Anki-linked rows, cards being sent, deleted cards and audit/manual sources are never replaced. The cards the person changed by hand, 手直し (candidates.hand_edited_sql: fields saved, a sentence chosen with これにする, a send with edits, or a clip cut again in the editor), are kept too unless the press says to rebuild them: the request carries keep_edited (POST /v1/media/{id}/recalc, /fix, /subtitle_file, /mine?fresh=true), and a rebuild nobody pressed (an apply, a re-read, the judge’s joins) keeps them. Each source in /v1/queue/sources says how many it asks about (hand_edited), and the confirm says how many stay. A decision or an edit made after the press, while the job waits, is kept either way.

Kept rows remain in the staging input, so the ordinary duplicate and option-budget logic still accounts for them, and the line guard gives a kept card’s line no competing card of its word. A rebuild of the rows links a kept 手直し card back to its line the way a hand-made one is (segments.relink_candidates): by its sentence, or the one joined row its sentence is part of; a line that no longer exists leaves it with no line, as a hand-made card is left.

What the person did to a replaced card is not lost with it (QA 2026-10-22). Its 承認 / 却下 stays in gone_decision as it stood (candidates.keep_decisions, called by both hard deletes: forget and a source delete), so the day it was made still counts it: Today’s レビュー and the pipeline bars both read ledger.stats.DECIDED, live cards and gone ones. A Queue OK moves to the card made again of the same word on the same line (_carry_checks), moved and never copied, so it stays one check and one verifier label; automine.last_check shows it only while the new card’s clip is the cut that was heard. A card whose last check is an NG carries nothing: that NG was of its line, and a line made into cards again was released (解除). The gone card’s row names the card it became (made_again), so a 承認 and a Queue OK of one card still count once.

Existing recorded decisions, Anki links and stored clip overrides are recognized without a backfill. Aggregation also protects these rows against deduplication and option-cap deletion. Default dictionary enrichment skips saved edits to avoid overwriting a manually saved definition; an explicit force-enrich is still available.

The miner runs on a private temporary SQLite backup beside the source DB, not a RAM-backed /tmp (memory-only DBs use the temp default). Its internal commits never delete or rewrite live candidates. Progress is written through the original live connection, so the UI can show work while the old queue stays available. Successful candidate replacement and enrichment job handoff commit in one short BEGIN IMMEDIATE transaction.

A failure during mining, enqueueing or publication leaves the original live candidates and prior enrichment job intact. Temporary files are cleaned up by the context manager; enough temporary disk space for a database copy is needed.

PRAGMA data_version guards the whole snapshot against other connections’ commits, including new settings and user decisions. The final check is inside the write transaction. The guard deliberately errs on the side of retrying: even an unrelated concurrent database write can invalidate a rebuild. It does not reject the same connection’s own progress writes. Recalculation requires a committed connection and refuses an already-running enrichment for that media or the global all key. The conflict baseline precedes all preflight reads; the snapshot is checked again and publication requires a queued enrichment handoff. Old queued force/disabled-stage overrides are discarded so this is a fresh chain.

The stale_candidates badge still counts only replaceable subtitle orphans; it is not the total number of candidates an explicit recalculation replaces.

core/tests/sources/test_recalc_fresh.py compares recalculation with a genuinely fresh miner on the same input under changed settings, checks a future miner policy without modifying recalculation, and exercises protected history, regenerated fields, live progress, a concurrent user decision, committed staging failure, and atomic publication failure, the day’s decisions still counted once their cards are replaced, and a Queue OK following its card made again. Existing source/UI tests cover the endpoint, queue handoff, orphan badge, unchanged subtitles, and confirmation.