#!/usr/bin/env python3 """Offline compare candidate source against captured live deployment build commits. No Git fetch, cluster writes, releases, PRs or credentials are performed here. A diverged candidate requires reviewed integration; patch IDs alone are not proof. """ import argparse import json from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parents[1] def git(*args): result = subprocess.run(['git', *args], cwd=ROOT, capture_output=True, text=True) if result.returncode: raise ValueError('Required source commit is unavailable locally; fetch/review before release.') return result.stdout.strip() def audit(deployments, candidate): target = git('rev-parse', '--verify', '--end-of-options', candidate + '^{commit}') results = [] for deployment in deployments['items']: name = deployment['metadata']['name'] if name not in ('droboticflow-api', 'droboticflow-annotation-processor', 'droboticflow-worker'): continue for container in deployment['spec']['template']['spec']['containers']: live = next((e.get('value') for e in container.get('env', []) if e['name'] == 'DROBOTICFLOW_BUILD_COMMIT'), None) if not live: results.append({'deployment': name, 'result': 'missing_live_commit'}) continue live = git('rev-parse', '--verify', '--end-of-options', live + '^{commit}') ancestor = subprocess.run(['git', 'merge-base', '--is-ancestor', live, target], cwd=ROOT, capture_output=True) if ancestor.returncode not in (0, 1): raise ValueError('Cannot determine source lineage.') paths = git('diff', '--name-only', live, target, '--', 'src', 'internal', 'cmd', 'scripts', 'backend', 'operator-repos', 'Dockerfile.api', 'Dockerfile.worker', 'go.mod', 'go.sum', 'package.json').splitlines() results.append({'deployment': name, 'image': container['image'], 'liveCommit': live, 'candidateCommit': target, 'result': 'lineage_present' if ancestor.returncode == 0 else 'integration_review_required', 'differentPaths': paths}) present = {r['deployment'] for r in results} missing = sorted({'droboticflow-api', 'droboticflow-annotation-processor', 'droboticflow-worker'} - present) return {'candidate': target, 'components': results, 'missingComponents': missing, 'lineageComplete': not missing and all(r['result'] == 'lineage_present' for r in results), 'limitation': 'Lineage only: ancestry is not functional compatibility or deployment approval. Review differentPaths and run regressions. Snapshot freshness and cluster identity must be checked separately.'} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--snapshot', required=True, type=Path) parser.add_argument('--candidate', default='HEAD') parser.add_argument('--output', type=Path) args = parser.parse_args() try: result = audit(json.loads(args.snapshot.read_text()), args.candidate) encoded = json.dumps(result, ensure_ascii=False, indent=2) + '\n' if args.output: args.output.write_text(encoded) print(encoded) return 0 if result['lineageComplete'] else 2 except (ValueError, KeyError, OSError) as e: print('Source audit blocked: ' + type(e).__name__, file=sys.stderr) return 2 if __name__ == '__main__': sys.exit(main())