@veda-hq/kavach 0.0.0-stage → 0.5.6

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Veda Authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,135 @@
1
- # Temporary Holding Version
1
+ # @veda-hq/kavach
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ कवच, armour. Guards for coding agents: each tool call an agent makes is
4
+ judged before it runs. Part of [Veda](https://github.com/veda-hq/veda).
5
+
6
+ ## v0.5: three guards and a rule-check engine
7
+
8
+ | Guard | Refuses | Entry point |
9
+ | --- | --- | --- |
10
+ | Worktree boundary | Any write outside the agent's own git worktree: a Bash command, an Edit or a Write; and, while a push runs, any write into it (below). Read-only commands pass from any checkout. | `python3 python/kavach/worktree_guard.py` |
11
+ | Command guard | Git-hook bypasses (`--no-verify`, `core.hooksPath`…), history-destroying pushes, and the project's own command rules (settings below). | `python3 python/kavach/command_guard.py` |
12
+ | Rule checks | The project's own checks, written in TypeScript, refusing or warning with the rule's reason. | `kavach-rule-checks` (`dist/rule-checks/cli.js`) |
13
+
14
+ The two Python guards read one Claude Code `PreToolUse` event on stdin. Exit
15
+ 0 allows; exit 2 refuses, with `BLOCKED: <reason>` on stderr. Every other end
16
+ (a crash, a missing module, unreadable settings) refuses: Claude Code lets a
17
+ hook's exit 1 through, so a guard that fails must not allow. The rule-check
18
+ engine is the opposite by design: a check that crashes lets the call go
19
+ ahead with a warning, so a broken rule never stops every session.
20
+
21
+ ```json
22
+ {
23
+ "hooks": {
24
+ "PreToolUse": [
25
+ { "matcher": "Bash|Edit|Write|MultiEdit|NotebookEdit",
26
+ "hooks": [{ "type": "command", "command": "python3 node_modules/@veda-hq/kavach/python/kavach/worktree_guard.py" }] },
27
+ { "matcher": "Bash",
28
+ "hooks": [{ "type": "command", "command": "python3 node_modules/@veda-hq/kavach/python/kavach/command_guard.py" }] }
29
+ ]
30
+ }
31
+ }
32
+ ```
33
+
34
+ Python 3.12 or later, standard library only. Node 24 or later. MIT.
35
+
36
+ ## What "read-only" means
37
+
38
+ A Bash command passes from any checkout when the worktree boundary can prove
39
+ every part of it writes nothing: pipes, `sort`, `uniq`, `awk` and `sed`
40
+ without writing options or programs, input redirection, `2>/dev/null`,
41
+ `tee /dev/null`, `$(...)` and `<(...)` whose bodies are themselves read-only,
42
+ `for` loops over literal words, `xargs` running a read-only command, `[`,
43
+ `test`, read subcommands of `git` and `gh` (`gh api` with GET only). When it
44
+ cannot prove a command, it treats it as writing, and from outside an owned
45
+ worktree that refuses. An expansion that could become an option (`git log
46
+ $X`, where `X=--output=/elsewhere`) leaves the command unproven.
47
+
48
+ `corpus/corpus.json` is the adversarial test set: commands that write outside
49
+ the worktree, bypass hooks or force-push, each executed in real bash to prove
50
+ it does what its row says, and near-miss commands that must pass. Each guard
51
+ must refuse every attack row and pass every near-miss.
52
+
53
+ ## Settings
54
+
55
+ `KAVACH_CONFIG` names a JSON file; unset, every project rule is off. A file
56
+ that cannot be read, is not JSON, or has an unknown key refuses every call.
57
+
58
+ ```json
59
+ {
60
+ "worktree": {
61
+ "trustedCommands": [
62
+ { "script": "scripts/worktree.py", "pattern": "python3 scripts/worktree\\.py (?:status|retire --pr [1-9][0-9]*)" }
63
+ ]
64
+ },
65
+ "command": {
66
+ "protectedVariables": ["AGENT_REMOTE"],
67
+ "protectedVariablesReason": "it marks a hosted session; leave it as the session set it",
68
+ "pairedChanges": [
69
+ { "changes": "schema.sql", "requires": "changesets/", "reason": "a schema change needs its changeset" }
70
+ ]
71
+ }
72
+ }
73
+ ```
74
+
75
+ - `trustedCommands`: a script in the repository that may run from the agent's
76
+ own worktree although its writes cannot be read off its command line. The
77
+ pattern must match the whole command; trust only command lines of plain
78
+ words (a whole line with any shell metacharacter is never trusted by its
79
+ pattern alone).
80
+ - `protectedVariables`: environment variables a command may not assign, declare or
81
+ export.
82
+ - `pairedChanges`: a commit that may record a path ending in `changes` must
83
+ also certainly record a path containing `requires`. With no entries the
84
+ rule is off, and a commit whose repository the guard cannot resolve is no
85
+ longer refused for that reason alone (`--no-verify` and the rest still are).
86
+ Keep a CI check behind it: the guard reads one command line, not the files
87
+ other programs change.
88
+
89
+ ## While a push runs
90
+
91
+ A push's checks read the worktree it was started from, and an edit made
92
+ while they run either fails the push at its end or leaves it checking what
93
+ was not pushed. So while a push runs, the worktree boundary refuses writes
94
+ into that worktree: any Bash command it cannot prove read-only, run there
95
+ (a trusted command included), and any Edit, Write or NotebookEdit of a path
96
+ git does not ignore. Reads still run, and the file tools can still write
97
+ files git ignores (drafts, scratch); a Bash command that writes one is still
98
+ refused. The refusal names the push's pid and start. Commands that only reach
99
+ the network, such as `gh pr comment`, wait too: the guard cannot tell what an
100
+ unproven command writes.
101
+
102
+ It knows a push is running from the marker the project's pre-push hook
103
+ writes. Write it when a real push starts and clear it on every exit:
104
+
105
+ ```sh
106
+ kavach-rule-checks push-marker write "$(git rev-parse --absolute-git-dir)" "$$"
107
+ trap 'kavach-rule-checks push-marker clear "$(git rev-parse --absolute-git-dir)" "$$"' EXIT
108
+ ```
109
+
110
+ Each push gets its own marker, `kavach-push-in-progress.<pid>.json` in the
111
+ worktree's own git folder, naming its pid and start. A marker whose pid has
112
+ ended or now belongs to a later process blocks nothing, so a killed push
113
+ leaves no lock behind, and a push in another worktree never blocks this one.
114
+ Without the marker, nothing here changes.
115
+
116
+ ## Refusal log
117
+
118
+ Each refusal of the two Python guards appends a row (time, guard, tool, first
119
+ line of the reason, start of the command) to
120
+ `<git common dir>/kavach/refusals.jsonl`. It is best effort and never changes
121
+ a verdict; `KAVACH_REFUSAL_LOG_OFF=1` turns it off. The rule-check engine
122
+ keeps its own fire log (`--fires-dir`).
123
+
124
+ ## Rule checks
125
+
126
+ ```sh
127
+ kavach-rule-checks --checks rules/checks.ts --registry rules/registry.json # the hook
128
+ kavach-rule-checks count --registry rules/registry.json --json # rules with no enforcer
129
+ kavach-rule-checks latency --rounds 5 # time the Bash hooks
130
+ ```
131
+
132
+ A checks module exports `CHECKS`, a list of `Check` (`@veda-hq/kavach/rule-checks`):
133
+ an id, the events and tools it applies to, and a function returning a
134
+ refusal, a warning or nothing. A Bash command passes one named check when it
135
+ ends with `# rule-pass <id>: <reason>`; the pass is logged.