@zhushanwen/pi-unified-hooks 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhushanwen/pi-unified-hooks",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Unified hooks extension - collect scattered hooks in one place for easy maintenance",
5
5
  "main": "index.ts",
6
6
  "type": "module",
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Network Timeout Guard Hook
3
+ *
4
+ * Intercepts bash tool calls that involve network operations.
5
+ * Blocks execution if no timeout is set and the command appears to be network-bound,
6
+ * prompting the AI to add timeout or use async mode.
7
+ */
8
+
9
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
10
+
11
+ /**
12
+ * Commands that indicate network activity.
13
+ * Matched against any segment of the bash command string (supports &&/||/; chaining).
14
+ */
15
+ const NETWORK_COMMAND_PATTERNS: Array<{ pattern: RegExp; label: string }> = [
16
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(pnpm|npm|yarn|bun)\s+(install|add|i|ci|update|upgrade|dlx|exec|create|init)/, label: "package manager" },
17
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(pip|pip3|uv|poetry|conda)\s+(install|add|sync|download|run)/, label: "Python package manager" },
18
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*curl\s/, label: "curl" },
19
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*wget\s/, label: "wget" },
20
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*git\s+(clone|fetch|pull|push|submodule)/, label: "git network" },
21
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*gh\s+(repo\s+clone|api|run|release|pr\s+checkout)/, label: "gh CLI" },
22
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*docker\s+(pull|push|build|run|compose\s+(pull|up|build))/, label: "docker" },
23
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*cargo\s+(install|update|build|check|fetch)/, label: "cargo" },
24
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*go\s+(install|get|mod\s+(download|tidy))/, label: "go" },
25
+ ];
26
+
27
+ /**
28
+ * Check if a command involves network operations.
29
+ */
30
+ function detectNetworkCommand(command: string): string | null {
31
+ for (const { pattern, label } of NETWORK_COMMAND_PATTERNS) {
32
+ if (pattern.test(command)) {
33
+ return label;
34
+ }
35
+ }
36
+ return null;
37
+ }
38
+
39
+ export function setupNetworkTimeoutGuard(pi: ExtensionAPI): void {
40
+ pi.on("tool_call", async (event: any) => {
41
+ // Only intercept bash tool calls
42
+ if (event.toolName !== "bash") return;
43
+
44
+ const { command, timeout } = event.input as { command: string; timeout?: number };
45
+
46
+ // No network command detected — let it through
47
+ const networkLabel = detectNetworkCommand(command);
48
+ if (!networkLabel) return;
49
+
50
+ // Timeout is already set — good, let it through
51
+ if (timeout != null && timeout > 0) return;
52
+
53
+ // Block and ask AI to add timeout
54
+ return {
55
+ block: true,
56
+ reason:
57
+ `[network-timeout-guard] Detected network command (${networkLabel}) without timeout.\n` +
58
+ `Please do one of the following:\n` +
59
+ `1. Set the bash tool's timeout parameter (recommended: 60-120s)\n` +
60
+ `2. If duration is unclear, start with timeout: 30 to probe, then adjust after timeout\n` +
61
+ `3. Accessing overseas sites (npmjs.org / GitHub / PyPI) may require a proxy — on timeout, run 'proxy' to enable proxy and retry`,
62
+ };
63
+ });
64
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Test Timeout Guard Hook
3
+ *
4
+ * Intercepts bash tool calls that execute tests across various frameworks.
5
+ * Blocks execution if no timeout is set, prompting the AI to add one.
6
+ *
7
+ * Covered ecosystems: Node.js (vitest, jest, mocha, cypress, playwright),
8
+ * Python (pytest, unittest), Java (maven, gradle), Go, Rust, .NET, Ruby.
9
+ */
10
+
11
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
12
+
13
+ /**
14
+ * Patterns that indicate test execution.
15
+ * Designed to match via pipe/semicolon/&& chaining as well as direct invocation.
16
+ */
17
+ const TEST_COMMAND_PATTERNS: Array<{ pattern: RegExp; label: string }> = [
18
+ // === Node.js / JS / TS ===
19
+ // Package manager test scripts: npm test, pnpm test, yarn test
20
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(pnpm|npm|yarn|bun)\s+test\b/, label: "npm test" },
21
+ // pnpm/npm run with test-related script names
22
+ {
23
+ pattern: /(^|\s|&&|\|{1,2}|;)\s*(pnpm|npm)\s+(--filter\s+\S+\s+)?run\s+\S*test/,
24
+ label: "npm run test",
25
+ },
26
+ // Direct test runner invocation via npx
27
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+vitest\b/, label: "vitest" },
28
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+jest\b/, label: "jest" },
29
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+mocha\b/, label: "mocha" },
30
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+(cypress|playwright)\s/, label: "e2e runner" },
31
+ // vue-cli-service / react-scripts test
32
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+vue-cli-service\s+test/, label: "vue-cli test" },
33
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*npx\s+react-scripts\s+test/, label: "react-scripts test" },
34
+ // Direct vitest/jest from node_modules
35
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*\.\/?node_modules\/\.bin\/(vitest|jest|mocha)\b/, label: "direct test runner" },
36
+
37
+ // === Python ===
38
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*pytest\b/, label: "pytest" },
39
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*python[3]?\s+-m\s+(pytest|unittest)\b/, label: "python test" },
40
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(uv|poetry)\s+run\s+pytest\b/, label: "uv/poetry pytest" },
41
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*nosetests\b/, label: "nosetests" },
42
+
43
+ // === Java / JVM ===
44
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(mvn|\.\/mvnw)\s+\S*test/, label: "maven test" },
45
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(gradle|\.\/gradlew)\s+\S*test/, label: "gradle test" },
46
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*sbt\s+test\b/, label: "sbt test" },
47
+
48
+ // === Go ===
49
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*go\s+test\b/, label: "go test" },
50
+
51
+ // === Rust ===
52
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*cargo\s+test\b/, label: "cargo test" },
53
+
54
+ // === .NET ===
55
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*dotnet\s+test\b/, label: "dotnet test" },
56
+
57
+ // === Ruby ===
58
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*(rspec|bundle\s+exec\s+rspec)\b/, label: "rspec" },
59
+ { pattern: /(^|\s|&&|\|{1,2}|;)\s*rake\s+test\b/, label: "rake test" },
60
+ ];
61
+
62
+ /**
63
+ * Check if a command executes tests.
64
+ */
65
+ function detectTestCommand(command: string): string | null {
66
+ for (const { pattern, label } of TEST_COMMAND_PATTERNS) {
67
+ if (pattern.test(command)) {
68
+ return label;
69
+ }
70
+ }
71
+ return null;
72
+ }
73
+
74
+ export function setupTestTimeoutGuard(pi: ExtensionAPI): void {
75
+ pi.on("tool_call", async (event: any) => {
76
+ if (event.toolName !== "bash") return;
77
+
78
+ const { command, timeout } = event.input as { command: string; timeout?: number };
79
+
80
+ const testLabel = detectTestCommand(command);
81
+ if (!testLabel) return;
82
+
83
+ // Timeout already set — let it through
84
+ if (timeout != null && timeout > 0) return;
85
+
86
+ // Block and require timeout
87
+ return {
88
+ block: true,
89
+ reason:
90
+ `[test-timeout-guard] Detected test command (${testLabel}) without timeout.\n` +
91
+ `Test duration is unpredictable — without timeout the process may hang indefinitely. Please do one of the following:\n` +
92
+ `1. Set the bash tool's timeout parameter (60-120s for unit tests, 180-300s for E2E tests)\n` +
93
+ `2. If test suite size or duration is unknown, start with a short timeout (e.g. 30s) to probe\n` +
94
+ `3. For known slow tests (cargo test, gradle test, etc.), allow up to 300s`,
95
+ };
96
+ });
97
+ }
package/src/index.ts CHANGED
@@ -9,8 +9,12 @@ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
9
9
 
10
10
  // Re-export hook modules for easy access
11
11
  export { setupToolErrorHandler } from "./hooks/tool-error-handler";
12
+ export { setupNetworkTimeoutGuard } from "./hooks/network-timeout-guard";
13
+ export { setupTestTimeoutGuard } from "./hooks/test-timeout-guard";
12
14
 
13
15
  import { setupToolErrorHandler } from "./hooks/tool-error-handler";
16
+ import { setupNetworkTimeoutGuard } from "./hooks/network-timeout-guard";
17
+ import { setupTestTimeoutGuard } from "./hooks/test-timeout-guard";
14
18
 
15
19
  /**
16
20
  * Extension factory - registers all unified hooks
@@ -23,6 +27,8 @@ export default function unifiedHooksExtension(pi: ExtensionAPI): void {
23
27
  // with hash-anchor mode, making oldText-based guard unreachable
24
28
  const hookModules = [
25
29
  { name: "tool-error-handler", setup: setupToolErrorHandler },
30
+ { name: "network-timeout-guard", setup: setupNetworkTimeoutGuard },
31
+ { name: "test-timeout-guard", setup: setupTestTimeoutGuard },
26
32
  ];
27
33
 
28
34
  for (const hook of hookModules) {