From 8e1fa0fb57cce61985d51a69998699a1250e7119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mu=C3=B1oz?= <233536261+pablo-ur-fixer@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:10:38 +0000 Subject: [PATCH] Report project exception messages in the enrollment worker failure line The enrollment worker logged only the exception class names and an authorization reason, so a Catacomb persistence failure appeared as "classes=...>CatacombBridgeError reason=unclassified" and could not be told apart from the other bridge checks. Add a JSON "detail=" list with the messages of exceptions raised by t2_* modules along the cause chain. Those messages are fixed strings; foreign exceptions are dropped and the text is bounded to printable ASCII, so the line still carries no identifiers or raw bridge replies. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_013Z5eh7nvcZfcnceC8vskKu --- CHANGELOG.md | 10 ++ src/t2-fprint-enrollment-worker.py | 35 +++++- tests/test_fprint_enrollment_worker_report.py | 113 ++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 tests/test_fprint_enrollment_worker_report.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e8199cc..06d055a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## Unreleased +### Changed + +- Report the messages of the project's own exception chain in the enrollment + worker's `enrollment stopped` line as a JSON `detail=` list, alongside the + existing class and reason fields. Only messages raised by `t2_*` modules are + included, so the line still carries no identifiers or raw bridge replies. + Without it, a Catacomb persistence failure logged only + `classes=...>CatacombBridgeError reason=unclassified`, which cannot be told + apart from the other bridge checks when reporting an issue. + ## 0.1.2 - 2026-10-28 On our test Mac, after T2Touch had run in Linux, macOS removed the account's diff --git a/src/t2-fprint-enrollment-worker.py b/src/t2-fprint-enrollment-worker.py index 418bd21..cfa7fbe 100644 --- a/src/t2-fprint-enrollment-worker.py +++ b/src/t2-fprint-enrollment-worker.py @@ -5,6 +5,7 @@ from __future__ import annotations import argparse +import json import os import sys from pathlib import Path @@ -25,6 +26,37 @@ def _exception_classes(error: BaseException) -> str: return ">".join(names) +# Messages raised by the project's own exception classes are fixed strings +# with no identifiers, so they are safe to log. Anything else (OSError text, +# socket errors, third-party exceptions) is dropped. +_DETAIL_MODULE_PREFIX = "t2_" +_DETAIL_MAX_MESSAGE = 120 +_DETAIL_MAX_MESSAGES = 6 + + +def _exception_detail(error: BaseException) -> str: + messages: list[str] = [] + current: BaseException | None = error + seen: set[int] = set() + while ( + current is not None + and id(current) not in seen + and len(messages) < _DETAIL_MAX_MESSAGES + ): + seen.add(id(current)) + module = type(current).__module__ + if isinstance(module, str) and module.startswith(_DETAIL_MODULE_PREFIX): + text = "".join( + character + for character in str(current)[:_DETAIL_MAX_MESSAGE] + if character.isascii() and character.isprintable() + ) + messages.append(text or "") + cause = current.__cause__ + current = cause if isinstance(cause, BaseException) else None + return json.dumps(messages, separators=(",", ":")) + + def _authorization_reason(error: BaseException) -> str: allowed = { "authorization login session differs from the claim": "session-changed", @@ -63,7 +95,8 @@ def main() -> int: print( "t2-fprint-enrollment-worker: enrollment stopped " f"stage={stage} classes={_exception_classes(error)} " - f"reason={_authorization_reason(error)}", + f"reason={_authorization_reason(error)} " + f"detail={_exception_detail(error)}", file=sys.stderr, ) return 1 diff --git a/tests/test_fprint_enrollment_worker_report.py b/tests/test_fprint_enrollment_worker_report.py new file mode 100644 index 0000000..17b45df --- /dev/null +++ b/tests/test_fprint_enrollment_worker_report.py @@ -0,0 +1,113 @@ +# SPDX-License-Identifier: GPL-2.0-only +import importlib.util +import json +import sys +import unittest +from pathlib import Path + + +SOURCE = Path(__file__).parents[1] / "src" +sys.path.insert(0, str(SOURCE)) +SPEC = importlib.util.spec_from_file_location( + "t2_fprint_enrollment_worker", SOURCE / "t2-fprint-enrollment-worker.py" +) +MODULE = importlib.util.module_from_spec(SPEC) +assert SPEC.loader +SPEC.loader.exec_module(MODULE) + +import t2_catacomb_bridge as catacomb_bridge +import t2_catacomb_protocol as catacomb_protocol +import t2_enrollment_persistence_operation as persistence_operation +import t2_fprint_worker as fprint_worker + + +def _chain(*errors: BaseException) -> BaseException: + """Link errors so that errors[0].__cause__ is errors[1], and so on.""" + for outer, inner in zip(errors, errors[1:]): + outer.__cause__ = inner + return errors[0] + + +class EnrollmentWorkerReportTests(unittest.TestCase): + def test_detail_reports_project_messages_along_the_cause_chain(self): + error = _chain( + fprint_worker.FprintWorkerError( + "enrollment stopped", stage="recover-terminal-identity" + ), + persistence_operation.PersistenceOperationError( + "Catacomb persistence stopped at prepare; reconciliation is required" + ), + catacomb_bridge.CatacombBridgeError( + "Catacomb command emitted an unexpected service event" + ), + ) + self.assertEqual( + MODULE._exception_classes(error), + "FprintWorkerError>PersistenceOperationError>CatacombBridgeError", + ) + self.assertEqual( + json.loads(MODULE._exception_detail(error)), + [ + "enrollment stopped", + "Catacomb persistence stopped at prepare; reconciliation is required", + "Catacomb command emitted an unexpected service event", + ], + ) + + def test_detail_includes_protocol_errors_and_drops_foreign_messages(self): + error = _chain( + fprint_worker.FprintWorkerError("enrollment stopped"), + catacomb_bridge.CatacombBridgeError("Catacomb prepare reply is invalid"), + catacomb_protocol.CatacombProtocolError( + "Catacomb prepare returned nonzero status" + ), + OSError(111, "Connection refused: /run/t2-touchid/private/socket"), + ValueError("expected message frame, received type 7"), + ) + self.assertEqual( + json.loads(MODULE._exception_detail(error)), + [ + "enrollment stopped", + "Catacomb prepare reply is invalid", + "Catacomb prepare returned nonzero status", + ], + ) + + def test_detail_sanitizes_and_bounds_messages(self): + long_message = "x" * 500 + error = _chain( + fprint_worker.FprintWorkerError("tab\there\nnewline é end"), + catacomb_bridge.CatacombBridgeError(long_message), + catacomb_bridge.CatacombBridgeError(""), + ) + detail = json.loads(MODULE._exception_detail(error)) + self.assertEqual(detail[0], "tabherenewline end") + self.assertEqual(detail[1], "x" * MODULE._DETAIL_MAX_MESSAGE) + self.assertEqual(detail[2], "") + + def test_detail_stops_at_the_message_limit_and_on_cycles(self): + errors = [ + catacomb_bridge.CatacombBridgeError(f"message {index}") + for index in range(MODULE._DETAIL_MAX_MESSAGES + 3) + ] + _chain(*errors) + self.assertEqual( + len(json.loads(MODULE._exception_detail(errors[0]))), + MODULE._DETAIL_MAX_MESSAGES, + ) + first = catacomb_bridge.CatacombBridgeError("first") + second = catacomb_bridge.CatacombBridgeError("second") + first.__cause__ = second + second.__cause__ = first + self.assertEqual( + json.loads(MODULE._exception_detail(first)), ["first", "second"] + ) + + def test_detail_without_project_errors_is_an_empty_list(self): + self.assertEqual( + MODULE._exception_detail(RuntimeError("/etc/t2-touchid.conf")), "[]" + ) + + +if __name__ == "__main__": + unittest.main()