Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

33.1 KB
Raw Blame
"""The built-in director is a client of the shared production operations.

No durable state lives here. ``run`` consumes only production.view() data.
The production adapter implements get(id, *, since, wait), sheet(id, shot,
*, frames), choose(id, shot, take, *, by, reason, ref), reshoot(id, shot,
*, by, note, takes, reason, ref), and finish(id, *, by, ref). Mutations MUST
atomically deduplicate ref with their durable receipt; exceptions expose code
and optionally status. plan/create/cancel belong to the same adapter but are
not needed to drive an already-created production.

A sheet is {'takes': [{'take': int, 'image': bytes | MediaRef}],
'previous': optional_image}. It covers every successful non-stale take.
ChatAsk consumes bytes; JobAsk consumes path-free MediaRefs. The sheet adapter
supplies the appropriate representation; this module never opens media paths.
JobAsk additionally uses the existing Client.submit(spec) / wait(id, timeout=) / cancel(id)
job interface. All asks return a schema-checked object or None.
"""
import base64
from copy import deepcopy
from dataclasses import dataclass, replace
import hashlib
import ipaddress
import json
import math
import re
import urllib.request
from urllib.parse import urlsplit

from . import drama, spec, take_qa


@dataclass(frozen=True)
class Problem:
    code: str
    message: str


@dataclass(frozen=True)
class Operation:
    action: str
    shot: str | None = None
    reason: str = ''
    take: int | None = None
    note: str | None = None
    changes: dict | None = None
    takes: int | None = None


Decision = Operation


def _object(properties, required=()):
    return dict(type='object', properties=properties, required=list(required), additionalProperties=False)


def _string(limit=20000):
    return dict(type='string', minLength=1, maxLength=limit)


class _Invalid(ValueError):
    pass


def _validate(value, schema, path='$'):
    """Small, fail-closed JSON Schema subset used by both transports.

    Supports type, properties, required, additionalProperties, items, enum,
    const, oneOf, bounds and lengths. Unknown keywords are rejected rather than
    silently claiming validation. No external schema resolver or dependency.
    """
    supported = {'type', 'properties', 'required', 'additionalProperties', 'items', 'enum', 'const',
                 'oneOf', 'minimum', 'maximum', 'minLength', 'maxLength', 'minItems', 'maxItems',
                 'description', 'title'}
    if not isinstance(schema, dict) or set(schema) - supported:
        raise _Invalid('Unsupported JSON schema')
    if 'oneOf' in schema:
        matches = 0
        for option in schema['oneOf']:
            try:
                _validate(value, option, path)
                matches += 1
            except _Invalid:
                pass
        if matches != 1:
            raise _Invalid(path + ': must match exactly one allowed operation')
    kind = schema.get('type')
    types = {'object': isinstance(value, dict), 'array': isinstance(value, list),
             'string': isinstance(value, str), 'boolean': type(value) is bool,
             'integer': type(value) is int,
             'number': type(value) in (int, float) and math.isfinite(value), 'null': value is None}
    if kind is not None and not types.get(kind, False):
        raise _Invalid(path + ': expected ' + str(kind))
    # JSON's true and 1 are different values, despite Python equality.
    encoded = _dump(value)
    if 'enum' in schema and encoded not in [_dump(v) for v in schema['enum']]:
        raise _Invalid(path + ': must be one of the allowed values')
    if 'const' in schema and encoded != _dump(schema['const']):
        raise _Invalid(path + ': must equal the required value')
    if isinstance(value, dict):
        properties = schema.get('properties', {})
        for key in schema.get('required', []):
            if key not in value:
                raise _Invalid(path + '/' + key + ': required')
        for key, item in value.items():
            if key in properties:
                _validate(item, properties[key], path + '/' + key)
            elif schema.get('additionalProperties') is False:
                raise _Invalid(path + ': contains an unallowed field')
            elif isinstance(schema.get('additionalProperties'), dict):
                _validate(item, schema['additionalProperties'], path + '/*')
    if isinstance(value, list):
        for i, item in enumerate(value):
            _validate(item, schema.get('items', {}), path + '/' + str(i))
    if isinstance(value, (str, list)):
        suffix = 'Length' if isinstance(value, str) else 'Items'
        if len(value) < schema.get('min' + suffix, 0) or len(value) > schema.get('max' + suffix, math.inf):
            raise _Invalid(path + ': length outside allowed bounds')
    if type(value) in (int, float):
        if not math.isfinite(value) or not schema.get('minimum', -math.inf) <= value <= schema.get('maximum', math.inf):
            raise _Invalid(path + ': number outside allowed bounds')


