agentdetox 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. agentdetox-0.2.0/.gitignore +6 -0
  2. agentdetox-0.2.0/CHANGELOG.md +64 -0
  3. agentdetox-0.2.0/LICENSE +202 -0
  4. agentdetox-0.2.0/PKG-INFO +298 -0
  5. agentdetox-0.2.0/README.md +262 -0
  6. agentdetox-0.2.0/pyproject.toml +74 -0
  7. agentdetox-0.2.0/src/agentdetox/__init__.py +1 -0
  8. agentdetox-0.2.0/src/agentdetox/__main__.py +5 -0
  9. agentdetox-0.2.0/src/agentdetox/adapters.py +161 -0
  10. agentdetox-0.2.0/src/agentdetox/agentscan.py +70 -0
  11. agentdetox-0.2.0/src/agentdetox/allowlist.py +101 -0
  12. agentdetox-0.2.0/src/agentdetox/cli.py +575 -0
  13. agentdetox-0.2.0/src/agentdetox/cluster.py +111 -0
  14. agentdetox-0.2.0/src/agentdetox/compat.py +76 -0
  15. agentdetox-0.2.0/src/agentdetox/consistency.py +289 -0
  16. agentdetox-0.2.0/src/agentdetox/context.py +272 -0
  17. agentdetox-0.2.0/src/agentdetox/corpus.py +68 -0
  18. agentdetox-0.2.0/src/agentdetox/daemon.py +380 -0
  19. agentdetox-0.2.0/src/agentdetox/demo.py +118 -0
  20. agentdetox-0.2.0/src/agentdetox/doctor.py +179 -0
  21. agentdetox-0.2.0/src/agentdetox/embed.py +104 -0
  22. agentdetox-0.2.0/src/agentdetox/engine.py +317 -0
  23. agentdetox-0.2.0/src/agentdetox/events.py +50 -0
  24. agentdetox-0.2.0/src/agentdetox/findings.py +64 -0
  25. agentdetox-0.2.0/src/agentdetox/hidden.py +247 -0
  26. agentdetox-0.2.0/src/agentdetox/hooks.py +83 -0
  27. agentdetox-0.2.0/src/agentdetox/install.py +134 -0
  28. agentdetox-0.2.0/src/agentdetox/integrity.py +371 -0
  29. agentdetox-0.2.0/src/agentdetox/ledger.py +145 -0
  30. agentdetox-0.2.0/src/agentdetox/persistence.py +203 -0
  31. agentdetox-0.2.0/src/agentdetox/policy.py +82 -0
  32. agentdetox-0.2.0/src/agentdetox/provenance.py +214 -0
  33. agentdetox-0.2.0/src/agentdetox/scan.py +114 -0
  34. agentdetox-0.2.0/src/agentdetox/session.py +113 -0
  35. agentdetox-0.2.0/src/agentdetox/settings.py +141 -0
  36. agentdetox-0.2.0/src/agentdetox/shell.py +108 -0
  37. agentdetox-0.2.0/src/agentdetox/status.py +132 -0
  38. agentdetox-0.2.0/src/agentdetox/trace.py +91 -0
  39. agentdetox-0.2.0/tests/conftest.py +76 -0
  40. agentdetox-0.2.0/tests/test_agent.py +186 -0
  41. agentdetox-0.2.0/tests/test_allow_config_status.py +309 -0
  42. agentdetox-0.2.0/tests/test_cli.py +53 -0
  43. agentdetox-0.2.0/tests/test_cluster_trace.py +57 -0
  44. agentdetox-0.2.0/tests/test_consistency.py +230 -0
  45. agentdetox-0.2.0/tests/test_daemon.py +245 -0
  46. agentdetox-0.2.0/tests/test_doctor.py +109 -0
  47. agentdetox-0.2.0/tests/test_engine.py +80 -0
  48. agentdetox-0.2.0/tests/test_hidden.py +125 -0
  49. agentdetox-0.2.0/tests/test_hooks.py +263 -0
  50. agentdetox-0.2.0/tests/test_ledger.py +160 -0
  51. agentdetox-0.2.0/tests/test_plugin.py +70 -0
  52. agentdetox-0.2.0/tests/test_policy.py +70 -0
  53. agentdetox-0.2.0/tests/test_provenance.py +170 -0
  54. agentdetox-0.2.0/tests/test_store.py +192 -0
  55. agentdetox-0.2.0/tests/test_windows.py +236 -0
