Skip to main content

artifactize_harness/claude/
command.rs

1//! How Claude Code is started: its arguments and environment. Claude Code runs with the
2//! user's own login and nothing else of theirs: no settings, hooks, CLAUDE.md, memory,
3//! skills or tools of its own; the only tools are the ones artifactize offers.
4
5use std::{collections::BTreeMap, ffi::OsString};
6
7use serde_json::{Value, json};
8
9use crate::platform;
10
11/// Claude Code's executable, found on `PATH`.
12pub const PROGRAM: &str = "claude";
13/// The MCP server artifactize hosts in this process; Claude Code names its tools
14/// `mcp__artifactize__<tool>`.
15pub const SERVER: &str = crate::stdio_mcp::NAME;
16
17/// What one Claude Code session is started with. A probe sets no model and offers no tools.
18#[derive(Debug, Default)]
19pub struct Launch<'a> {
20    pub model: Option<&'a str>,
21    pub effort: Option<&'a str>,
22    /// Appended to Claude Code's own system prompt.
23    pub system: Option<&'a str>,
24    /// The tools artifactize offers, by their own names.
25    pub tools: Vec<&'a str>,
26    /// The JSON Schema the final answer must match.
27    pub schema: Option<&'a Value>,
28}
29
30impl Launch<'_> {
31    pub fn args(&self) -> Vec<OsString> {
32        let mut args: Vec<String> = [
33            "-p",
34            "--input-format",
35            "stream-json",
36            "--output-format",
37            "stream-json",
38            "--verbose",
39            "--include-partial-messages",
40            // Claude Code's built-in tools are off: only the offered MCP tools are callable.
41            "--tools",
42            "",
43            "--strict-mcp-config",
44            "--permission-mode",
45            "dontAsk",
46            "--setting-sources",
47            "",
48            "--settings",
49            r#"{"disableAllHooks":true}"#,
50            "--no-session-persistence",
51            "--disable-slash-commands",
52            "--no-chrome",
53        ]
54        .map(str::to_owned)
55        .into();
56        if self.model.is_some() {
57            args.extend([
58                "--mcp-config".into(),
59                json!({"mcpServers": {SERVER: {"type": "sdk", "name": SERVER}}}).to_string(),
60            ]);
61        }
62        if !self.tools.is_empty() {
63            let tools: Vec<String> = self.tools.iter().map(|name| tool_name(name)).collect();
64            args.extend(["--allowedTools".into(), tools.join(",")]);
65        }
66        if let Some(model) = self.model {
67            args.extend([
68                "--model".into(),
69                model.into(),
70                "--thinking".into(),
71                "adaptive".into(),
72                "--thinking-display".into(),
73                "summarized".into(),
74            ]);
75        }
76        if let Some(effort) = self.effort {
77            args.extend(["--effort".into(), effort.into()]);
78        }
79        if let Some(system) = self.system {
80            args.extend(["--append-system-prompt".into(), system.into()]);
81        }
82        if let Some(schema) = self.schema {
83            args.extend(["--json-schema".into(), schema.to_string()]);
84        }
85        args.into_iter().map(OsString::from).collect()
86    }
87}
88
89/// The name Claude Code gives an offered tool.
90pub fn tool_name(name: &str) -> String {
91    format!("mcp__{SERVER}__{name}")
92}
93
94/// Removed from the inherited environment: a minimal mode that skips the subscription login,
95/// an effort that would override the declared one, a cap on every tool call, and the marks
96/// of a Claude Code session artifactize itself runs in, which this session is not part of.
97const CLEARED: [&str; 5] = [
98    "CLAUDE_CODE_SIMPLE",
99    "CLAUDE_CODE_EFFORT_LEVEL",
100    "MCP_TOOL_TIMEOUT",
101    "CLAUDECODE",
102    "CLAUDE_CODE_ENTRYPOINT",
103];
104
105/// Set for every session: no CLAUDE.md, auto memory, compaction, away summaries, advisor or
106/// tool search (which would hide the MCP tools); tool results up to 200,000 tokens, as large
107/// as artifactize's own; and none of Claude Code's silent fallbacks, a non-streaming retry
108/// that can leave a tool call unfinished and a switch to another model after a refusal.
109const SET: [(&str, &str); 9] = [
110    ("CLAUDE_CODE_DISABLE_CLAUDE_MDS", "1"),
111    ("CLAUDE_CODE_DISABLE_AUTO_MEMORY", "1"),
112    ("DISABLE_AUTO_COMPACT", "1"),
113    ("CLAUDE_CODE_ENABLE_AWAY_SUMMARY", "0"),
114    ("ENABLE_TOOL_SEARCH", "false"),
115    ("CLAUDE_CODE_DISABLE_ADVISOR_TOOL", "1"),
116    ("MAX_MCP_OUTPUT_TOKENS", "200000"),
117    ("CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK", "1"),
118    ("CLAUDE_CODE_DISABLE_REFUSAL_FALLBACK", "1"),
119];
120
121/// Claude Code's environment: the user's own, as for any command, so every login Claude
122/// Code has (a subscription, an API key, a cloud account) works, with [`CLEARED`] removed
123/// and [`SET`] set.
124pub fn environment(mut inherited: BTreeMap<OsString, OsString>) -> BTreeMap<OsString, OsString> {
125    for name in CLEARED {
126        platform::remove(&mut inherited, name.as_ref());
127    }
128    for (name, value) in SET {
129        platform::set(&mut inherited, name, value);
130    }
131    inherited
132}
133
134#[cfg(test)]
135mod tests;