fix: Argument list too long — промпт через файл + --entrypoint sh
Проблемы: 1. 18K system prompt + RAG context в аргументах → OSError Errno 7 2. Docker entrypoint claude перехватывает "bash" как prompt Решение: оба промпта в temp files, монтируются в Docker. --entrypoint sh -c "cat prompt | claude -p ..." Тест: НР=109% от ФОТ, приказ 812/пр — корректный ответ. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+21
-17
@@ -136,7 +136,6 @@ async def stream_claude(
|
||||
history[:] = history[-MAX_HISTORY:]
|
||||
|
||||
# Build full prompt for Claude CLI
|
||||
# Include last few history messages for context
|
||||
prompt_parts = []
|
||||
for msg in history[-6:]: # last 3 exchanges
|
||||
role = "Пользователь" if msg["role"] == "user" else "Ассистент"
|
||||
@@ -144,15 +143,22 @@ async def stream_claude(
|
||||
|
||||
full_prompt = "\n\n".join(prompt_parts)
|
||||
|
||||
# Write system prompt to temp file (can be 18K+ chars)
|
||||
# Write both prompts to temp files (avoid "Argument list too long")
|
||||
import tempfile
|
||||
system_file = tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".txt", delete=False, encoding="utf-8"
|
||||
)
|
||||
system_file.write(system_prompt)
|
||||
system_file.close()
|
||||
|
||||
prompt_file = tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".txt", delete=False, encoding="utf-8"
|
||||
)
|
||||
prompt_file.write(system_prompt)
|
||||
prompt_file.write(full_prompt)
|
||||
prompt_file.close()
|
||||
|
||||
# Docker command — mount only auth + system prompt file
|
||||
# Docker command — mount prompt files, use --entrypoint sh to avoid
|
||||
# Claude CLI intercepting "bash" as a prompt
|
||||
cmd = [
|
||||
"docker", "run", "--rm",
|
||||
"--memory=512m",
|
||||
@@ -160,18 +166,15 @@ async def stream_claude(
|
||||
"--network=host",
|
||||
"-v", f"{CLAUDE_CONFIG_DIR}:/home/worker/.claude",
|
||||
"-v", f"{os.path.expanduser('~')}/.claude.json:/home/worker/.claude.json",
|
||||
"-v", f"{prompt_file.name}:/tmp/system.txt:ro",
|
||||
"-v", f"{system_file.name}:/tmp/system.txt:ro",
|
||||
"-v", f"{prompt_file.name}:/tmp/prompt.txt:ro",
|
||||
"-e", f"HTTPS_PROXY=http://{DOCKER_HOST_GW}:{XRAY_PORT}",
|
||||
"-e", f"HTTP_PROXY=http://{DOCKER_HOST_GW}:{XRAY_PORT}",
|
||||
"-e", "HOME=/home/worker",
|
||||
"--entrypoint", "sh",
|
||||
DOCKER_IMAGE,
|
||||
"-p", full_prompt,
|
||||
"--model", "sonnet",
|
||||
"--output-format", "stream-json",
|
||||
"--verbose",
|
||||
"--system-prompt-file", "/tmp/system.txt",
|
||||
"--max-turns", "1", # no tools, just answer
|
||||
"--allowedTools", "", # no tools
|
||||
"-c",
|
||||
"cat /tmp/prompt.txt | claude -p --model sonnet --output-format stream-json --verbose --system-prompt-file /tmp/system.txt --max-turns 1",
|
||||
]
|
||||
|
||||
try:
|
||||
@@ -222,11 +225,12 @@ async def stream_claude(
|
||||
await asyncio.wait_for(proc.wait(), timeout=5)
|
||||
except asyncio.TimeoutError:
|
||||
proc.kill()
|
||||
# Cleanup temp file
|
||||
try:
|
||||
os.unlink(prompt_file.name)
|
||||
except OSError:
|
||||
pass
|
||||
# Cleanup temp files
|
||||
for f in [system_file.name, prompt_file.name]:
|
||||
try:
|
||||
os.unlink(f)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# Chain-of-Verification: check cited facts against context
|
||||
if full_response:
|
||||
|
||||
Reference in New Issue
Block a user