Skip to content

fix(auth): answer the kernel's input_request on the stdin channel - #326

Open
Zhong-fan wants to merge 2 commits into
googlecolab:mainfrom
Zhong-fan:fix/auth-stdin-reply
Open

Zhong-fan wants to merge 2 commits into
googlecolab:mainfrom
Zhong-fan:fix/auth-stdin-reply

Conversation

@Zhong-fan

Copy link
Copy Markdown

Fixes #269.

Summary

colab auth had two visible symptoms, both from the same hook:

  1. It printed a raw message dump instead of the sign-in prompt:
    {'header': {...}, 'content': {'prompt': 'Go to the following link...'}}.
  2. After pasting the verification code it hung forever. The kernel's input()
    never returned, so /content/.adc/adc.json was never written.

The stdin hook now shows the real prompt and sends the input_reply the kernel
is waiting for. On a live CPU runtime, colab auth prints the gcloud URL,
accepts the code, and leaves working credentials on the VM.

Why

jupyter_kernel_client calls stdin_hook(request) with the whole
input_request message and ignores the hook's return value. The hook must send
an input_reply itself. Our wrapper only returned the typed value, so the
kernel waited forever; it also passed the whole message to input(), which is
why the prompt looked like a dict.

Changes

  • src/colab_cli/runtime.py: read content.prompt / content.password, prompt
    locally (getpass for passwords), and send the value with wsclient.input().
    Mirror the library's default hook for the edges: EOF sends "\x04", Ctrl-C
    sends nothing, an already-queued message skips the reply, and a missing
    content mapping is tolerated.
  • tests/test_runtime_stdin.py: prompt and reply, password + getpass +
    redaction, EOF, Ctrl-C, stale reply, custom hook returning a value or None,
    and a malformed request.
  • tests/test_runtime.py: the existing stdin logging test now follows the real
    hook contract.
  • docs/04_automation_and_utility.md: corrected the colab auth description and
    added a log entry.

Verification

  • The new tests fail on main before the fix (prompt was the dict, no reply
    sent, password used input()) and pass with it:
    uv run pytest tests/test_runtime.py tests/test_runtime_stdin.py -q →
    19 passed.
  • uv run ruff check . and uv run ruff format --check . pass.
  • Live on a Colab CPU runtime, created and stopped during testing:
    • ColabRuntime.execute_code("print('ECHO:' + input('Prompt: '))", allow_stdin=True) with hello-from-stdin on stdin printed
      ECHO:hello-from-stdin.
    • colab auth printed the gcloud URL as normal text, accepted the pasted
      code, wrote /content/.adc/adc.json (831 bytes), and gcloud auth list
      listed the account.
    • colab stop, then colab sessions → no active sessions.

jupyter_kernel_client calls stdin_hook(request) and discards the return value, so the hook must send the input_reply itself. The wrapper returned the typed value without sending anything, which left colab auth waiting forever on its first prompt, and it passed the whole message dict to input(), so the raw dict was printed instead of the prompt.

Send the reply through the websocket client, print content.prompt, use getpass for password prompts, and redact those values in session history.
Mirror jupyter_kernel_client's default stdin hook: send an EOF marker on EOF, stay silent on Ctrl-C, and skip the reply when a channel already has a queued message. Send the reply before writing history so a logging failure cannot strand the kernel, tolerate a request without a content mapping, and document the custom stdin_hook contract.
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.

colab auth never completes — the pasted verification code is never sent to the kernel (input_reply missing)

1 participant