diff --git a/.claude/settings.json b/.claude/settings.json index 6777f0fe91..2685aed9fc 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -11,7 +11,9 @@ "DO_NOT_TRACK": "1", "OPENSPEC_TELEMETRY": "0", "OPENSPEC_NO_UPDATE_CHECK": "1", - "UV_MALWARE_CHECK": "1" + "UV_MALWARE_CHECK": "1", + "UV_COMPILE_BYTECODE": "0", + "VIRTUAL_ENV": "" }, "permissions": { "allow": [ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fa1de84ef7..9bea522a84 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -106,7 +106,9 @@ In bash, `${=F}`, `${(f)...}` and `print -r --` fail. The forms `printf '%s'`, ` printf '%s\n' '-----' # right ``` -3. **JSON from `uv run devops gh api` breaks in two ways.** The command writes a `Bytecode compiled` line and an `Elapsed:` line to stderr. In a git worktree, `uv run` also writes a `warning: VIRTUAL_ENV` line. The Bash tool shows these lines around the reply, and a `2>&1` pipe mixes them into the JSON. +3. **JSON from `uv run devops gh api` breaks in two ways.** The command writes an `Elapsed:` line to stderr. In a shell that has the environment of the devcontainer image, `uv run` also writes a `Bytecode compiled` line, because the image sets `UV_COMPILE_BYTECODE=1`. The Bash tool shows these lines around the reply, and a `2>&1` pipe mixes them into the JSON. + + The `env` of `.claude/settings.json` sets `UV_COMPILE_BYTECODE` to `0` and `VIRTUAL_ENV` to an empty value, which uv reads as unset. The VS Code extension of Claude Code starts the session with `VIRTUAL_ENV` set to the main `.venv`, so without this value `uv run` in a git worktree writes a `warning: VIRTUAL_ENV` line. The warning advises `--active`. Do not use it: it points the editable install of the main `.venv` at the worktree. In zsh, `echo "$VAR"` expands backslash escapes. A `\n` in a JSON string then becomes a raw newline, and the parser fails. Pipe stdout directly. Print a variable with `print -r --` or `printf '%s'`. Or read one field with `--jq`: diff --git a/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/.openspec.yaml b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/.openspec.yaml new file mode 100644 index 0000000000..9d55295e5d --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/.openspec.yaml @@ -0,0 +1,3 @@ +schema: spec-driven +created: 2026-11-11 +skip_specs: false diff --git a/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/design.md b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/design.md new file mode 100644 index 0000000000..1e5a8ae478 --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/design.md @@ -0,0 +1,14 @@ +# Design + +## Decisions + +- Set the two values in the `env` of `.claude/settings.json`. The file is in every worktree, and Claude Code reads it for each session, also for a `claude` that starts from a VS Code terminal with an active environment. +- Set `VIRTUAL_ENV` to an empty value. Claude Code cannot remove a variable that its parent sets, and uv reads an empty value as unset. +- Set `UV_COMPILE_BYTECODE` to `0` only for Claude Code. The image keeps the value `1` for the other shells and for the runner pods. + +## Rejected alternatives + +- `python.terminal.activateEnvironment: false` in `.devcontainer/devcontainer.json`: it acts only on VS Code terminals, and the agent shells do not start from them. +- `claudeCode.usePythonEnvironment: false` in `.devcontainer/devcontainer.json`: it acts only after a rebuild of the container, and only for the VS Code extension. A `claude` that starts from an activated terminal keeps `VIRTUAL_ENV`. +- `uv run -q`: it also hides the lines of an editable reinstall, which tell an agent that the venv changed. +- `UV_COMPILE_BYTECODE=0` in the root `Dockerfile`: the runner pods have a read-only root file system, so Python cannot write the `.pyc` files at run time. diff --git a/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/proposal.md b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/proposal.md new file mode 100644 index 0000000000..00929e0eec --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/proposal.md @@ -0,0 +1,44 @@ +# Proposal: #4588 + +## Why + +Issue #4588: each `uv run` in a Claude Code shell writes noise lines. The context audit of 2026-11 counts about 20.6M tokens a week for them. + +- In a git worktree, uv writes `warning: VIRTUAL_ENV=/workspaces/devops-cli/.venv does not match the project environment path .venv and will be ignored; use --active to target the active environment instead`. The advice is harmful: `--active` points the editable install of the main `.venv` at the worktree. +- Each `uv run` writes `Bytecode compiled 19212 files in ms`, because the devcontainer image sets `UV_COMPILE_BYTECODE=1` (`.devcontainer/Dockerfile`). + +## What Changes + +- The `env` of `.claude/settings.json` and of its template `src/devops_cli/templates/claude_settings.json.j2` sets `VIRTUAL_ENV` to `""` and `UV_COMPILE_BYTECODE` to `"0"`. The other keys of the file stay. +- A new test in `tests/commands/test_devcontainer.py` holds the two values in the settings that `devops devcontainer init` writes. The test that compares the scaffold with the committed file holds the committed file. +- `CONTRIBUTING.md` (Shell notes for agents, item 3) describes the two values and tells agents not to use `--active`. +- Not changed: `UV_COMPILE_BYTECODE=1` in the root `Dockerfile`, because the runner pods have a read-only root file system and need the compiled bytecode in the image; the `.devcontainer/Dockerfile`, `.devcontainer/devcontainer.json`, and `uv run -q`, which also hides the lines of an editable reinstall. + +## Capabilities + +### New Capabilities + +None. + +### Modified Capabilities + +- `devcontainer-agent-config`: a new requirement, "Claude Code sessions run uv with no environment warning and no bytecode line". + +## Impact + +- `.claude/settings.json`, `src/devops_cli/templates/claude_settings.json.j2`, `tests/commands/test_devcontainer.py`, `CONTRIBUTING.md`. +- A Claude Code session reads the new values when it starts. A running session keeps its old environment. +- Python writes the `.pyc` file of a module the first time it imports the module, not at install time. The `devops ci` gate is not changed: it runs `uv sync --compile-bytecode`, and the flag has priority over the variable (`tests/commands/test_ci.py`). + +## Feasibility + +Read on 2026-11-11. + +- The audit names `python.terminal.activateEnvironment: true` in `.devcontainer/devcontainer.json` as the cause. It is not the cause for agent shells. `/proc//environ` of the `claude` process holds `VIRTUAL_ENV=/workspaces/devops-cli/.venv`, and the extension host that starts it does not. The Kilo process, which the same extension host starts, has no `VIRTUAL_ENV`. The VS Code setting acts only on VS Code terminals. +- The source is the setting `claudeCode.usePythonEnvironment` of the Claude Code VS Code extension (default `true`, `package.json` of extension 2.1.322). Its `extension.js` asks the Python extension for the active environment and starts `claude` with `VIRTUAL_ENV` set to its `sys.prefix` and its `bin` first on `PATH`. +- uv reads an empty `VIRTUAL_ENV` as unset. In a scratch project with uv 0.13.6, `VIRTUAL_ENV= uv run python -c 'import sys;print(sys.prefix)'` writes no warning and prints the project `.venv`. With `UV_COMPILE_BYTECODE=0`, uv writes no `Bytecode compiled` line. +- Claude Code puts the `env` of the project settings into the environment of its Bash tool. A headless `claude -p` session in the worktree, started from a shell with `VIRTUAL_ENV=/workspaces/devops-cli/.venv` and `UV_COMPILE_BYTECODE=1`, ran `printenv VIRTUAL_ENV UV_COMPILE_BYTECODE; uv run python -c 'import sys;print(sys.prefix)'` and got an empty line, `0`, and the worktree `.venv` path, with no warning and no bytecode line. + +## Existing Tool Considered + +The `env` key of the Claude Code settings and the uv variables `VIRTUAL_ENV` and `UV_COMPILE_BYTECODE` do the work. No code is built. diff --git a/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/specs/devcontainer-agent-config/spec.md b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/specs/devcontainer-agent-config/spec.md new file mode 100644 index 0000000000..e771f4195c --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/specs/devcontainer-agent-config/spec.md @@ -0,0 +1,12 @@ +## ADDED Requirements + +### Requirement: Claude Code sessions run uv with no environment warning and no bytecode line +The `env` of `.claude/settings.json` and of its template SHALL set `VIRTUAL_ENV` to an empty value and `UV_COMPILE_BYTECODE` to `0`. Then `uv run` in a git worktree uses the `.venv` of the worktree and writes no `VIRTUAL_ENV` warning and no `Bytecode compiled` line. + +#### Scenario: The scaffold writes the Claude settings +- **WHEN** `devops devcontainer init` writes `.claude/settings.json` +- **THEN** its `env` holds `VIRTUAL_ENV` with the empty value and `UV_COMPILE_BYTECODE` with the value `0` + +#### Scenario: A Claude Code session runs uv in a worktree +- **WHEN** the session starts with `VIRTUAL_ENV` set to the main `.venv` and runs `uv run python` in a git worktree +- **THEN** uv writes no `VIRTUAL_ENV` warning and no `Bytecode compiled` line, and `sys.prefix` names the `.venv` of the worktree diff --git a/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/tasks.md b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/tasks.md new file mode 100644 index 0000000000..4cf6eb7a37 --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4588-stop-uv-noise-lines-in-agent/tasks.md @@ -0,0 +1,14 @@ +# Tasks + +## 1. The Claude settings + +- [x] 1.1 Add the test of the two values in the scaffolded Claude settings, and see it fail + - Verification: `$ uv run devops ci test tests/commands/test_devcontainer.py` +- [x] 1.2 Set `VIRTUAL_ENV` to `""` and `UV_COMPILE_BYTECODE` to `"0"` in `.claude/settings.json` and in `claude_settings.json.j2` + - Verification: `$ uv run devops ci test tests/commands/test_devcontainer.py tests/core/test_third_party_telemetry.py` + - Verification: `$ claude -p` in the worktree runs `uv run python -c 'import sys;print(sys.prefix)'` and gets only the worktree `.venv` path + +## 2. Documents + +- [x] 2.1 Describe the two values in `CONTRIBUTING.md` (Shell notes for agents, item 3) + - Verification: `$ uv run devops ci docs` diff --git a/openspec/specs/devcontainer-agent-config/spec.md b/openspec/specs/devcontainer-agent-config/spec.md index b69393832a..d267b634f4 100644 --- a/openspec/specs/devcontainer-agent-config/spec.md +++ b/openspec/specs/devcontainer-agent-config/spec.md @@ -39,3 +39,14 @@ Every skill directory in `.agents/skills/`, `.claude/skills/`, and `.kilocode/sk #### Scenario: The skill roots are compared - **WHEN** the invariant test inspects `.agents/skills`, `.claude/skills`, and `.kilocode/skills` - **THEN** all three directories contain the same six skill folders and identical `SKILL.md` files + +### Requirement: Claude Code sessions run uv with no environment warning and no bytecode line +The `env` of `.claude/settings.json` and of its template SHALL set `VIRTUAL_ENV` to an empty value and `UV_COMPILE_BYTECODE` to `0`. Then `uv run` in a git worktree uses the `.venv` of the worktree and writes no `VIRTUAL_ENV` warning and no `Bytecode compiled` line. + +#### Scenario: The scaffold writes the Claude settings +- **WHEN** `devops devcontainer init` writes `.claude/settings.json` +- **THEN** its `env` holds `VIRTUAL_ENV` with the empty value and `UV_COMPILE_BYTECODE` with the value `0` + +#### Scenario: A Claude Code session runs uv in a worktree +- **WHEN** the session starts with `VIRTUAL_ENV` set to the main `.venv` and runs `uv run python` in a git worktree +- **THEN** uv writes no `VIRTUAL_ENV` warning and no `Bytecode compiled` line, and `sys.prefix` names the `.venv` of the worktree diff --git a/src/devops_cli/templates/claude_settings.json.j2 b/src/devops_cli/templates/claude_settings.json.j2 index 6777f0fe91..2685aed9fc 100644 --- a/src/devops_cli/templates/claude_settings.json.j2 +++ b/src/devops_cli/templates/claude_settings.json.j2 @@ -11,7 +11,9 @@ "DO_NOT_TRACK": "1", "OPENSPEC_TELEMETRY": "0", "OPENSPEC_NO_UPDATE_CHECK": "1", - "UV_MALWARE_CHECK": "1" + "UV_MALWARE_CHECK": "1", + "UV_COMPILE_BYTECODE": "0", + "VIRTUAL_ENV": "" }, "permissions": { "allow": [ diff --git a/tests/commands/test_devcontainer.py b/tests/commands/test_devcontainer.py index 18b9a07ace..ebee472a58 100644 --- a/tests/commands/test_devcontainer.py +++ b/tests/commands/test_devcontainer.py @@ -1422,6 +1422,22 @@ def test_init_scaffolds_claude_settings_matching_the_repository_settings( assert (result.exit_code, scaffolded) == (0, committed) +def test_init_scaffolds_claude_settings_that_stop_the_uv_noise_lines( + runner: CliRunner, tmp_path: Path +) -> None: + """The VS Code extension starts Claude Code with `VIRTUAL_ENV` set to the main `.venv`, so + `uv run` in a worktree warns and advises `--active`. The image sets `UV_COMPILE_BYTECODE=1`, + so each `uv run` prints a `Bytecode compiled` line. uv reads an empty `VIRTUAL_ENV` as unset.""" + result = runner.invoke(app, ["init", str(tmp_path), "--name", "test-project"]) + + env = json.loads((tmp_path / ".claude" / "settings.json").read_text(encoding="utf-8"))["env"] + assert (result.exit_code, env.get("VIRTUAL_ENV"), env.get("UV_COMPILE_BYTECODE")) == ( + 0, + "", + "0", + ) + + class SubprocessCallsRecorder: def __init__(self, fake: ProcessFake) -> None: self.fake = fake