@hasna/hooks 0.2.20 → 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/README.md +45 -2
- package/bin/index.js +1157 -84
- package/dist/db/migrations/002_session_events.d.ts +10 -0
- package/dist/db/migrations/003_user_prompt_submit_event.d.ts +10 -0
- package/dist/db/pg-migrations.d.ts +1 -1
- package/dist/db/remote-storage.d.ts +9 -0
- package/dist/db/schema.d.ts +4 -3
- package/dist/db/storage-sync.d.ts +57 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.js +1042 -58
- package/dist/lib/installer.d.ts +25 -5
- package/dist/lib/registry.d.ts +3 -1
- package/dist/storage.d.ts +4 -0
- package/dist/storage.js +696 -0
- package/hooks/codewith-native-common.test.ts +61 -0
- package/hooks/codewith-native-common.ts +478 -0
- package/hooks/hook-agent-rules-version-check/LICENSE +191 -0
- package/hooks/hook-agent-rules-version-check/README.md +50 -0
- package/hooks/hook-agent-rules-version-check/package.json +53 -0
- package/hooks/hook-agent-rules-version-check/src/hook.test.ts +104 -0
- package/hooks/hook-agent-rules-version-check/src/hook.ts +215 -0
- package/hooks/hook-agent-rules-version-check/tsconfig.json +25 -0
- package/hooks/hook-announce-start/README.md +7 -5
- package/hooks/hook-announce-start/package.json +2 -2
- package/hooks/hook-announce-start/src/hook.ts +33 -14
- package/hooks/hook-fleet-blockers-gate/LICENSE +191 -0
- package/hooks/hook-fleet-blockers-gate/README.md +39 -0
- package/hooks/hook-fleet-blockers-gate/package.json +52 -0
- package/hooks/hook-fleet-blockers-gate/src/hook.test.ts +106 -0
- package/hooks/hook-fleet-blockers-gate/src/hook.ts +242 -0
- package/hooks/hook-fleet-blockers-gate/tsconfig.json +25 -0
- package/hooks/hook-fleet-catchup/LICENSE +191 -0
- package/hooks/hook-fleet-catchup/README.md +40 -0
- package/hooks/hook-fleet-catchup/package.json +52 -0
- package/hooks/hook-fleet-catchup/src/hook.test.ts +156 -0
- package/hooks/hook-fleet-catchup/src/hook.ts +283 -0
- package/hooks/hook-fleet-catchup/tsconfig.json +25 -0
- package/hooks/pre-bash/README.md +17 -0
- package/hooks/pre-bash/package.json +12 -0
- package/hooks/pre-bash/src/hook.ts +103 -0
- package/hooks/prompt-guard/README.md +17 -0
- package/hooks/prompt-guard/package.json +12 -0
- package/hooks/prompt-guard/src/hook.ts +76 -0
- package/hooks/session-start/README.md +17 -0
- package/hooks/session-start/package.json +12 -0
- package/hooks/session-start/src/hook.ts +150 -0
- package/hooks/stop-sync/README.md +17 -0
- package/hooks/stop-sync/package.json +12 -0
- package/hooks/stop-sync/src/hook.ts +69 -0
- package/hooks/worktree-guard/README.md +17 -0
- package/hooks/worktree-guard/package.json +12 -0
- package/hooks/worktree-guard/src/hook.ts +88 -0
- package/package.json +14 -6
- package/dist/cli/components/App.d.ts +0 -7
- package/dist/cli/components/CategorySelect.d.ts +0 -7
- package/dist/cli/components/DataTable.d.ts +0 -11
- package/dist/cli/components/Header.d.ts +0 -7
- package/dist/cli/components/HookSelect.d.ts +0 -11
- package/dist/cli/components/InstallProgress.d.ts +0 -9
- package/dist/cli/components/SearchView.d.ts +0 -9
- package/dist/cli/index.d.ts +0 -2
- package/dist/lib/db-writer.d.ts +0 -9
- package/dist/mcp/http.d.ts +0 -16
- package/dist/mcp/server.d.ts +0 -17
|
@@ -0,0 +1,191 @@
|
|
|
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 the 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 the 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 any 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
|
+
Copyright 2025 Hasna
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# hook-agent-rules-version-check
|
|
2
|
+
|
|
3
|
+
A SessionStart hook that verifies the rendered Hasna agent operating rules are current on this machine, warning the agent when they drift. Part of the Hasna fleet comms workflow.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
hooks install agent-rules-version-check
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
On `SessionStart` it greps the rendered instruction artifacts for the managed-block sentinel:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<!-- hasna:agent-operating-rules v=X.Y.Z ... -->
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Default artifacts scanned (missing files are skipped silently):
|
|
20
|
+
|
|
21
|
+
- `~/.claude/CLAUDE.md` and `~/.claude/.hasna/instructions/*.md`
|
|
22
|
+
- `~/.codex/AGENTS.md`
|
|
23
|
+
- `~/.config/opencode/AGENTS.md`
|
|
24
|
+
- `~/.cursor/rules/hasna-global.mdc`
|
|
25
|
+
|
|
26
|
+
The expected version is resolved in order:
|
|
27
|
+
|
|
28
|
+
1. `HOOKS_RULES_EXPECTED_VERSION` env var (explicit pin)
|
|
29
|
+
2. the configs CLI stored copy: `configs show agent-operating-rules --format content` (bounded timeout, fail-open)
|
|
30
|
+
3. cross-artifact consistency — with no authoritative source, all rendered artifacts must agree
|
|
31
|
+
|
|
32
|
+
On mismatch it injects a WARNING via `hookSpecificOutput.additionalContext` telling the agent to re-sync before risky or fleet-affecting work. It never blocks the session, and it stays silent when no sentinel is rendered at all (distribution not yet rolled out on this machine).
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
export HOOKS_RULES_CHECK_DISABLE=1 # Kill switch — skip entirely
|
|
38
|
+
export HOOKS_RULES_EXPECTED_VERSION=1.2.0 # Explicit expected version
|
|
39
|
+
export HOOKS_RULES_CONFIG_SLUG=agent-operating-rules # configs entry to consult
|
|
40
|
+
export HOOKS_RULES_FILES="/a.md:/b.md" # Override scanned paths (colon-separated)
|
|
41
|
+
export HOOKS_FLEET_TIMEOUT_MS=500 # configs CLI timeout (default 500)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Requirements
|
|
45
|
+
|
|
46
|
+
- `configs` CLI (@hasna/configs) — optional; without it the hook falls back to cross-artifact consistency checking
|
|
47
|
+
|
|
48
|
+
## Event
|
|
49
|
+
|
|
50
|
+
- **SessionStart**
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hasna/hook-agent-rules-version-check",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude Code SessionStart hook that compares the rendered hasna:agent-operating-rules sentinel version against configs state and warns on drift",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/hook.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/hook.js",
|
|
10
|
+
"types": "./dist/hook.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "bun build ./src/hook.ts --outdir ./dist --target node",
|
|
19
|
+
"prepublishOnly": "bun run build",
|
|
20
|
+
"typecheck": "tsc --noEmit"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"claude-code",
|
|
24
|
+
"claude",
|
|
25
|
+
"hook",
|
|
26
|
+
"fleet",
|
|
27
|
+
"rules",
|
|
28
|
+
"version",
|
|
29
|
+
"sentinel",
|
|
30
|
+
"drift",
|
|
31
|
+
"configs",
|
|
32
|
+
"agent-teams"
|
|
33
|
+
],
|
|
34
|
+
"author": "Hasna",
|
|
35
|
+
"license": "Apache-2.0",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/hasna/hooks.git"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"registry": "https://registry.npmjs.org/"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18",
|
|
46
|
+
"bun": ">=1.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/bun": "^1.3.8",
|
|
50
|
+
"@types/node": "^20",
|
|
51
|
+
"typescript": "^5.0.0"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { mkdtempSync, writeFileSync, rmSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { extractSentinelVersion, scanArtifacts, evaluate, defaultArtifactPaths } from "./hook";
|
|
6
|
+
|
|
7
|
+
describe("hook-agent-rules-version-check", () => {
|
|
8
|
+
describe("extractSentinelVersion", () => {
|
|
9
|
+
test("extracts version from a full sentinel", () => {
|
|
10
|
+
expect(
|
|
11
|
+
extractSentinelVersion("x\n<!-- hasna:agent-operating-rules v=1.2.3 sha256=abc123 -->\ny")
|
|
12
|
+
).toBe("1.2.3");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("extracts version without sha", () => {
|
|
16
|
+
expect(extractSentinelVersion("<!-- hasna:agent-operating-rules v=0.9.0 -->")).toBe("0.9.0");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("tolerates extra whitespace", () => {
|
|
20
|
+
expect(extractSentinelVersion("<!-- hasna:agent-operating-rules v=2.0.0 -->")).toBe("2.0.0");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("returns null when no sentinel present", () => {
|
|
24
|
+
expect(extractSentinelVersion("# just a doc\nno sentinel here")).toBeNull();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("does not match other hasna sentinels", () => {
|
|
28
|
+
expect(extractSentinelVersion("<!-- hasna:hacp v=1.0.0 -->")).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("scanArtifacts", () => {
|
|
33
|
+
test("collects sentinel hits and skips missing/plain files", () => {
|
|
34
|
+
const dir = mkdtempSync(join(tmpdir(), "rules-check-"));
|
|
35
|
+
try {
|
|
36
|
+
const withSentinel = join(dir, "a.md");
|
|
37
|
+
const withoutSentinel = join(dir, "b.md");
|
|
38
|
+
writeFileSync(withSentinel, "<!-- hasna:agent-operating-rules v=1.0.0 -->");
|
|
39
|
+
writeFileSync(withoutSentinel, "# no sentinel");
|
|
40
|
+
|
|
41
|
+
const hits = scanArtifacts([withSentinel, withoutSentinel, join(dir, "missing.md")]);
|
|
42
|
+
expect(hits).toHaveLength(1);
|
|
43
|
+
expect(hits[0]).toEqual({ file: withSentinel, version: "1.0.0" });
|
|
44
|
+
} finally {
|
|
45
|
+
rmSync(dir, { recursive: true, force: true });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("defaultArtifactPaths", () => {
|
|
51
|
+
test("covers the delivery-matrix runtimes", () => {
|
|
52
|
+
const paths = defaultArtifactPaths("/home/u");
|
|
53
|
+
expect(paths).toContain("/home/u/.claude/CLAUDE.md");
|
|
54
|
+
expect(paths).toContain("/home/u/.codex/AGENTS.md");
|
|
55
|
+
expect(paths).toContain("/home/u/.config/opencode/AGENTS.md");
|
|
56
|
+
expect(paths).toContain("/home/u/.cursor/rules/hasna-global.mdc");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("evaluate", () => {
|
|
61
|
+
test("silent when nothing is rendered", () => {
|
|
62
|
+
expect(evaluate([], "1.0.0")).toBeNull();
|
|
63
|
+
expect(evaluate([], null)).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("silent when all artifacts match the expected version", () => {
|
|
67
|
+
const hits = [
|
|
68
|
+
{ file: "/a.md", version: "1.0.0" },
|
|
69
|
+
{ file: "/b.md", version: "1.0.0" },
|
|
70
|
+
];
|
|
71
|
+
expect(evaluate(hits, "1.0.0")).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("warns and lists stale artifacts on mismatch", () => {
|
|
75
|
+
const hits = [
|
|
76
|
+
{ file: "/a.md", version: "1.0.0" },
|
|
77
|
+
{ file: "/b.md", version: "0.9.0" },
|
|
78
|
+
];
|
|
79
|
+
const warning = evaluate(hits, "1.0.0");
|
|
80
|
+
expect(warning).toContain("OUT OF DATE");
|
|
81
|
+
expect(warning).toContain("/b.md: v0.9.0");
|
|
82
|
+
expect(warning).not.toContain("/a.md");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("without expected version, warns when artifacts disagree", () => {
|
|
86
|
+
const hits = [
|
|
87
|
+
{ file: "/a.md", version: "1.0.0" },
|
|
88
|
+
{ file: "/b.md", version: "1.1.0" },
|
|
89
|
+
];
|
|
90
|
+
const warning = evaluate(hits, null);
|
|
91
|
+
expect(warning).toContain("DISAGREE");
|
|
92
|
+
expect(warning).toContain("/a.md: v1.0.0");
|
|
93
|
+
expect(warning).toContain("/b.md: v1.1.0");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("without expected version, silent when artifacts agree", () => {
|
|
97
|
+
const hits = [
|
|
98
|
+
{ file: "/a.md", version: "1.0.0" },
|
|
99
|
+
{ file: "/b.md", version: "1.0.0" },
|
|
100
|
+
];
|
|
101
|
+
expect(evaluate(hits, null)).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Claude Code Hook: agent-rules-version-check
|
|
5
|
+
*
|
|
6
|
+
* SessionStart hook that verifies the rendered Hasna agent operating rules
|
|
7
|
+
* are current on this machine. It greps the rendered instruction artifacts
|
|
8
|
+
* for the managed-block sentinel:
|
|
9
|
+
*
|
|
10
|
+
* <!-- hasna:agent-operating-rules v=X.Y.Z ... -->
|
|
11
|
+
*
|
|
12
|
+
* and compares the version against the expected state:
|
|
13
|
+
* 1. HOOKS_RULES_EXPECTED_VERSION env var (explicit pin), else
|
|
14
|
+
* 2. the configs CLI stored copy (`configs show <slug> --format content`), else
|
|
15
|
+
* 3. cross-file consistency (all rendered artifacts must agree).
|
|
16
|
+
*
|
|
17
|
+
* On mismatch it WARNS via SessionStart additionalContext — it never blocks
|
|
18
|
+
* the session (fail-open, deterministic commands only, bounded timeouts).
|
|
19
|
+
*
|
|
20
|
+
* Environment:
|
|
21
|
+
* - HOOKS_RULES_CHECK_DISABLE=1 → skip entirely
|
|
22
|
+
* - HOOKS_RULES_EXPECTED_VERSION=x.y.z → explicit expected version
|
|
23
|
+
* - HOOKS_RULES_CONFIG_SLUG=<slug> → configs entry holding the canonical block
|
|
24
|
+
* (default: agent-operating-rules)
|
|
25
|
+
* - HOOKS_RULES_FILES=<a:b:c> → override the scanned artifact paths
|
|
26
|
+
* - HOOKS_FLEET_TIMEOUT_MS=<n> → configs CLI exec timeout (default 500)
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { readFileSync, existsSync, readdirSync } from "fs";
|
|
30
|
+
import { join } from "path";
|
|
31
|
+
import { homedir } from "os";
|
|
32
|
+
import { execSync } from "child_process";
|
|
33
|
+
|
|
34
|
+
interface HookInput {
|
|
35
|
+
session_id: string;
|
|
36
|
+
cwd: string;
|
|
37
|
+
hook_event_name: string;
|
|
38
|
+
source?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface HookOutput {
|
|
42
|
+
continue: boolean;
|
|
43
|
+
hookSpecificOutput?: {
|
|
44
|
+
hookEventName: "SessionStart";
|
|
45
|
+
additionalContext: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SentinelHit {
|
|
50
|
+
file: string;
|
|
51
|
+
version: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const SENTINEL_RE = /<!--\s*hasna:agent-operating-rules\s+v=([^\s>]+)[^>]*-->/;
|
|
55
|
+
const DEFAULT_TIMEOUT_MS = 500;
|
|
56
|
+
|
|
57
|
+
function readStdinJson(): HookInput | null {
|
|
58
|
+
try {
|
|
59
|
+
const input = readFileSync(0, "utf-8").trim();
|
|
60
|
+
if (!input) return null;
|
|
61
|
+
return JSON.parse(input);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function respond(output: HookOutput): void {
|
|
68
|
+
console.log(JSON.stringify(output));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function timeoutMs(): number {
|
|
72
|
+
const raw = Number(process.env.HOOKS_FLEET_TIMEOUT_MS);
|
|
73
|
+
return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_TIMEOUT_MS;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Extract the sentinel version from rendered content, or null when absent. */
|
|
77
|
+
export function extractSentinelVersion(content: string): string | null {
|
|
78
|
+
const match = SENTINEL_RE.exec(content);
|
|
79
|
+
return match ? match[1] : null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The rendered instruction artifacts checked by default (per delivery matrix). */
|
|
83
|
+
export function defaultArtifactPaths(home: string): string[] {
|
|
84
|
+
const paths = [
|
|
85
|
+
join(home, ".claude", "CLAUDE.md"),
|
|
86
|
+
join(home, ".codex", "AGENTS.md"),
|
|
87
|
+
join(home, ".config", "opencode", "AGENTS.md"),
|
|
88
|
+
join(home, ".cursor", "rules", "hasna-global.mdc"),
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
// Claude Code managed instruction files live in a directory; include all .md files.
|
|
92
|
+
const claudeInstructionsDir = join(home, ".claude", ".hasna", "instructions");
|
|
93
|
+
try {
|
|
94
|
+
if (existsSync(claudeInstructionsDir)) {
|
|
95
|
+
for (const entry of readdirSync(claudeInstructionsDir)) {
|
|
96
|
+
if (entry.endsWith(".md")) paths.push(join(claudeInstructionsDir, entry));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
// unreadable dir → skip
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return paths;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function artifactPaths(): string[] {
|
|
107
|
+
const override = process.env.HOOKS_RULES_FILES;
|
|
108
|
+
if (override) {
|
|
109
|
+
return override.split(":").map((p) => p.trim()).filter(Boolean);
|
|
110
|
+
}
|
|
111
|
+
return defaultArtifactPaths(homedir());
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Scan artifacts for sentinels. Files that do not exist are skipped silently. */
|
|
115
|
+
export function scanArtifacts(paths: string[]): SentinelHit[] {
|
|
116
|
+
const hits: SentinelHit[] = [];
|
|
117
|
+
for (const path of paths) {
|
|
118
|
+
try {
|
|
119
|
+
if (!existsSync(path)) continue;
|
|
120
|
+
const content = readFileSync(path, "utf-8");
|
|
121
|
+
const version = extractSentinelVersion(content);
|
|
122
|
+
if (version) hits.push({ file: path, version });
|
|
123
|
+
} catch {
|
|
124
|
+
// unreadable file → skip (fail-open)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return hits;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Expected version from configs state; null when unavailable (fail-open). */
|
|
131
|
+
function expectedFromConfigs(): string | null {
|
|
132
|
+
const slug = process.env.HOOKS_RULES_CONFIG_SLUG || "agent-operating-rules";
|
|
133
|
+
if (!/^[A-Za-z0-9._-]{1,80}$/.test(slug)) return null;
|
|
134
|
+
try {
|
|
135
|
+
const out = execSync(`configs show ${slug} --format content`, {
|
|
136
|
+
encoding: "utf-8",
|
|
137
|
+
timeout: timeoutMs(),
|
|
138
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
139
|
+
});
|
|
140
|
+
return extractSentinelVersion(out);
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Compare rendered sentinels against the expected version.
|
|
148
|
+
* Returns a warning string, or null when everything is current/unknowable.
|
|
149
|
+
*/
|
|
150
|
+
export function evaluate(hits: SentinelHit[], expected: string | null): string | null {
|
|
151
|
+
if (hits.length === 0) {
|
|
152
|
+
// Rules not rendered on this machine (or runtime not installed) — nothing
|
|
153
|
+
// to compare. Distribution is a separate rollout task; stay silent.
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const versions = [...new Set(hits.map((h) => h.version))];
|
|
158
|
+
|
|
159
|
+
if (expected) {
|
|
160
|
+
const stale = hits.filter((h) => h.version !== expected);
|
|
161
|
+
if (stale.length > 0) {
|
|
162
|
+
return (
|
|
163
|
+
`Agent operating rules are OUT OF DATE on this machine (expected v${expected}):\n` +
|
|
164
|
+
stale.map((h) => `- ${h.file}: v${h.version}`).join("\n") +
|
|
165
|
+
`\nRe-sync before doing risky or fleet-affecting work.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// No authoritative expected version — enforce cross-artifact consistency.
|
|
172
|
+
if (versions.length > 1) {
|
|
173
|
+
return (
|
|
174
|
+
`Agent operating rules DISAGREE across rendered artifacts (no configs state available):\n` +
|
|
175
|
+
hits.map((h) => `- ${h.file}: v${h.version}`).join("\n") +
|
|
176
|
+
`\nRe-render the rules so every runtime carries the same version.`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function run(): void {
|
|
184
|
+
if (process.env.HOOKS_RULES_CHECK_DISABLE === "1") {
|
|
185
|
+
respond({ continue: true });
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const input = readStdinJson();
|
|
190
|
+
if (!input) {
|
|
191
|
+
respond({ continue: true });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const hits = scanArtifacts(artifactPaths());
|
|
196
|
+
const expected = process.env.HOOKS_RULES_EXPECTED_VERSION || expectedFromConfigs();
|
|
197
|
+
const warning = evaluate(hits, expected || null);
|
|
198
|
+
|
|
199
|
+
if (warning) {
|
|
200
|
+
respond({
|
|
201
|
+
continue: true,
|
|
202
|
+
hookSpecificOutput: {
|
|
203
|
+
hookEventName: "SessionStart",
|
|
204
|
+
additionalContext: `[hook-agent-rules-version-check] WARNING: ${warning}`,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
respond({ continue: true });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (import.meta.main) {
|
|
214
|
+
run();
|
|
215
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ESNext"],
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"allowImportingTsExtensions": true,
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"noUnusedLocals": true,
|
|
16
|
+
"noUnusedParameters": true,
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"declarationMap": true,
|
|
19
|
+
"outDir": "./dist",
|
|
20
|
+
"rootDir": "./src",
|
|
21
|
+
"types": ["bun-types"]
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist"]
|
|
25
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# hook-announce-start
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A SessionStart hook that fires when a session starts. Registers the agent profile, reads unread DMs/context and injects them into session context, and announces the agent's presence to the team space.
|
|
4
|
+
|
|
5
|
+
Previously bound to `Notification` (which does not fire at session start); as of 0.2.0 it binds to `SessionStart`. Reinstalling via `hooks install announce-start` migrates the settings entry to the new event automatically.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -10,12 +12,12 @@ hooks install announce-start
|
|
|
10
12
|
|
|
11
13
|
## How it works
|
|
12
14
|
|
|
13
|
-
On the
|
|
15
|
+
On the `SessionStart` event:
|
|
14
16
|
1. **Registers profile** — runs `hooks init` to ensure agent profile exists
|
|
15
|
-
2. **Fetches context** — runs `conversations context` to get unread DMs, online agents, recent activity
|
|
17
|
+
2. **Fetches context** — runs `conversations context` to get unread DMs, online agents, recent activity, and injects it via `hookSpecificOutput.additionalContext`
|
|
16
18
|
3. **Announces** — sends a start message to the configured conversation space
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
SessionStart also fires on resume/clear/compact; a marker file in `/tmp` ensures the announcement happens only once per session.
|
|
19
21
|
|
|
20
22
|
## Configuration
|
|
21
23
|
|
|
@@ -32,4 +34,4 @@ Optional but recommended:
|
|
|
32
34
|
|
|
33
35
|
## Event
|
|
34
36
|
|
|
35
|
-
- **
|
|
37
|
+
- **SessionStart** (announces once per session)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/hook-announce-start",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Claude Code hook that registers agent, reads messages, and announces to space on session start",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Claude Code SessionStart hook that registers agent, reads messages, and announces to space on session start",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/hook.js",
|
|
7
7
|
"exports": {
|