@hasna/hooks 0.2.6 → 0.2.8

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 (49) hide show
  1. package/bin/index.js +645 -43
  2. package/dist/index.js +89 -5
  3. package/hooks/hook-affected-tests/LICENSE +191 -0
  4. package/hooks/hook-affected-tests/README.md +37 -0
  5. package/hooks/hook-affected-tests/package.json +50 -0
  6. package/hooks/hook-affected-tests/src/hook.ts +148 -0
  7. package/hooks/hook-affected-tests/tsconfig.json +25 -0
  8. package/hooks/hook-agentmessages/bin/cli.ts +125 -0
  9. package/hooks/hook-announce-start/LICENSE +191 -0
  10. package/hooks/hook-announce-start/README.md +35 -0
  11. package/hooks/hook-announce-start/package.json +51 -0
  12. package/hooks/hook-announce-start/src/hook.ts +137 -0
  13. package/hooks/hook-announce-start/tsconfig.json +25 -0
  14. package/hooks/hook-announce-stop/LICENSE +191 -0
  15. package/hooks/hook-announce-stop/README.md +35 -0
  16. package/hooks/hook-announce-stop/package.json +51 -0
  17. package/hooks/hook-announce-stop/src/hook.ts +160 -0
  18. package/hooks/hook-announce-stop/tsconfig.json +25 -0
  19. package/hooks/hook-checkdocs/bun.lock +25 -0
  20. package/hooks/hook-commandlog/src/hook.ts +15 -38
  21. package/hooks/hook-conflict-detect/LICENSE +191 -0
  22. package/hooks/hook-conflict-detect/README.md +38 -0
  23. package/hooks/hook-conflict-detect/package.json +50 -0
  24. package/hooks/hook-conflict-detect/src/hook.ts +116 -0
  25. package/hooks/hook-conflict-detect/tsconfig.json +25 -0
  26. package/hooks/hook-costwatch/src/hook.ts +39 -42
  27. package/hooks/hook-dm-inject/LICENSE +191 -0
  28. package/hooks/hook-dm-inject/README.md +42 -0
  29. package/hooks/hook-dm-inject/package.json +51 -0
  30. package/hooks/hook-dm-inject/src/hook.ts +115 -0
  31. package/hooks/hook-dm-inject/tsconfig.json +25 -0
  32. package/hooks/hook-errornotify/src/hook.ts +20 -65
  33. package/hooks/hook-failure-to-task/LICENSE +191 -0
  34. package/hooks/hook-failure-to-task/README.md +40 -0
  35. package/hooks/hook-failure-to-task/package.json +51 -0
  36. package/hooks/hook-failure-to-task/src/hook.ts +171 -0
  37. package/hooks/hook-failure-to-task/tsconfig.json +25 -0
  38. package/hooks/hook-filelock/LICENSE +191 -0
  39. package/hooks/hook-filelock/README.md +44 -0
  40. package/hooks/hook-filelock/package.json +50 -0
  41. package/hooks/hook-filelock/src/hook.ts +147 -0
  42. package/hooks/hook-filelock/tsconfig.json +25 -0
  43. package/hooks/hook-sessionlog/src/hook.ts +11 -52
  44. package/hooks/hook-typecheck-gate/LICENSE +191 -0
  45. package/hooks/hook-typecheck-gate/README.md +46 -0
  46. package/hooks/hook-typecheck-gate/package.json +50 -0
  47. package/hooks/hook-typecheck-gate/src/hook.ts +152 -0
  48. package/hooks/hook-typecheck-gate/tsconfig.json +25 -0
  49. package/package.json +1 -1
