# Additional working directories

Same meaning as Claude Code's `additionalWorkingDirectories`.

|  | Main directory | Additional directory |
| --- | --- | --- |
| Model cwd (system prompt, `--cd`, tool ContextVar) | Project path | Unchanged |
| Conversation storage | `projects/<id>/chats/` | Unchanged |
| acceptEdits fence allowlist | Yes | Added |
| Told to the model | Working directory | “Also writable: /a, /b” |
| Stored in | Project binding | `SessionRunConfig`, session metadata |
| Change mid-session | Frozen on the first turn; [move or repair only](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/session/operations.html#main-project-binding) | Any time, from the next turn |

The main directory links through `project_workdir_for`; the fence side is in [Sandbox architecture](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/security/sandbox-architecture.html) §3.5. A session with no stored list starts from its project's source folders.

**Out of scope and extension points**

- CLAUDE.md and project settings are not loaded from additional directories. Permission rules follow the main project.
- No per-directory read-only grading: once listed, a directory is a writable safe zone.
- No global additional directories. Cross-session needs are served by projects.
- Attributed entries: change only `_as_str_list` and `check_path_safety`; schemaless storage needs no migration.
- MCP roots or multi-root IDE workspaces would use the same field as single source of truth.

## Data path

| Stage | Code | Behaviour |
| --- | --- | --- |
| Pick | `WorkingDirChips`, `POST /api/pick-folder` | Recent projects, or “Choose folder…” in the native picker |
| Send | `set_working_dirs {session_id, dirs}` | Whole list. A draft has no id yet, so the list rides on its first `chat` frame |
| Validate and save | `handle_set_working_dirs` in `ws_actions/session/run_config.py` | Each `expanduser()` must be a directory; one bad entry rejects the frame with a reason. Stores the expanded path, not the realpath. Broadcasts `working_dirs` |
| Persist | `save_session_run_config` | `None` leaves the list unchanged, so a chat frame never clears it |
| Reload | `session_loaded.data.settings` | Restores the list after a refresh or client switch |
| Each turn | `load_session_run_config` → `TurnRequest.additional_working_dirs` |  |
| Fence | `_path_is_safe` in `runtime/permissions/identity.py` → `check_path_safety` | `[current_worktree_path() or getcwd(), *dirs]` |
| Prompt | `runtime/context/prompt_assembly/components.py` | Line present only when the list is non-empty |

The dispatcher binds the real cwd into `current_worktree_path` each turn; `getcwd` is only a fallback.

## Composer

```
[my-project]  [foo ✕]  [bar ✕]  [＋]
```

- Project chip, then one chip per additional directory and an icon-only add button (`apps/web/components/chat/top-bar/chips/working-dir-chips.tsx`).
- Add and ✕ update local state optimistically; the `working_dirs` broadcast wins.
- State is `additionalWorkingDirsBySession` in the session store, fed by `session_loaded`, the broadcast and optimistic updates. Not in `ComposerSettings` or localStorage: it is server data.
- When `list_projects` reports `path_missing` for the session's project, the project chip turns to the orange warning palette with a Solar `danger-triangle` and offers “Locate folder…”.

## Invariants

| Rule | Because |
| --- | --- |
| Only the fence and the prompt | Wiring into cwd or storage breaks the table above |
| Fence and prompt cwd share `current_worktree_path()` | Otherwise the model thinks it can write, the fence blocks, and approvals loop |
| Whole-list replacement | Idempotent; no “already added” cases and no partial write |
| Schemaless session metadata | Old sessions read an empty list, no migration |
| Editable for the session's whole life | The only mid-session way to widen the fence |
| A missing main project resolves to `None` | Another directory would be a plausible cwd the user never chose |

**Tests**

| File | Covers |
| --- | --- |
| `tests/unit/store/session/test_session_config.py` | Round trip, “None leaves it unchanged”, `_as_str_list` sanitizing |
| `tests/unit/security/test_permission_rules.py` | `_path_is_safe`: inside, outside, and inside the ContextVar cwd |
| `tests/unit/webui/chat/test_ws_working_dirs.py` | Valid write and broadcast, whole-frame rejection, `session_loaded` round trip |
| `tests/unit/store/session/test_session_main_workdir.py` | Freeze, missing-directory resolution, relocate node, and editing additional directories after the freeze |
