"""Bounded independent A/B screening workers; never production inference."""
from __future__ import annotations

from collections import defaultdict
from pathlib import Path
import gc
import importlib.metadata
import json
import math
import os
import shutil
import time
import traceback
import types

import cv2
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader, WeightedRandomSampler

from .mask_model_training import (
    ROIMaskBoundaryUNet, ROIDataset, seed_all, native_metrics, result_record,
    summarize, atomic_torch, grouped_bootstrap,
)
from .mask_model_training_data import (
    PROJECT, FALSE_FLAGS, gpu_receipt, verify_dataset, digest, read, write, require, unletterbox,
)
from .mask_model_training_rfdetr import ValidityDataset
from .mask_model_improvement_data import OUTPUT, OLD
from .mask_model_improvement_adapter import mask_logits, mask_loss, mask_costs, native_logits, highest_class_query

UPDATES = 1000
CONFIGS = {
    'A0': dict(route='m1', factor='anchor', group_uniform=False, edge_weight=.25, freeze_epochs=0, photometric=False),
    'A1': dict(route='m1', factor='group_uniform_sampler_only', group_uniform=True, edge_weight=.25, freeze_epochs=0, photometric=False),
    'A2': dict(route='m1', factor='mask_only_edge_weight_zero', group_uniform=False, edge_weight=0., freeze_epochs=0, photometric=False),
    'A3_freeze': dict(route='m1', factor='encoder_freeze_first5epochs_only', group_uniform=False, edge_weight=.25, freeze_epochs=5, photometric=False),
    'A3_photometric': dict(route='m1', factor='weak_image_only_photometric_only_no_crop_jitter', group_uniform=False, edge_weight=.25, freeze_epochs=0, photometric=True),
    'B0': dict(route='m2', factor='nearest_dense_masked_adapter_anchor', point_loss=False),
    'B1': dict(route='m2', factor='valid_2048_uncertainty_uniform_point_loss_only', point_loss=True),
}
GENERIC_SHA = {'m1': 'f37072fd47e89c5e827621c5baffa7500819f7896bbacec160b1a16c560e07ec',
               'm2': '6de3da31b2572cac214a1c76cce4a92a13966d56390ac2b3a3de9a8dc2b2bca3'}


def storage_guard(output):
    total = sum(p.stat().st_size for p in Path(output).rglob('*') if p.is_file())
    require(total < 25*2**30 and shutil.disk_usage(output).free > 10*2**30,
            'Bounded experiment/reserve exceeded; no old files may be deleted')


def predicted_rle(mask):
    from pycocotools import mask as mu
    rle = mu.encode(np.asfortranarray(mask.astype(np.uint8)))
    require(np.array_equal(mu.decode(rle), mask), 'Prediction RLE mismatch')
    return dict(size=rle['size'], counts=rle['counts'].decode('ascii'))


def macro(records, field):
    groups = defaultdict(list)
    for r in records:
        groups[str(r[field])].append(r['iou'])
    return float(np.mean([np.mean(v) for v in groups.values()])) if groups else None


def selection_score(records):
    return macro(records, 'component')


def risk_coverage(records):
    """Fixed diagnostic cutoffs, NOT a GT-selected acceptance rule."""
    result = []
    for cutoff in (0., .5, .7, .8, .9, .95, .99):
        subset = [r for r in records if not r['empty_prediction'] and r['confidence'] >= cutoff]
        result.append(dict(cutoff=cutoff, count=len(subset), coverage=len(subset)/max(len(records), 1),
            groups=len({r['component'] for r in subset}), metrics=summarize(subset),
            iou_under_090_risk=sum(r['iou'] < .9 for r in subset)/len(subset) if subset else None))
    return result