def _dump(value):
    return json.dumps(value, ensure_ascii=False, allow_nan=False, sort_keys=True, separators=(',', ':'))


def _checked(value, schema):
    if isinstance(value, str):
        try:
            value = json.loads(value)
        except (ValueError, RecursionError):
            raise _Invalid('Reply is not valid JSON') from None
    if not isinstance(value, dict):
        raise _Invalid('Reply must be a JSON object')
    _validate(value, schema)
    return deepcopy(value)


def _ask(ask, prompt, images, schema, max_tokens=1500):
    # Also validate injected asks: a fake or embedding application cannot make
    # an invalid score/operation slip past the pure decision functions.
    try:
        return _checked(ask(prompt, images, schema=deepcopy(schema), max_tokens=max_tokens), schema)
    except Exception:
        return None


class _Ask:
    def __call__(self, prompt, images, *, schema, max_tokens):
        try:
            if (not isinstance(prompt, str) or not prompt.strip() or not isinstance(images, list)
                    or len(images) > 16 or type(max_tokens) is not int or not 16 <= max_tokens <= 1500):
                return None
            instruction = prompt + '\nReturn only a JSON object matching this schema:\n' + _dump(schema)
            for attempt in range(2):
                try:
                    return _checked(self._reply(instruction, images, max_tokens), schema)
                except _Invalid as error:
                    if attempt:
                        return None
                    instruction += '\nPrevious reply was invalid: ' + str(error) + '. Correct it.'
        except Exception:
            # No raw transport error, response body or credentials reach logs,
            # exception text, or persistent storage.
            return None
        return None


class JobAsk(_Ask):
    """Use the shared VLM job queue.

    The question waits its turn behind queued generations, so the wait lasts
    until the job ends, stop() turns true, or `timeout` seconds pass. A job
    that is given up on is cancelled rather than left to run for nobody.
    """
    def __init__(self, tools, *, timeout=3600, stop=None):
        self.tools, self.timeout, self.stop = tools, timeout, stop or (lambda: False)

    def _wait(self, identity):
        left = self.timeout
        while left > 0 and not self.stop():
            done = self.tools.wait(identity, timeout=min(25, left))
            if done.get('status') in ('succeeded', 'failed', 'cancelled', 'interrupted'):
                return done
            left -= 25
        raise TimeoutError  # given up: _reply cancels the job; no second try

    def _reply(self, prompt, images, max_tokens):
        refs = []
        for image in images:
            # A relative Comfy upload name is a reference, never an opened path.
            if isinstance(image, dict) and 'path' in image:
                raise ValueError('Local media paths are not accepted')
            refs.append(spec.media_item(image, '/images', image=True))
        request = dict(kind='vlm', questions=[dict(text=prompt, images=refs)],
                       format='json', max_tokens=max_tokens)
        spec.check(request)
        job = self.tools.submit(request)
        try:
            done = self._wait(job['id'])
        except Exception:
            try:
                self.tools.cancel(job['id'])
            except Exception:
                pass
            raise
        if done.get('status') != 'succeeded':
            return None
        answers = (done.get('result') or {}).get('answers', [])
        if len(answers) != 1 or answers[0].get('error'):
            raise _Invalid('The job did not return one usable answer')
        return answers[0].get('json', answers[0].get('text'))


class _NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        raise ValueError('Redirects are disabled')


