Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/claude_agent_sdk/_internal/transport/subprocess_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ def _build_command(self) -> list[str]:
if schema is not None:
cmd.extend(["--json-schema", json.dumps(schema)])

# Worktree support
if self._options.worktree is not None:
if isinstance(self._options.worktree, str):
cmd.extend(["--worktree", self._options.worktree])
elif self._options.worktree:
cmd.append("--worktree")

# Always use streaming mode with stdin (matching TypeScript SDK)
# This allows agents and other large configs to be sent via initialize request
cmd.extend(["--input-format", "stream-json"])
Expand Down
4 changes: 4 additions & 0 deletions src/claude_agent_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,10 @@ class ClaudeAgentOptions:
# When enabled, files can be rewound to their state at any user message
# using `ClaudeSDKClient.rewind_files()`.
enable_file_checkpointing: bool = False
# Create a new git worktree for this session.
# When True, creates a worktree with an auto-generated name.
# When a string, creates a worktree with the specified name.
worktree: bool | str | None = None


# SDK Control Protocol
Expand Down
23 changes: 23 additions & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,26 @@ def test_build_command_large_agents_work(self):
# No @filepath references should exist
cmd_str = " ".join(cmd)
assert "@" not in cmd_str

def test_build_command_with_worktree_bool(self):
"""Test building CLI command with worktree=True."""
transport = SubprocessCLITransport(
prompt="test",
options=make_options(worktree=True),
)
cmd = transport._build_command()
assert "--worktree" in cmd
# Should not have a name argument after --worktree
wt_idx = cmd.index("--worktree")
assert wt_idx + 1 >= len(cmd) or cmd[wt_idx + 1].startswith("--")

def test_build_command_with_worktree_name(self):
"""Test building CLI command with worktree as a named string."""
transport = SubprocessCLITransport(
prompt="test",
options=make_options(worktree="my-feature"),
)
cmd = transport._build_command()
assert "--worktree" in cmd
wt_idx = cmd.index("--worktree")
assert cmd[wt_idx + 1] == "my-feature"