mirror of
https://github.com/openai/codex.git
synced 2026-11-01 16:17:58 +00:00
Based on work from Vincent K - https://github.com/openai/codex/pull/19060 <img width="1836" height="642" alt="CleanShot 2026-04-29 at 20 47 40@2x" src="https://github.com/user-attachments/assets/b647bb89-65fe-40c8-80b0-7a6b7c984634" /> ## Why Compaction rewrites the conversation context that future model turns receive, but hooks currently have no deterministic lifecycle point around that rewrite. This adds compact lifecycle hooks so users can audit manual and automatic compaction, surface hook messages in the UI, and run post-compaction follow-up without overloading tool or prompt hooks. ## What Changed - Added `PreCompact` and `PostCompact` hook events across hook config, discovery, dispatch, generated schemas, app-server notifications, analytics, and TUI hook rendering. - Added trigger matching for compact hooks with the documented `manual` and `auto` matcher values. - Wired `PreCompact` before both local and remote compaction, and `PostCompact` after successful local or remote compaction. - Kept compact hook command input to lifecycle metadata: session id, Codex turn id, transcript path, cwd, hook event name, model, and trigger. - Made compact stdout handling consistent with other hooks: plain stdout is ignored as debug output, while malformed JSON-looking stdout is reported as failed hook output. - Added integration coverage for compact hook dispatch, trigger matching, post-compact execution, and the audited behavior that `decision:"block"` does not block compaction. ## Out of Scope - Hook-specific compaction blocking is not implemented; `decision:"block"` and exit-code-2 blocking semantics are intentionally unsupported for `PreCompact`. - Custom compaction instructions are not exposed to compact hooks in this PR. - Compact summaries, summary character counts, and summary previews are not exposed to compact hooks in this PR. ## Verification - `cargo test -p codex-hooks` - `cargo test -p codex-core manual_pre_compact_block_decision_does_not_block_compaction` - `cargo test -p codex-app-server hooks_list` - `cargo test -p codex-core config_schema_matches_fixture` - `cargo test -p codex-tui hooks_browser` ## Docs The developer documentation for Codex hooks should be updated alongside this feature to document `PreCompact` and `PostCompact`, the `manual`/`auto` matcher values, and the compact hook payload fields. --------- Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
155 lines
4.5 KiB
Rust
155 lines
4.5 KiB
Rust
use super::shared::v2_enum_from_core;
|
|
use codex_protocol::protocol::HookEventName as CoreHookEventName;
|
|
use codex_protocol::protocol::HookExecutionMode as CoreHookExecutionMode;
|
|
use codex_protocol::protocol::HookHandlerType as CoreHookHandlerType;
|
|
use codex_protocol::protocol::HookOutputEntry as CoreHookOutputEntry;
|
|
use codex_protocol::protocol::HookOutputEntryKind as CoreHookOutputEntryKind;
|
|
use codex_protocol::protocol::HookRunStatus as CoreHookRunStatus;
|
|
use codex_protocol::protocol::HookRunSummary as CoreHookRunSummary;
|
|
use codex_protocol::protocol::HookScope as CoreHookScope;
|
|
use codex_protocol::protocol::HookSource as CoreHookSource;
|
|
use codex_protocol::protocol::HookTrustStatus as CoreHookTrustStatus;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use ts_rs::TS;
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookEventName from CoreHookEventName {
|
|
PreToolUse, PermissionRequest, PostToolUse, PreCompact, PostCompact, SessionStart, UserPromptSubmit, Stop
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookHandlerType from CoreHookHandlerType {
|
|
Command, Prompt, Agent
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookExecutionMode from CoreHookExecutionMode {
|
|
Sync, Async
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookScope from CoreHookScope {
|
|
Thread, Turn
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookSource from CoreHookSource {
|
|
System,
|
|
User,
|
|
Project,
|
|
Mdm,
|
|
SessionFlags,
|
|
Plugin,
|
|
CloudRequirements,
|
|
LegacyManagedConfigFile,
|
|
LegacyManagedConfigMdm,
|
|
Unknown,
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookTrustStatus from CoreHookTrustStatus {
|
|
Managed, Untrusted, Trusted, Modified
|
|
}
|
|
);
|
|
|
|
fn default_hook_source() -> HookSource {
|
|
HookSource::Unknown
|
|
}
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookRunStatus from CoreHookRunStatus {
|
|
Running, Completed, Failed, Blocked, Stopped
|
|
}
|
|
);
|
|
|
|
v2_enum_from_core!(
|
|
pub enum HookOutputEntryKind from CoreHookOutputEntryKind {
|
|
Warning, Stop, Feedback, Context, Error
|
|
}
|
|
);
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export_to = "v2/")]
|
|
pub struct HookOutputEntry {
|
|
pub kind: HookOutputEntryKind,
|
|
pub text: String,
|
|
}
|
|
|
|
impl From<CoreHookOutputEntry> for HookOutputEntry {
|
|
fn from(value: CoreHookOutputEntry) -> Self {
|
|
Self {
|
|
kind: value.kind.into(),
|
|
text: value.text,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export_to = "v2/")]
|
|
pub struct HookRunSummary {
|
|
pub id: String,
|
|
pub event_name: HookEventName,
|
|
pub handler_type: HookHandlerType,
|
|
pub execution_mode: HookExecutionMode,
|
|
pub scope: HookScope,
|
|
pub source_path: AbsolutePathBuf,
|
|
#[serde(default = "default_hook_source")]
|
|
pub source: HookSource,
|
|
pub display_order: i64,
|
|
pub status: HookRunStatus,
|
|
pub status_message: Option<String>,
|
|
pub started_at: i64,
|
|
pub completed_at: Option<i64>,
|
|
pub duration_ms: Option<i64>,
|
|
pub entries: Vec<HookOutputEntry>,
|
|
}
|
|
|
|
impl From<CoreHookRunSummary> for HookRunSummary {
|
|
fn from(value: CoreHookRunSummary) -> Self {
|
|
Self {
|
|
id: value.id,
|
|
event_name: value.event_name.into(),
|
|
handler_type: value.handler_type.into(),
|
|
execution_mode: value.execution_mode.into(),
|
|
scope: value.scope.into(),
|
|
source_path: value.source_path,
|
|
source: value.source.into(),
|
|
display_order: value.display_order,
|
|
status: value.status.into(),
|
|
status_message: value.status_message,
|
|
started_at: value.started_at,
|
|
completed_at: value.completed_at,
|
|
duration_ms: value.duration_ms,
|
|
entries: value.entries.into_iter().map(Into::into).collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export_to = "v2/")]
|
|
pub struct HookStartedNotification {
|
|
pub thread_id: String,
|
|
pub turn_id: Option<String>,
|
|
pub run: HookRunSummary,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export_to = "v2/")]
|
|
pub struct HookCompletedNotification {
|
|
pub thread_id: String,
|
|
pub turn_id: Option<String>,
|
|
pub run: HookRunSummary,
|
|
}
|