def report(records):
    records = list(records)
    groups = defaultdict(list)
    for r in records:
        groups[r['component']].append(r['iou'])
    means = np.array([np.mean(groups[k]) for k in sorted(groups)])
    rng = np.random.default_rng(42)
    macro_bootstrap = [float(np.mean(means[rng.integers(0, len(means), len(means))]))
                       for _ in range(2000)] if len(means) else []
    return dict(all=summarize(records), independent_groups=len({r['component'] for r in records}),
        macro_group_iou=macro(records, 'component'), macro_class_iou=macro(records, 'class_id'),
        by_class={str(c): dict(metrics=summarize(r for r in records if r['class_id'] == c),
                    groups=len({r['component'] for r in records if r['class_id'] == c})) for c in range(1, 23)},
        strata={f: {str(v): dict(metrics=summarize(r for r in records if r[f] == v),
                        groups=len({r['component'] for r in records if r[f] == v}))
                     for v in sorted({r[f] for r in records}, key=str)}
                for f in ('component', 'policy_id', 'surface', 'structure', 'bbox_height_band')},
        grouped_bootstrap=grouped_bootstrap(records, repetitions=2000),
        macro_group_mean_95pct_ci=[float(np.quantile(macro_bootstrap, q)) for q in (.025, .975)] if macro_bootstrap else None,
        risk_coverage_diagnostic_not_quality_probability=risk_coverage(records),
        semantic_topology_quality='unknown; no invented hole/corner/face GT',
        accepted_count=0, all_target_denominator=len(records), accepted_quality=None,
        rejection_rate=1., release_state='blocked_no_calibrated_semantic_release',
        rejection_reasons=['uncalibrated_quality_score', 'semantic_trial_pending'] +
            (['insufficient_group_support'] if len({r['component'] for r in records}) < 10 else []) +
            (['native_quality_failed'] if records and (np.median([r['iou'] for r in records]) < .95 or
                np.mean([r['iou'] >= .9 for r in records]) < .9 or any(r['hd95_native_pixels'] is None or
                    r['hd95_native_pixels'] > max(2, .1*r['bbox_height']) for r in records)) else []),
        formal_val_test_prediction_count=0, blind_evaluation=False, deployment_authorized=False, **FALSE_FLAGS)


def m1_loss(mask, edge, target, boundary, valid, edge_weight=.25):
    with torch.autocast(device_type=mask.device.type, enabled=False):
        mask, edge, target, boundary, valid = (v.float() for v in (mask, edge, target, boundary, valid))
        count = valid.sum().clamp_min(1)
        bce = (F.binary_cross_entropy_with_logits(mask, target, reduction='none') * valid).sum()/count
        probability = mask.sigmoid() * valid
        dice = 1 - (2 * (probability * target).sum((1, 2, 3)) + 1) / ((probability + target*valid).sum((1, 2, 3)) + 1)
        edge_loss = (F.binary_cross_entropy_with_logits(edge, boundary, reduction='none',
                     pos_weight=edge.new_tensor(4.)) * valid).sum()/count
        return bce + dice.mean() + edge_weight * edge_loss


def soft_edge_loss(mask, target, valid):
    """Contract-only differentiable mask-gradient route, NOT used in A0-A3."""
    probability = mask.float().sigmoid()
    dx = F.pad(probability[..., :, 1:] - probability[..., :, :-1], (0, 1))
    dy = F.pad(probability[..., 1:, :] - probability[..., :-1, :], (0, 0, 0, 1))
    return ((dx.abs()+dy.abs()-target).abs()*valid).sum()/valid.sum().clamp_min(1)


def photometric(inputs, valid):
    mean = inputs.new_tensor([.485, .456, .406])[None, :, None, None]
    std = inputs.new_tensor([.229, .224, .225])[None, :, None, None]
    grey = (inputs*std+mean)[:, :1].clamp(0, 1)
    b = len(inputs)
    gain = torch.empty(b, 1, 1, 1, device=inputs.device).uniform_(.85, 1.15)
    gamma = torch.empty(b, 1, 1, 1, device=inputs.device).uniform_(.8, 1.2)
    sigma = torch.empty(b, 1, 1, 1, device=inputs.device).uniform_(0, 3/255)
    grey = (grey.pow(gamma)*gain+torch.randn_like(grey)*sigma).clamp(0, 1)
    # Weak blur uses a convex mixture, maximum roughly subpixel smoothing.
    blur_mix = torch.empty(b, 1, 1, 1, device=inputs.device).uniform_(0, .25)
    grey = grey*(1-blur_mix)+F.avg_pool2d(grey, 3, 1, 1)*blur_mix
    rgb = grey.expand(-1, 3, -1, -1)*valid
    return (rgb-mean)/std


