1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use std::path::Path;
use crate::kimi_native::diagnostics::{DiagResult, Severity};
#[derive(Debug, Clone, serde::Deserialize)]
struct HookConfigWrapper {
#[serde(default)]
hooks: Vec<crate::kimi_native::hook_spec::HookConfig>,
}
pub(super) async fn check_hooks(
hooks_dir: &Path,
project_dir: &Path,
results: &mut Vec<DiagResult>,
) {
// Check hooks (L1-033)
let expected_hooks = ["safety-check.sh", "completion-check.sh", "notify.sh"];
let mut missing_hooks = vec![];
for hook in &expected_hooks {
let path = hooks_dir.join(hook);
if path.exists() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = tokio::fs::metadata(&path).await {
let mode = meta.permissions().mode();
if mode & 0o111 != 0 {
results.push(DiagResult {
severity: Severity::Ok,
message: format!("Hook '{}' is executable", hook),
fix_hint: None,
});
} else {
results.push(DiagResult {
severity: Severity::Warning,
message: format!("Hook '{}' is not executable", hook),
fix_hint: Some(format!("chmod +x {}", path.display())),
});
}
}
}
#[cfg(not(unix))]
{
results.push(DiagResult {
severity: Severity::Ok,
message: format!("Hook '{}' exists", hook),
fix_hint: None,
});
}
} else {
missing_hooks.push(*hook);
}
}
if !missing_hooks.is_empty() {
results.push(DiagResult {
severity: Severity::Warning,
message: format!("Missing hooks: {}", missing_hooks.join(", ")),
fix_hint: Some("Run `omk kimi install` or `omk kimi sync`".to_string()),
});
}
// Validate hooks declared in .kimi/config.toml
let config_path = project_dir.join(".kimi").join("config.toml");
if config_path.exists() {
match tokio::fs::read_to_string(&config_path).await {
Ok(content) => match toml::from_str::<HookConfigWrapper>(&content) {
Ok(wrapper) => {
for hook in &wrapper.hooks {
let cmd_path = project_dir.join(&hook.command);
if cmd_path.exists() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = tokio::fs::metadata(&cmd_path).await {
let mode = meta.permissions().mode();
if mode & 0o111 != 0 {
results.push(DiagResult {
severity: Severity::Ok,
message: format!(
"Hook '{}' is executable",
hook.command
),
fix_hint: None,
});
} else {
results.push(DiagResult {
severity: Severity::Warning,
message: format!(
"Hook '{}' is not executable",
hook.command
),
fix_hint: Some(format!(
"chmod +x {}",
cmd_path.display()
)),
});
}
}
}
#[cfg(not(unix))]
{
results.push(DiagResult {
severity: Severity::Ok,
message: format!("Hook '{}' exists", hook.command),
fix_hint: None,
});
}
} else {
results.push(DiagResult {
severity: Severity::Warning,
message: format!(
"Hook '{}' references missing script",
hook.command
),
fix_hint: Some(format!(
"Create {} or run `omk kimi sync`",
cmd_path.display()
)),
});
}
}
}
Err(e) => {
results.push(DiagResult {
severity: Severity::Warning,
message: format!(
"Hook config '{}' is invalid TOML: {}",
config_path.display(),
e
),
fix_hint: Some(format!("Review and fix {}", config_path.display())),
});
}
},
Err(e) => {
results.push(DiagResult {
severity: Severity::Warning,
message: format!("Cannot read hook config '{}': {}", config_path.display(), e),
fix_hint: None,
});
}
}
}
}
pub(super) async fn check_hook_configs(dir: &Path, kimi_dir: &Path, results: &mut Vec<DiagResult>) {
// Check hook configs reference existing scripts (L1-033).
// config.toml is already validated in detail by check_hooks, so only
// check the example file here to avoid duplicate diagnostics.
let config_path = kimi_dir.join("hooks.toml.example");
if config_path.exists() {
match tokio::fs::read_to_string(&config_path).await {
Ok(content) => match toml::from_str::<HookConfigWrapper>(&content) {
Ok(wrapper) => {
let mut dangling = vec![];
for hook in &wrapper.hooks {
let cmd_path = dir.join(&hook.command);
if !cmd_path.exists() {
dangling.push(hook.command.clone());
}
}
if dangling.is_empty() {
results.push(DiagResult {
severity: Severity::Ok,
message: format!(
"Hook config '{}' references valid scripts",
config_path.display()
),
fix_hint: None,
});
} else {
results.push(DiagResult {
severity: Severity::Warning,
message: format!(
"Hook config '{}' references missing scripts: {}",
config_path.display(),
dangling.join(", ")
),
fix_hint: Some(
"Run `omk kimi sync` to restore missing hook scripts".to_string(),
),
});
}
}
Err(e) => {
results.push(DiagResult {
severity: Severity::Warning,
message: format!(
"Hook config '{}' is invalid TOML: {}",
config_path.display(),
e
),
fix_hint: Some(format!("Review and fix {}", config_path.display())),
});
}
},
Err(e) => {
results.push(DiagResult {
severity: Severity::Warning,
message: format!("Cannot read hook config '{}': {}", config_path.display(), e),
fix_hint: None,
});
}
}
}
}