process_registry_stdin.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Why a background process has no writable stdin, phrased as the fix the agent can act on."""
  2. import importlib.util
  3. import sys
  4. _NON_INTERACTIVE = "Pass input non-interactively instead (arguments, a here-doc, or `printf ... | cmd`)."
  5. def _pty_available() -> bool:
  6. return importlib.util.find_spec("winpty" if sys.platform == "win32" else "ptyprocess") is not None
  7. def _no_pty_reason(session) -> str:
  8. """Why a pty=true relaunch of this local pipe-mode session would not help, or "" when it would.
  9. ``spawn_local`` records a pty=true request that fell back to pipe mode, so the advice never
  10. repeats a request that already failed: an installed PTY library is not proof a PTY spawns."""
  11. if session.pty_fallback:
  12. return f"pty=true was requested but {session.pty_fallback}, so it runs without stdin"
  13. if session.output_log:
  14. return "background processes here run in an isolated worker that cannot give them a pseudo-terminal"
  15. if not _pty_available():
  16. return "the PTY library (ptyprocess/pywinpty) is not installed"
  17. return ""
  18. def already_exited_error(session) -> str:
  19. """An exited pipe-mode process had no stdin; if it read stdin it got EOF at once. Say so, or the agent
  20. retries blind."""
  21. if session.process is None or session.process.stdin is not None:
  22. return "Process has already finished"
  23. if reason := _no_pty_reason(session):
  24. return (f"Process has already finished. It ran without stdin ({reason}), so if it read stdin, it "
  25. f"got end-of-file at once. {_NON_INTERACTIVE}")
  26. return ("Process has already finished. It was started without stdin (terminal background=true without "
  27. "pty=true), so if it read stdin, it got end-of-file at once. Relaunch it with "
  28. "terminal(background=true, pty=true) to send input.")
  29. def stdin_unavailable_error(session) -> str:
  30. """Error for process_manage write/submit on a session without a stdin pipe or PTY.
  31. A local pipe-mode spawn connects stdin to /dev/null by design, so only a PTY relaunch can take
  32. input, and only where ``spawn_local`` can give one; a non-local backend has no live stdin."""
  33. if session.process is None:
  34. return ("Process stdin not available: it runs on a non-local terminal backend (or was recovered "
  35. f"after a restart), which has no live stdin. {_NON_INTERACTIVE}")
  36. if reason := _no_pty_reason(session):
  37. return f"This process has no stdin: {reason}. Relaunching with pty=true would not help. {_NON_INTERACTIVE}"
  38. return ("This process was started without stdin (terminal background=true without pty=true). "
  39. "Relaunch it with terminal(background=true, pty=true) to send input.")