Compare commits

...
Author SHA1 Message Date
james.maesandCodex e3fc68965f fix: audit explicitly selected repository branches
Co-Authored-By: Codex <noreply@openai.com>
2026-10-21 09:02:34 -05:00
3 changed files with 95 additions and 8 deletions

No files matched your search

+12
View File
@@ -3,6 +3,18 @@
`tools/audit_estate.py` classifies repositories from explicit mappings and observed source.
It never changes consumers, branch rules or publication settings.
Source auditing reads the repository's current default branch unless
`--live --source-branch main` (or an explicit `release/...` or `hotfix/...` branch)
is supplied. Run separate audits for each maintained source branch: a clean
default-branch report does not establish conformity on main or release branches.
The selected branch is resolved to an immutable commit before any files are read.
Reports identify both `source_branch` and the repository's `default_branch`;
the manifest's `default_branch` must still match the repository default.
Unavailable selected branches remain unknown without falling back to default.
Explicit branch selection requires live lookup and cannot relabel an offline SHA.
Required-check policy still controls which branch settings are inspected;
`--source-branch` changes source selection, not that enforcement policy.
- Circle profiles include android, mcp-policy and marketing. Their manifest and canonical
setup caller remain mandatory; a missing configuration is a finding.
- MCP, business-platform and the organization `.github` repository use approved native
+58
View File
@@ -26,6 +26,64 @@ class Snapshot:
class AuditTests(unittest.TestCase):
def test_live_snapshot_reads_requested_branch_and_keeps_default_identity(self):
entry = dict(ENTRY, sourceBranch='release/1.2')
replies = [{'default_branch': 'develop', 'archived': False}, {'sha': SHA},
{'truncated': False, 'tree': []}]
with patch.object(audit, 'gh', side_effect=replies) as gh:
snapshot = audit.Snapshot(entry, live=True)
self.assertEqual('develop', snapshot.default_branch)
self.assertEqual('release/1.2', snapshot.branch)
self.assertEqual('repos/QRun-IO/qctl/commits/release%2F1.2', gh.call_args_list[1].args[0])
def test_other_source_branch_does_not_change_manifest_default_branch(self):
entry = dict(ENTRY, name='qbit-help', nameWithOwner='QRun-IO/qbit-help')
manifest = {'profile': 'qbit', 'orb_version': VERSION, 'default_branch': 'develop',
'publish': {'snapshot': False, 'release_candidate': False, 'release': False}}
files = {'.qqq-ci.yml': audit.generator.yaml.safe_dump(manifest),
'.circleci/config.yml': audit.generator.yaml.safe_dump(audit.standard.canonical_caller('qbit', VERSION))}
snapshot = Snapshot(files)
snapshot.branch = 'main'
snapshot.default_branch = 'develop'
report = audit.audit(entry, snapshot, VERSION, SHA, POLICY)
self.assertEqual([], report['findings'])
self.assertEqual('main', report['source_branch'])
self.assertEqual('develop', report['default_branch'])
def test_selected_branch_read_failure_is_unknown_not_default_fallback(self):
captured = []
def factory(entry, live):
captured.append(entry)
raise audit.ReadError('branch unavailable')
report = audit.run([ENTRY], VERSION, SHA, {}, live=True,
snapshot_factory=factory, source_branch='main')
self.assertEqual('main', captured[0]['sourceBranch'])
self.assertEqual('unknown', report['results'][0]['status'])
self.assertEqual('main', report['results'][0]['source_branch'])
self.assertNotIn('sourceBranch', ENTRY)
def test_omitted_source_branch_still_reads_repository_default(self):
replies = [{'default_branch': 'develop', 'archived': False}, {'sha': SHA},
{'truncated': False, 'tree': []}]
with patch.object(audit, 'gh', side_effect=replies) as gh:
snapshot = audit.Snapshot(ENTRY, live=True)
self.assertEqual('develop', snapshot.branch)
self.assertEqual('develop', snapshot.default_branch)
self.assertEqual('repos/QRun-IO/qctl/commits/develop', gh.call_args_list[1].args[0])
def test_explicit_source_branch_cannot_relabel_offline_snapshot(self):
with self.assertRaises(ValueError):
audit.run([ENTRY], VERSION, SHA, {}, source_branch='main')
with self.assertRaises(audit.ReadError):
audit.Snapshot(dict(ENTRY, sourceBranch='main'), live=False)
def test_invalid_source_branch_is_rejected_before_api_calls(self):
for branch in ('', 'main?ref=develop', 'main\n', 123):
with self.subTest(branch=branch), patch.object(audit, 'gh') as gh:
with self.assertRaises(audit.ReadError):
audit.Snapshot(dict(ENTRY, sourceBranch=branch), live=True)
gh.assert_not_called()
def native(self, files=None, checks=None, policy=POLICY):
files = files if files is not None else {'.github/workflows/build.yml': audit.qctl.generate(SHA)}
return audit.audit(ENTRY, Snapshot(files, checks), VERSION, SHA, policy)
+25 -8
View File
@@ -6,6 +6,8 @@ Example: python tools/audit_estate.py --inventory inventory.json --orb-version 0
Policy: {"profiles":{"qbit":{"required_checks":{"main":["observed context"],
"develop":["observed context"]}}},"repositories":{"QRun-IO/example":{...}}}.
Only use contexts verified on hosted canaries. Absent policy is unknown, not pass.
Use --live --source-branch main (or release/...) to inspect that branch's source;
omitting it retains default-branch auditing. Unavailable branches remain unknown.
A profile/repository policy may set rc_worker_sha independently of --worker-sha
so an RC-only worker change does not require updating unchanged native workers.
"""
@@ -71,11 +73,17 @@ class Snapshot:
raise ReadError('invalid repository name')
self.live = live
self.path = entry.get('localPath', '')
self.branch = (entry.get('defaultBranchRef') or {}).get('name')
requested = entry.get('sourceBranch')
if requested is not None and (not live or not isinstance(requested, str)
or not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9._/-]*', requested)):
raise ReadError('explicit source branch requires a valid branch name and live lookup')
self.default_branch = (entry.get('defaultBranchRef') or {}).get('name')
self.branch = requested or self.default_branch
self.sha = entry.get('sha', '')
if live:
info = gh('repos/' + self.repo)
self.branch = info['default_branch']
self.default_branch = info['default_branch']
self.branch = requested or self.default_branch
if info['archived']:
raise ReadError('repository is now archived; refresh inventory')
self.sha = gh('repos/' + self.repo + '/commits/' + quote(self.branch, safe=''))['sha']
@@ -169,7 +177,8 @@ def audit(entry, snapshot, version, worker_sha, policy):
name = entry['name']
profile = policy.get('profile') or ('qbit' if name.startswith('qbit-') else PROFILES.get(name))
result = {'repository': entry['nameWithOwner'], 'sha': snapshot.sha,
'default_branch': snapshot.branch, 'profile': profile, 'findings': [], 'required_checks': {}}
'default_branch': getattr(snapshot, 'default_branch', snapshot.branch),
'source_branch': snapshot.branch, 'profile': profile, 'findings': [], 'required_checks': {}}
findings = result['findings']
def finding(code, path=None, detail=None):
findings.append({k: v for k, v in {'code': code, 'path': path, 'detail': detail}.items() if v is not None})
@@ -198,7 +207,7 @@ def audit(entry, snapshot, version, worker_sha, policy):
manifest = Path(directory) / 'manifest.yml'
manifest.write_text(raw, encoding='utf-8')
generator.load_manifest(manifest, pin)
if data.get('default_branch') != snapshot.branch:
if data.get('default_branch') != result['default_branch']:
finding('default_branch_drift', '.qqq-ci.yml')
if caller is not None:
standard.validate(caller, raw, pin)
@@ -324,9 +333,13 @@ def audit(entry, snapshot, version, worker_sha, policy):
return result
def run(entries, version, worker_sha, policies, live=False, snapshot_factory=Snapshot):
def run(entries, version, worker_sha, policies, live=False, snapshot_factory=Snapshot, source_branch=None):
if source_branch is not None and not live:
raise ValueError('Explicit source branch audits require --live')
results = []
for entry in entries:
if source_branch is not None:
entry = dict(entry, sourceBranch=source_branch)
if entry.get('isArchived'):
results.append({'repository': entry['nameWithOwner'], 'status': 'archived_skip'})
continue
@@ -337,10 +350,11 @@ def run(entries, version, worker_sha, policies, live=False, snapshot_factory=Sna
try:
results.append(audit(entry, snapshot_factory(entry, live), version, worker_sha, policy))
except (ReadError, OSError, ValueError, KeyError, subprocess.TimeoutExpired):
results.append({'repository': entry['nameWithOwner'], 'status': 'unknown', 'reason': 'source/API/policy read failed'})
results.append({'repository': entry['nameWithOwner'], 'source_branch': source_branch,
'status': 'unknown', 'reason': 'source/API/policy read failed'})
return {'inventory_count': len(entries), 'active_count': sum(not e.get('isArchived', False) for e in entries),
'covered_count': len(results), 'unique_repository_count': len({e['nameWithOwner'] for e in entries}),
'live': live, 'results': results}
'live': live, 'source_branch_selector': source_branch or 'default', 'results': results}
def main():
@@ -350,12 +364,15 @@ def main():
parser.add_argument('--worker-sha')
parser.add_argument('--policy', type=Path)
parser.add_argument('--live', action='store_true', help='Read current source and branch rules using existing gh authentication')
parser.add_argument('--source-branch', help='Inspect this branch instead of the repository default; requires --live')
parser.add_argument('--expected-active', type=int, default=68)
parser.add_argument('--output', type=Path)
args = parser.parse_args()
if args.source_branch is not None and not args.live:
parser.error('--source-branch requires --live')
entries = json.loads(args.inventory.read_text(encoding='utf-8'))
policies = json.loads(args.policy.read_text(encoding='utf-8')) if args.policy else {}
report = run(entries, args.orb_version, args.worker_sha, policies, args.live)
report = run(entries, args.orb_version, args.worker_sha, policies, args.live, source_branch=args.source_branch)
report['expected_active_count'] = args.expected_active
report['coverage_matches_expected'] = (report['active_count'] == args.expected_active
and report['unique_repository_count'] == report['inventory_count'])