use super::*;
pub(super) fn commands_digest(hooks: &CompiledLifecycleHooks) -> String {
let mut lines: Vec<String> = Vec::new();
hooks.for_each_command(|event, matcher, command| {
let entry = LifecycleHookCommandPreview {
event,
matcher: matcher.map(str::to_string),
command: command.command.clone(),
timeout_seconds: command.timeout_seconds,
};
if let Ok(line) = serde_json::to_string(&entry) {
lines.push(line);
}
});
lines.sort();
vtcode_commons::utils::calculate_sha256(lines.join("\n").as_bytes())
}
pub async fn restore_workspace_hook_approval(engine: &LifecycleHookEngine, workspace: &std::path::Path) {
if !engine.workspace_gated() {
return;
}
let digest = engine.command_digest().to_string();
let Ok(Some(record)) = load_lifecycle_hook_approval(workspace).await else {
return;
};
if record.config_digest == digest {
engine.approve_workspace_hooks().await;
}
}
pub async fn carry_or_restore_workspace_hook_approval(
previous: Option<&LifecycleHookEngine>,
next: &LifecycleHookEngine,
workspace: &std::path::Path,
) {
let previous_approved_same_set = match previous {
Some(prev) => {
prev.workspace_gated()
&& prev.command_digest() == next.command_digest()
&& !prev.workspace_hooks_need_approval().await
}
None => false,
};
if previous_approved_same_set {
next.approve_workspace_hooks().await;
} else {
restore_workspace_hook_approval(next, workspace).await;
}
}