Skip to content

Commit d158d8f

Browse files
authored
Merge pull request #12 from kenryu42/refactor-commands
Refactor commands
2 parents b554f6a + 7442ddb commit d158d8f

13 files changed

Lines changed: 800 additions & 177 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# --- Project Specific ---
22
.cursor/
33
.claude/
4-
.opencode/
54
log*
65
plan-*.md
76
*-plan-*.md
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
description: Set custom rules for Safety Net
3+
---
4+
5+
You are helping the user configure custom blocking rules for claude-code-safety-net.
6+
7+
## Context
8+
9+
### Schema Documentation
10+
11+
!`npx -y cc-safety-net --custom-rules-doc`
12+
13+
## Your Task
14+
15+
Follow this flow exactly:
16+
17+
### Step 1: Ask for Scope
18+
19+
Ask: **Which scope would you like to configure?**
20+
- **User** (`~/.cc-safety-net/config.json`) - applies to all your projects
21+
- **Project** (`.safety-net.json`) - applies only to this project
22+
23+
### Step 2: Show Examples and Ask for Rules
24+
25+
Show examples in natural language:
26+
- "Block `git add -A` and `git add .` to prevent blanket staging"
27+
- "Block `npm install -g` to prevent global package installs"
28+
- "Block `docker system prune` to prevent accidental cleanup"
29+
30+
Ask the user to describe rules in natural language. They can list multiple.
31+
32+
### Step 3: Generate JSON Config
33+
34+
Parse user input and generate valid schema JSON using the schema documentation above.
35+
36+
### Step 4: Show Config and Confirm
37+
38+
Display the generated JSON and ask:
39+
- "Does this look correct?"
40+
- "Would you like to modify anything?"
41+
42+
### Step 5: Check and Handle Existing Config
43+
44+
1. Check existing User Config with `cat ~/.cc-safety-net/config.json 2>/dev/null || echo "No user config found"`
45+
2. Check existing Project Config with `cat .safety-net.json 2>/dev/null || echo "No project config found"`
46+
47+
If the chosen scope already has a config:
48+
Show the existing config to the user.
49+
Ask: **Merge** (add new rules, duplicates use new version) or **Replace**?
50+
51+
### Step 6: Write and Validate
52+
53+
Write the config to the chosen scope, then validate with `npx -y cc-safety-net --verify-config`.
54+
55+
If validation errors:
56+
- Show specific errors
57+
- Offer to fix with your best suggestion
58+
- Confirm before proceeding
59+
60+
### Step 7: Confirm Success
61+
62+
Tell the user:
63+
1. Config saved to [path]
64+
2. **Changes take effect immediately** - no restart needed
65+
3. Summary of rules added
66+
67+
## Important Notes
68+
69+
- Custom rules can only ADD restrictions, not bypass built-in protections
70+
- Rule names must be unique (case-insensitive)
71+
- Invalid config → entire config ignored, only built-in rules apply
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: Verify custom rules for Safety Net
3+
---
4+
5+
You are helping the user verify the custom rules config file.
6+
7+
## Your Task
8+
9+
Run `npx -y cc-safety-net --verify-config` to check current validation status
10+
11+
If the config has validation errors:
12+
1. Show the specific validation errors
13+
2. Run `npx -y cc-safety-net --custom-rules-doc` to read the schema documentation
14+
3. Offer to fix them with your best suggestion
15+
4. Ask for confirmation before proceeding
16+
5. After fixing, run `npx -y cc-safety-net --verify-config` to verify again

AGENTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ A Claude Code / OpenCode plugin that blocks destructive git and filesystem comma
2323

2424
Runs on commit (in order): knip → lint-staged (biome check --write)
2525

26+
## Commit Conventions
27+
28+
When committing changes to files in `commands/`, `hooks/`, or `.opencode/`, use only `fix` or `feat` commit types. These directories contain user-facing skill definitions and hook configurations that represent features or fixes to the plugin's capabilities.
29+
2630
## Code Style (TypeScript)
2731

2832
### Formatting

CLAUDE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ The hook receives JSON input on stdin containing `tool_name` and `tool_input`. F
6060
- Use `type | null` syntax (not `undefined` where possible)
6161
- Use kebab-case for file names (`rules-git.ts`, not `rulesGit.ts`)
6262

63+
## Commit Conventions
64+
65+
When committing changes to files in `commands/`, `hooks/`, or `.opencode/`, use only `fix` or `feat` commit types. These directories contain user-facing skill definitions and hook configurations that represent features or fixes to the plugin's capabilities.
66+
6367
## Environment Variables
6468

6569
- `SAFETY_NET_STRICT=1`: Strict mode (fail-closed on unparseable hook input/commands)

CONTRIBUTING.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ bun run check
5353

