Metadata-Version: 2.4
Name: snapodoo
Version: 0.3.0
Summary: Odoo 19 hot reload, XML/XPath linting and editor integration (sabkd language server + sabk_agent)
License: LGPL-3.0-only
Project-URL: Homepage, https://github.com/yazanassad3/snapodoo
Project-URL: Source, https://github.com/yazanassad3/snapodoo
Project-URL: Issues, https://github.com/yazanassad3/snapodoo/issues
Project-URL: Changelog, https://github.com/yazanassad3/snapodoo/releases
Classifier: Framework :: Odoo
Classifier: Framework :: Odoo :: 19.0
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Debuggers
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pygls<2,>=1.3
Requires-Dist: lxml
Provides-Extra: watch
Requires-Dist: watchdog; extra == "watch"
Dynamic: license-file

<p align="center">
  <img src="docs/banner.svg" alt="SnapOdoo: hot reload for Odoo 19. Save. It's live. No restart." width="100%">
</p>

<p align="center">
  <a href="https://github.com/yazanassad3/snapodoo/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/yazanassad3/snapodoo/actions/workflows/ci.yml/badge.svg"></a>
  <a href="https://pypi.org/project/snapodoo/"><img alt="PyPI" src="https://img.shields.io/pypi/v/snapodoo?color=714B67"></a>
  <a href="https://marketplace.visualstudio.com/items?itemName=snapodoo.snapodoo"><img alt="VS Code Marketplace" src="https://img.shields.io/visual-studio-marketplace/v/snapodoo.snapodoo?label=VS%20Code&color=007ACC"></a>
  <a href="https://open-vsx.org/extension/snapodoo/snapodoo"><img alt="Open VSX" src="https://img.shields.io/open-vsx/v/snapodoo/snapodoo?label=Open%20VSX%20(Cursor)&color=A60EE5"></a>
  <a href="https://plugins.jetbrains.com/search?search=SnapOdoo"><img alt="JetBrains Marketplace" src="https://img.shields.io/badge/JetBrains-PyCharm%202024.1%2B-000000?logo=jetbrains"></a>
  <br>
  <img alt="Python 3.10+" src="https://img.shields.io/badge/python-3.10%2B-3776AB?logo=python&logoColor=white">
  <img alt="Odoo 19" src="https://img.shields.io/badge/Odoo-19.0-714B67?logo=odoo&logoColor=white">
  <a href="LICENSE"><img alt="License: LGPL-3.0" src="https://img.shields.io/badge/license-LGPL--3.0-blue"></a>
</p>

<p align="center">
  <b>Save a Python file, and your running Odoo 19 server uses the new code, without a restart.</b><br>
  Plus XML/XPath linting that understands Odoo view inheritance. For <b>VS Code</b>, <b>Cursor</b> and <b>PyCharm</b>.
</p>

<p align="center">
  <a href="#-installation">Installation</a> ·
  <a href="#-how-it-works">How it works</a> ·
  <a href="#-safety-guarantees">Safety</a> ·
  <a href="#-troubleshooting">Troubleshooting</a> ·
  <a href="CONTRIBUTING.md">Contributing</a>
</p>

---

<!--
  DEMO GIF: how to record docs/demo.gif (about 15 s, 1280 px wide, under 8 MB):
    1. Split the screen: editor on the left, the Odoo page that uses the method on the right.
    2. Start recording (Linux: Peek or Kooha; macOS: Kap; Windows: ScreenToGif).
    3. Change one line in a method → Ctrl+S → zoom on the status bar turning into "⚡ … [HOT]"
       → reload or click in the browser and show the new behavior. No restart.
    4. Save it as docs/demo.gif and replace the line below with: ![SnapOdoo demo](docs/demo.gif)
-->
<p align="center"><i>🎬 Demo GIF coming soon: edit → save → <code>⚡ [HOT]</code> → the change is live in the browser.</i></p>

## ✨ Why SnapOdoo?

Every Odoo developer knows the loop: **change one line → restart the server → wait → log in again → click back to
where you were.** Dozens of times a day.

