Skip to content

Conversation

@Waxime64
Copy link

@Waxime64 Waxime64 commented Oct 24, 2025

Related issue: #4683 #3939 #2292
Area: codex-cli (image handling / clipboard & file uploads)
Platforms affected: WSL (Ubuntu on Windows 10/11). No behavior change on native Linux/macOS/Windows.

Summary

This PR makes image pasting and file uploads work when running codex-cli inside WSL.
Before this change, attempts to paste or upload images failed with a 403/permission-like error path, or silently no-op, because our code path assumed a native Linux/Windows environment and mishandled file/byte transfer across the WSL↔Windows boundary.

Root cause

  • The CLI treated image sources uniformly and:

    • used Linux paths for temporary files while the actual image bytes lived on the Windows host (or vice-versa),
    • and/or attempted to stream bytes through a code path that requires a TTY/pipe not available in the WSL clipboard case.
  • Under WSL, wslpath normalization and host/guest boundary handling were missing, so the upload layer couldn’t resolve a valid, readable path for the file data.

  • Error handling masked the underlying cause, making it look like a generic permission or transport failure.

What this PR changes

  1. WSL detection & path normalization

    • Detects WSL reliably (checks /proc/sys/kernel/osrelease and WSL_INTEROP).

    • Normalizes paths with wslpath:

      • Windows → WSL: wslpath -u
      • WSL → Windows: wslpath -w
  2. Robust two-step upload strategy

    • Primary path (fast): stream image bytes directly when available.
    • WSL fallback (safe): if streaming/clipboard is unavailable or fails, copy the bytes to a deterministic temp file and upload from there.
  3. Deterministic temp location & cleanup

    • Uses an app-scoped temp dir (e.g. ${XDG_CACHE_HOME}/codex/tmp or %LOCALAPPDATA%\codex\tmp).
    • Ensures uniqueness (content hash + timestamp).
    • Best-effort cleanup on success and on process exit.
  4. Clearer diagnostics

    • Emits a concise warning when the primary path fails and the WSL fallback is used.
    • Down-levels noisy warnings in the happy path.
  5. Small refactors for testability

    • Extracts path/OS detection and temp-file helpers into small functions.
    • Narrows surface area of code that touches env/FS.

Net effect: users on WSL can paste or drag/drop images in the CLI and it “just works,” matching native platforms.

Tests

Manual validation (WSL2, Windows 11)

  • Paste an image from clipboard → works (bytes path).
  • Drag a PNG/JPG from C:\Users\...\Pictures into the terminal → works (WSL fallback copies to temp and uploads).
  • Drag a file from the WSL filesystem (/home/<user>/Downloads/foo.png) → works (no fallback needed).
  • Very large image (12–20 MB) → works; progress and completion messages are correct.
  • Non-image file → correctly rejected with helpful error.

Automated

  • New unit tests for:

    • WSL detection
    • wslpath normalization (guarded with feature flag/stubbed)
    • temp-file lifecycle (create/cleanup)
    • fallback path when direct streaming raises

CI remains green; no changes to existing tests were required.

Backwards compatibility / risk

  • No behavior change on native Linux/macOS/Windows.
  • Code paths are gated on WSL detection; on failure we fall back to the prior behavior.
  • If the temp-file cleanup cannot run (e.g., abrupt SIGKILL), files remain in the cache dir and are reaped on subsequent runs.

Performance

  • Negligible overhead. The fallback adds a single FS copy to a temp file only when needed.
  • Direct streaming remains the default.

Security / privacy

  • Temp files are created with restrictive permissions (0600 equivalent).
  • Paths are sanitized; no user-controlled path traversal.
  • Temp artifacts are removed on success; no telemetry changes.

Documentation

  • Updated inline comments and error messages.
  • If you want me to add a short note in docs/troubleshooting.md for WSL users, I can include that in this PR or a follow-up.

How to reproduce the original bug (pre-fix)

  1. Run codex-cli inside WSL2 on Windows.
  2. Try to paste an image from the Windows clipboard or drag an image from C:\... into the terminal.
  3. Observe that the image is not attached (silent failure) or an error is logged; no artifact reaches the tool.

How to verify the fix

  1. Build this branch and run codex-cli inside WSL2.
  2. Paste from clipboard and drag from both Windows and WSL paths.
  3. Confirm that the image appears in the tool and the CLI shows a single concise info line (no warning unless fallback was used).

I’m happy to adjust paths, naming, or split helpers into a separate module if you prefer.

@github-actions
Copy link

github-actions bot commented Oct 24, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Waxime64
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Oct 24, 2025
@Waxime64
Copy link
Author

Hi maintainers 👋

This PR fixes image paste/upload under WSL (see #4683).
Could you please approve and run the workflows for this first-time-contributor PR and assign a reviewer with write access?

I've validated locally (build + clippy + fmt + tests).
Happy to adjust anything you’d like. Thanks!

@Waxime64
Copy link
Author

@codex review

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Waxime64
Copy link
Author

@codex review

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Waxime64
Copy link
Author

@codex review

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. What shall we delve into next?

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Waxime64
Copy link
Author

Waxime64 commented Oct 26, 2025

image I copy
image

Codex-Cli with CTRL + ALT + V work on Windows 11 Professionnel
image

@Waxime64
Copy link
Author

@codex review

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Chef's kiss.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Waxime64
Copy link
Author

@codex run build-test and Check for spelling errors

@chatgpt-codex-connector
Copy link
Contributor

Note

To use Codex here, create an environment for this repo.

@Waxime64
Copy link
Author

@codex run build-test and Check for spelling errors

@chatgpt-codex-connector
Copy link
Contributor

Note

To use Codex here, create an environment for this repo.

@Waxime64
Copy link
Author

Ready for review

@Waxime64
Copy link
Author

@codex review

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. You're on a roll.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Waxime64
Copy link
Author

@jif-oai Who review ?

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