@torch.no_grad()
def m1_predictions(model, dataset):
    model.eval()
    records = []
    for image, _, _, _, record, transform, target in dataset.items:
        start = time.monotonic()
        logits, _ = model(image[None].cuda())
        probability = native_logits(logits[0, 0], transform).sigmoid().cpu().numpy()
        mask = probability >= .5
        confidence = float(probability[mask].mean()) if mask.any() else 0.
        entry = result_record(record, native_metrics(mask, target), confidence, time.monotonic()-start)
        entry.update(development_split=record['development_split'], confidence_semantics='foreground_mean_NOT_quality_probability')
        entry['native_prediction_rle'] = predicted_rle(mask)
        records.append(entry)
    return records


def train_m1(output, out, records, config, weights):
    seed_all()
    train = ROIDataset(output, records, 'train')
    dev = ROIDataset(output, records, 'dev')
    cal = ROIDataset(output, records, 'calibration')
    model = ROIMaskBoundaryUNet(weights).cuda()
    optimizer = torch.optim.AdamW([
        dict(params=model.encoder.parameters(), lr=3e-5),
        dict(params=[p for n, p in model.named_parameters() if not n.startswith('encoder.')], lr=3e-4)], weight_decay=1e-4)
    generator = torch.Generator().manual_seed(42)
    group_sizes = defaultdict(int)
    for *_, r, _, _ in train.items:
        group_sizes[r['component']] += 1
    sampler = None
    if config['group_uniform']:
        sampler = WeightedRandomSampler([1/group_sizes[item[4]['component']] for item in train.items],
                                        len(train), replacement=True, generator=generator)
    loader = DataLoader(train, batch_size=8, shuffle=sampler is None, sampler=sampler,
                        num_workers=0, generator=generator)
    step, epoch, exposures, best, best_step = 0, 0, 0, -1., 0
    started = time.monotonic()
    gradient_audit_done = False
    while step < UPDATES:
        storage_guard(output)
        epoch += 1
        frozen = epoch <= config['freeze_epochs']
        for p in model.encoder.parameters():
            p.requires_grad_(not frozen)
        model.train()
        for module in model.encoder.modules():
            if isinstance(module, nn.BatchNorm2d):
                module.eval()
        losses = []
        for image, target, boundary, valid in loader:
            if step >= UPDATES:
                break
            image, target, boundary, valid = (v.cuda() for v in (image, target, boundary, valid))
            if config['photometric']:
                image = photometric(image, valid)
            optimizer.zero_grad(set_to_none=True)
            mask, edge = model(image)
            loss = m1_loss(mask, edge, target, boundary, valid, config['edge_weight'])
            require(bool(torch.isfinite(loss)), 'Nonfinite loss')
            if not gradient_audit_done and not frozen:
                shared = model.up0[0].weight
                mask_grad = torch.autograd.grad(m1_loss(mask, edge, target, boundary, valid, 0), shared, retain_graph=True)[0]
                edge_term = m1_loss(mask, edge, target, boundary, valid, .25)-m1_loss(mask, edge, target, boundary, valid, 0)
                edge_grad = torch.autograd.grad(edge_term, shared, retain_graph=True)[0]
                write(out/'shared_gradient_audit.json', dict(mask_gradient_norm=float(mask_grad.norm()),
                    edge_gradient_norm=float(edge_grad.norm()), cosine=float(F.cosine_similarity(mask_grad.flatten(), edge_grad.flatten(), dim=0)),
                    direct_edge_gradient_to_mask_head=False, inference_uses_boundary_head=False,
                    effective_edge_weight=config['edge_weight'], mask_derived_soft_edge_used=False))
                gradient_audit_done = True
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 5.)
            optimizer.step()
            step += 1; exposures += len(image)
            losses.append(float(loss.detach()))
            if step == 1:
                write(out/'first_actual_training_batch.json', dict(state='actual_forward_backward_optimizer_step_succeeded',
                    optimizer_step=step, physical_batch=len(image), loss=losses[-1], pid=os.getpid(),
                    peak_cuda_bytes=torch.cuda.max_memory_allocated(), seconds=time.monotonic()-started))
                print(json.dumps(dict(run=out.name, actual_optimizer_step=step, loss=losses[-1])), flush=True)
        predictions = m1_predictions(model, dev)
        score = selection_score(predictions)
        if score > best + .001:
            best, best_step = score, step
            atomic_torch(out/'best.pt', dict(model=model.state_dict(), step=step, epoch=epoch, native_macro_group_iou=score))
        atomic_torch(out/'last.pt', dict(model=model.state_dict(), optimizer=optimizer.state_dict(),
            step=step, epoch=epoch, best_step=best_step, torch_rng=torch.get_rng_state(), cuda_rng=torch.cuda.get_rng_state()))
        entry = dict(epoch=epoch, optimizer_step=step, samples_exposed=exposures, encoder_frozen=frozen,
            train_loss=float(np.mean(losses)), dev_native=summarize(predictions),
            dev_macro_group_iou=score, dev_macro_class_iou=macro(predictions, 'class_id'),
            best_step=best_step, elapsed_seconds=time.monotonic()-started,
            peak_cuda_bytes=torch.cuda.max_memory_allocated())
        write(out/'epochs'/f'{epoch:04d}.json', entry)
        print(json.dumps(dict(run=out.name, **entry)), flush=True)
    model.load_state_dict(torch.load(out/'best.pt', map_location='cpu', weights_only=True)['model'])
    for split, dataset in [('dev', dev), ('calibration', cal)]:
        predictions = m1_predictions(model, dataset)
        write(out/f'{split}_predictions.jsonl', predictions, lines=True)
        write(out/f'{split}_quality.json', report(predictions))
    return dict(actual_optimizer_updates=step, epochs=epoch, samples_exposed=exposures,
                best_step=best_step, checkpoint='best.pt', checkpoint_sha256=digest(out/'best.pt'))