SnapOdoo replaces it for the most common change, editing the body of a method:

| | Traditional loop | With SnapOdoo |
| --- | --- | --- |
| Apply a method change | Restart Odoo, wait for the registry to load | **Save.** The code swap takes **< 1 ms** (0.79 ms mean, measured) |
| Back to testing | Log in again, navigate back, reproduce the state | Stay on the same page. Your session and data are untouched |
| A change that *does* need a restart | You find out when something behaves oddly | You're told **before you save** (⚠ WARM / COLD) |
| Broken XPath in an inherited view | Error on module update (`-u`) | Red underline **while you type** |
| **Example day**: 40 method edits, ~30 s per restart cycle | **~20 min of waiting** | **~0 min** |

The "example day" is an illustration: plug in your own restart time and edit count. Measured on the live
benchmark (Odoo 19, 8 concurrent clients, 40 patches): the swap takes **0.79 ms**, a change is live in a median
of **0.39 s** while requests are in flight (the gate waits for them), and there were **0** mixed-version
transactions.

## 🚀 Features

| | |
| --- | --- |
| 🔥 **Hot patching on save** | Method bodies, added and removed methods, new imports. `super()`, name mangling and decorator-wrapped methods keep working. |
| 🚦 **Quiescence gate** | Waits for in-flight requests and cron jobs, so no transaction ever runs half old code and half new code. |
| ↩️ **Revert** | Undo any patch, newest first. A patch is all-or-nothing. |
| 🔍 **XML/XPath linter** | Broken XPath targets (checked against the parent arch as Odoo 19 combines it), unknown fields and XML IDs, missing `depends`. |
| 🛠️ **Odoo 19 quick fixes** | `attrs` / `states` → Python expressions, `<tree>` → `<list>`, `view_mode`, `t-esc`. One fix at a time, or the whole file. |
| 🧭 **Registry inspector** | A model's live MRO: which addon contributes which class, its fields, and which members are patched. |
| 👁️ **Visual checks** (optional) | A headless browser diffs a view's DOM and pixels before and after a change. |
| 🔒 **Safe by design** | Refuses production databases, accepts local authenticated connections only, masks PII before anything reaches your editor. |

---

## 📦 Installation

SnapOdoo has **two halves**. Set up both (about 5 minutes):

| Half | Runs where | Needed for |
| --- | --- | --- |
| **`sabk_agent`** (Step 1) | Inside your *development* Odoo 19 server | Hot patching |
| **Editor extension** (Step 2) | VS Code, Cursor or PyCharm | Everything (XML linting works even without Step 1) |

**Requirements:** Odoo **19.0**, Python **3.10+**, **Linux or macOS** (on Windows, use WSL).

### Step 1: Odoo 19 server setup (`sabk_agent`)

**1.1 Install the Python package into the Python that runs Odoo.** It provides `sabk_mask`, which the agent
needs:

```bash
# From PyPI:
/path/to/odoo-venv/bin/pip install snapodoo
# …or from the GitHub release:
/path/to/odoo-venv/bin/pip install https://github.com/yazanassad3/snapodoo/releases/download/v0.3.0/snapodoo-0.3.0-py3-none-any.whl
```

**1.2 Put the `sabk_agent` addon on your `addons_path`.** Pick one:

```bash
# a) From the GitHub release: unzip into a directory that is already on addons_path
cd /path/to/your/extra-addons
curl -LO https://github.com/yazanassad3/snapodoo/releases/download/v0.3.0/sabk_agent-19.0.1.0.0.zip
unzip sabk_agent-19.0.1.0.0.zip          # creates ./sabk_agent/

# b) Or clone the repository and add the clone itself to addons_path
git clone https://github.com/yazanassad3/snapodoo.git ~/snapodoo
#    odoo.conf:  addons_path = /path/to/odoo/addons,/path/to/your/addons,/home/<you>/snapodoo
```