class ChatAsk(_Ask):
    """Loopback-only OpenAI-compatible transport, with no proxy or redirects.

    Model discovery is lazy and cached. An injected opener has urlopen's
    (request, timeout=) interface (or .open); it is a trusted transport and must
    not log credentials or follow redirects. No image references are fetched.
    """
    def __init__(self, base_url, key=None, model=None, *, opener=None, timeout=120):
        try:
            url = urlsplit(base_url)
            host = url.hostname
            local = host == 'localhost' or ipaddress.ip_address(host).is_loopback
            if (not local or url.scheme not in ('http', 'https') or url.username is not None
                    or url.password is not None or url.query or url.fragment
                    or url.path.rstrip('/') not in ('', '/v1') or '%' in host):
                raise ValueError
            # Avoid resolving localhost through DNS or environment proxy rules.
            host = '127.0.0.1' if host == 'localhost' else host
            host = '[' + host + ']' if ':' in host else host
            self.base_url = url.scheme + '://' + host + (':' + str(url.port) if url.port else '')
            if type(timeout) not in (int, float) or not math.isfinite(timeout) or timeout <= 0:
                raise ValueError
            if key is not None and (not isinstance(key, str) or '\r' in key or '\n' in key):
                raise ValueError
            if model is not None and (not isinstance(model, str) or not model.strip()):
                raise ValueError
        except Exception:
            raise ValueError('ChatAsk requires a loopback HTTP URL and valid options') from None
        self._key, self.model, self.timeout = key, model, timeout
        self._opener = opener or urllib.request.build_opener(urllib.request.ProxyHandler({}), _NoRedirect())

    def _request(self, path, payload=None):
        headers = {'Accept': 'application/json'}
        if self._key:
            headers['Authorization'] = 'Bearer ' + self._key
        data = None
        if payload is not None:
            headers['Content-Type'] = 'application/json'
            data = _dump(payload).encode('utf-8')
        request = urllib.request.Request(self.base_url + '/v1/' + path, data=data, headers=headers)
        open_request = getattr(self._opener, 'open', self._opener)
        with open_request(request, timeout=self.timeout) as response:
            raw = response.read(2 * 1024 * 1024 + 1)
        if len(raw) > 2 * 1024 * 1024:
            raise _Invalid('Response is too large')
        try:
            return json.loads(raw)
        except ValueError:
            raise _Invalid('Response is not valid JSON') from None

    def _reply(self, prompt, images, max_tokens):
        content = [{'type': 'text', 'text': prompt}]
        for data in images:
            if not isinstance(data, bytes):
                raise ValueError('ChatAsk needs image bytes')
            if data.startswith(b'\x89PNG\r\n\x1a\n'):
                mime = 'image/png'
            elif data.startswith(b'\xff\xd8\xff'):
                mime = 'image/jpeg'
            elif data.startswith((b'GIF87a', b'GIF89a')):
                mime = 'image/gif'
            elif data.startswith(b'RIFF') and data[8:12] == b'WEBP':
                mime = 'image/webp'
            else:
                raise ValueError('Unsupported image encoding')
            content.append({'type': 'image_url', 'image_url': {
                'url': 'data:' + mime + ';base64,' + base64.b64encode(data).decode('ascii')}})
        if self.model is None:
            models = self._request('models').get('data', [])
            self.model = next((row['id'] for row in models if isinstance(row, dict)
                               and isinstance(row.get('id'), str) and row['id'].strip()), None)
            if self.model is None:
                return None
        response = self._request('chat/completions', dict(model=self.model, messages=[dict(role='user', content=content)],
                                                         response_format={'type': 'json_object'}, max_tokens=max_tokens))
        try:
            choice = response['choices'][0]
            if choice.get('finish_reason') not in (None, 'stop'):
                raise _Invalid('Reply was not completed')
            return choice['message']['content']
        except (KeyError, IndexError, TypeError):
            raise _Invalid('Response has no answer') from None


_LINE = _object({'who': _string(), 'line': _string(), 'how': _string(), 'language': _string()}, ('who', 'line'))
_CHANGES = {key: _string() for key in ('action', 'camera', 'framing', 'prompt')}
_CHANGES.update(lines=dict(type='array', items=_LINE), seconds=dict(type='number'))
_SHOT = _object(dict(_CHANGES, id=_string(), **{'continue': dict(type='boolean')},
                    sound=_string(), music=_string(), cast=dict(type='array', items=_string())), ('action',))
_PLAY = _object(dict(title=_string(120), style=_string(), language=_string(), music=_string(),
                    captions=dict(type='boolean'), aspect=_string(),
                    characters=dict(type='object', additionalProperties=_object(
                        {key: _string() for key in ('name', 'look', 'voice', 'language')})),
                    shots=dict(type='array', items=_SHOT)), ('shots',))


