Skip to content

Commit d93f2aa

Browse files
committed
fix(hook-message-injector): add validation to prevent empty message injection and improve logging
- Add content validation in injectHookMessage() to prevent empty hook content injection - Add logging to claude-code-hooks and keyword-detector for better debugging - Document timing issues in empty-message-sanitizer comments - Update README with improved setup instructions 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent e779ae7 commit d93f2aa

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,26 @@ Let the LLM Agent handle the grunt work. Paste this link and ask it to install a
129129
<details>
130130
<summary>Instructions for LLM Agents (click to expand)</summary>
131131

132-
If you're an LLM Agent helping set up oh-my-opencode, follow these steps:
132+
If you're an LLM Agent helping set up oh-my-opencode, Welcome! You'll now help your user to install the best agent harness, OhMyOpenCode. Tell user this with greetings, with test 'oMoMoMoMo...'
133133

134+
follow these steps:
134135

135-
### Step 1: Verify OpenCode Installation
136+
### Step 0: Ask user about the setup.
137+
138+
Since OpenCode Supports various models, and our plugin suggests various frontier LLMs. Thus, you must ask user, following questions:
139+
1. Do you have a Claude Pro/Max Subscription? Otherwise, we'll use 'opencode/big-pickle' (known for glm 4.6) as a main orchestrator model (not recommended)
140+
-> If user said yes, add ToDo: "Setup user Claude Pro/Max Auth"
141+
-> If user said no, add ToDo: "Override OmO agent model on oh-my-opencode.json as opencode/big-pickle"
142+
2. Do you have a ChatGPT Subscription? Otherwise, we'll use 'claude-opus-4-5' with 32k thinking option for oracle agent.
143+
-> If user said yes, add ToDo: "Setup user ChatGPT Auth"
144+
-> If user said no, add ToDo: "Override oracle agent model on oh-my-opencode.json as claude-opus-4-5 with 32k thinking"
145+
3. Will you integrate Gemini models? Otherwise, you cannot enjoy the beautiful design.
146+
-> If user said yes, add ToDo: "Setup Google OAuth"
147+
-> If user said no, add ToDo: "Override frontend-ui-ux-engineer agent model on oh-my-opencode.json as claude-opus-4-5 with 32k thinking"
148+
149+
### Step 1: Install OpenCode, if not
150+
151+
Run following command to verify OpenCode Installation:
136152

137153
```bash
138154
if command -v opencode &> /dev/null; then
@@ -143,10 +159,17 @@ else
143159
fi
144160
```
145161

146-
If OpenCode isn't installed, check the [OpenCode Installation Guide](https://opencode.ai/docs). To save context, spawn a subagent to handle installation and report back.
162+
If OpenCode isn't installed, check the [OpenCode Installation Guide](https://opencode.ai/docs).
163+
Spawn a subagent to handle installation and report back - to save context.
147164

148165
### Step 2: Configure oh-my-opencode Plugin
149166

167+
#### JSONC
168+
- If `~/.config/opencode/opencode.jsonc` exists, work in there to add `oh-my-opencode` plugin
169+
170+
#### JSON
171+
- If no opencode.jsonc exists, run following command to create and register it.
172+
150173
```bash
151174
mkdir -p ~/.config/opencode
152175

@@ -169,14 +192,15 @@ fi
169192

170193
```bash
171194
opencode --version # Should be 1.0.132 or higher
172-
cat ~/.config/opencode/opencode.json # Should contain "oh-my-opencode" in plugin array
195+
cat ~/.config/opencode/opencode.json # Should contain "oh-my-opencode" in plugin array, or maybe check jsonc
173196
```
174197

175198
### Step 4: Configure Authentication
176199

177-
Once oh-my-opencode is in the plugin list, use tmux to walk the user through authentication. Complete all steps below.
200+
As your todo, please configure authentication as user have answered to you.
201+
Following is the configuration guides for each providers. Please use interactive terminal like tmux to do following:
178202

179-
#### 4.1 Anthropic (Claude)
203+
#### Anthropic (Claude)
180204

181205
```bash
182206
opencode auth login

src/features/hook-message-injector/injector.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export function injectHookMessage(
7171
hookContent: string,
7272
originalMessage: OriginalMessageContext
7373
): boolean {
74+
// Validate hook content to prevent empty message injection
75+
if (!hookContent || hookContent.trim().length === 0) {
76+
console.warn("[hook-message-injector] Attempted to inject empty hook content, skipping injection", {
77+
sessionID,
78+
hasAgent: !!originalMessage.agent,
79+
hasModel: !!(originalMessage.model?.providerID && originalMessage.model?.modelID)
80+
})
81+
return false
82+
}
83+
7484
const messageDir = getOrCreateMessageDir(sessionID)
7585

7686
const needsFallback =

src/hooks/claude-code-hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
111111

112112
if (result.messages.length > 0) {
113113
const hookContent = result.messages.join("\n\n")
114+
log(`[claude-code-hooks] Injecting ${result.messages.length} hook messages`, { sessionID: input.sessionID, contentLength: hookContent.length })
114115
const message = output.message as {
115116
agent?: string
116117
model?: { modelID?: string; providerID?: string }

src/hooks/empty-message-sanitizer/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ interface MessageWithParts {
88
}
99

1010
type MessagesTransformHook = {
11+
// NOTE: This sanitizer runs on experimental.chat.messages.transform hook,
12+
// which executes AFTER chat.message hooks. Filesystem-injected messages
13+
// from hooks like claude-code-hooks and keyword-detector may bypass this
14+
// sanitizer if they inject empty content. Validation should be done at
15+
// injection time in injectHookMessage().
16+
1117
"experimental.chat.messages.transform"?: (
1218
input: Record<string, never>,
1319
output: { messages: MessageWithParts[] }

src/hooks/keyword-detector/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function createKeywordDetectorHook() {
4343
}
4444

4545
const context = messages.join("\n")
46+
log(`[keyword-detector] Injecting context for ${messages.length} keywords`, { sessionID: input.sessionID, contextLength: context.length })
4647
const success = injectHookMessage(input.sessionID, context, {
4748
agent: message.agent,
4849
model: message.model,

0 commit comments

Comments
 (0)