#!/usr/bin/env python3
"""Track a durable assistant workflow; publish results only after the package audit."""
import argparse
from datetime import datetime
import hashlib
import json
import os
from pathlib import Path
import statistics
import time
from zoneinfo import ZoneInfo


def read(path):
    return json.loads(path.read_text())


def sha(path):
    digest = hashlib.sha256()
    with path.open('rb') as stream:
        for chunk in iter(lambda: stream.read(4 * 1024 * 1024), b''):
            digest.update(chunk)
    return digest.hexdigest()


def write(path, data):
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(path.suffix + '.tmp')
    temporary.write_text(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
    temporary.replace(path)


def status(output):
    result = {'updated_at': datetime.now(ZoneInfo('Asia/Shanghai')).isoformat(),
              'state': 'running', 'output': str(output)}
    merge = output/'merge_summary.json'
    if merge.exists():
        result['manual_merge'] = read(merge)
    metrics = output / 'training/metrics.jsonl'
    rows = []
    command_path = output / 'training_command.json'
    if command_path.exists():
        config = read(command_path)['config']
        result['training_runtime'] = {key: config.get(key)
            for key in ('batch_size', 'effective_batch', 'workers', 'validation_batch_size')}
    if metrics.exists():
        for line in metrics.read_text().splitlines():
            try:
                rows.append(json.loads(line))
            except json.JSONDecodeError:
                pass  # A writer may still be appending its last row.
    if rows:
        best = max(rows, key=lambda row: row['validation']['AP50_95'])
        result.update(completed_epochs=len(rows), latest_epoch=rows[-1]['epoch'],
                      best_epoch=best['epoch'], best_validation_AP50_95=best['validation']['AP50_95'])
        if len(rows) >= 10:
            seconds = statistics.median(row['epoch_seconds'] for row in rows[:10])
            result['first_ten_epochs_median_seconds'] = seconds
            result['max_training_remaining_seconds_estimate'] = max(0, 300 - len(rows)) * seconds
    if (output / 'training/completed.json').exists():
        result['stage'] = 'calibrate_predict_package'
    else:
        result['stage'] = 'training'
    if (output / 'prediction_manifest.json').exists():
        result['prediction'] = {key: read(output / 'prediction_manifest.json').get(key)
                                for key in ('state', 'expected_frames', 'completed_frames', 'errors')}
    return result, rows


def finish(output, rows, project):
    audit = read(output / 'package_audit.json')
    completion = read(output / 'training/completed.json')
    command = read(output / 'training_command.json')
    partition = read(output / 'manual_partition.json')
    prediction = read(output / 'prediction_manifest.json')
    threshold = read(output / 'confidence_threshold.json')
    imported = read(output / 'manual_import_manifest.json')
    archive = output / f"human_correction_remaining_{audit['frames']}_v1.zip"
    best = max(rows, key=lambda row: row['validation']['AP50_95'])
    checks = {
        'audit_passed': audit['state'] == 'passed' and audit['zip_crc'] == 'passed',
        'zip_sha256': sha(archive) == audit['zip_sha256'],
        'model_sha256': sha(output / 'training/best.pt') == completion['best_checkpoint_sha256']
            == command['checkpoint_sha256'] == prediction['checkpoint_sha256']
            == threshold['checkpoint_sha256'],
        'run_config_sha256': sha(output / 'training/run.json') == completion['run_config_sha256'],
        'exact_prediction_coverage': prediction['state'] == 'complete'
            and prediction['completed_frames'] == prediction['expected_frames'] == audit['frames']
            and prediction['errors'] == 0,
        'manual_frames_excluded': audit['manual_overlap'] == 0,
        'group_disjoint': not set(partition['train_groups']) & set(partition['validation_groups']),
        'frame_disjoint': not set(partition['train_frame_ids']) & set(partition['validation_frame_ids']),
        'labels_sha256': sha(output / 'inputs/annotations.jsonl') == partition['manual_annotations_sha256'],
        'no_test_scoring': audit['validation_test_labels_scored'] is False
            and threshold['test_accessed'] is False,
    }
    if not all(checks.values()):
        raise RuntimeError('Delivery checks failed: ' + str(checks))
    summary = {'state': 'complete', 'completed_at': datetime.now(ZoneInfo('Asia/Shanghai')).isoformat(),
        'training_runtime': {key: command['config'].get(key)
            for key in ('batch_size', 'effective_batch', 'workers', 'validation_batch_size')},
        'manual_frames': imported['frame_count'], 'manual_status_counts': imported['status_counts'],
        'manual_boxes': imported['box_count'], 'training_frames': len(partition['train_frame_ids']),
        'validation_frames': len(partition['validation_frame_ids']),
        'completed_epochs': completion['completed_epochs'], 'best_epoch': best['epoch'],
        'stopped_reason': completion['stopped_reason'],
        'best_validation': {key: best['validation'].get(key)
                            for key in ('AP50', 'AP75', 'AP50_95', 'precision',
                                        'recall_at_threshold_iou_0.5', 'false_positives_per_frame')},
        'threshold': threshold['selected_confidence'],
        'threshold_metrics': threshold['selected_validation_metrics'],
        'predicted_frames': prediction['completed_frames'], 'errors': prediction['errors'],
        'split_counts': prediction['by_split'], 'package_audit': audit,
        'archive': str(archive), 'archive_sha256': audit['zip_sha256'],
        'checkpoint': str(output / 'training/best.pt'),
        'checkpoint_sha256': completion['best_checkpoint_sha256'], 'checks': checks,
        'validation_used_for_model_selection': True, 'test_labels_scored': False}
    merge = output/'merge_summary.json'
    if merge.exists():
        summary['manual_merge'] = read(merge)
    write(output / 'delivery_manifest.json', summary)
    marker = f'<!-- assistant-delivery:{output.name} -->'
    v = summary['best_validation']
    body = (f'\n\n{marker}\n## {summary["completed_at"][:10]}：累计人工标签辅助分支完成\n\n'
        f'{summary["manual_frames"]}张人工帧按组隔离为{summary["training_frames"]}训练、'
        f'{summary["validation_frames"]}验证。YOLO26s实际{summary["completed_epochs"]}个epoch，'
        f'因{summary["stopped_reason"]}停止；最佳epoch{summary["best_epoch"]}，'
        f'内部人工验证AP50/AP75/AP50:95={v["AP50"]:.5f}/{v["AP75"]:.5f}/{v["AP50_95"]:.5f}。'
        f'阈值{summary["threshold"]:.2f}，校准召回{summary["threshold_metrics"]["recall"]:.5f}。'
        '该验证集参与选轮与校准，指标不作为独立泛化结果；未进行测试标签评分。\n\n'
        f'剩余{summary["predicted_frames"]}帧预测全部成功、0错误，人工帧重叠0，'
        '全量配对/尺寸/坐标/待审状态与ZIP CRC审计通过。'
        f'包：`{archive.relative_to(project)}`；SHA-256 `{summary["archive_sha256"]}`。'
        '零检测仍待审，val/test候选仅用于人工修正。'
        f'完整结果见 `{(output / "delivery_manifest.json").relative_to(project)}`。\n')
    for name in ('training_log.md', 'change_log.md', 'session_summary.md'):
        path = project / 'docs' / name
        text = path.read_text()
        if marker not in text:
            path.write_text(text + body)
    path = project / 'docs/human_correction_assistant.md'
    text = path.read_text()
    if merge.exists():
        import subprocess
        import sys
        subprocess.run([sys.executable, str(project/'scripts/finalize_manual_6552.py'),
                        '--output', str(output)], check=True)
        if marker not in text:
            path.write_text(text + body)
        report = project/'docs/human_correction_6552_report.md'
        if report.exists() and marker not in report.read_text():
            report.write_text(report.read_text() + body)
        write(project/'outputs/human_correction_latest.json', {
            'state':'complete','delivery':str(output/'delivery_manifest.json'),
            'archive':str(archive),'checkpoint':str(output/'training/best.pt')})
        write(project/'outputs/anylabeling_latest.json', {
            'kind':'model_assisted_correction_delivery','delivery':str(output/'delivery_manifest.json'),
            'protocol':str(project/'outputs/protocol_v6_clean'), 'remaining_review_package':str(archive)})
        readme = project/'README.md'
        current = readme.read_text()
        if marker not in current:
            readme.write_text(current + body)
        return summary
    start = text.index('## 当前分支：累计1,716帧')
    end = text.index('## 上一分支：', start)
    if marker not in text:
        path.write_text(text[:start] + body.strip() + '\n\n' + text[end:])
    path = project / 'README.md'
    text = path.read_text()
    pending = '最新1,716帧人工修正辅助分支正在导入、冻结分组划分和重新训练；预计重预测8,806帧。'
    text = text.replace(pending, '最新1,716帧人工修正辅助分支已完成训练和8,806帧候选包全量审计。')
    path.write_text(text)
    return summary


def alive(pid):
    try:
        os.kill(pid, 0)
        # A zombie is not a running workflow.
        return Path(f'/proc/{pid}/stat').read_text().split(') ', 1)[1][0] != 'Z'
    except (ProcessLookupError, FileNotFoundError):
        return False


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', required=True)
    parser.add_argument('--workflow-pid', type=int, required=True)
    parser.add_argument('--status-alias')
    args = parser.parse_args()
    project = Path(__file__).resolve().parent.parent
    output = (project / args.output).resolve()
    def publish(current):
        write(output / 'workflow_status.json', current)
        if args.status_alias:
            write((project / args.status_alias).resolve(),
                  {**current, 'redirected_to': str(output / 'workflow_status.json'),
                   'previous_branch_archived': True})
    disk_paused = False
    while True:
        import shutil
        import signal
        if alive(args.workflow_pid) and shutil.disk_usage(project).free < 50 * 2**30:
            if not disk_paused:
                os.killpg(args.workflow_pid, signal.SIGSTOP)
                disk_paused = True
            current, _ = status(output)
            current.update(state='paused_for_disk_reserve', reason='Available disk space below 50 GiB; workflow process group stopped.')
            publish(current)
            time.sleep(30)
            continue
        if disk_paused and alive(args.workflow_pid):
            os.killpg(args.workflow_pid, signal.SIGCONT)
            disk_paused = False
        current, rows = status(output)
        if (output / 'package_audit.json').exists() and not alive(args.workflow_pid):
            current = finish(output, rows, project)
            publish(current)
            print(json.dumps(current, ensure_ascii=False), flush=True)
            return
        if not alive(args.workflow_pid):
            current.update(state='failed_resumable', failure='Workflow stopped before complete package audit.')
            publish(current)
            raise SystemExit(current['failure'])
        publish(current)
        time.sleep(30)


if __name__ == '__main__':
    main()
