session_managed_worker_log.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """Bounded, redacted per-profile sink for managed-worker stderr.
  2. The worker routes its own stderr AND its ordinary stdout (``os.dup2``) here, so tracebacks,
  3. provider error echoes and tool prints land in this stream. The owner drains it on a dedicated
  4. thread (an undrained pipe would block the child), redacts every line before it touches disk,
  5. and keeps ``<home>/logs/managed-worker.log`` private (0600) and capped at
  6. ``MAX_LOG_BYTES`` plus one rotated copy.
  7. """
  8. import logging
  9. import os
  10. from pathlib import Path
  11. import threading
  12. logger = logging.getLogger(__name__)
  13. LOG_NAME = 'managed-worker.log'
  14. MAX_LOG_BYTES = 1024 * 1024
  15. # A line longer than this is redacted whole, then cut; the rest of it is discarded.
  16. MAX_LINE_BYTES = 64 * 1024
  17. KEPT_LINE_CHARS = 4096
  18. _locks = {}
  19. _locks_guard = threading.Lock()
  20. def worker_log_path(profile_id):
  21. """The owning profile's sink, or None when the authority has no home path (test doubles)."""
  22. home = Path(str(profile_id))
  23. return home / 'logs' / LOG_NAME if home.is_absolute() else None
  24. def _lock_for(path):
  25. with _locks_guard:
  26. return _locks.setdefault(str(path), threading.Lock())
  27. def _append(path, text):
  28. """Append under the per-file lock; rotate once the cap would be exceeded. 0600 always."""
  29. from hermes_constants import mkdir_under_hermes_home
  30. data = text.encode('utf-8', 'replace')
  31. with _lock_for(path):
  32. mkdir_under_hermes_home(path.parent)
  33. try:
  34. size = path.stat().st_size
  35. except FileNotFoundError:
  36. size = 0
  37. if size and size + len(data) > MAX_LOG_BYTES:
  38. os.replace(path, path.with_name(LOG_NAME + '.1'))
  39. fd = os.open(path, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o600)
  40. try:
  41. if hasattr(os, 'fchmod'):
  42. os.fchmod(fd, 0o600)
  43. os.write(fd, data[:MAX_LOG_BYTES])
  44. finally:
  45. os.close(fd)
  46. def _redact(line, scrub):
  47. from agent.redact import redact_sensitive_text
  48. for value in sorted((v for v in scrub() if isinstance(v, str) and v), key=len, reverse=True):
  49. line = line.replace(value, '***')
  50. return redact_sensitive_text(line, force=True)
  51. def drain_worker_stderr(stream, path, pid, scrub):
  52. """Thread body: read ``stream`` to EOF; ``scrub()`` returns this worker's exact secrets
  53. (launch key, assignment secret) known so far. Never stops reading on a write failure."""
  54. skipping = False
  55. warned = False
  56. try:
  57. while True:
  58. chunk = stream.readline(MAX_LINE_BYTES)
  59. if not chunk:
  60. return
  61. whole = chunk.endswith(b'\n')
  62. if skipping:
  63. skipping = not whole
  64. continue
  65. skipping = not whole
  66. try:
  67. line = _redact(chunk.decode('utf-8', 'replace'), scrub)
  68. _append(path, f'[pid {pid}] ' + (line if whole else line[:KEPT_LINE_CHARS] + ' [truncated]\n'))
  69. except (OSError, ValueError) as exc:
  70. # Keep draining regardless: a child blocked on a full stderr pipe would wedge.
  71. if not warned:
  72. warned = True
  73. logger.warning('managed worker log %s not written (%s); dropping its lines', path,
  74. type(exc).__name__)
  75. finally:
  76. stream.close()