@torch.no_grad()
def rf_predictions(model, items, *, diagnostic=False):
    model.eval()
    records, baseline = [], []
    for image, record, transform, target in items:
        start = time.monotonic()
        outputs = model(image[None].cuda())
        query, confidence = highest_class_query(outputs)
        raw = mask_logits(outputs)[0, query]
        probability = native_logits(raw, transform).sigmoid().cpu().numpy()
        entry = result_record(record, native_metrics(probability >= .5, target), confidence, time.monotonic()-start)
        entry.update(development_split=record['development_split'], selected_query=query,
                     confidence_semantics='classification_score_NOT_boundary_quality_probability')
        entry['native_prediction_rle'] = predicted_rle(probability >= .5)
        records.append(entry)
        if diagnostic:
            # Original double-threshold path retained separately as B2 inference
            # diagnostic. NEVER used for B0/B1 selection or loss attribution.
            w, h = transform['output_size']
            hard = (F.interpolate(raw[None, None], size=(h, w), mode='bilinear', align_corners=False).sigmoid()[0, 0] > .5).cpu().numpy()
            old = unletterbox(hard.astype(np.float32), transform) >= .5
            baseline.append(dict(target_key=record['target_key'], component=record['component'],
                once_threshold=native_metrics(probability >= .5, target),
                old_double_threshold=native_metrics(old, target),
                differing_native_pixels=int(np.count_nonzero(old != (probability >= .5))),
                query= query, fixed_threshold=.5, diagnostic_only_not_loss_change=True))
    return records, baseline


def rf_items(output, records, split, transforms):
    items = []
    for r in records:
        if r['development_split'] != split:
            continue
        s = 'valid' if split == 'dev' else split
        image = cv2.imread(str(output/'dataset_rfdetr'/s/Path(r['image']).name), cv2.IMREAD_GRAYSCALE)
        rgb = np.repeat(image[None].astype(np.float32)/255, 3, axis=0)
        rgb = (rgb-np.array([.485, .456, .406], np.float32)[:, None, None])/np.array([.229, .224, .225], np.float32)[:, None, None]
        target = cv2.imread(str(output/r['native_mask']), cv2.IMREAD_GRAYSCALE) > 0
        items.append((torch.from_numpy(rgb), r, transforms[str(r['image_id'])], target))
    return items


