#!/usr/bin/env python3 """Derive only qualified-observer capability markers from the original Linux pack. Original sources/artifacts/units/cache are never modified. Only agent programs are rebuilt. Helper/updater/native contract executables remain exact originals. """ import hashlib import json import os from pathlib import Path import shutil import uuid import linux_updater_qualification as q ROOT=Path(__file__).resolve().parents[1] BASE=ROOT/'devstate/linux-updater-qualification/21d20391-6102-4996-8a7a-efbd3e78bd7d' BASE_MANIFEST='9ed5f153fe76665150aaab298c4b21d74d7199b2312a28fa23aa9e30b13ddd08' MODULE='crates/agent/src/agent_update_apply.rs' MAIN='crates/agent/src/main.rs' def main(): os.umask(0o077) assert q.sha((BASE/'manifest.json').read_bytes())==BASE_MANIFEST old=json.loads((BASE/'manifest.json').read_text());case=ROOT/'devstate/linux-updater-capability-build'/str(uuid.uuid4());case.mkdir(mode=0o700,parents=True) fixed=(ROOT/MODULE).read_bytes();old_module=(BASE/'source0'/MODULE).read_bytes() # Closed byte transformation only, including the normal formatter's import wrap. source=old_module.decode().replace('agent_update::{ArtifactDescriptor, ArtifactRole, Platform},','agent_update::{\n APPLY_CAPABILITY, ArtifactDescriptor, ArtifactRole, CHECK_CAPABILITY, Platform,\n },') source=source.replace(' vec![\n "update_agent".into(),',' vec![\n APPLY_CAPABILITY.into(),\n CHECK_CAPABILITY.into(),\n "update_agent".into(),') assert source.encode()==fixed,'exact minimal agent capability transformation required' main=(ROOT/MAIN).read_bytes();old_main=(BASE/'source0'/MAIN).read_text() block=''' // Planning pins must be refreshed before the 90-second stored-data // planning limit. One inflight blocking collector bounds pressure; // heartbeats never perform this metadata work. if update_at.elapsed() >= Duration::from_secs(60) { self.queue_update_observation(generation)?; update_at = Instant::now(); } ''' needle=' if !busy && capability_at.elapsed()>=Duration::from_secs(60) {capability_at=Instant::now();if self.capabilities()!=advertised{return Ok(());}}' assert old_main.count(block)==1 and old_main.count(needle)==1 moved='\n'.join(' '+line if line else line for line in block.splitlines())+'\n' assert old_main.replace(block,'').replace(needle,moved+needle).encode()==main,'exact existing observer poll move before capability check required' q.write(case/'entered.json',q.encode({'base_manifest_sha256':BASE_MANIFEST,'module_sha256':q.sha(fixed),'main_sha256':q.sha(main),'no_target_mutations':True,'original_case_unchanged':True})) cache=case/'cache';cache.mkdir(mode=0o700) # Copy, never hardlink: compiler output cannot change the original cache. shutil.copytree(BASE/'cache/release',cache/'release') artifacts={};input_hashes={} for version in ('source0','source1'): expected=json.loads((BASE/(version+'-inputs.json')).read_text());names=sorted(expected) assert q.capture(BASE/version,names)==expected target=case/version;shutil.copytree(BASE/version,target) module=target/MODULE;module.chmod(0o600);assert module.read_bytes()==old_module;module.write_bytes(fixed);module.chmod(0o444) entry=target/MAIN;entry.chmod(0o600);assert entry.read_bytes()==old_main.encode();entry.write_bytes(main);entry.chmod(0o444) actual=q.capture(target,names);assert {n for n in names if actual[n]!=expected[n]}=={MODULE,MAIN} q.readonly(target);q.write(case/(version+'-inputs.json'),q.encode(actual));input_hashes[version]=q.sha((case/(version+'-inputs.json')).read_bytes()) q.run(case,target,cache,version+'-clippy',['cargo','clippy','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--all-targets','--','-D','warnings']) q.run(case,target,cache,version+'-agent-release',['cargo','build','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--bin','rmm-agent']) output=case/'artifacts'/version;output.mkdir(mode=0o700,parents=True) agent=q.copy_artifact(cache/'release/rmm-agent',output/'rmm-agent');assert agent['sha256']!=old['artifacts'][version]['agent']['sha256'] artifacts[version]={**old['artifacts'][version],'agent':agent} assert q.capture(target,names)==actual assert artifacts['source0']['agent']['sha256']!=artifacts['source1']['agent']['sha256'] for label,item in [('helper0',old['artifacts']['source0']['helper']),('helper1',old['artifacts']['source1']['helper']),('updater',old['stable_updater'])]: assert q.sha(Path(item['path']).read_bytes())==item['sha256'],label for unit in old['units'].values():assert q.sha(Path(unit['path']).read_bytes())==unit['sha256'] manifest={'schema_version':1,'profile':'linux_compatible_updater_capability_derivative_v2','base_manifest_sha256':BASE_MANIFEST,'base_manifest':str(BASE/'manifest.json'),'changed_modules':{MODULE:q.sha(fixed),MAIN:q.sha(main)},'poll_harvest_before_capability_check':True,'sampler_frequency_unchanged':True,'source0_version':'0.1.0','source1_version':'0.1.1','source_input_hashes':input_hashes,'builder':q.BUILDER,'artifacts':artifacts,'stable_updater':old['stable_updater'],'units':old['units'],'old_sources_artifacts_units_unchanged':True,'only_agents_rebuilt':True,'source0_and_source1_clippy_passed':True,'native_vm_tested':False,'actual_qualified_capability_still_requires_native_baseline':True,'bootstrap_installed':False,'apply_executed':False} q.write(case/'manifest.json',q.encode(manifest));q.write(case/'completed.json',q.encode({'manifest_sha256':q.sha((case/'manifest.json').read_bytes())})) print(json.dumps({'case':str(case),'manifest_sha256':q.sha((case/'manifest.json').read_bytes()),'artifacts':artifacts})) if __name__=='__main__':main()