feat: PTY attach/detach (bonus, counts 2x) - #3
Closed
mylastresort wants to merge 4 commits into
Closed
Conversation
- Replace goroutine+channel ptyLogger with syscall.Poll-based approach that properly cancels blocking PTY reads via os.Pipe signal - Fix client handleAttach: restore goroutines (were commented out), use c.Rd for socket reads to avoid bufio.Reader data loss - Remove debug prints from server handleAttach - 30/32 tests pass (C4, HR-2 are env-specific)
mylastresort
force-pushed
the
feat/pty-attach
branch
from
July 23, 2026 20:38
f28ca88 to
9636f24
Compare
Server now sends 'DETACH OK\r' after handleAttach returns, so the client knows the session is over (e.g. when the attached process exits from Ctrl+D). Client socket reader detects the marker and closes the done channel, returning to the prompt. Without this, pressing Ctrl+D in an attached shell would exit the shell but leave the client hung forever.
- Wait for stdin goroutine to exit before restoring terminal, preventing orphaned goroutine from stealing input from readline - Server sends DETACH OK after handleAttach returns so client knows the session is over (e.g. when attached process exits from Ctrl+D)
… rl.Close() stale instance
- Replace os.Stdin.Read with unix.Read to avoid Go runtime poller
conflicting with unix.Poll on the same fd, which caused stdin
reads to hang and Ctrl+A D detach to never be detected
- Change defer rl.Close() to defer func(){ rl.Close() }() so the
closure captures the rl variable by reference, ensuring the
correct (latest) readline instance is closed on exit
- Server handleAttach also uses unix.Poll with stop pipe for
clean PTY reader goroutine cancellation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bonus feature: allows users to "attach" a supervised process to their console, interact with it in real-time, then "detach" to return it to background supervision. This counts for 2x points per the evaluation spec.
Depends on PR #2 (critical bug fixes).
How it works
creack/ptyptyLoggergoroutine pipes PTY output to log files when detachedattach <name>commandATTACH OKresponse, enters raw bidirectional copy mode (PTY master <-> socket)golang.org/x/term), copies stdin <-> socket bidirectionallyArchitecture
Testing
4 new attach tests (Section J) added to
tests/full_test.sh:Test Results
29/32 pass (3 pre-existing env-specific failures, same as PR #2):
* C3b, C4, HR-2: pre-existing environment-specific issues
Files Changed
internal/job/job.goptyLogger,Attach()/Detach()methodsinternal/server/socket.gohandleAttach()for raw bidirectional socket<->PTY copyinternal/manager/manager.goATTACH/DETACHconstants,AttachJob()/DetachJob()internal/manager/execute.goAttachFdon Response, ATTACH/DETACH inExecute()internal/parser/interpreter/interpreter.goattachcommandcmd/client/main.gohandleAttach()with raw terminal, Ctrl+A,D detachgo.mod/go.sumgithub.com/creack/pty,golang.org/x/termtests/attach_config.tomltests/full_test.sh