From f4b597d3f97caae9acae0b0e466092b7723acf21 Mon Sep 17 00:00:00 2001 From: Dylan Roy Date: Wed, 7 Oct 2026 10:50:13 +0000 Subject: [PATCH] Refuse repeated endpoint identities in paginated release snapshots Implements the reviewed BQ-28 design (main#275/6029127112) with the BQ-35 refinements R1/R2 (6035804991), under BQ44 token BQ44-R1-20261007T1034Z-c8si. Two files only. - Every paginated read now names an identity policy: repository-local PR number, exact changed-file filename, or positive exact-integer id. A paginated read without a policy fails closed through read()'s error path. - The check runs after the raw rows are written and before they are used. Repeated identities are refused, never deduplicated; the raw file keeps every row in order, the resource error makes the run incomplete, and later resources and repositories continue. - Malformed PR rows keep their per-row error path, so valid later PRs still run. Repeated valid PR numbers refuse the open-PR collection: open-prs.json is kept, open_pr_count is omitted and no per-PR read runs; the error says "open pull requests not processed". - Uniqueness is not completeness: an omitted row with no duplicate, or the API's own caps, cannot be detected here. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015wNj8LPTKXsaT68G3DgPPh --- tests/test_release_snapshot.py | 177 +++++++++++++++++++++++++++++++++ tools/release_snapshot.py | 60 +++++++++-- 2 files changed, 228 insertions(+), 9 deletions(-) diff --git a/tests/test_release_snapshot.py b/tests/test_release_snapshot.py index 3694a85a8..bce94b646 100644 --- a/tests/test_release_snapshot.py +++ b/tests/test_release_snapshot.py @@ -209,4 +209,181 @@ def corrupt_second(oid,data): self.assertFalse(out.exists(), 'failed snapshot must not publish a readable partial zip') self.assertFalse(list(root.glob('.snapshot-*')), 'temporary snapshot was not cleaned') + +def _identity_fixture(root): + fixture = root / 'fixture'; fixture.mkdir() + def git(*args): + return subprocess.check_output(['git', '-C', str(fixture), *args], + stderr=subprocess.DEVNULL).decode().strip() + git('init', '-q'); git('config', 'user.email', 'test@example.invalid') + git('config', 'user.name', 'test') + (fixture / 'proof.txt').write_bytes(b'unchanged source\n') + git('add', '.'); git('commit', '-qm', 'fixture') + return fixture, git('rev-parse', 'HEAD') + + +def _identity_collect(root, overrides): + """Mocked collect(); `overrides` maps (repository, resource) to raw rows.""" + fixture, sha = _identity_fixture(root) + calls = [] + class API: + def get(self, path): + name = path.split('/')[2] + resource = '/'.join(path.split('/')[3:]).split('?')[0] + page = 2 if 'page=2&' in path else 1 + calls.append((name, resource)) + for (repo, wanted), value in overrides.items(): + if repo == name and (resource == wanted or + (wanted.startswith('*') and resource.endswith(wanted[1:]))): + return value(page) if callable(value) else value + if not resource: + return {'private': False, 'full_name': f'{m.OWNER}/{name}', 'default_branch': 'main'} + if resource == 'commits/main': + return {'sha': sha} + if resource == 'pulls': + return [{'number': 7}] if name == 'main' else [] + if resource == 'pulls/7': + return {'number': 7, 'head': {'sha': sha, 'repo': {'full_name': f'{m.OWNER}/main'}}, + 'base': {'sha': sha}, 'user': {'login': 'fixture-author'}} + if resource.endswith('check-runs'): return {'check_runs': [], 'total_count': 0} + if resource == 'actions/runs': return {'workflow_runs': [], 'total_count': 0} + if resource.endswith('/status'): return {'state': 'success'} + return [] + out = root / 'out' + with mock.patch.object(m, 'REPOS', ('main', 'Math-')), \ + mock.patch.object(m, 'Client', API), \ + mock.patch.object(m, 'repo_url', return_value=str(fixture)): + result = m.collect(out) + return result, out, sha, calls + + +class IdentityPolicyTests(unittest.TestCase): + # (resource label in the error record, fake-API resource, raw output path, raw rows) + ROUTES = ( + ('/pulls/7/files', 'pulls/7/files', 'pr-7/files.json', + [{'filename': 'a.txt', 'sha': 'b1'}, {'filename': 'a.txt', 'sha': 'b2'}]), + ('/pulls/7/reviews', 'pulls/7/reviews', 'pr-7/reviews.json', + [{'id': 11, 'state': 'COMMENTED'}, {'id': 11, 'state': 'COMMENTED'}]), + ('/issues/7/comments', 'issues/7/comments', 'pr-7/comments.json', + [{'id': 12, 'body': 'x'}, {'id': 12, 'body': 'y'}]), + ('/pulls/7/comments', 'pulls/7/comments', 'pr-7/inline-comments.json', [{'id': 13}, {'id': 13}]), + ('/check-runs', '*/check-runs', 'pr-7/checks.json', [{'id': 14}, {'id': 14}]), + ('/actions/runs', 'actions/runs', 'pr-7/runs.json', [{'id': 15}, {'id': 15}]), + ('/rulesets', 'rulesets', 'rulesets.json', [{'id': 16}, {'id': 16}]), + ) + WRAPPED = {'/check-runs': 'check_runs', '/actions/runs': 'workflow_runs'} + + def test_duplicate_identity_is_refused_on_every_route_with_raw_rows_kept(self): + for label, resource, raw_path, rows in self.ROUTES: + with self.subTest(route=label), tempfile.TemporaryDirectory() as d: + value = rows + if label in self.WRAPPED: + value = {'total_count': len(rows), self.WRAPPED[label]: rows} + result, out, sha, _ = _identity_collect(pathlib.Path(d), {('main', resource): value}) + errors = [e for e in result['errors'] if e['repository'] == 'main'] + self.assertFalse(result['complete']) + self.assertEqual(len(errors), 1, errors) + self.assertIn(label, errors[0]['resource']) + self.assertIn('duplicate', errors[0]['error']) + self.assertEqual(json.loads((out / 'main' / raw_path).read_text()), rows) + later = result['repositories'][1] + self.assertEqual(later['default_commit'], sha) + self.assertEqual(len(later['snapshots']), 1) + + def test_duplicate_valid_pr_numbers_refuse_the_open_pr_collection(self): + with tempfile.TemporaryDirectory() as d: + rows = [{'number': 7, 'v': 'first'}, {'number': 7, 'v': 'second'}] + result, out, sha, calls = _identity_collect(pathlib.Path(d), {('main', 'pulls'): rows}) + errors = [e for e in result['errors'] if e['repository'] == 'main'] + self.assertEqual([e['resource'] for e in errors], ['/pulls?state=open']) + self.assertIn('duplicate', errors[0]['error']) + self.assertIn('not processed', errors[0]['error']) + self.assertEqual(json.loads((out / 'main' / 'open-prs.json').read_text()), rows) + main = result['repositories'][0] + self.assertNotIn('open_pr_count', main) + self.assertEqual(main['pull_requests'], []) + self.assertNotIn(('main', 'pulls/7'), calls) + self.assertFalse((out / 'main' / 'pr-7').exists()) + self.assertEqual(len(result['repositories'][1]['snapshots']), 1) + + def test_invalid_then_valid_pr_keeps_partial_progress(self): + with tempfile.TemporaryDirectory() as d: + rows = [{'number': '../outside'}, {'number': 7}] + result, out, sha, calls = _identity_collect(pathlib.Path(d), {('main', 'pulls'): rows}) + errors = [e for e in result['errors'] if e['repository'] == 'main'] + self.assertEqual(len(errors), 1) + self.assertIn('positive integer PR number', errors[0]['error']) + main = result['repositories'][0] + self.assertEqual(main['open_pr_count'], 2) + self.assertEqual([p['number'] for p in main['pull_requests']], [7]) + self.assertTrue((out / 'main' / 'pr-7' / 'detail.json').exists()) + + def test_same_blob_at_different_filenames_is_accepted(self): + with tempfile.TemporaryDirectory() as d: + rows = [{'filename': 'a.txt', 'sha': 'same'}, {'filename': 'b.txt', 'sha': 'same'}, + {'filename': 'A.txt', 'sha': 'same'}, {'filename': 'a.txt ', 'sha': 'same'}] + result, out, sha, calls = _identity_collect(pathlib.Path(d), {('main', 'pulls/7/files'): rows}) + self.assertTrue(result['complete'], result['errors']) + self.assertEqual(json.loads((out / 'main' / 'pr-7' / 'files.json').read_text()), rows) + + def test_repeated_id_across_pages_with_matching_total_is_refused(self): + def runs(page): + if page == 1: + return {'total_count': 101, 'workflow_runs': [{'id': i} for i in range(1, 101)]} + return {'total_count': 101, 'workflow_runs': [{'id': 100}]} + with tempfile.TemporaryDirectory() as d: + result, out, sha, calls = _identity_collect(pathlib.Path(d), {('main', 'actions/runs'): runs}) + errors = [e for e in result['errors'] if e['repository'] == 'main'] + self.assertEqual(len(errors), 1) + self.assertIn('duplicate', errors[0]['error']) + self.assertEqual(len(json.loads((out / 'main' / 'pr-7' / 'runs.json').read_text())), 101) + + def test_identity_values(self): + check = m.check_identity + check([{'id': 1}, {'id': 2}], m.ID_IDENTITY) + check([], m.ID_IDENTITY) + for rows in ([{'id': 1}, {'id': 1}], [{'id': 3, 'x': 1}, {'id': 3, 'x': 2}], + [{'id': True}], [{'id': '1'}], [{'id': 1.0}], [{'id': 0}], [{'id': -1}], + [{}], ['1'], [None]): + with self.subTest(rows=rows), self.assertRaises(ValueError): + check(rows, m.ID_IDENTITY) + check([{'filename': 'a'}, {'filename': 'A'}, {'filename': 'a '}], m.FILENAME_IDENTITY) + for rows in ([{'filename': 'a', 'sha': 'x'}, {'filename': 'a', 'sha': 'y'}], + [{'filename': ''}], [{'filename': 1}], [{'sha': 'x'}], ['a']): + with self.subTest(rows=rows), self.assertRaises(ValueError): + check(rows, m.FILENAME_IDENTITY) + # Malformed PR rows are left to the per-row path; only valid numbers must be unique. + check([{'number': 'x'}, {'number': 'x'}, {'number': 7}, None], m.PR_IDENTITY) + with self.assertRaises(ValueError): + check([{'number': 7}, {'number': 'x'}, {'number': 7}], m.PR_IDENTITY) + + def test_missing_identity_policy_fails_closed(self): + for policy in (None, 'id', object()): + with self.subTest(policy=policy), self.assertRaises(ValueError): + m.check_identity([{'id': 1}], policy) + + def test_every_paginated_read_names_its_identity_policy(self): + import ast + tree = ast.parse((ROOT / 'tools/release_snapshot.py').read_text()) + expected = {'/pulls?state=open': 'PR_IDENTITY', "f'/pulls/{n}/files'": 'FILENAME_IDENTITY', + "f'/pulls/{n}/reviews'": 'ID_IDENTITY', "f'/issues/{n}/comments'": 'ID_IDENTITY', + "f'/pulls/{n}/comments'": 'ID_IDENTITY', "f'/commits/{sha}/check-runs'": 'ID_IDENTITY', + "f'/actions/runs?head_sha={sha}'": 'ID_IDENTITY', '/rulesets': 'ID_IDENTITY'} + seen = {} + for node in ast.walk(tree): + if not (isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'read'): + continue + kw = {k.arg: k.value for k in node.keywords} + if not (isinstance(kw.get('paginated'), ast.Constant) and kw['paginated'].value is True): + self.assertNotIn('identity', kw) + continue + first = node.args[0] + label = first.value if isinstance(first, ast.Constant) else ast.get_source_segment( + (ROOT / 'tools/release_snapshot.py').read_text(), first) + self.assertIn('identity', kw, label) + self.assertIsInstance(kw['identity'], ast.Name, label) + seen[label] = kw['identity'].id + self.assertEqual(seen, expected) + + if __name__ == '__main__': unittest.main() diff --git a/tools/release_snapshot.py b/tools/release_snapshot.py index 8cba82a59..04dedec58 100644 --- a/tools/release_snapshot.py +++ b/tools/release_snapshot.py @@ -179,6 +179,41 @@ def pr_number(value): return value['number'] +# Endpoint identity policies for paginated collections. Keys are collection-local. +PR_IDENTITY = 'pull request number' +FILENAME_IDENTITY = 'changed-file filename' +ID_IDENTITY = 'positive integer id' + + +def check_identity(rows, policy) -> None: + """Refuse a repeated identity in one collection. Rows are never deduplicated or reordered.""" + if policy not in (PR_IDENTITY, FILENAME_IDENTITY, ID_IDENTITY): + raise ValueError('paginated collection has no explicit identity policy') + if not isinstance(rows, list): + raise ValueError('expected a list collection') + seen = {} + for position, row in enumerate(rows): + if policy == PR_IDENTITY: + try: + key = pr_number(row) + except ValueError: + continue # Malformed PR rows keep the per-row error path in collect(). + elif policy == FILENAME_IDENTITY: + if not isinstance(row, dict) or type(row.get('filename')) is not str or not row['filename']: + raise ValueError(f'expected nonempty string filename at row {position}') + key = row['filename'] + else: + if not isinstance(row, dict) or type(row.get('id')) is not int or row['id'] <= 0: + raise ValueError(f'expected positive integer id at row {position}') + key = row['id'] + if key in seen: + message = f'duplicate {policy} {key!r} at rows {seen[key]} and {position}' + if policy == PR_IDENTITY: + message += '; open pull requests not processed' + raise ValueError(message) + seen[key] = position + + def validate_pr(value, number): if pr_number(value) != number: raise ValueError('PR identity mismatch') @@ -204,11 +239,13 @@ def collect(out: Path, include_pr_sources: bool = True) -> dict: summary['repositories'].append(row) dest = out/name; dest.mkdir() path = f'repos/{OWNER}/{name}' - def read(suffix, filename, key=None, paginated=False, validate=None): + def read(suffix, filename, key=None, paginated=False, validate=None, identity=None): try: value = pages(client.get, path+suffix, key) if paginated else client.get(path+suffix) write_json(dest/filename, value) # Retain raw evidence even when its target schema is unusable. + if paginated: + check_identity(value, identity) if validate is not None: validate(value) return value @@ -222,7 +259,7 @@ def read(suffix, filename, key=None, paginated=False, validate=None): branch = meta['default_branch'] commit = read('/commits/'+urllib.parse.quote(branch,safe=''), 'default-commit.json', validate=validate_commit) - prs = read('/pulls?state=open', 'open-prs.json', paginated=True) + prs = read('/pulls?state=open', 'open-prs.json', paginated=True, identity=PR_IDENTITY) targets = {} if commit is not None: sha = checked_sha(commit['sha']); row['default_commit'] = sha; targets[sha] = ['default'] @@ -244,19 +281,24 @@ def read(suffix, filename, key=None, paginated=False, validate=None): item = {k:detail.get(k) for k in ('number','title','html_url','draft','mergeable','mergeable_state','state')} item.update(head=sha, base=detail['base']['sha'], author=detail['user']['login']) row['pull_requests'].append(item) - read(f'/pulls/{n}/files', prefix+'files.json', paginated=True) - read(f'/pulls/{n}/reviews', prefix+'reviews.json', paginated=True) - read(f'/issues/{n}/comments', prefix+'comments.json', paginated=True) - read(f'/pulls/{n}/comments', prefix+'inline-comments.json', paginated=True) - read(f'/commits/{sha}/check-runs', prefix+'checks.json', key='check_runs', paginated=True) + read(f'/pulls/{n}/files', prefix+'files.json', paginated=True, + identity=FILENAME_IDENTITY) + read(f'/pulls/{n}/reviews', prefix+'reviews.json', paginated=True, identity=ID_IDENTITY) + read(f'/issues/{n}/comments', prefix+'comments.json', paginated=True, + identity=ID_IDENTITY) + read(f'/pulls/{n}/comments', prefix+'inline-comments.json', paginated=True, + identity=ID_IDENTITY) + read(f'/commits/{sha}/check-runs', prefix+'checks.json', key='check_runs', paginated=True, + identity=ID_IDENTITY) read(f'/commits/{sha}/status', prefix+'combined-status.json') - read(f'/actions/runs?head_sha={sha}', prefix+'runs.json', key='workflow_runs', paginated=True) + read(f'/actions/runs?head_sha={sha}', prefix+'runs.json', key='workflow_runs', + paginated=True, identity=ID_IDENTITY) if include_pr_sources: if detail['head'].get('repo') and detail['head']['repo']['full_name'] == f'{OWNER}/{name}': targets.setdefault(sha,[]).append(f'PR{n}') else: item['source_snapshot'] = 'not captured: fork outside same-repository scope' - read('/rulesets', 'rulesets.json', paginated=True) + read('/rulesets', 'rulesets.json', paginated=True, identity=ID_IDENTITY) with tempfile.TemporaryDirectory(prefix='release-git-') as tmp: repo = Path(tmp) subprocess.run(['git','init','--bare','-q',str(repo)],check=True,timeout=30)