5454
## Claude Code
5555

56-
1. **Build and link the project**:
56+
1. **Build the project**:
5757
```bash
58-
bun run build && bun link
58+
bun run build
5959
```
6060

6161
2. **Disable the safety-net plugin** in Claude Code (if installed) and exit Claude Code completely.
@@ -74,19 +74,14 @@ bun run check
7474
git checkout -b test-branch
7575
```
7676

77-
5. **Unlink the project** when you're done:
78-
```bash
79-
bun unlink
80-
```
81-
8277
> [!NOTE]
8378
> See the [official documentation](https://docs.anthropic.com/en/docs/claude-code/plugins#test-your-plugins-locally) for more details on testing plugins locally.
8479
8580
## OpenCode
8681

87-
1. **Build and link the project**:
82+
1. **Build the project**:
8883
```bash
89-
bun run build && bun link
84+
bun run build
9085
```
9186

9287
2. **Update your OpenCode config** (`~/.config/opencode/opencode.json` or `opencode.jsonc`):
@@ -109,6 +104,7 @@ bun run check
109104

110105
> [!NOTE]
111106
> Remove `"cc-safety-net"` from the plugin array if it exists, to avoid conflicts with the npm version.
107+
> Or comment out the line if you're using `opencode.jsonc`.
112108
113109
3. **Restart OpenCode** to load the changes.
114110

@@ -123,13 +119,6 @@ bun run check
123119
git checkout -b test-branch
124120
```
125121

