Repository navigation
Commit e669c74
test(session): protocol smoke + real-PTY tests (PR-B-4)
Two new test files covering the Phase 1 transport modules, plus two
genuine production bugs in PTYTransport surfaced by writing them.
tests/test_transport_protocol.py (17 tests)
- Event dataclass fields + frozen semantics (5 event types).
- TransportEventT union guarded against accidental add/remove — 5
elements exactly (Stopped merged Exited+Cancelled, not 4 not 6).
- SessionConfig frozen; field set matches the public contract.
- SessionAlreadyActiveError inherits RuntimeError (so generic handlers
still catch) but has its own class (so route layer can map to HTTP
409 without swallowing other RuntimeErrors).
- AgentSessionTransport Protocol exposes the expected method surface.
tests/test_pty_transport.py (15 tests)
- _build_child_env unit tests: passes plain keys, strips *_PASSWORD
case-insensitively, strips *_SECRET / *_TOKEN suffixes (but not the
SAFE_TOKENIZED_VALUE false-positive case), strips the
STUDYLOOP_TEST_AGENT_CMD fuzz-injection hatch, deny-list membership
sanity check.
- Failure paths: missing binary → FileNotFoundError; double-start →
RuntimeError("called twice"); reuse-after-end → RuntimeError;
end() idempotent on already-ended transport.
- Happy path with real pty.fork() + /bin/cat:
* start() emits Started first.
* send_input(b"hello\n") round-trips via OutputBytes (tolerates
the PTY echo mode — cat sees its own echo AND our write).
* Child exit (tested with /bin/sh -c "sleep 0.2; exit 0" because
/usr/bin/true exits so fast that SIGCHLD can fire before
_register_pid gets a chance) emits Stopped(returncode=0,
reason="exit") AND events() closes promptly via the queue
sentinel.
* resize() round-trips TIOCSWINSZ without raising.
* cancel() emits Stopped(reason="cancel"), exactly once.
- Context manager: async-with calls end() and second end() is a no-op.
Production bugs fixed (caught during TDD of the cancel + exit tests)
pty.py — SIGCHLD handler registered per-loop, not process-globally
The prior code tracked install state with a module-level bool, so
once the handler was installed on ANY event loop it claimed
registration forever. When the loop was replaced (new FastAPI
lifespan, pytest-asyncio function-scope, any lifecycle that recreates
the loop), subsequent PTY children's exits were silently lost —
SIGCHLD had no handler on the new loop. Fix: remember the loop we
registered against and re-install whenever get_running_loop() returns
a different object. Single long-running loop (production FastAPI) =
one-time install as before; test isolation now works.
pty.py — cancel() no longer double-emits Stopped
cancel() used to push Stopped(reason="cancel") + None from its own
coroutine, AFTER waiting for the grace window. Meanwhile SIGCHLD was
already firing inside that grace window, calling on_exit(), which
pushed Stopped(reason="exit") + None first. events() consumed "exit"
and closed the queue before "cancel" could be delivered — callers got
reason="exit" even for user-initiated cancellations. Fix: set a
_cancel_requested flag in cancel() BEFORE signalling; on_exit checks
the flag and emits the correct reason. cancel() no longer pushes
events itself — SIGCHLD is the single source of truth for Stopped.
Full suite: 1974 passed (up from 1942 pre-PR-B-4), zero regressions.
Ruff clean, format clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent c845a76 commit e669c74
3 files changed
Lines changed: 553 additions & 13 deletions
File tree
- packages/studyloop
- src/studyloop/session/transports
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
111 | 120 | | |
| 121 | + | |
| 122 | + | |
112 | 123 | | |
113 | | - | |
| 124 | + | |
114 | 125 | | |
115 | 126 | | |
116 | 127 | | |
| |||
210 | 221 | | |
211 | 222 | | |
212 | 223 | | |
| 224 | + | |
213 | 225 | | |
214 | 226 | | |
215 | 227 | | |
| |||
266 | 278 | | |
267 | 279 | | |
268 | 280 | | |
269 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
270 | 287 | | |
271 | 288 | | |
272 | 289 | | |
| |||
320 | 337 | | |
321 | 338 | | |
322 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
323 | 345 | | |
324 | 346 | | |
325 | 347 | | |
326 | 348 | | |
327 | | - | |
328 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
329 | 352 | | |
330 | 353 | | |
331 | 354 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | 355 | | |
337 | 356 | | |
338 | 357 | | |
| |||
0 commit comments