Skip to content

feat(chat): add --workdir/-C flag to set working directory at session start#3732

Open
sandikodev wants to merge 3 commits intoaws:mainfrom
sandikodev:feat/chat-workdir-flag
Open

feat(chat): add --workdir/-C flag to set working directory at session start#3732
sandikodev wants to merge 3 commits intoaws:mainfrom
sandikodev:feat/chat-workdir-flag

Conversation

@sandikodev
Copy link
Copy Markdown

Problem

When working with multiple projects, users must cd to the correct directory before starting kiro-cli chat to get the right session context, LSP discovery, and .kiro config. There is no way to specify a working directory inline.

Solution

Add --workdir / -C flag to kiro-cli chat:

kiro-cli chat --workdir /path/to/project
kiro-cli chat -C ~/project/wezterm

# Also works with other flags
kiro-cli chat -C /path/to/project --list-sessions
kiro-cli chat -C /path/to/project --resume

Implementation

set_current_dir is called at the very start of ChatArgs::execute, before any other initialization — so LSP discovery, .kiro config loading, session lookup by path, and --resume all use the specified directory correctly.

The change is minimal: one new field in ChatArgs and four lines in execute.

Closes: (no issue yet — happy to create one if preferred)

…le freshness check

The current str_replace implementation uses exact byte matching only.
When the model's old_str has minor differences from the file (indentation
drift, whitespace, or small context edits), the match fails and the model
either retries wastefully or falls back to destructive shell commands.

Implement str_replace_fuzzy() with a 3-strategy fallback chain inspired
by opencode and cline's diff-apply approaches:

1. Exact match — unchanged behaviour for the common case
2. Line-trimmed match — compares lines after trim(), then replaces using
   byte offsets (prefix-sum table) into the original content. Handles
   indentation drift (tab vs spaces, different indent levels).
3. Block-anchor match — uses first+last line as anchors, scores middle
   lines with Levenshtein similarity, picks the best candidate above a
   0.6 threshold. Handles minor edits in surrounding context lines.

Also adds file freshness checking:
- fs_read now records the file mtime into FileLineTracker.last_read_mtime
  whenever it reads a file
- fs_write str_replace checks the current mtime against the recorded value
  before writing; if the file was modified externally it returns a clear
  error asking the model to re-read before retrying

Also:
- validate() rejects empty old_str before reaching fuzzy matching
- tool_index.json description updated to reflect fuzzy tolerance and
  reinforce read-before-write / no-sed-fallback guidance

Key correctness properties:
- Strategies 2 and 3 return byte ranges — replacement is always at the
  correct position even if matched text appears elsewhere in the file
- block_anchor_match skips first==last anchors (false positive guard)
- similarity_score respects actual content window bounds
- levenshtein uses O(n) rolling-row space, char count for denominator
- build_line_offsets prefix-sum gives O(1) offset lookup
- strip_empty_boundary_lines handles both leading and trailing empty lines

11 tests cover all strategies, edge cases, and error messages.
… start

Allows users to start a chat session in a specific directory without
needing to cd first or restart the session:

  kiro-cli chat --workdir /path/to/project
  kiro-cli chat -C /path/to/project

The working directory is set before any other initialization, so LSP
discovery, .kiro config loading, and context detection all use the
specified path.
…session

Allows changing the working directory without restarting the session:

  /changedir /path/to/project
  /cd ~/project/wezterm
  /cd          (goes to $HOME)

After changing directory, the user is prompted to run /code init
to reinitialize code intelligence for the new directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant