config: add repeatable UserInstructions declarations

This commit is contained in:
Adam Perry
2026-07-07 00:56:13 +00:00
parent 935c40c5be
commit e9e1a2922c
13 changed files with 248 additions and 16 deletions
+27 -7
View File
@@ -46,6 +46,10 @@ pub struct HookEventsToml {
pub post_compact: Vec<MatcherGroup>,
#[serde(rename = "SessionStart", default)]
pub session_start: Vec<MatcherGroup>,
// UserInstructions is unconditional, so it stores handlers directly
// instead of wrapping them in matcher groups.
#[serde(rename = "UserInstructions", default)]
pub user_instructions: Vec<HookHandlerConfig>,
#[serde(rename = "UserPromptSubmit", default)]
pub user_prompt_submit: Vec<MatcherGroup>,
#[serde(rename = "SubagentStart", default)]
@@ -65,6 +69,7 @@ impl HookEventsToml {
pre_compact,
post_compact,
session_start,
user_instructions,
user_prompt_submit,
subagent_start,
subagent_stop,
@@ -76,6 +81,7 @@ impl HookEventsToml {
&& pre_compact.is_empty()
&& post_compact.is_empty()
&& session_start.is_empty()
&& user_instructions.is_empty()
&& user_prompt_submit.is_empty()
&& subagent_start.is_empty()
&& subagent_stop.is_empty()
@@ -90,12 +96,13 @@ impl HookEventsToml {
pre_compact,
post_compact,
session_start,
user_instructions,
user_prompt_submit,
subagent_start,
subagent_stop,
stop,
} = self;
[
let matcher_group_handlers = [
pre_tool_use,
permission_request,
post_tool_use,
@@ -106,14 +113,16 @@ impl HookEventsToml {
subagent_start,
subagent_stop,
stop,
]
.into_iter()
.flatten()
.map(|group| group.hooks.len())
.sum()
];
matcher_group_handlers
.into_iter()
.flatten()
.map(|group| group.hooks.len())
.sum::<usize>()
+ user_instructions.len()
}
pub fn into_matcher_groups(self) -> [(HookEventName, Vec<MatcherGroup>); 10] {
pub fn into_matcher_groups(self) -> [(HookEventName, Vec<MatcherGroup>); 11] {
[
(HookEventName::PreToolUse, self.pre_tool_use),
(HookEventName::PermissionRequest, self.permission_request),
@@ -121,6 +130,17 @@ impl HookEventsToml {
(HookEventName::PreCompact, self.pre_compact),
(HookEventName::PostCompact, self.post_compact),
(HookEventName::SessionStart, self.session_start),
(
HookEventName::UserInstructions,
if self.user_instructions.is_empty() {
Vec::new()
} else {
vec![MatcherGroup {
matcher: None,
hooks: self.user_instructions,
}]
},
),
(HookEventName::UserPromptSubmit, self.user_prompt_submit),
(HookEventName::SubagentStart, self.subagent_start),
(HookEventName::SubagentStop, self.subagent_stop),
+62
View File
@@ -112,6 +112,68 @@ statusMessage = "checking"
);
}
#[test]
fn user_instructions_event_deserializes_from_toml() {
let parsed: HookEventsToml = toml::from_str(
r#"
[[UserInstructions]]
type = "command"
command = "python3 /tmp/user_instructions.py"
"#,
)
.expect("UserInstructions hook TOML should deserialize");
assert_eq!(
parsed,
HookEventsToml {
user_instructions: vec![HookHandlerConfig::Command {
command: "python3 /tmp/user_instructions.py".to_string(),
command_windows: None,
timeout_sec: None,
r#async: false,
status_message: None,
}],
..Default::default()
}
);
}
#[test]
fn user_instructions_event_preserves_multiple_hooks() {
let parsed = toml::from_str::<HookEventsToml>(
r#"
[[UserInstructions]]
type = "command"
command = "python3 /tmp/first.py"
[[UserInstructions]]
type = "command"
command = "python3 /tmp/second.py"
"#,
)
.expect("multiple UserInstructions hooks should deserialize");
assert_eq!(
parsed.user_instructions,
vec![
HookHandlerConfig::Command {
command: "python3 /tmp/first.py".to_string(),
command_windows: None,
timeout_sec: None,
r#async: false,
status_message: None,
},
HookHandlerConfig::Command {
command: "python3 /tmp/second.py".to_string(),
command_windows: None,
timeout_sec: None,
r#async: false,
status_message: None,
},
]
);
}
#[test]
fn hooks_toml_deserializes_inline_events_and_state_map() {
let parsed: HooksToml = toml::from_str(
@@ -1,8 +1,8 @@
//! Hook events are append-only across requirements layers. The managed hook
//! directory is different: only one directory is usable on a given platform, so
//! conflicting values for the active platform fail closed. The inactive platform
//! field is first-filled to allow the same layer stack to carry OS-specific
//! directories.
//! directory is a singleton: only one directory is usable on a given platform,
//! so conflicting values for the active platform fail closed.
//! The inactive platform field is first-filled to allow the same layer stack to
//! carry OS-specific directories.
use crate::HookEventsToml;
use crate::ManagedHooksRequirementsToml;
@@ -210,12 +210,12 @@ fn append_hook_events(existing: &mut HookEventsToml, incoming: HookEventsToml) -
pre_compact,
post_compact,
session_start,
user_instructions,
user_prompt_submit,
subagent_start,
subagent_stop,
stop,
} = incoming;
let mut changed = false;
changed |= append_vec(&mut existing.pre_tool_use, pre_tool_use);
changed |= append_vec(&mut existing.permission_request, permission_request);
@@ -223,6 +223,7 @@ fn append_hook_events(existing: &mut HookEventsToml, incoming: HookEventsToml) -
changed |= append_vec(&mut existing.pre_compact, pre_compact);
changed |= append_vec(&mut existing.post_compact, post_compact);
changed |= append_vec(&mut existing.session_start, session_start);
changed |= append_vec(&mut existing.user_instructions, user_instructions);
changed |= append_vec(&mut existing.user_prompt_submit, user_prompt_submit);
changed |= append_vec(&mut existing.subagent_start, subagent_start);
changed |= append_vec(&mut existing.subagent_stop, subagent_stop);
@@ -9,7 +9,7 @@
//! - `remote_sandbox_config` is evaluated within each layer before merging.
//! - `rules.prefix_rules` append high-priority rules first.
//! - `hooks` append high-priority event groups first while failing closed on
//! active managed-dir conflicts.
//! active managed-dir and `UserInstructions` conflicts.
//! - `permissions.filesystem.deny_read` is a high-priority-first union across
//! layers.
@@ -802,6 +802,50 @@ managed_dir = "/managed/high"
assert!(err.to_string().contains("Low (req_low)"));
}
#[test]
fn user_instructions_hooks_append_across_requirements_layers() {
let composed = compose_with_hook_directory_field(
vec![
layer(
"req_low",
"Low",
r#"
[[hooks.UserInstructions]]
type = "command"
command = "low"
"#,
),
layer(
"req_high",
"High",
r#"
[[hooks.UserInstructions]]
type = "command"
command = "high"
"#,
),
],
HookDirectoryField::ManagedDir,
)
.expect("UserInstructions hooks should compose")
.expect("requirements present");
assert_eq!(
composed,
expected_requirements(
r#"
[[hooks.UserInstructions]]
type = "command"
command = "high"
[[hooks.UserInstructions]]
type = "command"
command = "low"
"#
)
);
}
#[test]
fn active_windows_managed_dir_conflicts_fail_closed() {
let err = compose_with_hook_directory_field(