Skip to content

Fix: claude subprocess inherited stdin — leaked the learner's answer key#3

Open
JMCCavender wants to merge 1 commit into
mainfrom
fix/stdin-isolation
Open

Fix: claude subprocess inherited stdin — leaked the learner's answer key#3
JMCCavender wants to merge 1 commit into
mainfrom
fix/stdin-isolation

Conversation

@JMCCavender

Copy link
Copy Markdown
Owner

The tutor could read the learner's unsent answers

subprocess.run(capture_output=True) captures stdout/stderr but inherits stdin. When MarbleTutor's stdin is seekable (heredoc, redirected file — i.e. any scripted or automated session), the claude CLI rewinds fd 0 and reads the entire input from position 0 — including lines the REPL had already consumed.

Impact: every piped session leaked the learner's full script — all future answers plus the /check commands — into the tutor's context on every call. Gates could grade against answers not yet given; "cold opens" referenced things the learner hadn't said. This was the true root cause of the graded-before-teaching symptom that #2's prompt fix partially addressed (that fix remains correct and needed — but this was the deeper bug). Interactive tty use was never affected, which is why it hid.

Repro (before fix): a heredoc-fed session where claude echoed the full script back verbatim, including the already-consumed y confirmation line.

Fix: stdin=subprocess.DEVNULL on _default_invoke and claude_available, plus a regression test asserting the kwarg.

Verified with the first honest end-to-end run:

  • Cold open genuinely teaches (no phantom grading)
  • A thin answer fails the gate: "Not yet — let's keep exploring…"
  • A skill claimed but not demonstrated ("I could match pictures") is held at the gate until the learner actually does it
  • Mid-session EOF saves; resume shows ✓ markers and continues to 🎉 Complete

Tests: 69 passing.

🤖 Generated with Claude Code

subprocess.run(capture_output=True) captures stdout/stderr but INHERITS
stdin. With a seekable stdin (heredoc, redirected file), the claude CLI
rewinds fd 0 and reads the learner's entire pending input from position 0
— including lines the REPL already consumed. Every scripted/piped session
therefore leaked all future answers into the tutor's context: gates could
grade against answers not yet given, and openings referenced content the
learner "hadn't said yet" (the real root cause behind the graded-cold-open
symptom previously attributed to prompt framing alone).

Reproduced: a heredoc-fed session where claude echoed back the full
script verbatim, including already-consumed lines. Fixed with
stdin=subprocess.DEVNULL on both _default_invoke and claude_available.

Verified end-to-end with a clean scripted run: cold opens now teach,
a thin answer honestly fails the gate ("Not yet"), a claimed-but-not-
demonstrated skill is held at the gate until demonstrated, and resume →
completion works. Interactive tty use was never affected.

Tests: 69 passing (+1 asserting stdin=DEVNULL is passed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant