From bad322e903520639246495ebb9e96fa143d970de Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:31:57 -0700 Subject: [PATCH] fix: inspect unassociated account facts failure logs --- docs/account-archive-history.md | 8 +++- scripts/diagnose_snapshot_request_logs.py | 55 +++++++++++++++++------ tests/test_snapshot_log_diagnostic.py | 35 +++++++++++++++ 3 files changed, 82 insertions(+), 16 deletions(-) diff --git a/docs/account-archive-history.md b/docs/account-archive-history.md index bbfdf6cc..76a75542 100644 --- a/docs/account-archive-history.md +++ b/docs/account-archive-history.md @@ -7,8 +7,12 @@ default-off `snapshot_request_logs_only` mode. It requires the exact current main SHA, the approved main ref, `run_readonly_diagnostic=true`, and a past UTC window of at most 30 minutes. It cannot be combined with a schema inspection. It reads bounded Scheduler completion and Cloud Run request logs for the fixed -account-facts sync target, then reads application logs only for a uniquely -identified request trace. Truncated or inaccessible reads remain explicit; the +account-facts sync target, then reads application logs for a uniquely +identified request trace. If none match, it also reads bounded text logs with +the fixed account-facts marker from the same service and window. These are +unassociated candidates: even a single candidate does not establish that it +belongs to the request. Multiple candidates are ambiguous and truncated or +inaccessible reads remain explicit; the Scheduler-to-request association is based only on target and time, not a proven common invocation. Only fixed categories, HTTP status codes and counts are printed. No Scheduler job, broker request or sync is invoked. diff --git a/scripts/diagnose_snapshot_request_logs.py b/scripts/diagnose_snapshot_request_logs.py index a5f527ff..08ef3a00 100644 --- a/scripts/diagnose_snapshot_request_logs.py +++ b/scripts/diagnose_snapshot_request_logs.py @@ -53,6 +53,20 @@ def _resource(row, kind, project, service, location): == (service if kind == 'cloud_run_revision' else service + '-account-facts-sync-scheduler')) +def _classification(row): + msg = row.get('textPayload') + if not isinstance(msg, str): + return None + client = re.search(r'account_facts_sync client failed exc_type=\w+ reason=([a-z_]+) stage=([a-z_]+) http_status=(None|[1-5][0-9]{2})(?:\s|$)', msg) + other = re.search(r'account_facts_sync (?:collect|build|publish) failed reason=([a-z_]+)(?:\s|$)', msg) + if client and client.group(1) in REASONS and client.group(2) in STAGES: + return (client.group(1), client.group(2), + None if client.group(3) == 'None' else int(client.group(3))) + if other and other.group(1) in REASONS: + return (other.group(1), 'unclassified', None) + return None + + def summarize(rows, app_rows, project, service, region, run_region=None): run_region = run_region or region job = f'projects/{project}/locations/{region}/jobs/{service}-account-facts-sync-scheduler' @@ -94,18 +108,7 @@ def summarize(rows, app_rows, project, service, region, run_region=None): matched = [row for row in app_rows if _resource(row, 'cloud_run_revision', project, service, run_region) and row.get('trace') == trace and not row.get('httpRequest')] result['app_match_count'] = len(matched) - classifications = [] - for row in matched: - msg = row.get('textPayload') - if not isinstance(msg, str): - continue - client = re.search(r'account_facts_sync client failed exc_type=\w+ reason=([a-z_]+) stage=([a-z_]+) http_status=(None|[1-5][0-9]{2})(?:\s|$)', msg) - other = re.search(r'account_facts_sync (?:collect|build|publish) failed reason=([a-z_]+)(?:\s|$)', msg) - if client and client.group(1) in REASONS and client.group(2) in STAGES: - classifications.append((client.group(1), client.group(2), - None if client.group(3) == 'None' else int(client.group(3)))) - elif other and other.group(1) in REASONS: - classifications.append((other.group(1), 'unclassified', None)) + classifications = [value for row in matched if (value := _classification(row)) is not None] if len(matched) == 1 and len(classifications) == 1: result['app_reason'], result['app_stage'], result['app_http_status'] = classifications[0] return result @@ -164,7 +167,30 @@ def run(project, service, region, run_region, start, end, *, read=_read_logs): output['app_read_status'] = app_status if app_status != 'ok': return {**output, **partial} - return {**output, **summarize(rows, app_rows, project, service, region, run_region)} + result = {**output, **summarize(rows, app_rows, project, service, region, run_region)} + if result['app_match_count']: + return result + candidate_filter = (f'{window} AND resource.type="cloud_run_revision" ' + f'AND resource.labels.project_id="{project}" AND resource.labels.location="{run_region}" ' + f'AND resource.labels.service_name="{service}" AND textPayload:"account_facts_sync"') + candidate_status, candidates = read(candidate_filter, project) + result.update(candidate_read_status=candidate_status, candidate_count=None, + candidate_relation='unknown', candidate_reason='unknown', + candidate_stage='unknown', candidate_http_status=None) + if candidate_status != 'ok': + return result + candidates = [row for row in candidates if _resource(row, 'cloud_run_revision', project, service, run_region) + and not row.get('httpRequest') and isinstance(row.get('textPayload'), str) + and 'account_facts_sync' in row['textPayload']] + result['candidate_count'] = len(candidates) + if len(candidates) == 1: + result['candidate_relation'] = 'same_service_and_window_only' + classification = _classification(candidates[0]) + if classification is not None: + result['candidate_reason'], result['candidate_stage'], result['candidate_http_status'] = classification + elif len(candidates) > 1: + result['candidate_relation'] = 'ambiguous' + return result def main(): @@ -177,7 +203,8 @@ def main(): except Exception: output = {'status': 'unavailable'} print(json.dumps(output, sort_keys=True)) - return 0 if output['status'] == 'ok' and output.get('app_read_status') not in {'permission_denied', 'unavailable', 'truncated'} else 1 + failures = {'permission_denied', 'unavailable', 'truncated'} + return 0 if output['status'] == 'ok' and output.get('app_read_status') not in failures and output.get('candidate_read_status') not in failures else 1 if __name__ == '__main__': diff --git a/tests/test_snapshot_log_diagnostic.py b/tests/test_snapshot_log_diagnostic.py index e55de921..cb7b93e1 100644 --- a/tests/test_snapshot_log_diagnostic.py +++ b/tests/test_snapshot_log_diagnostic.py @@ -108,6 +108,41 @@ def forbidden(*_args): run(PROJECT, SERVICE, 'us-central1', 'us-central1', '2026-10-30T01:00:00Z', '2026-10-30T02:00:01Z', read=forbidden) + def test_untraced_app_log_is_only_an_unassociated_candidate(self): + calls = [] + untraced = app(); del untraced['trace'] + def read(query, project): + calls.append(query) + return ('ok', [[scheduler(), request()], [], [untraced]][len(calls) - 1]) + result = run(PROJECT, SERVICE, 'us-central1', 'us-central1', + '2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read) + self.assertEqual(result['app_reason'], 'unknown') + self.assertEqual(result['candidate_relation'], 'same_service_and_window_only') + self.assertEqual(result['candidate_reason'], 'cache_missing') + self.assertEqual(result['candidate_count'], 1) + self.assertIn('textPayload', calls[2]) + + def test_multiple_or_truncated_candidates_never_classified(self): + for status, rows in [('ok', [app(), app()]), ('truncated', [app()])]: + calls = [] + def read(query, project): + calls.append(query) + return [('ok', [scheduler(), request()]), ('ok', []), (status, rows)][len(calls) - 1] + result = run(PROJECT, SERVICE, 'us-central1', 'us-central1', + '2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read) + self.assertEqual(result['candidate_reason'], 'unknown') + + def test_unrecognized_candidate_never_exposes_payload(self): + calls = [] + unsafe = app('secret-account-123'); del unsafe['trace'] + def read(query, project): + calls.append(query) + return ('ok', [[scheduler(), request()], [], [unsafe]][len(calls) - 1]) + result = run(PROJECT, SERVICE, 'us-central1', 'us-central1', + '2026-10-30T01:57:00Z', '2026-10-30T02:10:00Z', read=read) + self.assertEqual(result['candidate_reason'], 'unknown') + self.assertNotIn('secret-account-123', str(result)) + if __name__ == '__main__': unittest.main()