> [!IMPORTANT]
> **Don't install `sabk_agent` from the Apps menu.** It isn't a database module. It is loaded
> server-wide with `--load` (next step), and never appears in any database.

**1.3 Start Odoo in development mode with the agent switched on.** Name the database(s) it may touch:

```bash
SABK_ENABLE=1 SABK_DB_ALLOWLIST=my_dev_db \
./odoo-bin -c odoo.conf -d my_dev_db \
    --workers=0 --dev=xml,qweb --load=base,rpc,web,sabk_agent
```

| Flag | Why |
| --- | --- |
| `SABK_ENABLE=1` | Explicit opt-in. Without it the agent stays inactive. |
| `SABK_DB_ALLOWLIST=my_dev_db` | The only database(s) it may patch (comma-separated). |
| `--workers=0` | One process, so the patch reaches all of Odoo. |
| `--dev=xml,qweb` | Dev mode. **Not** `--dev=reload` or `all`, which restart on every save. |
| `--load=base,rpc,web,sabk_agent` | Odoo 19's default server-wide modules plus the agent. |

**1.4 Check the log.** You should see these two lines:

```
INFO    odoo.addons.sabk_agent.server: sabk_agent listening on /run/user/1000/sabk/agent.sock
WARNING odoo.addons.sabk_agent: sabk_agent ACTIVE (dev only): socket=… token_file=… db=my_dev_db
```

