Skip to main content

zeph_commands/handlers/
cd.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Working-directory switch command: `/cd`.
5
6use std::future::Future;
7use std::pin::Pin;
8
9use crate::context::CommandContext;
10use crate::{CommandError, CommandHandler, CommandOutput, SlashCategory};
11
12/// Switch the session's primary working directory, or report the current one (#6032).
13///
14/// User-facing entry point into the same mechanism the LLM-invoked `set_working_directory`
15/// tool already uses — reachable identically from CLI, TUI, and ACP via the shared slash
16/// dispatch path. Conversation history, active goals, and skill state are preserved; only
17/// cwd-derived state (file-tool root, repo-map, and — unless `--safe-mode` is active —
18/// CLAUDE.md/AGENTS.md instructions) is affected.
19pub struct CdCommand;
20
21impl CommandHandler<CommandContext<'_>> for CdCommand {
22    fn name(&self) -> &'static str {
23        "/cd"
24    }
25
26    fn description(&self) -> &'static str {
27        "Change the session's working directory (no arg: show current)"
28    }
29
30    fn args_hint(&self) -> &'static str {
31        "[path]"
32    }
33
34    fn category(&self) -> SlashCategory {
35        SlashCategory::Session
36    }
37
38    fn requires_auth(&self) -> bool {
39        true
40    }
41
42    fn handle<'a>(
43        &'a self,
44        ctx: &'a mut CommandContext<'_>,
45        args: &'a str,
46    ) -> Pin<Box<dyn Future<Output = Result<CommandOutput, CommandError>> + Send + 'a>> {
47        // No handler-level span here: `change_working_directory` already opens
48        // `core.commands.cd` per NFR-004 — a second nested `commands.cd.handle` span around
49        // this thin delegation would be redundant tracing overhead.
50        Box::pin(async move {
51            let result = ctx.agent.change_working_directory(args).await?;
52            Ok(CommandOutput::Message(result))
53        })
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use crate::handlers::test_helpers::{MockDebug, MockMessages, MockSession, make_ctx};
61    use crate::sink::NullSink;
62
63    #[test]
64    fn cd_name_and_description() {
65        assert_eq!(CdCommand.name(), "/cd");
66        assert!(!CdCommand.description().is_empty());
67    }
68
69    #[test]
70    fn cd_requires_auth() {
71        assert!(CdCommand.requires_auth());
72    }
73
74    #[tokio::test]
75    async fn cd_returns_message() {
76        let mut sink = NullSink;
77        let mut debug = MockDebug;
78        let mut messages = MockMessages;
79        let session = MockSession;
80        let mut agent = crate::NullAgent;
81        let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
82        // NullAgent's default change_working_directory returns an error — verifies the
83        // handler propagates it rather than swallowing it.
84        let out = CdCommand.handle(&mut ctx, "").await;
85        assert!(out.is_err());
86    }
87}