Skip to content

Commit 04f4e5d

Browse files
7noheclaude
andcommitted
fix(claude-code): make subscription mode actually generate docs
claude-code subscription runs finished with an empty module tree and no markdown while the agent falsely reported success. Two causes, both stemming from the session not being in true bypassPermissions (an org managed policy can disable bypass, downgrading --dangerously-skip-permissions to acceptEdits): 1. Workspace boundary: acceptEdits only auto-approves reads/edits inside the working dir; caw chdir's into the output subdir, so source files in the parent repo are denied. Pin cwd to the repo root for claude-code (str_replace_editor writes via absolute paths, so --output is honored). Codex keeps the output-dir cwd for its native file_change. 2. MCP tools: acceptEdits does not auto-approve --mcp-config tools, so CodeWiki's own toolkit was denied. caw only emits --disallowedTools; wrap subprocess.Popen to inject --allowedTools mcp__<server> for each server in the --mcp-config. Belongs upstream in caw's ClaudeCodeSession. Comments cite the official Claude Code permission-mode / CLI docs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 62d6db2 commit 04f4e5d

1 file changed

Lines changed: 83 additions & 1 deletion

File tree

‎codewiki/src/be/caw_backend.py‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,63 @@ def _patched(self) -> list[str]:
120120
# --- end stopgap --------------------------------------------------------------
121121

122122

123+
# --- claude-code allowedTools stopgap -----------------------------------------
124+
# Custom MCP-server tools (added via `--mcp-config`) are not auto-approved under
125+
# `acceptEdits`, and `bypassPermissions` may be disabled by an org managed
126+
# policy (see the cwd note below), so caw's `--dangerously-skip-permissions`
127+
# alone is not enough: CodeWiki's own toolkit (str_replace_editor,
128+
# read_code_components, generate_sub_module_documentation) gets denied
129+
# ("you haven't granted it yet"), the agent writes nothing, and the run
130+
# "succeeds" with an empty module tree. Grant the toolkit explicitly with
131+
# `--allowedTools` using the permission rule syntax:
132+
# https://code.claude.com/docs/en/settings#permission-rule-syntax
133+
# `--allowedTools` flag: https://code.claude.com/docs/en/cli-reference
134+
# caw's ClaudeCodeSession only ever emits `--disallowedTools`, so wrap
135+
# subprocess.Popen to inject `--allowedTools mcp__<server>` for every server in
136+
# the --mcp-config. Belongs upstream in caw; remove once it grows a
137+
# first-class allowed_tools knob for its toolkit servers.
138+
_CLAUDE_ALLOWED_PATCH_APPLIED = False
139+
140+
141+
def _patch_claude_allowed_tools() -> None:
142+
global _CLAUDE_ALLOWED_PATCH_APPLIED
143+
if _CLAUDE_ALLOWED_PATCH_APPLIED:
144+
return
145+
import json as _json
146+
import subprocess as _sp
147+
148+
_orig_popen = _sp.Popen
149+
150+
def _popen(cmd, *args, **kwargs):
151+
try:
152+
if (
153+
isinstance(cmd, (list, tuple))
154+
and cmd
155+
and os.path.basename(str(cmd[0])) == "claude"
156+
and "--mcp-config" in cmd
157+
and "--allowedTools" not in cmd
158+
and "--allowed-tools" not in cmd
159+
):
160+
cfg_path = cmd[list(cmd).index("--mcp-config") + 1]
161+
servers = list(
162+
_json.load(open(cfg_path)).get("mcpServers", {}).keys()
163+
)
164+
if servers:
165+
allowed = ",".join(f"mcp__{s}" for s in servers)
166+
cmd = list(cmd) + ["--allowedTools", allowed]
167+
logger.info("Injected --allowedTools for MCP servers: %s", servers)
168+
except Exception as e: # never break the spawn on a patch hiccup
169+
logger.warning("claude allowedTools patch skipped: %s", e)
170+
return _orig_popen(cmd, *args, **kwargs)
171+
172+
_sp.Popen = _popen
173+
_CLAUDE_ALLOWED_PATCH_APPLIED = True
174+
175+
176+
_patch_claude_allowed_tools()
177+
# --- end stopgap --------------------------------------------------------------
178+
179+
123180
class CawBackend(LLMBackend):
124181
"""Routes LLM operations through the claude / codex CLI subscription."""
125182

@@ -304,9 +361,34 @@ def _run_module_agent_sync(
304361
# they're cwd-independent. Safe to mutate process-wide cwd because
305362
# documentation_generator processes modules sequentially and recursive
306363
# _run_module_agent_sync calls chdir to the same absolute_docs_path.
364+
# caw runs claude with `--dangerously-skip-permissions`, expecting
365+
# bypassPermissions ("Everything" runs without asking). But an org
366+
# managed policy can DISABLE bypass mode, in which case the flag is
367+
# downgraded to `acceptEdits` — observed here: both
368+
# `--dangerously-skip-permissions` and `--permission-mode
369+
# bypassPermissions` report permissionMode=acceptEdits.
370+
# permission modes: https://code.claude.com/docs/en/permission-modes
371+
# (see #skip-all-checks-with-bypasspermissions-mode and its
372+
# disableBypassPermissionsMode managed setting).
373+
# Under acceptEdits, auto-approval is limited to reads/edits INSIDE the
374+
# working directory or additionalDirectories; paths outside that scope
375+
# still prompt (== denied in non-interactive `-p`):
376+
# https://code.claude.com/docs/en/permission-modes#auto-approve-file-edits-with-acceptedits-mode
377+
# caw chdir's into the output subdir (for codex's native file_change),
378+
# so the source tree in the PARENT repo is out-of-scope and every
379+
# source Read is denied -> empty docs. claude writes via CodeWiki's
380+
# str_replace_editor (absolute deps path), so it does NOT need cwd
381+
# pinned to the output dir. Pin cwd to the repo root so BOTH the source
382+
# tree and the output dir fall inside the workspace (alt: --add-dir,
383+
# https://code.claude.com/docs/en/cli-reference). Codex keeps the
384+
# output-dir chdir because its file_change resolves relative paths.
307385
original_cwd = os.getcwd()
386+
if self._caw_provider == "codex":
387+
run_cwd = working_dir
388+
else:
389+
run_cwd = str(os.path.abspath(config.repo_path))
308390
try:
309-
os.chdir(working_dir)
391+
os.chdir(run_cwd)
310392
try:
311393
traj = agent.completion(user_prompt)
312394
finally:

0 commit comments

Comments
 (0)