If you see `sabk_agent inactive: <reason>` instead, the reason tells you which flag is missing (see
[Troubleshooting](#-troubleshooting)).

<details>
<summary><b>🐳 Odoo in Docker, or on another machine?</b></summary>

- **Docker:** run the container with your own uid (`--user "$(id -u):$(id -g)"`). Bind-mount a socket directory
  (`-v "$XDG_RUNTIME_DIR/sabk:/run/sabk"`) and set `SABK_SOCKET=/run/sabk/agent.sock` inside the container. Then,
  in the editor, set *socket* to `$XDG_RUNTIME_DIR/sabk/agent.sock` (written out in full) and **Path map**
  (`sabk.pathMap`) to `HOST=CONTAINER` prefixes, for example `/home/me/addons=/mnt/extra-addons`. If the uids
  differ, allow yours with `SABK_ALLOW_UIDS`.
- **Staging host:** bridge the agent's socket to your laptop without opening a port. Use
  `sabkd remote ssh user@host --remote-socket … --local-socket …` (OpenSSH socket forwarding), or
  `sabkd remote init / serve / connect` for mutual TLS. The token check, the sandbox and masking still apply
  end to end.

</details>

### Step 2: Editor setup

<details open>
<summary><b>🟦 VS Code</b></summary>

1. **Install:** open the Extensions view (`Ctrl+Shift+X`), search **SnapOdoo** and click **Install**.
   *Offline:* download `snapodoo-0.3.0.vsix` from the [latest release](https://github.com/yazanassad3/snapodoo/releases/latest), then

   ```bash
   code --install-extension snapodoo-0.3.0.vsix
   ```

2. **Open your addons folder** (any folder that contains a `__manifest__.py`).
3. **First run:** if SnapOdoo says it needs `pygls` and `lxml`, click **Install Automatically**. It creates a
   private virtualenv just for SnapOdoo. Nothing is installed globally.
4. **Verify:** the status bar (bottom left) shows **`⚡ ready · 0 patches`**. Hover for details, click for the
   menu.

</details>

<details>
<summary><b>🟪 Cursor</b></summary>

1. **Install:** Cursor uses the Open VSX registry. In the Extensions view (`Ctrl+Shift+X`), search **SnapOdoo**
   and click **Install**.
   *Offline:* download the `.vsix` from the [latest release](https://github.com/yazanassad3/snapodoo/releases/latest), then

   ```bash
   cursor --install-extension snapodoo-0.3.0.vsix
   ```

2. **Steps 2 to 4 are the same as for VS Code:** open your addons, accept **Install Automatically** if asked, and look for
   `⚡ ready · 0 patches`.
3. **For teams:** commit this as `.vscode/extensions.json` in your addons repository. VS Code and Cursor will
   then suggest SnapOdoo to everyone who opens it:

   ```json
   { "recommendations": ["snapodoo.snapodoo"] }
   ```

</details>

<details>
<summary><b>🟩 PyCharm</b> (Community &amp; Professional, 2024.1+)</summary>

1. **Install:** go to **Settings → Plugins → Marketplace**, search **SnapOdoo** and click **Install**. Its
   dependency **LSP4IJ** (Red Hat's language-server client) is installed automatically. Restart the IDE.
   *Offline:* first install **LSP4IJ** from the Marketplace, because plugins installed from disk don't fetch their
   dependencies. Then go to **Settings → Plugins → ⚙ → Install Plugin from Disk…** and pick
   `snapodoo-pycharm-0.3.0.zip` from the [latest release](https://github.com/yazanassad3/snapodoo/releases/latest).
2. **Open your addons project.** SnapOdoo starts when it finds a `__manifest__.py`.
3. **First run:** if a balloon says Python dependencies are missing, click **Install Automatically**.
4. **Verify:** the status bar (bottom right) shows **`⚡ ready · 0 patches`**.

| Where / key | What |
| --- | --- |
| **Tools → SnapOdoo** | Revert Last Patch, Inspect Running Registry…, Toggle Zen Mode, Restart Language Server, Rebuild Addons Index, visual checks, Show Log |
| Click the status bar item | The same menu |
| `Alt+Enter` | XML quick fixes (Odoo 19 rewrites) |
| `Ctrl+Alt+Shift+Z` | Zen mode (`Ctrl+Alt+Z` is PyCharm's Rollback) |
| **Settings → Tools → SnapOdoo** | Settings (per project; the visual-check password is kept in the IDE password safe) |

</details>

**Configuration is usually unnecessary.** SnapOdoo reads `addons_path` from `odoo.conf` or `.odoorc` in the
project root, then `$ODOO_RC`, then `~/.odoorc`, so the linter knows Odoo core fields and views. If your config
is elsewhere, set **odoo.conf** (`sabk.odooConf`). If you changed `SABK_SOCKET`, set the agent **socket** and
**token file** to match.

### Step 3: Your first hot patch (30 seconds)

1. With Odoo running (Step 1) and your addons open (Step 2), check the status bar: **`⚡ ready · 0 patches`**.
   Hover it: it should say `Agent: pid …, Odoo 19.0, db my_dev_db`.
2. Open any model file in your addon and change the **body** of a method you can trigger from the UI. A button
   action is ideal, for example:

   ```python
   def action_confirm(self):
       _logger.info("SnapOdoo says hi")   # ← add a line like this
       return super().action_confirm()
   ```

3. **Save** (`Ctrl+S`). The status bar turns into **`⚡ 0.79ms [HOT] · 1 patch`** (your number will differ).
4. Click the button in Odoo, **without restarting.** The new code runs.
5. Optional: run **SnapOdoo: Revert Last Patch**. The old code runs again; your file is untouched.

Now add a field (`x = fields.Char()`) to the same class. SnapOdoo shows **⚠ COLD** *before* you save, because
that change needs a restart or `-u`. Delete the field again.

---

## 🧠 How it works

```mermaid
sequenceDiagram
    autonumber
    actor Dev as You
    participant Ed as Editor<br/>(VS Code · Cursor · PyCharm)
    participant Sd as sabkd<br/>language server
    box rgba(113,75,103,0.25) sabk_agent, inside the Odoo 19 process
        participant Sb as Sandbox
        participant Gt as Quiescence gate
        participant In as Injector
    end
    Dev->>Ed: edit a method
    Ed->>Sd: LSP didChange
    Sd-->>Ed: diagnostics (XPath, fields, ⚠ WARM/COLD before you save)
    Dev->>Ed: Ctrl+S
    Ed->>Sd: LSP didSave
    Sd->>Sd: AST diff vs. the code Odoo is running → HOT?
    Sd->>Sb: apply (Unix socket 0600 + token)
    Sb->>Sb: every loaded DB allowlisted, none production-like
    Sb->>Gt: ok
    Gt->>Gt: wait for in-flight requests & crons, hold new ones
    Gt->>In: exclusive
    In->>In: compile + swap methods in place (< 1 ms), reset memos, evict caches
    In-->>Sd: result (PII-masked before it leaves Odoo)
    Sd-->>Ed: ⚡ 0.79ms [HOT] · 1 patch
```

Each saved file is classified by its most demanding change. The classifier is conservative: anything it doesn't
positively recognize as safe escalates.

| Tier | Example | What happens |
| --- | --- | --- |
| **NOOP** | comments, whitespace, docstrings | nothing |
| **HOT** | a method body; a method added or removed | patched live, in place |
| **WARM** | changed `@api.depends` / `@api.constrains`, `@http.route` controllers | flagged: restart the server |
| **COLD** | fields, `_name` / `_inherit`, new classes, `__manifest__.py` | flagged: `-u` or restart |

SnapOdoo always diffs against the code Odoo is **actually running**, not the last version saved on disk. After a
WARM or COLD save it keeps reporting "needs restart" (`↻` in the status bar). It never assumes the server picked
up a change it didn't.

## 🔒 Safety guarantees

SnapOdoo changes a live process, so it is deliberately hard to point at the wrong one:

- **Inert unless explicitly enabled.** The agent starts only with `SABK_ENABLE=1`, `--workers=0`, a `--dev` mode
  without `reload`, and *every* database in `SABK_DB_ALLOWLIST`. Otherwise it logs one line and does nothing.
- **Refuses production, on every patch.** All loaded databases are re-checked. A database is refused if it isn't
  allowlisted, is marked `sabk.environment = production`, has an enterprise subscription without having been
  neutralized, or has a production-like name (`prod`, `live`, …).
- **Local and authenticated.** The agent uses a Unix socket only (directory `0700`, socket `0600`), never TCP. The
  peer's uid is verified on Linux, and a secret token is required (compared in constant time).
- **No mixed transactions.** The quiescence gate guarantees that each request runs entirely on old code or
  entirely on new code. A patch is atomic and revertible.
- **PII never leaves Odoo unmasked.** Names, emails, phones, VAT numbers, IBANs and card numbers become
  deterministic, format-valid fakes. Passwords, tokens and keys are redacted. The editor side masks everything a
  second time.

Details, and how to report a vulnerability privately: [SECURITY.md](SECURITY.md).

## 🔍 XML lint codes

All checks run on the unsaved buffer as you type.

| Code | Meaning |
| --- | --- |
| `xpath-not-found` | The spec target is missing from the parent arch, combined as Odoo 19 does it (depth-first from the root view, ordered by priority and id, limited to this module's dependency closure). |
| `xpath-needs-dependency` | The target exists only if a module outside `depends` is installed. The message names it. |
| `xpath-syntax`, `xpath-eval`, `move-not-found`, `invalid-position`, … | The inheritance spec itself is invalid. |
| `unknown-field` | The field doesn't exist on the view's model, or on the comodel inside x2many subviews. |
| `unknown-view`, `unknown-xmlid` | Unknown `inherit_id`, `ref=`, `ref('…')`, `groups=`, `%(…)d`, `t-call`, menu parent or action. |
| `obsolete-attrs`, `obsolete-states`, `tree-tag`, `view-mode-tree`, `deprecated-t-esc` | Constructs Odoo 19 rejects or deprecates. Each has a quick fix. |
| `hot-warm`, `hot-cold` (Python) | A change that can't be hot-patched, compared with the code the server is running. |

## ⌨️ Command line

The same engine also works without an editor (`pip install snapodoo` provides the `sabkd` command):

```bash
sabkd watch --addons ~/src/my_addons     # hot-patch on save from a terminal (stdin: journal | revert <id> | stale | status)
sabkd lint views.xml --odoo-conf odoo.conf --workspace ~/src/my_addons   # exit code 1 on errors (CI-friendly)
sabkd status | journal | revert <id>     # query or control the running agent
sabkd verify my_module.view_partner_form --url http://127.0.0.1:8069 --db my_dev_db   # visual check
sabkd read res.partner --fields name,email      # read records through the agent (masked, read-only)
sabkd remote ssh user@staging --remote-socket /run/user/1001/sabk/agent.sock --local-socket ~/.sabk/staging.sock
```

## 🩺 Troubleshooting

<details>
<summary><b>Status bar says "agent offline"</b></summary>

The editor can't reach `sabk_agent`. Check in order:

1. Is Odoo running, and does its log show `sabk_agent ACTIVE`? If it shows `sabk_agent inactive: …`, fix the
   reason it names: missing `SABK_ENABLE=1`, `--workers` not 0, `--dev=reload`, or a database not in
   `SABK_DB_ALLOWLIST`.
2. Socket location: the agent logs `sabk_agent listening on <path>`. If that isn't
   `$XDG_RUNTIME_DIR/sabk/agent.sock`, set the editor's **socket** (`sabk.socketPath`) and **token file**
   (`sabk.tokenFile`, `agent.token` next to the socket) to the logged path.
3. Docker: see the Docker notes in Step 1.

</details>

<details>
<summary><b>⚠ WARM / COLD: "was not hot-patched"</b></summary>

Not a bug. The change can't be applied safely to a running server.
**WARM** (decorator changes, new `@api.constrains`, controllers): restart Odoo.
**COLD** (fields, `_inherit`, new models, `__manifest__.py`): update the module (`-u my_module`) or restart.
The `↻ N` counter in the status bar shows how many files are waiting for a restart.

</details>

<details>
<summary><b>"DEFERRED: requests kept the gate busy"</b></summary>

A long request or cron job was still running when the patch waited (1.5 s by default). Nothing was changed.
Save again, or raise `sabk.quiesceTimeoutMs`.

</details>

<details>
<summary><b>"Patch refused by the database sandbox"</b></summary>

A loaded database isn't in `SABK_DB_ALLOWLIST` or looks like production. Use a dedicated, neutralized
development database. If a dev database's name merely *looks* like production, set the system parameter
`sabk.environment = development` in it.

</details>

<details>
<summary><b>"SnapOdoo needs Python 3.10+ with pygls (&lt;2) and lxml"</b></summary>

Click **Install Automatically** (or run the *Install Python Dependencies* command). To use your own interpreter,
set **Python interpreter** (`sabk.pythonPath`) to a Python 3.10+ that has `pygls>=1.3,<2` and `lxml`.
pygls 2 is not supported yet.

</details>

<details>
<summary><b>The linter flags core fields or views as unknown</b></summary>

The index doesn't include Odoo core. Set **odoo.conf** (`sabk.odooConf`) to the config whose `addons_path`
lists Odoo's addons, then run **Rebuild Addons Index**.

</details>

<details>
<summary><b>PyCharm: no status bar item</b></summary>

The project must contain a `__manifest__.py`, and **LSP4IJ** must be installed and enabled. Use
**Tools → SnapOdoo → Restart Language Server** to start it manually, and **Show Log** to see why it stopped.

</details>

## ⚠️ Limitations

- Odoo **19.0** only. The injector relies on Odoo 19's model composition.
- Single-process mode (`--workers=0`). With prefork workers, each worker has its own copy of the code.
- Only HOT changes are applied live. Everything else is flagged with what to do instead.

## 🤝 Contributing

Bug reports, ideas and pull requests are welcome. Start with [CONTRIBUTING.md](CONTRIBUTING.md) (dev setup and
tests) and [DEVELOPMENT.md](DEVELOPMENT.md) (internals and the live benchmark). Please follow the
[Code of Conduct](CODE_OF_CONDUCT.md), and report security issues privately ([SECURITY.md](SECURITY.md)).

## 📄 License

[LGPL-3.0](LICENSE), the same license as Odoo Community.
