@hasna/hooks 0.2.7 → 0.2.9
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/bin/index.js +100 -2
- package/dist/index.js +85 -1
- package/hooks/hook-affected-tests/LICENSE +191 -0
- package/hooks/hook-affected-tests/README.md +37 -0
- package/hooks/hook-affected-tests/package.json +50 -0
- package/hooks/hook-affected-tests/src/hook.ts +148 -0
- package/hooks/hook-affected-tests/tsconfig.json +25 -0
- package/hooks/hook-announce-start/LICENSE +191 -0
- package/hooks/hook-announce-start/README.md +35 -0
- package/hooks/hook-announce-start/package.json +51 -0
- package/hooks/hook-announce-start/src/hook.ts +137 -0
- package/hooks/hook-announce-start/tsconfig.json +25 -0
- package/hooks/hook-announce-stop/LICENSE +191 -0
- package/hooks/hook-announce-stop/README.md +35 -0
- package/hooks/hook-announce-stop/package.json +51 -0
- package/hooks/hook-announce-stop/src/hook.ts +160 -0
- package/hooks/hook-announce-stop/tsconfig.json +25 -0
- package/hooks/hook-conflict-detect/LICENSE +191 -0
- package/hooks/hook-conflict-detect/README.md +38 -0
- package/hooks/hook-conflict-detect/package.json +50 -0
- package/hooks/hook-conflict-detect/src/hook.ts +116 -0
- package/hooks/hook-conflict-detect/tsconfig.json +25 -0
- package/hooks/hook-dm-inject/LICENSE +191 -0
- package/hooks/hook-dm-inject/README.md +42 -0
- package/hooks/hook-dm-inject/package.json +51 -0
- package/hooks/hook-dm-inject/src/hook.ts +115 -0
- package/hooks/hook-dm-inject/tsconfig.json +25 -0
- package/hooks/hook-failure-to-task/LICENSE +191 -0
- package/hooks/hook-failure-to-task/README.md +40 -0
- package/hooks/hook-failure-to-task/package.json +51 -0
- package/hooks/hook-failure-to-task/src/hook.ts +171 -0
- package/hooks/hook-failure-to-task/tsconfig.json +25 -0
- package/hooks/hook-filelock/LICENSE +191 -0
- package/hooks/hook-filelock/README.md +44 -0
- package/hooks/hook-filelock/package.json +50 -0
- package/hooks/hook-filelock/src/hook.ts +147 -0
- package/hooks/hook-filelock/tsconfig.json +25 -0
- package/hooks/hook-typecheck-gate/LICENSE +191 -0
- package/hooks/hook-typecheck-gate/README.md +46 -0
- package/hooks/hook-typecheck-gate/package.json +50 -0
- package/hooks/hook-typecheck-gate/src/hook.ts +152 -0
- package/hooks/hook-typecheck-gate/tsconfig.json +25 -0
- package/package.json +1 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# hook-announce-stop
|
|
2
|
+
|
|
3
|
+
A Stop hook that automatically cleans up and announces when an agent session ends. Releases file locks, posts a summary to the team space, and adds notes to in-progress tasks.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
hooks install announce-stop
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
On every `Stop` event:
|
|
14
|
+
1. **Releases file locks** — scans `~/.hooks/locks/` and removes all locks held by this session
|
|
15
|
+
2. **Posts summary** — sends a message to the configured conversation space via `conversations send`
|
|
16
|
+
3. **Updates tasks** — adds a comment to any in-progress tasks noting the session ended
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
Set environment variables to customize behavior:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
export HOOKS_AGENT_NAME="agent-frontend" # Agent display name (default: session ID)
|
|
24
|
+
export HOOKS_SPACE="engineering" # Conversation space to post to (default: general)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
Optional but recommended:
|
|
30
|
+
- `conversations` CLI for posting to spaces
|
|
31
|
+
- `todos` CLI for updating task status
|
|
32
|
+
|
|
33
|
+
## Event
|
|
34
|
+
|
|
35
|
+
- **Stop** — runs after Claude finishes each response
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hasna/hook-announce-stop",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude Code hook that releases locks, posts session summary, and updates tasks on Stop",
|
|
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
|
+
"announcement",
|
|
27
|
+
"stop",
|
|
28
|
+
"locks",
|
|
29
|
+
"summary",
|
|
30
|
+
"agent-teams"
|
|
31
|
+
],
|
|
32
|
+
"author": "Hasna",
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/hasna/hooks.git"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"registry": "https://registry.npmjs.org/"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18",
|
|
44
|
+
"bun": ">=1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/bun": "^1.3.8",
|
|
48
|
+
"@types/node": "^20",
|
|
49
|
+
"typescript": "^5.0.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Claude Code Hook: announce-stop
|
|
5
|
+
*
|
|
6
|
+
* Stop hook that:
|
|
7
|
+
* 1. Releases all file locks held by this session
|
|
8
|
+
* 2. Posts a summary to the agent's conversation space
|
|
9
|
+
* 3. Updates in-progress tasks to reflect the session ending
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { readFileSync, existsSync, readdirSync, unlinkSync } from "fs";
|
|
13
|
+
import { join } from "path";
|
|
14
|
+
import { homedir } from "os";
|
|
15
|
+
import { execSync } from "child_process";
|
|
16
|
+
|
|
17
|
+
interface HookInput {
|
|
18
|
+
session_id: string;
|
|
19
|
+
cwd: string;
|
|
20
|
+
hook_event_name: string;
|
|
21
|
+
transcript_path?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface HookOutput {
|
|
25
|
+
continue: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface LockEntry {
|
|
29
|
+
file: string;
|
|
30
|
+
session_id: string;
|
|
31
|
+
agent?: string;
|
|
32
|
+
locked_at: string;
|
|
33
|
+
expires_at: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const LOCK_DIR = join(homedir(), ".hooks", "locks");
|
|
37
|
+
|
|
38
|
+
function readStdinJson(): HookInput | null {
|
|
39
|
+
try {
|
|
40
|
+
const input = readFileSync(0, "utf-8").trim();
|
|
41
|
+
if (!input) return null;
|
|
42
|
+
return JSON.parse(input);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function respond(output: HookOutput): void {
|
|
49
|
+
console.log(JSON.stringify(output));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function releaseSessionLocks(sessionId: string): string[] {
|
|
53
|
+
const released: string[] = [];
|
|
54
|
+
|
|
55
|
+
if (!existsSync(LOCK_DIR)) return released;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const files = readdirSync(LOCK_DIR).filter((f) => f.endsWith(".lock"));
|
|
59
|
+
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
const lockPath = join(LOCK_DIR, file);
|
|
62
|
+
try {
|
|
63
|
+
const lock: LockEntry = JSON.parse(readFileSync(lockPath, "utf-8"));
|
|
64
|
+
if (lock.session_id === sessionId) {
|
|
65
|
+
unlinkSync(lockPath);
|
|
66
|
+
released.push(lock.file);
|
|
67
|
+
}
|
|
68
|
+
} catch {}
|
|
69
|
+
}
|
|
70
|
+
} catch {}
|
|
71
|
+
|
|
72
|
+
return released;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function getProjectName(cwd: string): string {
|
|
76
|
+
return cwd.split("/").filter(Boolean).pop() || "project";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function buildSummary(
|
|
80
|
+
input: HookInput,
|
|
81
|
+
releasedLocks: string[]
|
|
82
|
+
): string {
|
|
83
|
+
const project = getProjectName(input.cwd);
|
|
84
|
+
const agent = process.env.HOOKS_AGENT_NAME || `session:${input.session_id.slice(0, 8)}`;
|
|
85
|
+
const lines: string[] = [
|
|
86
|
+
`Agent **${agent}** finished working on **${project}**.`,
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
if (releasedLocks.length > 0) {
|
|
90
|
+
const names = releasedLocks.map((f) => f.split("/").pop()).join(", ");
|
|
91
|
+
lines.push(`Released locks: ${names}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
lines.push(`Session: \`${input.session_id.slice(0, 8)}\``);
|
|
95
|
+
return lines.join(" | ");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function postToSpace(summary: string): void {
|
|
99
|
+
const space = process.env.HOOKS_SPACE || "general";
|
|
100
|
+
try {
|
|
101
|
+
execSync(`conversations send "${summary.replace(/"/g, "'")}" --space "${space}"`, {
|
|
102
|
+
encoding: "utf-8",
|
|
103
|
+
timeout: 10_000,
|
|
104
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
105
|
+
});
|
|
106
|
+
console.error(`[hook-announce-stop] Posted to space '${space}'`);
|
|
107
|
+
} catch {
|
|
108
|
+
console.error(`[hook-announce-stop] Could not post to space (conversations CLI unavailable)`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function updateInProgressTasks(sessionId: string): void {
|
|
113
|
+
try {
|
|
114
|
+
// List tasks in_progress assigned to this session and add a note
|
|
115
|
+
const output = execSync(
|
|
116
|
+
`todos list --status in_progress --json`,
|
|
117
|
+
{ encoding: "utf-8", timeout: 10_000, stdio: ["pipe", "pipe", "pipe"] }
|
|
118
|
+
);
|
|
119
|
+
const tasks = JSON.parse(output.trim());
|
|
120
|
+
if (!Array.isArray(tasks) || tasks.length === 0) return;
|
|
121
|
+
|
|
122
|
+
for (const task of tasks.slice(0, 10)) {
|
|
123
|
+
if (!task.id) continue;
|
|
124
|
+
try {
|
|
125
|
+
execSync(
|
|
126
|
+
`todos comment ${task.id} "Session ${sessionId.slice(0, 8)} ended — task may need to be resumed"`,
|
|
127
|
+
{ encoding: "utf-8", timeout: 5_000, stdio: ["pipe", "pipe", "pipe"] }
|
|
128
|
+
);
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
} catch {}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function run(): void {
|
|
135
|
+
const input = readStdinJson();
|
|
136
|
+
|
|
137
|
+
if (!input) {
|
|
138
|
+
respond({ continue: true });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 1. Release all file locks held by this session
|
|
143
|
+
const released = releaseSessionLocks(input.session_id);
|
|
144
|
+
if (released.length > 0) {
|
|
145
|
+
console.error(`[hook-announce-stop] Released ${released.length} lock(s)`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 2. Post summary to conversation space
|
|
149
|
+
const summary = buildSummary(input, released);
|
|
150
|
+
postToSpace(summary);
|
|
151
|
+
|
|
152
|
+
// 3. Update any in-progress tasks
|
|
153
|
+
updateInProgressTasks(input.session_id);
|
|
154
|
+
|
|
155
|
+
respond({ continue: true });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (import.meta.main) {
|
|
159
|
+
run();
|
|
160
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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,38 @@
|
|
|
1
|
+
# hook-conflict-detect
|
|
2
|
+
|
|
3
|
+
A PreToolUse hook that checks for unresolved git merge conflict markers before editing files. Prevents editing files mid-conflict and making things worse.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
hooks install conflict-detect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
Before every `Edit` or `Write`:
|
|
14
|
+
1. Reads the target file
|
|
15
|
+
2. Checks for all three conflict markers: `<<<<<<<`, `=======`, `>>>>>>>`
|
|
16
|
+
3. If all three are present → blocks the edit with a helpful message
|
|
17
|
+
4. Otherwise → approves normally
|
|
18
|
+
|
|
19
|
+
## Example block message
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
File 'auth.ts' contains unresolved git merge conflicts. Resolve them before editing:
|
|
23
|
+
|
|
24
|
+
<<<<<<< HEAD
|
|
25
|
+
const user = await getUser(id);
|
|
26
|
+
=======
|
|
27
|
+
>>>>>>> feature/new-auth
|
|
28
|
+
|
|
29
|
+
Run `git diff auth.ts` to see the full conflict.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Why all three markers?
|
|
33
|
+
|
|
34
|
+
The hook requires all three markers to be present to avoid false positives on files that legitimately use `<` or `>` characters.
|
|
35
|
+
|
|
36
|
+
## Event
|
|
37
|
+
|
|
38
|
+
- **PreToolUse** (matcher: `Edit|Write`)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hasna/hook-conflict-detect",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude Code hook that blocks edits on files with unresolved git merge conflict markers",
|
|
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
|
+
"git",
|
|
27
|
+
"conflicts",
|
|
28
|
+
"merge",
|
|
29
|
+
"safety"
|
|
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,116 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Claude Code Hook: conflict-detect
|
|
5
|
+
*
|
|
6
|
+
* PreToolUse hook that checks for git merge conflict markers before editing files.
|
|
7
|
+
* Blocks edits on files that contain unresolved conflicts (<<<<<<, =======, >>>>>>>).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFileSync, existsSync } from "fs";
|
|
11
|
+
import { basename } from "path";
|
|
12
|
+
import { execSync } from "child_process";
|
|
13
|
+
|
|
14
|
+
interface HookInput {
|
|
15
|
+
session_id: string;
|
|
16
|
+
cwd: string;
|
|
17
|
+
tool_name: string;
|
|
18
|
+
tool_input: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface HookOutput {
|
|
22
|
+
decision: "approve" | "block";
|
|
23
|
+
reason?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Git conflict marker patterns
|
|
27
|
+
const CONFLICT_MARKERS = [
|
|
28
|
+
/^<{7}\s/m, // <<<<<<< HEAD
|
|
29
|
+
/^={7}$/m, // =======
|
|
30
|
+
/^>{7}\s/m, // >>>>>>> branch
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
function readStdinJson(): HookInput | null {
|
|
34
|
+
try {
|
|
35
|
+
const input = readFileSync(0, "utf-8").trim();
|
|
36
|
+
if (!input) return null;
|
|
37
|
+
return JSON.parse(input);
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function respond(output: HookOutput): void {
|
|
44
|
+
console.log(JSON.stringify(output));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hasConflictMarkers(filePath: string): { found: boolean; lines: string[] } {
|
|
48
|
+
try {
|
|
49
|
+
if (!existsSync(filePath)) return { found: false, lines: [] };
|
|
50
|
+
|
|
51
|
+
const content = readFileSync(filePath, "utf-8");
|
|
52
|
+
const hasConflicts = CONFLICT_MARKERS.every((pattern) => pattern.test(content));
|
|
53
|
+
|
|
54
|
+
if (!hasConflicts) return { found: false, lines: [] };
|
|
55
|
+
|
|
56
|
+
// Extract lines with conflict markers for the reason message
|
|
57
|
+
const conflictLines = content
|
|
58
|
+
.split("\n")
|
|
59
|
+
.filter((line) => /^(<{7}|={7}|>{7})/.test(line))
|
|
60
|
+
.slice(0, 6);
|
|
61
|
+
|
|
62
|
+
return { found: true, lines: conflictLines };
|
|
63
|
+
} catch {
|
|
64
|
+
return { found: false, lines: [] };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isTrackedByGit(filePath: string, cwd: string): boolean {
|
|
69
|
+
try {
|
|
70
|
+
execSync(`git ls-files --error-unmatch "${filePath}"`, {
|
|
71
|
+
cwd,
|
|
72
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
73
|
+
timeout: 5_000,
|
|
74
|
+
});
|
|
75
|
+
return true;
|
|
76
|
+
} catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function run(): void {
|
|
82
|
+
const input = readStdinJson();
|
|
83
|
+
|
|
84
|
+
if (!input) {
|
|
85
|
+
respond({ decision: "approve" });
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const filePath = input.tool_input?.file_path as string | undefined;
|
|
90
|
+
|
|
91
|
+
if (!filePath || typeof filePath !== "string") {
|
|
92
|
+
respond({ decision: "approve" });
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const { found, lines } = hasConflictMarkers(filePath);
|
|
97
|
+
|
|
98
|
+
if (!found) {
|
|
99
|
+
respond({ decision: "approve" });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const name = basename(filePath);
|
|
104
|
+
const markerPreview = lines.join("\n");
|
|
105
|
+
|
|
106
|
+
console.error(`[hook-conflict-detect] Conflict markers found in ${name} — blocking edit`);
|
|
107
|
+
|
|
108
|
+
respond({
|
|
109
|
+
decision: "block",
|
|
110
|
+
reason: `File '${name}' contains unresolved git merge conflicts. Resolve them before editing:\n\n${markerPreview}\n\nRun \`git diff ${name}\` to see the full conflict.`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (import.meta.main) {
|
|
115
|
+
run();
|
|
116
|
+
}
|