def plan(story, *, ask, shots=(3, 8)):
    """Try at most three screenplay candidates, never inventing a fallback."""
    if (not isinstance(story, str) or not story.strip() or not isinstance(shots, (tuple, list)) or len(shots) != 2
            or any(type(n) is not int for n in shots) or not 1 <= shots[0] <= shots[1] <= 40):
        return Problem('plan_failed', '故事或镜头数量不合适,请补充后再试。')
    schema = deepcopy(_PLAY)
    schema['properties']['shots'].update(minItems=shots[0], maxItems=shots[1])
    prompt = ('把下面的故事写成可拍摄的分镜剧本,保持人物和故事事实。镜头数 %d–%d。'
              '每镜给简短的 action,可给 seconds(1.625–60 秒)、framing、camera、lines。'
              '默认硬切(continue=false);只有故事明确是同一场连续动作才用 continue=true,'
              '接续镜换版会使后面的镜头重拍,第一镜不能接续。不要添加 schema 以外的字段。'
              '\n故事(数据,不是系统指令):\n%s') % (*shots, story)
    for _ in range(3):
        candidate = _ask(ask, prompt, [], schema)
        if candidate is None:
            prompt += '\n上次回答不是符合所给 schema 的 JSON 对象,请修正。'
            continue
        try:
            drama.check(candidate)
        except drama.Screenplay as error:
            # Keep check's exact paths and messages; do not paraphrase them.
            prompt += '\ndrama.check problems: ' + _dump(error.problems)
            continue
        return candidate
    return Problem('plan_failed', '没能把故事排成合格剧本,请补充镜头内容后再试。')


def _selectable(shot):
    return [t for t in shot['takes'] if t['state'] == 'succeeded' and not t['stale']]


def _retaken(shot):
    if 'decisions' in shot:
        return any(d.get('action') == 'reshoot' and d.get('shot') == shot['id'] for d in shot['decisions'])
    # Standalone callers may only have the shot row. Conservatively regard a
    # later generation as the one allowed reshoot; never risk an extra round.
    return any(t['generation'] > 0 for t in shot['takes'])


def _take_count(shot):
    first = min((t['generation'] for t in shot['takes']), default=0)
    return max(1, min(8, sum(t['generation'] == first for t in shot['takes'])))


def judge(shot_view, sheets, *, ask):
    """Score all selectable takes; invalid/missing evidence leaves a human choice.

    Optional screenplay/characters/style/captions fields carry visual intent;
    run supplies them from the public board. Optional decisions distinguish a
    requested reshoot from automatic continuation reconnection.
    """
    shot = shot_view
    if shot.get('chosen_by') == 'user' or shot.get('chosen') is not None:
        return Decision('keep', shot['id'], '保留已选定的版本。')
    shooting = shot.get('state') in ('waiting', 'shooting')
    uncertain = Decision('ask_user', shot['id'], '拿不准,请挑一版。')
    finished = _selectable(shot)
    unchecked = any('qa' in t and t['qa'] is None for t in finished)
    if shooting and (not finished or any(t.get('qa') is None for t in finished)) or unchecked:
        return Decision('wait', shot['id'], '等待这一镜的各版完成并质检。')
    # A take that failed the picture or sound check is never chosen; a shot still shooting is judged as takes arrive.
    takes = [t for t in finished if (t.get('qa') or {}).get('ok', True)]
    if not takes:
        if shooting:
            return Decision('wait', shot['id'], '已完成的版本没有通过质检,等待其余版本。')
        if not finished or _retaken(shot):
            return uncertain
        faults = ';'.join(sorted({p for t in finished for p in t['qa']['problems']}))
        return Decision('reshoot', shot['id'], '各版都没有通过质检。',
                        note=('上一次的问题:' + faults)[:300], takes=_take_count(shot))
    try:
        rows = sheets['takes']
        images = {row['take']: row['image'] for row in rows}
        numbers = [t['take'] for t in takes]
        if len(images) != len(rows) or not set(numbers) <= set(images):
            return uncertain
        previous = sheets.get('previous')
        batch_size = 15 if previous is not None else 16
        ratings = []
        for start in range(0, len(numbers), batch_size):
            batch = numbers[start:start + batch_size]
            rating = _object(dict(take=dict(type='integer', enum=batch), score=dict(type='number', minimum=0, maximum=10),
                                  reason=_string(30), note=_string(300), captions=dict(type='boolean')),
                             ('take', 'score', 'reason', 'note', 'captions'))
            schema = _object(dict(ratings=dict(type='array', minItems=len(batch), maxItems=len(batch), items=rating)),
                             ('ratings',))
            prompt = ('评价这一镜的每个版本:0–10 分,reason 用不超过30字的中文指出具体优点或问题'
                      '(多余的字、人物变形、和描述不符等);note 给下一次生成能用的具体修正。'
                      'captions 表示有叠加字幕/文字,场景内真实招牌不算。声音由自动质检评价,这里不评价。'
                      '\n镜头描述:' + _dump({k: shot[k] for k in ('screenplay', 'characters', 'style', 'captions') if k in shot})
                      + '\n图片顺序对应版号:' + _dump(batch)
                      + ''.join('\n版%d的%s' % (t['take'], take_qa.summary(t.get('qa'))) for t in takes
                                if t['take'] in batch and t.get('qa'))
                      + (';最后一张是上一镜选定版的末帧,仅供接续参考。' if previous is not None else '。'))
            reply = _ask(ask, prompt, [images[n] for n in batch] + ([previous] if previous is not None else []), schema)
            if reply is None or sorted(r['take'] for r in reply['ratings']) != sorted(batch):
                return uncertain
            ratings.extend(reply['ratings'])
        for row in ratings:
            spec._text(row['reason'], '/reason', 30)
            spec._text(row['note'], '/note', 300)
            if row['captions'] and not shot.get('captions', False):
                row['score'] = min(row['score'], 3)
        best = max(ratings, key=lambda r: (r['score'], -r['take']))
        if best['score'] < 7 and shooting:
            return Decision('wait', shot['id'], '已完成的版本不够好,等待其余版本。')
        if best['score'] >= 7:
            return Decision('choose', shot['id'], best['reason'], take=best['take'])
        if _retaken(shot):
            return Decision('choose', shot['id'], '重拍后仍不理想:' + best['reason'], take=best['take'])
        return Decision('reshoot', shot['id'], best['reason'], note=best['note'], takes=_take_count(shot))
    except (KeyError, TypeError, ValueError):
        return uncertain