@@ -0,0 +1,44 @@
1
+ # hook-filelock
2
+
3
+ A PreToolUse hook that checks file locks before any Edit/Write/NotebookEdit operation. Prevents multiple agents from editing the same file simultaneously.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ hooks install filelock
9
+ ```
10
+
11
+ ## How it works
12
+
13
+ Before every file edit:
14
+ 1. Checks `~/.hooks/locks/<file>.lock` for an existing lock
15
+ 2. If locked by another agent → blocks the edit
16
+ 3. If unlocked or locked by same session → acquires the lock and approves
17
+ 4. Locks auto-expire after 30 minutes
18
+
19
+ ## Lock files
20
+
21
+ Locks are stored at `~/.hooks/locks/`. Each lock contains:
22
+ - `session_id` — which session holds the lock
23
+ - `agent` — optional agent name (`HOOKS_AGENT_NAME` env var)
24
+ - `locked_at` / `expires_at` — timestamps
25
+
26
+ ## Configuration
27
+
28
+ Set `HOOKS_AGENT_NAME` environment variable to identify which agent holds the lock:
29
+
30
+ ```bash
31
+ export HOOKS_AGENT_NAME="agent-frontend"
32
+ ```
33
+
34
+ ## Releasing locks
35
+
36
+ Locks expire automatically after 30 minutes. To release manually:
37
+
38
+ ```bash
39
+ rm ~/.hooks/locks/<encoded-path>.lock
40
+ ```
41
+
42
+ ## Event
43
+
44
+ - **PreToolUse** (matcher: `Edit|Write|NotebookEdit`)
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@hasna/hook-filelock",
3
+ "version": "0.1.0",
4
+ "description": "Claude Code hook that auto-checks file locks before edits to prevent multi-agent conflicts",
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
+ "filelock",
27
+ "locking",
28
+ "multi-agent",
29
+ "coordination"
30
+ ],
31
+ "author": "Hasna",
32
+ "license": "Apache-2.0",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/hasna/hooks.git"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ },
41
+ "engines": {
42
+ "node": ">=18",
43
+ "bun": ">=1.0"
44
+ },
45
+ "devDependencies": {
46
+ "@types/bun": "^1.3.8",
47
+ "@types/node": "^20",
48
+ "typescript": "^5.0.0"
49
+ }
50
+ }
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env bun
2
+
3
+ /**
4
+ * Claude Code Hook: filelock
5
+ *
6
+ * PreToolUse hook that checks for file locks before any Edit/Write/NotebookEdit.
7
+ * Creates locks in ~/.hooks/locks/ so multiple agents can coordinate editing.
8
+ *
9
+ * Lock files are automatically expired after 30 minutes.
10
+ * The same session can always edit its own locked files.
11
+ */
12
+
13
+ import { readFileSync, existsSync, mkdirSync, writeFileSync, unlinkSync } from "fs";
14
+ import { join, basename } from "path";
15
+ import { homedir } from "os";
16
+
17
+ interface HookInput {
18
+ session_id: string;
19
+ cwd: string;
20
+ tool_name: string;
21
+ tool_input: Record<string, unknown>;
22
+ }
23
+
24
+ interface HookOutput {
25
+ decision: "approve" | "block";
26
+ reason?: string;
27
+ }
28
+
29
+ interface LockEntry {
30
+ file: string;
31
+ session_id: string;
32
+ agent?: string;
33
+ locked_at: string;
34
+ expires_at: string;
35
+ }
36
+
37
+ const LOCK_DIR = join(homedir(), ".hooks", "locks");
38
+ const LOCK_TTL_MS = 30 * 60 * 1000; // 30 minutes
39
+
40
+ function readStdinJson(): HookInput | null {
41
+ try {
42
+ const input = readFileSync(0, "utf-8").trim();
43
+ if (!input) return null;
44
+ return JSON.parse(input);
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ function respond(output: HookOutput): void {
51
+ console.log(JSON.stringify(output));
52
+ }
53
+
54
+ function getLockFilePath(filePath: string): string {
55
+ const safe = filePath.replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 200);
56
+ return join(LOCK_DIR, `${safe}.lock`);
57
+ }
58
+
59
+ function checkFileLock(
60
+ filePath: string,
61
+ sessionId: string
62
+ ): { locked: boolean; lockedBy?: string; lockedAt?: string } {
63
+ const lockFilePath = getLockFilePath(filePath);
64
+ if (!existsSync(lockFilePath)) return { locked: false };
65
+
66
+ try {
67
+ const lock: LockEntry = JSON.parse(readFileSync(lockFilePath, "utf-8"));
68
+
69
+ // Expire old locks
70
+ if (new Date(lock.expires_at).getTime() < Date.now()) {
71
+ try {
72
+ unlinkSync(lockFilePath);
73
+ } catch {}
74
+ return { locked: false };
75
+ }
76
+
77
+ // Same session — allow
78
+ if (lock.session_id === sessionId) return { locked: false };
79
+
80
+ return {
81
+ locked: true,
82
+ lockedBy: lock.agent || lock.session_id.slice(0, 8),
83
+ lockedAt: lock.locked_at,
84
+ };
85
+ } catch {
86
+ return { locked: false };
87
+ }
88
+ }
89
+
90
+ function acquireLock(filePath: string, sessionId: string): void {
91
+ mkdirSync(LOCK_DIR, { recursive: true });
92
+ const now = new Date();
93
+ const lock: LockEntry = {
94
+ file: filePath,
95
+ session_id: sessionId,
96
+ agent: process.env.HOOKS_AGENT_NAME,
97
+ locked_at: now.toISOString(),
98
+ expires_at: new Date(now.getTime() + LOCK_TTL_MS).toISOString(),
99
+ };
100
+ writeFileSync(getLockFilePath(filePath), JSON.stringify(lock, null, 2));
101
+ }
102
+
103
+ export function run(): void {
104
+ const input = readStdinJson();
105
+
106
+ if (!input) {
107
+ respond({ decision: "approve" });
108
+ return;
109
+ }
110
+
111
+ const filePath = (input.tool_input.file_path || input.tool_input.notebook_path) as
112
+ | string
113
+ | undefined;
114
+
115
+ if (!filePath) {
116
+ respond({ decision: "approve" });
117
+ return;
118
+ }
119
+
120
+ const { locked, lockedBy, lockedAt } = checkFileLock(filePath, input.session_id);
121
+
122
+ if (locked) {
123
+ const name = basename(filePath);
124
+ const age = lockedAt
125
+ ? ` (locked ${Math.round((Date.now() - new Date(lockedAt).getTime()) / 60000)}m ago)`
126
+ : "";
127
+ console.error(`[hook-filelock] Blocked: ${name} is locked by ${lockedBy}${age}`);
128
+ respond({
129
+ decision: "block",
130
+ reason: `File '${name}' is locked by another agent (${lockedBy})${age}. Wait for the lock to be released or coordinate first.`,
131
+ });
132
+ return;
133
+ }
134
+
135
+ // Acquire lock for this session
136
+ try {
137
+ acquireLock(filePath, input.session_id);
138
+ } catch (err) {
139
+ console.error(`[hook-filelock] Could not write lock: ${err}`);
140
+ }
141
+
142
+ respond({ decision: "approve" });
143
+ }
144
+
145
+ if (import.meta.main) {
146
+ run();
147
+ }
@@ -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
+ }
@@ -3,18 +3,11 @@
3
3
  /**
4
4
  * Claude Code Hook: sessionlog
5
5
  *
6
- * PostToolUse hook that logs every tool call to a session log file.
7
- * Creates .claude/session-log-<date>.jsonl in the project directory.
8
- *
9
- * Each line is a JSON object with:
10
- * - timestamp: ISO string
11
- * - tool_name: name of the tool that was called
12
- * - tool_input: first 500 characters of the stringified tool input
13
- * - session_id: current session ID
6
+ * PostToolUse hook that logs every tool call to SQLite (~/.hooks/hooks.db).
14
7
  */
15
8
 
16
- import { readFileSync, existsSync, mkdirSync, appendFileSync } from "fs";
17
- import { join } from "path";
9
+ import { readFileSync } from "fs";
10
+ import { writeHookEvent } from "../../../src/lib/db-writer";
18
11
 
19
12
  interface HookInput {
20
13
  session_id: string;
@@ -41,42 +34,6 @@ function respond(output: HookOutput): void {
41
34
  console.log(JSON.stringify(output));
42
35
  }
43
36
 
44
- function getDateString(): string {
45
- const now = new Date();
46
- const year = now.getFullYear();
47
- const month = String(now.getMonth() + 1).padStart(2, "0");
48
- const day = String(now.getDate()).padStart(2, "0");
49
- return `${year}-${month}-${day}`;
50
- }
51
-
52
- function truncate(str: string, maxLength: number): string {
53
- if (str.length <= maxLength) return str;
54
- return str.slice(0, maxLength) + "...";
55
- }
56
-
57
- function logToolCall(input: HookInput): void {
58
- const claudeDir = join(input.cwd, ".claude");
59
-
60
- // Create .claude/ directory if it doesn't exist
61
- if (!existsSync(claudeDir)) {
62
- mkdirSync(claudeDir, { recursive: true });
63
- }
64
-
65
- const dateStr = getDateString();
66
- const logFile = join(claudeDir, `session-log-${dateStr}.jsonl`);
67
-
68
- const toolInputStr = truncate(JSON.stringify(input.tool_input), 500);
69
-
70
- const logEntry = {
71
- timestamp: new Date().toISOString(),
72
- tool_name: input.tool_name,
73
- tool_input: toolInputStr,
74
- session_id: input.session_id,
75
- };
76
-
77
- appendFileSync(logFile, JSON.stringify(logEntry) + "\n");
78
- }
79
-
80
37
  export function run(): void {
81
38
  const input = readStdinJson();
82
39
 
@@ -85,12 +42,14 @@ export function run(): void {
85
42
  return;
86
43
  }
87
44
 
88
- try {
89
- logToolCall(input);
90
- } catch (error) {
91
- const errMsg = error instanceof Error ? error.message : String(error);
92
- console.error(`[hook-sessionlog] Warning: failed to log tool call: ${errMsg}`);
93
- }
45
+ writeHookEvent({
46
+ session_id: input.session_id,
47
+ hook_name: "sessionlog",
48
+ event_type: "PostToolUse",
49
+ tool_name: input.tool_name,
50
+ tool_input: JSON.stringify(input.tool_input),
51
+ project_dir: input.cwd,
52
+ });
94
53
 
95
54
  respond({ continue: true });
96
55
  }
@@ -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,46 @@
1
+ # hook-typecheck-gate
2
+
3
+ A Stop hook that runs TypeScript type checking before Claude finishes. Blocks the session if type errors are found, forcing Claude to fix them first.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ hooks install typecheck-gate
9
+ ```
10
+
11
+ ## How it works
12
+
13
+ On every `Stop` event:
14
+ 1. Detects the TypeScript check command (from `package.json` scripts or `tsconfig.json`)
15
+ 2. Runs the command
16
+ 3. If it passes → allows Claude to stop
17
+ 4. If it fails → blocks stop with error details, forcing Claude to fix them
18
+
19
+ ## Configuration
20
+
21
+ Add to `~/.claude/settings.json` or `.claude/settings.json`:
22
+
23
+ ```json
24
+ {
25
+ "typecheckGateConfig": {
26
+ "enabled": true,
27
+ "command": "bun run typecheck"
28
+ }
29
+ }
30
+ ```
31
+
32
+ | Option | Default | Description |
33
+ |--------|---------|-------------|
34
+ | `enabled` | `true` | Enable/disable the hook |
35
+ | `command` | auto-detect | Override the typecheck command |
36
+
37
+ ## Auto-detection
38
+
39
+ If no `command` is configured, the hook detects the right command:
40
+ 1. Checks `package.json` scripts: `typecheck`, `type-check`, `tsc`, `build:types`
41
+ 2. Falls back to `bunx tsc --noEmit` if `tsconfig.json` exists
42
+ 3. Skips silently if no TypeScript project detected
43
+
44
+ ## Event
45
+
46
+ - **Stop** — runs after Claude finishes each response
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@hasna/hook-typecheck-gate",
3
+ "version": "0.1.0",
4
+ "description": "Claude Code hook that runs TypeScript type checking on Stop and blocks if errors are found",
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
+ "typescript",
27
+ "typecheck",
28
+ "quality",
29
+ "gate"
30
+ ],
31
+ "author": "Hasna",
32
+ "license": "Apache-2.0",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/hasna/hooks.git"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ },
41
+ "engines": {
42
+ "node": ">=18",
43
+ "bun": ">=1.0"
44
+ },
45
+ "devDependencies": {
46
+ "@types/bun": "^1.3.8",
47
+ "@types/node": "^20",
48
+ "typescript": "^5.0.0"
49
+ }
50
+ }