def train_m2(output, out, records, config, weights):
    import pytorch_lightning as pl
    from rfdetr import RFDETRSegSmall
    from rfdetr.training import RFDETRDataModule, RFDETRModelModule, build_trainer
    from rfdetr.training.callbacks.coco_eval import _get_ema_inner_module
    transforms = read(output/'private/rfdetr_transforms.json')
    dev = rf_items(output, records, 'dev', transforms)
    cal = rf_items(output, records, 'calibration', transforms)
    class Data(RFDETRDataModule):
        def setup(self, stage):
            require(stage in ('fit', 'validate'), 'Formal val/test/predict forbidden')
            super().setup(stage)
            for name in ('_dataset_train', '_dataset_val'):
                dataset = getattr(self, name)
                if dataset is not None and not isinstance(dataset, ValidityDataset):
                    setattr(self, name, ValidityDataset(dataset, transforms))
    class Receipt(pl.Callback):
        def __init__(self):
            self.best, self.best_step, self.samples = -1., 0, 0
            self.started, self.last_eval = time.monotonic(), -1

        def evaluation_model(self, trainer, module):
            for callback in trainer.callbacks:
                ema = _get_ema_inner_module(callback)
                if ema is not None:
                    return ema.model
            raise ValueError('EMA required; do not silently select base model')

        def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx):
            self.samples += len(batch[1])
            if trainer.global_step >= 1 and not (out/'first_actual_training_batch.json').exists():
                write(out/'first_actual_training_batch.json', dict(state='actual_forward_backward_optimizer_step_succeeded',
                    optimizer_step=int(trainer.global_step), physical_batch=len(batch[1]),
                    accumulation=4, samples_exposed=self.samples, pid=os.getpid(),
                    peak_cuda_bytes=torch.cuda.max_memory_allocated(), seconds=time.monotonic()-self.started))
                print(json.dumps(dict(run=out.name, actual_optimizer_step=int(trainer.global_step))), flush=True)

        def evaluate_native(self, trainer, module):
            storage_guard(output)
            if self.last_eval == trainer.global_step:
                return
            self.last_eval = trainer.global_step
            evaluation = self.evaluation_model(trainer, module)
            predictions, _ = rf_predictions(evaluation, dev)
            score = selection_score(predictions)
            if score > self.best + .001:
                self.best, self.best_step = score, int(trainer.global_step)
                atomic_torch(out/'best.pt', dict(model=evaluation.state_dict(), step=self.best_step,
                    epoch=int(trainer.current_epoch)+1, native_macro_group_iou=score, ema=True))
            metrics = {k: float(v.detach().cpu()) for k, v in trainer.callback_metrics.items()
                       if isinstance(v, torch.Tensor) and v.numel() == 1 and torch.isfinite(v)}
            entry = dict(epoch=int(trainer.current_epoch)+1, optimizer_step=int(trainer.global_step),
                samples_exposed=self.samples, native_dev=summarize(predictions),
                dev_macro_group_iou=score, dev_macro_class_iou=macro(predictions, 'class_id'),
                upstream_metrics=metrics, best_step=self.best_step,
                elapsed_seconds=time.monotonic()-self.started, peak_cuda_bytes=torch.cuda.max_memory_allocated())
            write(out/'epochs'/f'step_{trainer.global_step:05d}.json', entry)
            print(json.dumps(dict(run=out.name, **entry)), flush=True)

        def on_validation_end(self, trainer, pl_module):
            if not trainer.sanity_checking and trainer.global_step:
                self.evaluate_native(trainer, pl_module)

        def on_train_end(self, trainer, pl_module):
            self.evaluate_native(trainer, pl_module)

    seed_all(); pl.seed_everything(42, workers=True)
    model = RFDETRSegSmall(num_classes=1, resolution=576, device='cuda:0', pretrain_weights=str(weights),
                          gradient_checkpointing=True, compile=False)
    n = sum(r['development_split'] == 'train' for r in records)
    steps_per_epoch = math.ceil(math.ceil(n/2)/4)
    epochs = math.ceil(UPDATES/steps_per_epoch)
    tc = model.get_train_config(dataset_dir=str(output/'dataset_rfdetr'), output_dir=str(out/'upstream'),
        epochs=epochs, batch_size=2, grad_accum_steps=4, eval_batch_size=1,
        lr=1e-4, lr_encoder=1e-5, seed=42, num_workers=0, multi_scale=False,
        expanded_scales=False, scale_jitter=False, aug_config={}, use_ema=True,
        early_stopping=False, run_test=False, tensorboard=False, wandb=False,
        progress_bar=None, checkpoint_interval=1000, compute_val_loss=True,
        class_names=['fork_face'], square_resize_div_64=True, eval_masks_head_resolution=False,
        accelerator='gpu', devices=1, pin_memory=True, amp_dtype='auto', save_dataset_grids=False,
        lr_drop=1000)
    mc = model.model_config
    write(out/'rfdetr_config.json', dict(model=mc.model_dump(mode='json'), training=tc.model_dump(mode='json'),
        exact_max_optimizer_steps=UPDATES, expected_steps_per_full_epoch=steps_per_epoch,
        baseline_shared_contracts='FP32 accumulation, ignore lattice interpolation, query-order/zero-match safety',
        B0_B1_single_factor='matched mask loss only; same matcher/architecture/EMA/optimizer/data/postprocess',
        selection='same native macro-group IoU .5, min_delta .001; upstream AP diagnostic not native selection',
        B2_inference_only='same raw logits once-threshold used for BOTH; old double-threshold diagnostic separately'))
    del model; gc.collect(); torch.cuda.empty_cache()
    module = RFDETRModelModule(mc, tc)
    module.criterion.improvement_point_loss = config['point_loss']
    module.criterion.loss_masks = types.MethodType(mask_loss, module.criterion)
    module.criterion.matcher._compute_mask_costs = types.MethodType(mask_costs, module.criterion.matcher)
    callback = Receipt()
    trainer = build_trainer(tc, mc, accelerator='gpu', devices=1, max_steps=UPDATES,
                            num_sanity_val_steps=0, enable_progress_bar=False)
    trainer.callbacks.append(callback)
    trainer.fit(module, Data(mc, tc))
    require(trainer.global_step == UPDATES, 'Optimizer update budget mismatch')
    # Last optimizer/EMA state is saved by upstream; custom last is weights only.
    atomic_torch(out/'last.pt', dict(model=module.model.state_dict(), step=int(trainer.global_step), ema=True))
    module.model.load_state_dict(torch.load(out/'best.pt', map_location='cpu', weights_only=True)['model'])
    for split, items in [('dev', dev), ('calibration', cal)]:
        predictions, diagnostic = rf_predictions(module.model, items, diagnostic=True)
        write(out/f'{split}_predictions.jsonl', predictions, lines=True)
        write(out/f'{split}_quality.json', report(predictions))
        write(out/f'B2_inference_only_{split}_double_threshold_diagnostic.jsonl', diagnostic, lines=True)
    return dict(actual_optimizer_updates=int(trainer.global_step), epochs=int(trainer.current_epoch),
        samples_exposed=callback.samples, best_step=callback.best_step, checkpoint='best.pt',
        checkpoint_sha256=digest(out/'best.pt'), EMA=True)


