@hasna/hooks 0.2.7 → 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.
- 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,40 @@
|
|
|
1
|
+
# hook-failure-to-task
|
|
2
|
+
|
|
3
|
+
A PostToolUse hook that automatically creates a todo task whenever a test or build command fails. Keeps a visible record of failures so nothing gets forgotten.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
hooks install failure-to-task
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
After every Bash tool execution:
|
|
14
|
+
1. Checks if the command is a test/build command (`bun test`, `npm test`, `tsc`, etc.)
|
|
15
|
+
2. If the exit code is non-zero → creates a task with the error details
|
|
16
|
+
3. Always approves (non-blocking)
|
|
17
|
+
|
|
18
|
+
## Task creation
|
|
19
|
+
|
|
20
|
+
Tasks are created via the `todos` CLI if installed. Falls back to `~/.hooks/tasks/<id>.json`.
|
|
21
|
+
|
|
22
|
+
Task format:
|
|
23
|
+
- **Title**: `Fix failing \`bun test\` in my-project`
|
|
24
|
+
- **Priority**: high
|
|
25
|
+
- **Tags**: `failure`, `tests` or `build`
|
|
26
|
+
- **Description**: includes command, exit code, and error snippet
|
|
27
|
+
|
|
28
|
+
## Detected commands
|
|
29
|
+
|
|
30
|
+
| Pattern | Category |
|
|
31
|
+
|---------|----------|
|
|
32
|
+
| `bun test`, `npm test`, `yarn test`, `pnpm test` | tests |
|
|
33
|
+
| `pytest`, `jest`, `vitest` | tests |
|
|
34
|
+
| `bun build`, `npm run build`, `yarn build` | build |
|
|
35
|
+
| `tsc`, `bunx tsc` | build |
|
|
36
|
+
| `bun run typecheck`, `npm run typecheck` | build |
|
|
37
|
+
|
|
38
|
+
## Event
|
|
39
|
+
|
|
40
|
+
- **PostToolUse** (matcher: `Bash`)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hasna/hook-failure-to-task",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude Code hook that creates a todo task when test or build commands fail",
|
|
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
|
+
"tasks",
|
|
27
|
+
"failures",
|
|
28
|
+
"tests",
|
|
29
|
+
"build",
|
|
30
|
+
"automation"
|
|
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,171 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Claude Code Hook: failure-to-task
|
|
5
|
+
*
|
|
6
|
+
* PostToolUse hook that creates a todo task when a test or build command fails.
|
|
7
|
+
* Uses the `todos` CLI if available, otherwise writes to ~/.hooks/tasks/.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFileSync, mkdirSync, writeFileSync } from "fs";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
import { execSync } from "child_process";
|
|
14
|
+
|
|
15
|
+
interface HookInput {
|
|
16
|
+
session_id: string;
|
|
17
|
+
cwd: string;
|
|
18
|
+
tool_name: string;
|
|
19
|
+
tool_input: Record<string, unknown>;
|
|
20
|
+
tool_output?: Record<string, unknown> | string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface HookOutput {
|
|
24
|
+
continue: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const TEST_BUILD_PATTERNS: RegExp[] = [
|
|
28
|
+
/\bbun\s+test\b/,
|
|
29
|
+
/\bnpm\s+(run\s+)?test\b/,
|
|
30
|
+
/\byarn\s+test\b/,
|
|
31
|
+
/\bpnpm\s+(run\s+)?test\b/,
|
|
32
|
+
/\bpytest\b/,
|
|
33
|
+
/\bvitest\b/,
|
|
34
|
+
/\bjest\b/,
|
|
35
|
+
/\bbun\s+(run\s+)?build\b/,
|
|
36
|
+
/\bnpm\s+(run\s+)?build\b/,
|
|
37
|
+
/\byarn\s+build\b/,
|
|
38
|
+
/\bpnpm\s+(run\s+)?build\b/,
|
|
39
|
+
/\bbunx?\s+tsc\b/,
|
|
40
|
+
/\bnpm\s+(run\s+)?typecheck\b/,
|
|
41
|
+
/\bbun\s+(run\s+)?typecheck\b/,
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
function readStdinJson(): HookInput | null {
|
|
45
|
+
try {
|
|
46
|
+
const input = readFileSync(0, "utf-8").trim();
|
|
47
|
+
if (!input) return null;
|
|
48
|
+
return JSON.parse(input);
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function respond(output: HookOutput): void {
|
|
55
|
+
console.log(JSON.stringify(output));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isTestOrBuild(command: string): boolean {
|
|
59
|
+
return TEST_BUILD_PATTERNS.some((p) => p.test(command));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getExitCode(output: unknown): number | null {
|
|
63
|
+
if (typeof output !== "object" || output === null) return null;
|
|
64
|
+
const o = output as Record<string, unknown>;
|
|
65
|
+
const code = o.exit_code ?? o.exitCode;
|
|
66
|
+
if (typeof code === "number") return code;
|
|
67
|
+
if (typeof code === "string") return parseInt(code, 10);
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getErrorSnippet(output: unknown): string {
|
|
72
|
+
if (typeof output === "string") return output.slice(0, 600);
|
|
73
|
+
if (typeof output === "object" && output !== null) {
|
|
74
|
+
const o = output as Record<string, unknown>;
|
|
75
|
+
return `${o.stdout || o.output || ""}\n${o.stderr || o.error || ""}`.trim().slice(0, 600);
|
|
76
|
+
}
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function createTask(cwd: string, command: string, exitCode: number, snippet: string): void {
|
|
81
|
+
const projectName = cwd.split("/").filter(Boolean).pop() || "project";
|
|
82
|
+
const shortCmd = command.split(/\s+/).slice(0, 4).join(" ");
|
|
83
|
+
const title = `Fix failing \`${shortCmd}\` in ${projectName}`;
|
|
84
|
+
const description = [
|
|
85
|
+
`A ${shortCmd.includes("test") ? "test" : "build"} command failed during a coding session.`,
|
|
86
|
+
"",
|
|
87
|
+
`**Command:** \`${command}\``,
|
|
88
|
+
`**Exit code:** ${exitCode}`,
|
|
89
|
+
`**Project:** \`${cwd}\``,
|
|
90
|
+
"",
|
|
91
|
+
"**Error output:**",
|
|
92
|
+
"```",
|
|
93
|
+
snippet || "(no output captured)",
|
|
94
|
+
"```",
|
|
95
|
+
"",
|
|
96
|
+
"**Acceptance criteria:**",
|
|
97
|
+
`- \`${command}\` passes without errors`,
|
|
98
|
+
].join("\n");
|
|
99
|
+
|
|
100
|
+
// Try todos CLI first
|
|
101
|
+
try {
|
|
102
|
+
const escapedTitle = title.replace(/"/g, "'");
|
|
103
|
+
const escapedDesc = description.replace(/"/g, "'").replace(/\n/g, "\\n");
|
|
104
|
+
execSync(
|
|
105
|
+
`todos add "${escapedTitle}" --description "${escapedDesc}" --priority high --tags "failure,${shortCmd.includes("test") ? "tests" : "build"}"`,
|
|
106
|
+
{ cwd, timeout: 15_000, stdio: "pipe" }
|
|
107
|
+
);
|
|
108
|
+
console.error(`[hook-failure-to-task] Task created via todos CLI: "${title}"`);
|
|
109
|
+
return;
|
|
110
|
+
} catch {
|
|
111
|
+
// Fall back to local file
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Local fallback: write to ~/.hooks/tasks/
|
|
115
|
+
try {
|
|
116
|
+
const taskDir = join(homedir(), ".hooks", "tasks");
|
|
117
|
+
mkdirSync(taskDir, { recursive: true });
|
|
118
|
+
const taskId = `failure-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
|
|
119
|
+
const task = {
|
|
120
|
+
id: taskId,
|
|
121
|
+
title,
|
|
122
|
+
description,
|
|
123
|
+
status: "pending",
|
|
124
|
+
priority: "high",
|
|
125
|
+
tags: ["failure", shortCmd.includes("test") ? "tests" : "build"],
|
|
126
|
+
created_at: new Date().toISOString(),
|
|
127
|
+
source: "hook-failure-to-task",
|
|
128
|
+
project_dir: cwd,
|
|
129
|
+
};
|
|
130
|
+
writeFileSync(join(taskDir, `${taskId}.json`), JSON.stringify(task, null, 2));
|
|
131
|
+
console.error(`[hook-failure-to-task] Task saved locally: ${taskId}`);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error(`[hook-failure-to-task] Could not create task: ${err}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function run(): void {
|
|
138
|
+
const input = readStdinJson();
|
|
139
|
+
|
|
140
|
+
if (!input) {
|
|
141
|
+
respond({ continue: true });
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (input.tool_name !== "Bash") {
|
|
146
|
+
respond({ continue: true });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const command = input.tool_input?.command as string | undefined;
|
|
151
|
+
if (!command || !isTestOrBuild(command)) {
|
|
152
|
+
respond({ continue: true });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const exitCode = getExitCode(input.tool_output);
|
|
157
|
+
if (exitCode === null || exitCode === 0) {
|
|
158
|
+
respond({ continue: true });
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const snippet = getErrorSnippet(input.tool_output);
|
|
163
|
+
console.error(`[hook-failure-to-task] Command failed (exit ${exitCode}): ${command}`);
|
|
164
|
+
createTask(input.cwd, command, exitCode, snippet);
|
|
165
|
+
|
|
166
|
+
respond({ continue: true });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (import.meta.main) {
|
|
170
|
+
run();
|
|
171
|
+
}
|
|
@@ -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,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
|
+
}
|