@veda-hq/kavach 0.0.0-stage → 0.3.0

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,108 @@
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.3: 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. 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
+ ## Refusal log
90
+
91
+ Each refusal of the two Python guards appends a row (time, guard, tool, first
92
+ line of the reason, start of the command) to
93
+ `<git common dir>/kavach/refusals.jsonl`. It is best effort and never changes
94
+ a verdict; `KAVACH_REFUSAL_LOG_OFF=1` turns it off. The rule-check engine
95
+ keeps its own fire log (`--fires-dir`).
96
+
97
+ ## Rule checks
98
+
99
+ ```sh
100
+ kavach-rule-checks --checks rules/checks.ts --registry rules/registry.json # the hook
101
+ kavach-rule-checks count --registry rules/registry.json --json # rules with no enforcer
102
+ kavach-rule-checks latency --rounds 5 # time the Bash hooks
103
+ ```
104
+
105
+ A checks module exports `CHECKS`, a list of `Check` (`@veda-hq/kavach/rule-checks`):
106
+ an id, the events and tools it applies to, and a function returning a
107
+ refusal, a warning or nothing. A Bash command passes one named check when it
108
+ ends with `# rule-pass <id>: <reason>`; the pass is logged.