# tinker_cookbook.utils.code_state

### [**tinker_cookbook.utils.code_state**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/utils/code_state.py#L11) (_modules_)

Capture the current code state for one or more Python modules as a diff string.

For each module this function locates its filesystem path, discovers the enclosing Git repository, records the current commit hash (HEAD), and includes combined staged+unstaged changes (`git diff HEAD`) for the entire repository. The output is suitable for storage alongside experiment metadata to reproduce the exact code state later.

**Parameters:**

- [**modules**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/utils/code_state.py#L11) (_Sequence[str | ModuleType]_) – Module import names (e.g. "tinker_cookbook.rl") or already-imported module objects.

**Returns:** _str_ – Concatenated sections, one per repository, each beginning with a header of the form `### repo: <repo_root> @ <commit_hash>` followed by the repo-wide diff. Modules not in a Git repository get a short note instead.

```
    state = code_state(["tinker_cookbook", "tinker"])
    with open("code.diff", "w") as f:
        f.write(state)
    ```