@@ -0,0 +1,6 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.egg-info/
4
+ .pytest_cache/
5
+ dist/
6
+ bench/data/
@@ -0,0 +1,64 @@
1
+ # Changelog
2
+
3
+ Notable changes to AgentDetox. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/);
4
+ before 1.0, a minor version may change commands, settings or the snapshot store.
5
+
6
+ ## [0.2.0] - 2026-10-28
7
+
8
+ The first release on PyPI. 0.1.0 was a knowledge-base scanner published on GitHub as `ragscan`;
9
+ 0.2.0 renames it AgentDetox and extends it to what agents trust and reload: memory, rules,
10
+ instruction and skill files, and config.
11
+
12
+ ### Added
13
+
14
+ - **Claude Code protection** (`agentdetox install claude-code`). Hooks check every write to
15
+ memory, CLAUDE.md, rules, skills and config, including shell redirects, `tee`, `sed -i`, `cp`,
16
+ `mv` and `rm`. Planted instructions are denied, unless you typed or pasted the text yourself,
17
+ in which case you are asked. Changes to hooks, MCP servers or permissions (the prompt says what
18
+ is added or removed), suspicious writes, and memory that points package installs at a
19
+ non-default registry need your approval. At session start, files changed outside
20
+ the agent since the last snapshot are reported with the command that restores each one.
21
+ `CLAUDE_CONFIG_DIR` is followed.
22
+ - **Claude Code plugin** (`/plugin install agentdetox --marketplace chriswu727/agentdetox`), as an
23
+ alternative to `install claude-code`. It runs the installed `agentdetox`, or its own release
24
+ through `uvx`; `install` and `doctor` notice when both routes register the hooks.
25
+ - **Ledger** (`agentdetox log`, `agentdetox blame <file>`). Every change to a watched file is
26
+ recorded with where it came from: your own words, the agent, a named web page or tool output it
27
+ copied, or a change made outside the agent's edit tools. The versions before and after are kept,
28
+ so `blame` can say where each line came from.
29
+ - **Snapshots, tamper detection and restore** (`agentdetox agent snapshot|verify|restore|scan`).
30
+ The store in `~/.agentdetox` is private (0700/0600). A restore quarantines the current file
31
+ instead of deleting it.
32
+ - **`agentdetox demo`**: an attack caught and undone in a throwaway folder, using the real hooks.
33
+ `install` ends by saying what is now watched.
34
+ - **Contradiction detection** (experimental; optional `nli` extra, then `agentdetox fetch-models`). A
35
+ multilingual NLI model compares new memory with what the agent already knows. It records
36
+ conflicts by default and asks only if you set it to. One background model server per store
37
+ keeps the model loaded, so a write costs the hook about 0.1 s.
38
+ - **Provenance check.** A memory write mostly copied from web pages, MCP data, command output or
39
+ files the agent just read asks for approval, unless you asked the agent to remember it.
40
+ - **Day-to-day commands.** `agentdetox status`, `events` (each with the command or file it was
41
+ about), `doctor`, and `allow <id>` to
42
+ permanently allow a reviewed false positive. `config <check> ask|record|off` sets each check;
43
+ the settings live in `~/.agentdetox/config.toml`, and `AGENTDETOX_<CHECK>` overrides them.
44
+ - **Guard on AgentDetox's own state.** An agent needs approval to change AgentDetox's settings,
45
+ allowlist or snapshots, or to run the commands that do.
46
+ - **Experimental hooks for Codex and Cursor.** OpenClaw gets tamper detection only.
47
+ - **Windows support**, tested in CI but not yet on a real machine. Windows has no model server yet.
48
+ - **Benchmarks and an evaluation lab.** An agent memory poisoning benchmark (v0, built from public
49
+ data only) and a provenance benchmark, in `bench/`. The lab in `lab/` replays attacks and
50
+ everyday use against real Claude Code sessions, with and without AgentDetox.
51
+
52
+ ### Security
53
+
54
+ - Background processes and the `python -m` hook fallback start Python with `-I`. Without it, a
55
+ repository could plant an `agentdetox` package that the hook would import.
56
+
57
+ ## [0.1.0] - 2026-10-27
58
+
59
+ Released on GitHub as `ragscan`. It scans RAG knowledge bases for hidden prompt injections and
60
+ PoisonedRAG-style poisoning (CleanBase clique detection), and traces wrong answers back to the
61
+ passages responsible.
62
+
63
+ [0.2.0]: https://github.com/chriswu727/agentdetox/compare/ba1cc83...main
64
+ [0.1.0]: https://github.com/chriswu727/agentdetox/commit/ba1cc83
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,298 @@
1
+ Metadata-Version: 2.5
2
+ Name: agentdetox
3
+ Version: 0.2.0
4
+ Summary: Protection for what AI coding agents remember and obey: checks what they write to memory, rules, skills and config, records where each change came from, and puts back what was tampered with.
5
+ Project-URL: Homepage, https://github.com/chriswu727/agentdetox
6
+ Project-URL: Issues, https://github.com/chriswu727/agentdetox/issues
7
+ Project-URL: Changelog, https://github.com/chriswu727/agentdetox/blob/main/CHANGELOG.md
8
+ Author: Yichen Wu
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: agent-memory,ai-agents,claude-code,data-poisoning,llm,mcp,prompt-injection,rag,security
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: MacOS
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Security
23
+ Requires-Python: >=3.11
24
+ Requires-Dist: numpy>=1.24
25
+ Provides-Extra: dev
26
+ Requires-Dist: mypy>=1.18; extra == 'dev'
27
+ Requires-Dist: pip-audit>=2.7; extra == 'dev'
28
+ Requires-Dist: pytest>=7; extra == 'dev'
29
+ Requires-Dist: ruff>=0.15; extra == 'dev'
30
+ Provides-Extra: nli
31
+ Requires-Dist: torch>=2.1; extra == 'nli'
32
+ Requires-Dist: transformers>=4.40; extra == 'nli'
33
+ Provides-Extra: st
34
+ Requires-Dist: sentence-transformers>=2.2; extra == 'st'
35
+ Description-Content-Type: text/markdown
36
+
37
+ # AgentDetox
38
+
39
+ Protection for what AI coding agents remember and obey. AgentDetox watches an agent's memory,
40
+ instruction and rules files, skills and config: it checks what the agent writes there, notices what
41
+ changes behind its back, and puts things back.
42
+
43
+ > **Status: preview.** Claude Code is supported. Codex and Cursor hooks are experimental, and
44
+ > OpenClaw gets tamper detection only. Developed on macOS; on Windows the tests run in CI, but it
45
+ > has not yet been used on a real machine. Where it is going:
46
+ > [ROADMAP.md](https://github.com/chriswu727/agentdetox/blob/main/ROADMAP.md).
47
+
48
+ Agents learn from web pages, files, emails and tool output, and some of it is written into memory
49
+ or rules that every later session trusts. One poisoned write can steer an agent for weeks, as
50
+ [Cisco's persistent-memory compromise of Claude Code](https://blogs.cisco.com/ai/identifying-and-remediating-a-persistent-memory-compromise-in-claude-code)
51
+ (April 2026) and MemGhost (arXiv 2607.05189, July 2026) showed. Today's models refuse much of the
52
+ obvious injected text they read. What they cannot see is a file changed behind their back, by a
53
+ dependency's install script or another program, which they then load as your instructions; and
54
+ smaller models still save planted text when asked to remember "the setup steps".
55
+
56
+ ## See it
57
+
58
+ ```bash
59
+ agentdetox demo
60
+ ```
61
+
62
+ In a throwaway folder, with nothing else read or changed, the demo shows an agent trying to save a
63
+ planted instruction to its memory (blocked), a dependency's install script slipping the same
64
+ instruction into CLAUDE.md (reported to the agent at the next session, with the command that undoes
65
+ it), and the file being restored. It runs the real hooks, so it doubles as a self-test.
66
+
67
+ ## Install
68
+
69
+ ```bash
70
+ uv tool install agentdetox # or: pipx install agentdetox
71
+ ```
72
+
73
+ To run the latest code from `main` instead:
74
+ `uv tool install git+https://github.com/chriswu727/agentdetox`.
75
+
76
+ Then install the Claude Code plugin, from inside Claude Code:
77
+
78
+ ```text
79
+ /plugin install agentdetox --marketplace chriswu727/agentdetox
80
+ ```
81
+
82
+ or add the same hooks to your settings directly:
83
+
84
+ ```bash
85
+ agentdetox install claude-code --apply # adds three hooks to ~/.claude/settings.json, backed up first
86
+ ```
87
+
88
+ Restart Claude Code to load the hooks. Use one route, not both: `install` declines when the plugin
89
+ is enabled, and `agentdetox doctor` warns if the hooks are registered twice. The plugin runs the
90
+ `agentdetox` you installed; without one it runs its own release through `uvx`, and with neither it
91
+ says so at session start instead of failing on every tool call.
92
+
93
+ A tool install keeps AgentDetox in an environment of its own; `pip install agentdetox` works too.
94
+ If you move Claude Code's folder with `CLAUDE_CONFIG_DIR`, AgentDetox follows it. The first session
95
+ scans what is already there before trusting it; `agentdetox agent scan` does the same at any time.
96
+
97
+ ## What it does in Claude Code
98
+
99
+ - **Before a write** to memory, CLAUDE.md, rules, skills or config (through Write, Edit or a shell
100
+ command such as a redirect, `tee`, `sed -i`, `cp`, `mv` or `rm`), the new content is checked.
101
+ Planted instructions are denied, with the reason sent to the agent. If the flagged text is
102
+ something you typed or pasted yourself, you are asked instead (hidden text is still denied, since
103
+ you could not see what you pasted). An agent changing its own hooks,
104
+ MCP servers or permissions needs your approval, and the prompt says exactly what is added or
105
+ removed.
106
+ - **After the write**, the file is trusted as it now is, and the change is recorded with where it
107
+ came from.
108
+ - **At session start**, files changed outside the agent since the last session are compared with
109
+ the snapshot. Suspicious changes are reported to the agent, which is told not to follow them and
110
+ to tell you, with the command that restores each file. Edits you made yourself that look benign
111
+ are accepted silently, and recorded as made outside the agent.
112
+
113
+ **Where memory came from.** A memory write that is mostly copied from web pages, MCP data, command
114
+ output or files the agent just read asks for approval, unless you asked the agent to remember it.
115
+ This is how text planted in an email or page usually reaches memory. Text the model rewrites in its
116
+ own words is not caught this way.
117
+
118
+ **What the rules look for** is what a benign memory almost never asks for: running code fetched
119
+ from the network, reverse shells, loosening the agent's own approvals or sandbox, reading
120
+ credentials, acting silently, standing orders to fetch a URL every session, hidden text, and
121
+ injection phrasing. Memory that points package installs at a registry other than the default ones
122
+ and the big public mirrors asks rather than blocks, since teams do run private registries. English
123
+ and Chinese.
124
+
125
+ **Cleaning up.** `agentdetox agent verify` shows what changed and why it is suspicious;
126
+ `agentdetox agent restore <file>` puts back the last trusted version and moves the current one to
127
+ `~/.agentdetox/quarantine`, never deleting it. Snapshot copies that no longer back any file are
128
+ deleted once a day (`agentdetox agent gc` does it now); quarantined files are kept. If the snapshot
129
+ list itself is damaged, or deleted while its copies remain, AgentDetox says so at every session
130
+ instead of quietly trusting whatever is on disk, until you check and run
131
+ `agentdetox agent snapshot --reset`.
132
+
133
+ ## Day to day
134
+
135
+ ```bash
136
+ agentdetox status # health, what is watched, how checks are set, recent activity
137
+ agentdetox events # everything blocked, asked, recorded or noticed
138
+ agentdetox log # every change to memory, rules and config, and where it came from
139
+ agentdetox blame ~/.claude/CLAUDE.md # where each line of a file came from
140
+ agentdetox allow 3f9c2a1b7e # stop flagging something you reviewed
141
+ agentdetox config copied record # change a check
142
+ agentdetox doctor # check the hooks are installed, current and runnable
143
+ ```
144
+
145
+ Each change AgentDetox sees is recorded with its origin: your own words, the agent, a web page or
146
+ tool output the agent copied (named), or a change made outside the agent's edit tools.
147
+ `agentdetox blame <file>` shows that per line; lines from before AgentDetox was installed show as
148
+ "earlier", and lines changed since the last recorded change as "unrecorded".
149
+
150
+ Every message from AgentDetox carries an id. If what it flagged is fine, `agentdetox allow <id>`
151
+ stops it being flagged again in write checks, session notices and scans alike. An id stands for the
152
+ flagged line, the pair of contradicting entries, or the source text was copied from (that file, page
153
+ or command), not for one occurrence; `agentdetox allow --list` and `--remove` review and undo.
154
+
155
+ Settings live in `~/.agentdetox/config.toml` (`agentdetox config` shows them). Each check is `ask`,
156
+ `record` (the write goes through and is logged) or `off`; agents without an ask prompt record
157
+ instead of asking. An `AGENTDETOX_<KEY>` environment variable overrides the file.
158
+
159
+ | Key | Default | Check |
160
+ |---|---|---|
161
+ | `suspicious` | ask | writes that look like planted instructions but are not clear-cut |
162
+ | `copied` | ask | memory copied from untrusted input the agent just read |
163
+ | `contradiction` | record | new memory that contradicts existing memory (experimental, below) |
164
+
165
+ The settings, the allowlist and the snapshots are guarded like the agent's own config: an agent
166
+ writing under `~/.agentdetox`, deleting it, or running `agentdetox allow`, `config <key> <value>`,
167
+ `install`, `uninstall`, `agent snapshot` or `agent restore` needs your approval, or an injected
168
+ instruction could switch a check off or allow itself.
169
+
170
+ Hooks fail open: if AgentDetox itself errors, the agent's action proceeds. If an attacker deletes
171
+ the hooks, the session-start check cannot run; `agentdetox agent verify` (by hand or in CI) still
172
+ reports the removal.
173
+
174
+ ## What it does not catch
175
+
176
+ - Detection is not a security boundary. An adaptive attacker who knows these rules can evade them,
177
+ and a plausible-looking false fact with nothing to contradict passes.
178
+ - Planted text the model rewrites in its own words before saving it, unless a rule still matches.
179
+ - A change made outside the agent is reported at the next session, not the moment it happens.
180
+ - The ledger knows what the hooks saw. A change the agent makes with a shell command is recorded as
181
+ made outside the agent, and text it copied in its own words as the agent's.
182
+ - The guard on AgentDetox's own settings recognises plain commands and file writes. An agent that
183
+ can run arbitrary code can still change them (a script that edits `~/.agentdetox` is not
184
+ inspected), and nothing stops a person or process outside the agent.
185
+ - On Windows the shell checks understand Git Bash commands (`/c/...` paths, `agentdetox.exe`), not
186
+ PowerShell or cmd.exe syntax.
187
+
188
+ ## Other agents
189
+
190
+ | Agent | Files watched | Write checks | Tamper detection |
191
+ |---|---|---|---|
192
+ | Claude Code | CLAUDE.md, CLAUDE.local.md, `.claude/rules`, AGENTS.md, auto memory, skills, subagents, commands, settings, `.mcp.json`, `~/.claude.json` | yes | yes |
193
+ | Codex | AGENTS(.override).md, `~/.codex/memories`, `.agents/skills`, `config.toml`, `hooks.json`, `rules/*.rules` | experimental (`install codex` prints the hook) | yes |
194
+ | Cursor | `.cursor/rules`, `.cursorrules`, AGENTS.md, `.cursor/skills`, `mcp.json`, `hooks.json` | experimental; Tab edits cannot be checked | yes |
195
+ | OpenClaw | AGENTS, SOUL, USER, IDENTITY, BOOT, MEMORY, daily memory, skills, `openclaw.json`, extensions | not yet (needs a plugin) | yes |
196
+
197
+ Codex and Cursor cannot ask mid-task: checks set to `ask` record instead, and changes to the
198
+ agent's own hooks, MCP servers or permissions are blocked. The provenance check needs Claude Code's
199
+ session transcript.
200
+
201
+ ## Contradiction detection (experimental)
202
+
203
+ Optional: `uv tool install 'agentdetox[nli]'`, then `agentdetox fetch-models`. A memory write that
204
+ contradicts what the agent already knows is a common sign of a fabricated fact. This uses a
205
+ multilingual NLI model (mDeBERTa-v3, about 560 MB) and **records instead of asking**: after a
206
+ write, the hook hands the check to a background model server and returns (about 0.1 s), and
207
+ conflicts are logged (`agentdetox events`). The server loads the model on first use (about 1 GB of
208
+ memory) and exits after 30 minutes without work; `agentdetox daemon status` shows it. On real memory
209
+ the model still misreads fragments of notes as contradicting each other, which is why it does not
210
+ ask; `agentdetox config contradiction ask` makes it ask. Windows has no model server yet: each
211
+ related write starts a background worker that loads the model (about 6 s). The
212
+ [roadmap](https://github.com/chriswu727/agentdetox/blob/main/ROADMAP.md) replaces this model with
213
+ an LLM judge. Measurements:
214
+ [bench/MEMORY_BENCHMARK.md](https://github.com/chriswu727/agentdetox/blob/main/bench/MEMORY_BENCHMARK.md).
215
+
216
+ ## Also: scanning a RAG knowledge base
217
+
218
+ ```bash
219
+ pip install 'agentdetox[st]' # drop [st] for a numpy-only install
220
+ agentdetox scan ./knowledge_base # directory of .txt/.md/.html, or a .jsonl export
221
+ agentdetox trace kb.jsonl -q "Who founded Acme?" -a "Bob Smith"
222
+ ```
223
+
224
+ Try it on the bundled example: `agentdetox scan examples/kb`.
225
+
226
+ | Detector | What it catches | How |
227
+ |---|---|---|
228
+ | `hidden-unicode` | ASCII smuggled in invisible Unicode tag characters (decoded and shown), zero-width characters splitting words, bidi controls | deterministic |
229
+ | `hidden-html` | Text hidden with `display:none`, `font-size:0`, white text or `hidden`, and HTML comments that carry instructions | deterministic |
230
+ | `injection-phrase` | Instruction overrides, "don't tell the user", chat-template tokens, exfiltration instructions with a destination, text addressed to the AI. English and Chinese | deterministic |
231
+ | `exfil-link` | Markdown images whose URL has a templated query, the zero-click exfiltration channel behind EchoLeak-style attacks | deterministic |
232
+ | `question-template` | Three or more different passages that open with the same question, which is how PoisonedRAG's black-box attack builds its passages | deterministic |
233
+ | `poison-cluster` | Groups of mutually similar passages written to push one false answer, even without the question prefix | CleanBase clique detection |
234
+ | `duplicate-text` | The exact same text repeated many times | deterministic |
235
+
236
+ `scan` exits with status 1 when any finding is at least `--fail-on` severity (default `high`),
237
+ so it can gate ingestion in CI. A similarity clique on its own is reported as `medium`, because
238
+ legitimately repetitive content (versioned docs, templated pages) also forms cliques; it becomes
239
+ `high` only for members that also open with a question or carry injection markers. Use
240
+ `--fail-on medium` if you want paraphrase-only poisoning to block ingestion too.
241
+
242
+ `poison-cluster` implements CleanBase (Jin et al., arXiv 2605.00460): a k-nearest-neighbour
243
+ cosine-similarity graph (k=10), edges kept above mu + 2.5 sigma of a 50% edge sample, and every
244
+ passage in a clique of 3 or more flagged; exact duplicates are collapsed first. `trace` takes a
245
+ question and the wrong answer your system gave, ranks the top `4 x top-k` passages by the mean
246
+ z-score of similarity to the question, similarity to the wrong answer and whether the passage
247
+ contains the answer, and splits them with 1-D 2-means. It is a cheap approximation of RAGOrigin
248
+ (Zhang et al., IEEE S&P 2026); treat its output as a ranked lead list.
249
+
250
+ Limits: `poison-cluster` cannot see an attacker who plants only one or two passages per target
251
+ question or makes them deliberately dissimilar, and needs a few hundred passages to estimate its
252
+ threshold; `injection-phrase` will flag security documentation that quotes attacks; brute-force kNN
253
+ is fine for tens of thousands of passages, not millions. Measurements against PoisonedRAG and
254
+ other scanners:
255
+ [bench/KB_BENCHMARK.md](https://github.com/chriswu727/agentdetox/blob/main/bench/KB_BENCHMARK.md).
256
+
257
+ ## Development
258
+
259
+ ```bash
260
+ ./scripts/check.sh # lint, type checks and tests: what every change must pass
261
+ ./scripts/update.sh # the checks, then update the installed agentdetox, then `agentdetox doctor`
262
+ ```
263
+
264
+ [CONTRIBUTING.md](https://github.com/chriswu727/agentdetox/blob/main/CONTRIBUTING.md) says where
265
+ each kind of change belongs, [docs/architecture.md](https://github.com/chriswu727/agentdetox/blob/main/docs/architecture.md)
266
+ how the pieces fit, and [docs/decisions.md](https://github.com/chriswu727/agentdetox/blob/main/docs/decisions.md)
267
+ why. [SECURITY.md](https://github.com/chriswu727/agentdetox/blob/main/SECURITY.md) covers reporting
268
+ and the threat model, [PRIVACY.md](https://github.com/chriswu727/agentdetox/blob/main/PRIVACY.md)
269
+ what is stored and read.
270
+
271
+ `lab/` replays attacks and everyday use against real Claude Code sessions, with and without
272
+ AgentDetox ([lab/README.md](https://github.com/chriswu727/agentdetox/blob/main/lab/README.md));
273
+ its latest results are in `lab/results/latest.md`.
274
+
275
+ How a hook call flows: `adapters.py` turns an agent's event into a planned write or a shell
276
+ command, `engine.py` runs the checks and reports signals, `policy.py` decides from your settings
277
+ and the agent's abilities whether to block, ask or record, and the adapter formats the reply.
278
+ `hooks.py` wires these together, and `session.py` runs the session-start check. The engine knows
279
+ nothing about agents, so a new integration needs only an adapter. The detectors are in
280
+ `hidden.py` and `persistence.py` (rules), `provenance.py` (copied text) and `consistency.py`
281
+ (contradictions). The snapshot store is in `integrity.py`, the ledger in `ledger.py`, and the model
282
+ server in `daemon.py`.
283
+
284
+ Installed hooks call a fixed executable, so updating it takes effect on the next hook call with
285
+ no reinstall; a model server still running the old build is stopped by `update.sh`, and otherwise
286
+ replaces itself on its next job. Re-run `agentdetox install claude-code --apply` only when
287
+ `agentdetox doctor` says the registered hooks are from an older version. To remove everything:
288
+ `/plugin uninstall agentdetox` or `agentdetox uninstall claude-code`, `agentdetox daemon stop`,
289
+ then `uv tool uninstall agentdetox`, then delete `~/.agentdetox` (this also deletes quarantined
290
+ files).
291
+
292
+ The Claude Code plugin lives in `plugins/agentdetox` and the marketplace entry in
293
+ `.claude-plugin/marketplace.json`; `claude plugin validate .` checks both. Its version, the version
294
+ its hook script pins, and the package version must match (`tests/test_plugin.py`).
295
+
296
+ ## License
297
+
298
+ Apache-2.0