diff --git a/packages/syft-enclave/src/syft_enclaves/enclave_job_info.py b/packages/syft-enclave/src/syft_enclaves/enclave_job_info.py index f83b6c5deb9..6e167b4e1c6 100644 --- a/packages/syft-enclave/src/syft_enclaves/enclave_job_info.py +++ b/packages/syft-enclave/src/syft_enclaves/enclave_job_info.py @@ -25,6 +25,7 @@ LogRequest, RequestedPermissionsUpdate, load_log_request, + log_head_length, log_request_file_name, log_request_warning, longest_log_head, @@ -361,6 +362,18 @@ def _log_path(self, log_file: str) -> Path: # The data owners may have shared only the first chars of the log. return longest_log_head(self.job_review_path, log_file) or full + def log_is_truncated(self, log_file: str) -> bool: + """Whether the viewers show only the first chars of ``log_file``. + + True when the data owners shared a copy of the first ``n_chars`` chars + that is ``n_chars`` long. A shorter copy holds the whole log. + """ + path = self._log_path(log_file) + n_chars = log_head_length(path, log_file) + if n_chars is None: + return False + return len(path.read_text(errors="replace")) >= n_chars + def reject(self, reason: Optional[str] = None) -> None: """Reject this job in the DO's own approval file. diff --git a/packages/syft-enclave/src/syft_enclaves/log_requests.py b/packages/syft-enclave/src/syft_enclaves/log_requests.py index ac507a8fb25..327c7f46365 100644 --- a/packages/syft-enclave/src/syft_enclaves/log_requests.py +++ b/packages/syft-enclave/src/syft_enclaves/log_requests.py @@ -119,6 +119,19 @@ def log_head_name(log_file: str, n_chars: int) -> str: return f"{path.stem}_{n_chars}{path.suffix}" +def log_head_length(path: Path, log_file: str) -> Optional[int]: + """The ``n_chars`` of ``path`` when it is a copy of the first chars of ``log_file``. + + None for any other file, such as the log itself. + """ + log = Path(log_file) + prefix = f"{log.stem}_" + if path.suffix != log.suffix or not path.stem.startswith(prefix): + return None + n_chars = path.stem[len(prefix) :] + return int(n_chars) if n_chars.isdigit() else None + + def write_log_head( review_dir: Path, log_file: str, n_chars: Optional[int] ) -> Optional[str]: diff --git a/packages/syft-enclave/tests/test_read_permissions.py b/packages/syft-enclave/tests/test_read_permissions.py index e9cd8cb8f71..e3736cec308 100644 --- a/packages/syft-enclave/tests/test_read_permissions.py +++ b/packages/syft-enclave/tests/test_read_permissions.py @@ -14,6 +14,7 @@ os.environ["PRE_SYNC"] = "false" from syft_job.job import JobInfo # noqa: E402 +from syft_job.job_stdout import LOG_TRUNCATION_MESSAGE # noqa: E402 from syft_job.models import JobState, JobStatus # noqa: E402 from syft_job.read_permissions import ( # noqa: E402 LOGS_WARNING, @@ -828,6 +829,24 @@ def test_requested_stderr_head_reaches_submitter(): assert not has_file(ds, "stderr.txt") assert str(ds.jobs["j"].stderr) == full[:10] assert ds.jobs["j"].approved_read_permissions == {"stderr_10.txt": [ds.email]} + # The viewer says it shows only the first chars. + assert ds.jobs["j"].log_is_truncated("stderr.txt") + assert repr(ds.jobs["j"].stderr) == f"{full[:10]}\n{LOG_TRUNCATION_MESSAGE}" + assert LOG_TRUNCATION_MESSAGE in ds.jobs["j"].stderr._repr_html_() + + +def test_log_head_longer_than_log_not_truncated(): + enclave, do1, do2, ds = run_crash_sharing_nothing() + for do in (do1, do2): + request_stderr(do, readers=ds.email, n_chars=100_000) + run_and_share(enclave, ds) + + enclave_review = Path(enclave._rds.job_client.jobs["j"].job_review_path) + full = (enclave_review / "stderr.txt").read_text() + assert has_file(ds, "stderr_100000.txt") + assert str(ds.jobs["j"].stderr) == full + assert not ds.jobs["j"].log_is_truncated("stderr.txt") + assert LOG_TRUNCATION_MESSAGE not in repr(ds.jobs["j"].stderr) def test_one_owner_requesting_log_shares_nothing(): @@ -846,6 +865,7 @@ def test_log_request_defaults_to_data_owners(): assert readers_of(enclave, "stderr.txt", (ds, do1, do2)) == [do1, do2] assert has_file(do1, "stderr.txt") assert not has_file(ds, "stderr.txt") + assert not do1.jobs["j"].log_is_truncated("stderr.txt") def test_default_log_readers_ignore_owners_named_by_submitter(): diff --git a/packages/syft-job/src/syft_job/job.py b/packages/syft-job/src/syft_job/job.py index 04b4866c003..5b705171829 100644 --- a/packages/syft-job/src/syft_job/job.py +++ b/packages/syft-job/src/syft_job/job.py @@ -256,6 +256,13 @@ def _list_output_files() -> List[Path]: return output_files + def log_is_truncated(self, log_file: str) -> bool: + """Whether the viewers show only the first chars of ``log_file``. + + Always False here: only an enclave job can share part of a log. + """ + return False + @property def stdout(self) -> StdoutViewer: """Get a viewer for the stdout content.""" diff --git a/packages/syft-job/src/syft_job/job_repr.py b/packages/syft-job/src/syft_job/job_repr.py index 76f0bbd0d83..6811c02a03a 100644 --- a/packages/syft-job/src/syft_job/job_repr.py +++ b/packages/syft-job/src/syft_job/job_repr.py @@ -4,6 +4,8 @@ from html import escape from typing import TYPE_CHECKING, List +from .job_stdout import LOG_TRUNCATION_MESSAGE + if TYPE_CHECKING: from .job import JobInfo @@ -81,9 +83,12 @@ def __repr__(self) -> str: lines = content.split("\n") if len(lines) <= 3: - return content + text = content else: - return f"StderrViewer({len(lines)} lines, {len(content)} chars)" + text = f"StderrViewer({len(lines)} lines, {len(content)} chars)" + if self.job_info.log_is_truncated("stderr.txt"): + text += f"\n{LOG_TRUNCATION_MESSAGE}" + return text def _repr_html_(self) -> str: """HTML representation for Jupyter notebooks with scrollable view.""" @@ -128,6 +133,8 @@ def _repr_html_(self) -> str: lines = clean_content.split("\n") char_count = len(clean_content) line_count = len(lines) + if self.job_info.log_is_truncated("stderr.txt"): + html_content += f"\n{escape(LOG_TRUNCATION_MESSAGE)}" return f"""