def predeclare(output=OUTPUT):
    output = Path(output)
    require((output/'private/preservation_after_freeze.json').is_file(), 'After-freeze static preservation check required')
    require((output/'contracts_execution_pin.xml').is_file(), 'Contract receipt required')
    manifest, _ = verify_dataset(output)
    code = {str(p.relative_to(PROJECT)): digest(p) for p in
            sorted((PROJECT/'pallet').glob('mask_model_improvement*.py')) +
            sorted((PROJECT/'scripts').glob('*mask_model_improvement*.py')) +
            sorted((PROJECT/'tests').glob('test_mask_model_improvement*.py')) +
            sorted((PROJECT/'pallet').glob('mask_model_training*.py'))}
    write(output/'execution_plan.json', dict(configurations=CONFIGS, seed=42, updates=UPDATES,
        dataset_manifest_sha256=digest(output/'dataset_manifest.json'), splits=manifest['splits'],
        independent_components=manifest['independent_components'], code_sha256=code,
        contracts_junit_sha256=digest(output/'contracts_execution_pin.xml'),
        source_binding_sha256=digest(output/'private/source_binding.json'),
        preservation_before_sha256=digest(output/'private/preservation_before.json'),
        preservation_after_freeze_sha256=digest(output/'private/preservation_after_freeze.json'),
        selection='dev native macro-group mean IoU .5 only; min_delta .001; no per-target oracle',
        calibration='held-out fold1, evaluate frozen best after training only; not used in model/threshold selection',
        postprocessing='M1/M2 raw FP32 logits->native->sigmoid->single threshold .5, no threshold search',
        candidate_gate='No A4/A5/seeds43/44 automatically. At most two only after completed fixed-screen evidence and independent approval.',
        release='minimum10groups unchanged + medianIoU .95 +90%IoU>=.90 + per-targetHD95; semantic trial pending',
        class_weight_cap_experiment=False, crop_jitter_experiment=False, final_quality_guaranteed=False,
        official_val_test_predictions=0, production_activation=False))
    snap = output/'code_snapshot'; snap.mkdir(mode=0o700)
    for name, expected in code.items():
        destination = snap/Path(name).name
        with destination.open('xb') as stream:
            stream.write((PROJECT/name).read_bytes())
        destination.chmod(0o600)
        require(digest(destination) == expected, 'Code pin copy mismatch')
    return code


