# harden_state_dir

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/_paths/harden_state_dir)

Create a state directory and restrict it to its owner.

The state directory holds `sessions.db` and `history.jsonl`, which hold
full conversation content and are written with the default file mode. The
directory permissions are the only thing that keeps another local user out,
so every creator must go through this function rather than a bare `mkdir`.

## Signature

```python
harden_state_dir(
    state_dir: Path | None = None,
) -> bool
```

## Description

**Note:**

Failures are logged, never raised. `mkdir` can fail on a read-only or
full filesystem, and `chmod` is routinely refused on CIFS/exFAT mounts.
Neither is a reason to abort the launch that needed the directory.

On Windows the `chmod` step is skipped. POSIX mode bits do not restrict
access there, so the directory keeps the ACL it inherits from its
parent and this function adds no protection of its own.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `state_dir` | `Path \| None` | No | Directory to create, defaulting to the active profile's. `state_migration` passes its own, since it migrates a profile that is not necessarily the active one. (default: `None`) |

## Returns

`bool`

Whether the directory now exists. A caller that must not write into a

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/e4c1ffe9ee63d7854147de490ca27495ef157a7b/libs/code/deepagents_code/_paths.py#L516)