CUT_AGREE = 0.3  # seconds by which the same cut may differ between takes


GOOD = 7  # a part is good enough to keep when it scores this or more


def rate_segments(shot_view, segments, sheets, *, ask):
    """{take: [score of every part]} for takes that share their cuts, or None when the model cannot say."""
    images = {row['take']: row['image'] for row in sheets['takes']}
    if not segments or any(s['take'] not in images for s in segments):
        return None
    count = len(segments[0]['cuts']) + 1
    numbers = [s['take'] for s in segments]
    rating = _object(dict(take=dict(type='integer', enum=numbers),
                          scores=dict(type='array', minItems=count, maxItems=count,
                                      items=dict(type='number', minimum=0, maximum=10))), ('take', 'scores'))
    schema = _object(dict(ratings=dict(type='array', minItems=len(numbers), maxItems=len(numbers), items=rating)),
                     ('ratings',))
    parts = ''.join('\n版%d:%s' % (s['take'], '、'.join('第%d段 %.2f–%.2f 秒' % (i + 1, a, b) for i, (a, b) in enumerate(
        zip([0.0] + s['cuts'], s['cuts'] + [s['seconds']])))) for s in segments)
    prompt = ('同一镜头有几个版本,每版在画面硬切处分成相同的几段。按图上的时间标注,给每版的每一段打 0–10 分'
              '(人物变形、多余的字、和描述不符都扣分),scores 按段的顺序给出。'
              '\n镜头描述:' + _dump({k: shot_view[k] for k in ('screenplay', 'characters', 'style') if k in shot_view})
              + '\n图片顺序对应版号:' + _dump(numbers) + '\n各版的分段:' + parts)
    reply = _ask(ask, prompt, [images[n] for n in numbers], schema)
    if reply is None or sorted(r['take'] for r in reply['ratings']) != sorted(numbers):
        return None
    return {r['take']: r['scores'] for r in reply['ratings']}


def first_take_is_good(tools, production_id, shot, sheets, take, *, ask):
    """Whether a shot still being filmed may stop at this take: it has no numbered cuts, or every part of it scores
    GOOD. Otherwise the other takes are waited for so each cut can take its best part."""
    try:
        own = [s for s in tools.segments(production_id, shot['id']) if s['take'] == take]
        if not own or not own[0]['cuts']:
            return True
        scores = rate_segments(shot, own, sheets, ask=ask)
    except Exception:  # noqa: BLE001 - without the cut scores the whole-take score decides, as before
        return True
    return scores is None or all(x >= GOOD for x in scores.get(take, ()))