126-
6. **Manually copy or add commands** to `~/.config/opencode/command` or `.opencode/command` if you modified or added any new commands.
127-
128-
7. **Unlink the project** when you're done:
129-
```bash
130-
bun unlink
131-
```
132-
133122
> [!NOTE]
134123
> See the [official documentation](https://opencode.ai/docs/plugins/) for more details on OpenCode plugins.
135124

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ This hook provides **semantic command analysis**: it parses arguments, understan
5757

5858
## Prerequisites
5959

60-
- **Node.js**: Version 18 or higher is required to run this plugin in *Claude Code*
60+
- **Node.js**: Version 18 or higher is required to run this plugin
6161

6262
## Quick Start
6363

@@ -87,8 +87,8 @@ Paste this into any LLM agent (Claude Code, OpenCode, Cursor, etc.):
8787
Install the cc-safety-net plugin in `~/.config/opencode/opencode.json` (or `.jsonc`) according to the schema at: https://opencode.ai/config.json
8888
8989
Then copy the following files to `~/.config/opencode/command/`:
90-
- https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/commands/set-custom-rules.md
91-
- https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/commands/verify-custom-rules.md
90+
- https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/.opencode/command/set-custom-rules.md
91+
- https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/.opencode/command/verify-custom-rules.md
9292
```
9393

9494
**Option B: Manual setup**

commands/set-custom-rules.md

Lines changed: 93 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,124 @@
11
---
2-
description: Set custom rules for the project
3-
allowed-tools: Bash, Read, Write, Glob, AskUserQuestion
2+
description: Set custom rules for Safety Net
3+
allowed-tools: Bash, Read, Write, AskUserQuestion
44
---
55

6-
# Set Custom Rules for Safety Net
7-
86
You are helping the user configure custom blocking rules for claude-code-safety-net.
9-
ALWAYS use AskUserQuestion tool when you need to ask the user questions.
7+
8+
## Context
9+
10+
### Schema Documentation
11+
12+
!`npx -y cc-safety-net --custom-rules-doc`
1013

1114
## Your Task
1215

1316
Follow this flow exactly:
1417

15-
### Step 1: Run `npx -y cc-safety-net --custom-rules-doc` to read the full schema details, field constraints, and usage examples
18+
### Step 1: Ask for Scope
19+
20+
Use AskUserQuestion to let user select scope:
21+
22+
```json
23+
{
24+
"questions": [
25+
{
26+
"question": "Which scope would you like to configure?",
27+
"header": "Configure",
28+
"multiSelect": false,
29+
"options": [
30+
{
31+
"label": "User",
32+
"description": "(`~/.cc-safety-net/config.json`) - applies to all your projects"
33+
},
34+
{
35+
"label": "Project",
36+
"description": "(`.safety-net.json`) - applies only to this project"
37+
}
38+
]
39+
}
40+
]
41+
}
42+
```
1643

17-
### Step 2: Show Examples and Ask for Scope
44+
### Step 2: Show Examples and Ask for Rules
1845

1946
Show examples in natural language:
2047
- "Block `git add -A` and `git add .` to prevent blanket staging"
2148
- "Block `npm install -g` to prevent global package installs"
2249
- "Block `docker system prune` to prevent accidental cleanup"
2350

24-
Ask: **Which scope would you like to configure?**
25-
- **User** (`~/.cc-safety-net/config.json`) - applies to all your projects
26-
- **Project** (`.safety-net.json`) - applies only to this project
27-
28-
### Step 3: Ask for Rules
29-
3051
Ask the user to describe rules in natural language. They can list multiple.
3152

32-
### Step 4: Generate JSON Config
33-
34-
Parse user input and generate valid schema JSON.
35-
36-
### Step 5: Show Config and Confirm
37-
38-
Display the generated JSON and ask:
39-
- "Does this look correct?"
40-
- "Would you like to modify anything?"
41-
42-
### Step 6: Check for Existing Config
43-
44-
Check if config exists at target location:
45-
```bash
46-
cat ~/.cc-safety-net/config.json 2>/dev/null # user scope
47-
cat .safety-net.json 2>/dev/null # project scope
53+
### Step 3: Generate and Show JSON Config
54+
55+
Parse user input and generate valid schema JSON using the schema documentation above.
56+
57+
Then show the generated config JSON to the user.
58+
59+
### Step 4: Ask for Confirmation
60+
61+
Use AskUserQuestion to let user choose:
62+
63+
```json
64+
{
65+
"questions": [
66+
{
67+
"question": "Does this look correct?",
68+
"header": "Confirmation",
69+
"multiSelect": false,
70+
"options": [
71+
{
72+
"label": "Yes",
73+
},
74+
{
75+
"label": "No",
76+
}
77+
]
78+
}
79+
]
80+
}
4881
```
4982

50-
If exists:
51-
1. Show existing config
52-
2. Ask: **Merge** (add new rules, duplicates use new version) or **Replace**?
53-
54-
### Step 7: Validate and Write
55-
56-
For user scope, ensure directory exists:
57-
```bash
58-
mkdir -p ~/.cc-safety-net
83+
### Step 5: Check and Handle Existing Config
84+
85+
1. Check existing User Config with `cat ~/.cc-safety-net/config.json 2>/dev/null || echo "No user config found"`
86+
2. Check existing Project Config with `cat .safety-net.json 2>/dev/null || echo "No project config found"`
87+
88+
If the chosen scope already has a config:
89+
90+
Show the existing config to the user.
91+
Use AskUserQuestion tool to let user choose:
92+
```json
93+
{
94+
"questions": [
95+
{
96+
"question": "The chosen scope already has a config. What would you like to do?",
97+
"header": "Configure",
98+
"multiSelect": false,
99+
"options": [
100+
{
101+
"label": "Merge",
102+
},
103+
{
104+
"label": "Replace",
105+
}
106+
]
107+
}
108+
]
109+
}
59110
```
60111

61-
Write config, then validate:
62-
```bash
63-
npx -y cc-safety-net --verify-config
64-
```
112+
### Step 6: Write and Validate
113+
114+
Write the config to the chosen scope, then validate with `npx -y cc-safety-net --verify-config`.
65115

66116
If validation errors:
67117
- Show specific errors
68118
- Offer to fix with your best suggestion
69119
- Confirm before proceeding
70120

71-
### Step 8: Confirm Success
121+
### Step 7: Confirm Success
72122

73123
Tell the user:
74124
1. Config saved to [path]

commands/verify-custom-rules.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
---
2-
description: Verify custom rules for the project
3-
allowed-tools: Bash, AskUserQuestion
2+
description: Verify custom rules for Safety Net
3+
allowed-tools: Bash, Read, Write, AskUserQuestion
44
---
55

6-
# Verify Custom Rules for Safety Net
7-
86
You are helping the user verify the custom rules config file.
9-
ALWAYS use AskUserQuestion tool when you need to ask the user questions.
107

118
## Your Task
129

13-
Follow this flow exactly:
14-
15-
### Step 1: Run `npx -y cc-safety-net --custom-rules-doc` to read the full schema details, field constraints, and usage examples
16-
17-
### Step 2: Run `npx -y cc-safety-net --verify-config` to verify the config file
18-
19-
If the config has validation errors, inform the user:
20-
- Show the specific validation errors
21-
- Offer to fix them with your best suggestion
22-
- Ask for confirmation before proceeding
10+
Run `npx -y cc-safety-net --verify-config` to check current validation status
2311

24-
After fixing the config, run `npx -y cc-safety-net --verify-config` to verify it again.
12+
If the config has validation errors:
13+
1. Show the specific validation errors
14+
2. Run `npx -y cc-safety-net --custom-rules-doc` to read the schema documentation
15+
3. Use AskUserQuestion tool to offer fixes with your best suggestions
16+
4. After fixing, run `npx -y cc-safety-net --verify-config` to verify again

0 commit comments

Comments
 (0)