def worker(name, output=OUTPUT):
    output = Path(output).resolve()
    config = CONFIGS[name]
    route = config['route']
    out = output/name
    out.mkdir(mode=0o700)
    (out/'epochs').mkdir(mode=0o700)
    started = time.monotonic()
    try:
        plan = read(output/'execution_plan.json')
        require(all(digest(PROJECT/n) == h for n, h in plan['code_sha256'].items()), 'Pinned code changed after declaration')
        require(digest(output/'dataset_manifest.json') == plan['dataset_manifest_sha256'], 'Dataset changed')
        storage_guard(output)
        gpu = gpu_receipt(route)
        require(torch.cuda.is_available() and torch.cuda.device_count() == 1, 'One UUID bound GPU required')
        torch.cuda.set_device(0); torch.set_num_threads(4); cv2.setNumThreads(1)
        require(importlib.metadata.version('rfdetr') == '1.11.0' and torch.__version__.startswith('2.6.0'), 'Pinned dependencies changed')
        _, records = verify_dataset(output)
        path = OLD/'generic_pretraining'/('resnet18-f37072fd.pth' if route == 'm1' else 'rf-detr-seg-small.pt')
        require(digest(path) == GENERIC_SHA[route], 'Generic pretrained SHA mismatch; task checkpoint forbidden')
        write(out/'worker_start.json', dict(config=config, gpu=gpu, seed=42,
            generic_weights_path=str(path.relative_to(PROJECT)), generic_sha256=GENERIC_SHA[route],
            old_finetuned_checkpoint_used=False, optimizer_updates_budget=UPDATES,
            dataset_manifest_sha256=plan['dataset_manifest_sha256'], code_sha256=plan['code_sha256'],
            dependencies={n: importlib.metadata.version(n) for n in ('torch', 'torchvision', 'rfdetr', 'pytorch-lightning')},
            formal_val_test_predictions=0, production_writes=0, **FALSE_FLAGS))
        torch.cuda.reset_peak_memory_stats()
        result = (train_m1 if route == 'm1' else train_m2)(output, out, records, config, path)
        verify_dataset(output)
        write(out/'completion.json', dict(state='completed_bounded_exploratory_screen_not_release',
            name=name, **result, elapsed_seconds=time.monotonic()-started,
            peak_cuda_bytes=torch.cuda.max_memory_allocated(), gpu=gpu,
            native_threshold=.5, production_writes=0, formal_val_test_predictions=0, **FALSE_FLAGS))
        print(json.dumps(dict(name=name, state='completed', **result)), flush=True)
    except BaseException as error:
        write(out/'failure.json', dict(state='failed_not_complete_evidence_retained', error_type=type(error).__name__,
            error=str(error), traceback=traceback.format_exc(), elapsed_seconds=time.monotonic()-started))
        raise