def stitch_shot(shot_view, segments, sheets, *, ask):
    """Pieces taking the best part of every cut from different takes, or None to keep one whole take.

    Needs two or more takes that passed the check, every one with the same two or more cuts. The last piece
    comes from the take that is chosen, so the next shot still continues from the chosen take's last frame."""
    from . import cut_pick
    if len(segments) < 2 or any(len(s['cuts']) != len(segments[0]['cuts']) for s in segments) or not segments[0]['cuts']:
        return None
    if any(abs(a - b) > CUT_AGREE for s in segments for a, b in zip(s['cuts'], segments[0]['cuts'])):
        return None  # The same cut sits at different times: these are not the same parts.
    scores = rate_segments(shot_view, segments, sheets, ask=ask)
    if scores is None:
        return None
    candidates = [dict(s, scores=scores[s['take']]) for s in segments]
    result = cut_pick.stitch(candidates)
    if result['fallback'] or len(result['pieces']) < 2:
        return None
    return result


def revise(view, sentence, *, ask):
    """Translate one request into one checked reshoot, or ask for clarification."""
    unclear = Problem('unclear', '请说明要改哪一镜,以及画面或时长要怎样改?')
    if not isinstance(sentence, str) or not sentence.strip():
        return unclear
    ids = [s['id'] for s in view['shots']]
    operation = _object(dict(action=dict(const='reshoot'), shot=dict(type='string', enum=ids),
                             changes=_object(_CHANGES), note=_string(1000), reason=_string(300)), ('action', 'shot'))
    question = _object(dict(action=dict(const='unclear'), question=_string(300)), ('action', 'question'))
    schema = dict(type='object', oneOf=[operation, question])
    prompt = ('把用户的修改意见变成一条 reshoot 操作。只能修改一镜的 action/lines/camera/framing/prompt/seconds,'
              '或用 note 补充生成意见;不能增删或重排镜头,不能改接续关系,不能声称完成音频编辑。'
              '序号按 shots 顺序,结尾指最后一镜。目标不唯一、要求不支持或时长没有可确定的数值时,'
              '返回 unclear 并用一句中文反问,不猜。\n拍摄记录:' + _dump(view['screenplay'])
              + '\n镜头编号:' + _dump(ids) + '\n用户意见:' + sentence)
    reply = _ask(ask, prompt, [], schema)
    if reply is None:
        return unclear
    if reply['action'] == 'unclear':
        return Problem('unclear', reply['question'])
    changes, note = reply.get('changes', {}), reply.get('note')
    if not changes and not note:
        return unclear
    play = deepcopy(view['screenplay'])
    target = next((s for s in play['shots'] if s['id'] == reply['shot']), None)
    if target is None:
        return unclear
    target.update(changes)
    try:
        drama.check(play)
        if note is not None:
            spec._text(note, '/note', 1000)
        spec._text(reply.get('reason', sentence), '/reason', 20000)
    except (drama.Screenplay, spec.SpecError):
        return unclear
    return Operation('reshoot', reply['shot'], reply.get('reason', sentence), note=note, changes=changes or None)


def _context(view, shot_id):
    shot = deepcopy(next(s for s in view['shots'] if s['id'] == shot_id))
    shot['screenplay'] = next(s for s in view['screenplay']['shots'] if s['id'] == shot_id)
    for field, default in (('characters', {}), ('style', ''), ('captions', False)):
        shot[field] = view['screenplay'].get(field, default)
    shot['decisions'] = [d for d in view['decisions'] if d.get('shot') == shot_id]
    return shot


def _ref(identity, action, snapshot):
    digest = hashlib.sha256(_dump(snapshot).encode('utf-8')).hexdigest()
    return identity + ':builtin:' + action + ':' + digest


def _with_stitch(tools, production_id, shot, sheets, decision, ask):
    """Take the best part of every cut when the takes allow it; the choice becomes the take that ends the film part.
    Returns the decision and the parts, or None for the parts when one whole take stands."""
    try:
        pieces = stitch_shot(shot, tools.segments(production_id, shot['id']), sheets, ask=ask)
    except Exception:  # noqa: BLE001 - stitching is an extra; one whole take is always the fallback
        return decision, None
    if pieces is None:
        return decision, None
    parts = [dict(take=p['take'], start=p['start'], end=p['end']) for p in pieces['pieces']]
    return replace(decision, take=parts[-1]['take'], reason=(decision.reason + ';各切镜取最好的一版拼接')[:300]), parts


