Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/syft-enclave/src/syft_enclaves/enclave_job_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
LogRequest,
RequestedPermissionsUpdate,
load_log_request,
log_head_length,
log_request_file_name,
log_request_warning,
longest_log_head,
Expand Down Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions packages/syft-enclave/src/syft_enclaves/log_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
20 changes: 20 additions & 0 deletions packages/syft-enclave/tests/test_read_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand Down
7 changes: 7 additions & 0 deletions packages/syft-job/src/syft_job/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
11 changes: 9 additions & 2 deletions packages/syft-job/src/syft_job/job_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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"""
<style>
Expand Down
14 changes: 12 additions & 2 deletions packages/syft-job/src/syft_job/job_stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import html
import re
from html import escape
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .job import JobInfo

LOG_TRUNCATION_MESSAGE = (
"[truncated: the data owners shared only the start of this log]"
)


class StdoutViewer:
"""A viewer for stdout content with scrollable display in Jupyter notebooks."""
Expand Down Expand Up @@ -88,9 +93,12 @@ def __repr__(self) -> str:

lines = content.split("\n")
if len(lines) <= 3:
return content
text = content
else:
return f"StdoutViewer({len(lines)} lines, {len(content)} chars)"
text = f"StdoutViewer({len(lines)} lines, {len(content)} chars)"
if self.job_info.log_is_truncated("stdout.txt"):
text += f"\n{LOG_TRUNCATION_MESSAGE}"
return text

def _repr_html_(self) -> str:
"""HTML representation for Jupyter notebooks with scrollable view."""
Expand Down Expand Up @@ -140,6 +148,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("stdout.txt"):
html_content += f"\n{escape(LOG_TRUNCATION_MESSAGE)}"

return f"""
<style>
Expand Down
Loading