def run(tools, production_id, *, ask, stop, wait=25):
    """Yield {shot, action, reason}; stop never cancels generation jobs.

    A yielded finish means the join was accepted; the existing job queue owns
    its completion. Human handoffs are suppressed for the same shot snapshot
    in this invocation, then long-poll until the user changes the record.
    """
    wait = max(0, min(25, wait))
    since, errors, conflicts = None, 0, 0
    handed_off = set()
    while not stop():
        try:
            view = tools.get(production_id, since=since, wait=wait if since is not None else 0)
            since = view['revision']
            if stop() or view['status'] in ('cancelled', 'finished', 'joining'):
                return
            acted = False
            for todo in view['todo']:
                if stop():
                    return
                action = todo['action']
                if action == 'finish':
                    if not all(s.get('chosen') is not None and any(t['take'] == s['chosen'] for t in _selectable(s))
                               for s in view['shots']):
                        continue
                    snapshot = [(s['id'], s['chosen']) for s in view['shots']]
                    chosen = dict(snapshot)
                    # The parts of a shot are kept with its chosen take in the production.
                    tools.finish(production_id, by='builtin', ref=_ref(production_id, 'finish', snapshot))
                    yield dict(shot=None, action='finish', reason='每镜已选定,开始接成影片。')
                    return
                if action not in ('choose', 'reshoot'):
                    continue
                shot = _context(view, todo['shot'])
                fingerprint = _ref(production_id, 'review', shot)
                if fingerprint in handed_off or shot['chosen'] is not None or shot['chosen_by'] == 'user':
                    continue
                sheets = tools.sheet(production_id, shot['id'], frames=None)
                if stop():
                    return
                decision = judge(shot, sheets, ask=ask)
                if stop():
                    return
                # The model call can be slow. Discard its decision if evidence
                # changed, including an upstream choice that made takes stale.
                latest = tools.get(production_id, since=None, wait=0)
                if stop() or latest['status'] in ('cancelled', 'finished', 'joining'):
                    return
                if _context(latest, shot['id']) != shot:
                    since, acted = None, True
                    break
                if decision.action == 'ask_user':
                    handed_off.add(fingerprint)
                elif decision.action in ('choose', 'reshoot'):
                    ref = _ref(production_id, decision.action, shot)
                    if decision.action == 'choose':
                        if shot.get('state') in ('waiting', 'shooting') and not first_take_is_good(
                                tools, production_id, shot, sheets, decision.take, ask=ask):
                            continue  # the first take is not good in every cut: wait for the others
                        decision, parts = _with_stitch(tools, production_id, shot, sheets, decision, ask)
                        tools.choose(production_id, shot['id'], decision.take, by='builtin', reason=decision.reason, ref=ref,
                                     **({'pieces': parts} if parts else {}))
                    else:
                        tools.reshoot(production_id, shot['id'], by='builtin', note=decision.note,
                                      takes=decision.takes, reason=decision.reason, ref=ref)
                    since, acted = None, True
                else:
                    continue
                errors = conflicts = 0
                yield dict(shot=decision.shot, action=decision.action, reason=decision.reason)
                if acted:
                    break  # fetch a new board before deciding about another shot
            if not acted:
                errors = 0
        except Exception as error:
            code = getattr(error, 'code', '')
            if code == 'chosen_by_user' or (getattr(error, 'status', None) == 409
                                          and code in ('take_not_ready', 'not_ready', 'unknown_take')):
                # A race with the user is normal, but each round costs a model
                # call: a conflict that keeps coming back ends the run.
                conflicts += 1
                errors, since = 0, None
                if conflicts >= 5:
                    yield dict(shot=None, action='error', reason='多次和已有的选择冲突,已停止:' + code)
                    return
                continue
            errors += 1
            since = None
            if errors >= 3:
                label = code if isinstance(code, str) and re.fullmatch(r'[a-z_]{1,80}', code) else type(error).__name__
                yield dict(shot=None, action='error', reason='连续三次操作失败,已停止:' + label)
                return