nexus-memory-hooks 1.3.0

Agent hooks system for Nexus Memory System - automated memory extraction
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Common types for the hooks system

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Support tier for an agent hook integration.
///
/// Describes the *actual* depth of lifecycle integration Nexus has with
/// a given agent, not the theoretical maximum. Each agent integration
/// must report its honest tier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SupportTier {
    /// Dedicated hook implementation with native skill/hook file installation.
    /// Multiple lifecycle events are wired (session start/end, checkpoint, error, compact).
    NativeLifecycle,

    /// Agent uses a shared generic wrapper (CLIHook) for process detection
    /// and atexit fallback. No dedicated hook implementation exists.
    WrapperLifecycle,

    /// Process detection only. No native hooks, no session lifecycle events.
    /// Memory capture relies on process monitoring and inactivity detection.
    MonitorOnly,
}

impl std::fmt::Display for SupportTier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SupportTier::NativeLifecycle => write!(f, "native-lifecycle"),
            SupportTier::WrapperLifecycle => write!(f, "wrapper-lifecycle"),
            SupportTier::MonitorOnly => write!(f, "monitor-only"),
        }
    }
}

/// Supported agent types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AgentType {
    // Native lifecycle integrations
    ClaudeCode,
    PiMono,
    OhMyPi,
    PiSkills,

    // Monitor-only (process detection, no native hooks)
    Gemini,
    Qwen,

    // CLI wrapper agents
    OpenCode,
    Codex,
    Amp,
    Droid,
    Hermes,

    // Generic
    Generic,
}

impl std::fmt::Display for AgentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AgentType::ClaudeCode => write!(f, "claude-code"),
            AgentType::Gemini => write!(f, "gemini"),
            AgentType::Qwen => write!(f, "qwen"),
            AgentType::PiMono => write!(f, "pi-mono"),
            AgentType::OhMyPi => write!(f, "oh-my-pi"),
            AgentType::PiSkills => write!(f, "pi-skills"),
            AgentType::OpenCode => write!(f, "opencode"),
            AgentType::Codex => write!(f, "codex"),
            AgentType::Amp => write!(f, "amp"),
            AgentType::Droid => write!(f, "droid"),
            AgentType::Hermes => write!(f, "hermes"),
            AgentType::Generic => write!(f, "generic"),
        }
    }
}

impl AgentType {
    /// Parse from string
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "claude-code" | "claude" => Some(Self::ClaudeCode),
            "gemini" => Some(Self::Gemini),
            "qwen" => Some(Self::Qwen),
            "pi-mono" | "pimono" | "pi" => Some(Self::PiMono),
            "oh-my-pi" | "ohmypi" | "omp" => Some(Self::OhMyPi),
            "pi-skills" => Some(Self::PiSkills),
            "opencode" => Some(Self::OpenCode),
            "codex" => Some(Self::Codex),
            "amp" => Some(Self::Amp),
            "droid" | "factory" | "factory-cli" => Some(Self::Droid),
            "hermes" | "hermes-cli" => Some(Self::Hermes),
            _ => None,
        }
    }

    /// Get the detection layer type for this agent
    pub fn detection_layer(&self) -> DetectionLayer {
        match self {
            AgentType::ClaudeCode
            | AgentType::PiMono
            | AgentType::OhMyPi
            | AgentType::PiSkills
            | AgentType::Droid => DetectionLayer::Native,
            AgentType::Gemini | AgentType::Qwen => DetectionLayer::Monitor,
            AgentType::OpenCode | AgentType::Codex | AgentType::Amp | AgentType::Hermes => {
                DetectionLayer::CLI
            }
            AgentType::Generic => DetectionLayer::CLI,
        }
    }

    /// Get process names to detect for this agent
    pub fn process_names(&self) -> &'static [&'static str] {
        match self {
            AgentType::ClaudeCode => &["claude", "claude-code"],
            AgentType::Gemini => &["gemini", "gemini-cli"],
            AgentType::Qwen => &["qwen", "qwen-agent"],
            AgentType::PiMono => &["pi", "pi-coding-agent", "pi-mono"],
            AgentType::OhMyPi => &["omp", "oh-my-pi", "ohmypi"],
            AgentType::PiSkills => &["pi-skills"],
            AgentType::OpenCode => &["opencode"],
            AgentType::Codex => &["codex", "codex-cli"],
            AgentType::Amp => &["amp"],
            AgentType::Droid => &["droid", "factory-cli"],
            AgentType::Hermes => &["hermes", "hermes-cli"],
            AgentType::Generic => &[],
        }
    }

    /// Get config directory for this agent
    pub fn config_dir(&self) -> &'static str {
        match self {
            AgentType::ClaudeCode => ".claude",
            AgentType::Gemini => ".gemini",
            AgentType::Qwen => ".qwen",
            AgentType::PiMono => ".pi",
            AgentType::OhMyPi => ".omp",
            AgentType::PiSkills => ".pi-skills",
            AgentType::OpenCode => ".opencode",
            AgentType::Codex => ".codex",
            AgentType::Amp => ".amp",
            AgentType::Droid => ".factory",
            AgentType::Hermes => ".hermes",
            AgentType::Generic => ".nexus",
        }
    }

    /// Get skills directory relative to config dir
    pub fn skills_dir(&self) -> &'static str {
        match self {
            AgentType::ClaudeCode => "skills",
            AgentType::PiMono => "agent/skills",
            AgentType::OhMyPi => "agent/skills",
            AgentType::PiSkills => "skills",
            _ => "skills",
        }
    }

    /// Get the honest support tier for this agent type.
    ///
    /// This reflects what Nexus *actually* implements, not what the agent
    /// theoretically supports. Agents with dedicated hook files, skill
    /// installation, and multiple lifecycle events are `NativeLifecycle`.
    /// Agents sharing a generic CLI wrapper are `WrapperLifecycle`.
    /// Agents with process detection only are `MonitorOnly`.
    pub fn support_tier(&self) -> SupportTier {
        match self {
            // Dedicated hook implementations with skill installation + lifecycle events
            AgentType::ClaudeCode => SupportTier::NativeLifecycle,
            AgentType::PiMono => SupportTier::NativeLifecycle,
            AgentType::OhMyPi => SupportTier::NativeLifecycle,
            AgentType::PiSkills => SupportTier::NativeLifecycle,

            // Process detection only — no native hooks wired
            AgentType::Gemini => SupportTier::MonitorOnly,
            AgentType::Qwen => SupportTier::MonitorOnly,

            // Generic CLI wrapper (CLIHook) — atexit + process detection
            AgentType::OpenCode
            | AgentType::Codex
            | AgentType::Amp
            | AgentType::Hermes
            | AgentType::Generic => SupportTier::WrapperLifecycle,
            // Dedicated Factory settings.json lifecycle integration
            AgentType::Droid => SupportTier::NativeLifecycle,
        }
    }
}

/// Detection layer type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DetectionLayer {
    /// Native hooks (Skills, Functions, etc.) - 100% reliability
    Native,
    /// CLI-based hooks (atexit, signals) - 95% reliability
    CLI,
    /// Process monitoring - 95% reliability
    Monitor,
    /// Inactivity detection - 90% reliability
    Inactivity,
    /// Buffer recovery - 99% reliability
    Buffer,
}

impl std::fmt::Display for DetectionLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DetectionLayer::Native => write!(f, "native"),
            DetectionLayer::CLI => write!(f, "cli"),
            DetectionLayer::Monitor => write!(f, "monitor"),
            DetectionLayer::Inactivity => write!(f, "inactivity"),
            DetectionLayer::Buffer => write!(f, "buffer"),
        }
    }
}

/// Source of extraction trigger
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExtractionSource {
    /// Native hook triggered
    NativeHook(String),
    /// Process monitor detected session end
    ProcessMonitor,
    /// Inactivity timeout
    InactivityTimeout,
    /// Signal handler (SIGTERM/SIGINT)
    SignalHandler(String),
    /// Buffer recovery after crash
    BufferRecovery,
    /// Manual trigger
    Manual,
}

impl std::fmt::Display for ExtractionSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExtractionSource::NativeHook(name) => write!(f, "native:{}", name),
            ExtractionSource::ProcessMonitor => write!(f, "process_monitor"),
            ExtractionSource::InactivityTimeout => write!(f, "inactivity_timeout"),
            ExtractionSource::SignalHandler(sig) => write!(f, "signal:{}", sig),
            ExtractionSource::BufferRecovery => write!(f, "buffer_recovery"),
            ExtractionSource::Manual => write!(f, "manual"),
        }
    }
}

/// SKILL.md front matter metadata
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SkillMetadata {
    pub name: String,
    pub description: Option<String>,
    pub version: Option<String>,
    pub author: Option<String>,
    #[serde(default)]
    pub triggers: Vec<SkillTrigger>,
    pub priority: Option<String>,
}

/// Skill trigger types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SkillTrigger {
    OnSessionEnd,
    OnCheckpoint,
    OnCompletion,
    OnError,
    Manual,
}

impl std::fmt::Display for SkillTrigger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SkillTrigger::OnSessionEnd => write!(f, "on_session_end"),
            SkillTrigger::OnCheckpoint => write!(f, "on_checkpoint"),
            SkillTrigger::OnCompletion => write!(f, "on_completion"),
            SkillTrigger::OnError => write!(f, "on_error"),
            SkillTrigger::Manual => write!(f, "manual"),
        }
    }
}

/// Process information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessInfo {
    pub pid: u32,
    pub name: String,
    pub status: String,
    pub command: Option<String>,
    pub working_dir: Option<String>,
    pub create_time: Option<DateTime<Utc>>,
    pub cpu_percent: Option<f32>,
    pub memory_bytes: Option<u64>,
}

/// Session activity status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionActivity {
    pub agent_type: AgentType,
    pub is_active: bool,
    pub processes: Vec<ProcessInfo>,
    pub session_id: Option<String>,
    pub last_activity: Option<DateTime<Utc>>,
    pub context: HashMap<String, serde_json::Value>,
}

impl SessionActivity {
    pub fn new(agent_type: AgentType) -> Self {
        Self {
            agent_type,
            is_active: false,
            processes: Vec::new(),
            session_id: None,
            last_activity: None,
            context: HashMap::new(),
        }
    }

    pub fn with_active(mut self, active: bool) -> Self {
        self.is_active = active;
        self
    }

    pub fn with_session_id(mut self, id: impl Into<String>) -> Self {
        self.session_id = Some(id.into());
        self
    }

    pub fn with_process(mut self, process: ProcessInfo) -> Self {
        self.processes.push(process);
        self.is_active = true;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_type_display() {
        assert_eq!(AgentType::ClaudeCode.to_string(), "claude-code");
        assert_eq!(AgentType::PiMono.to_string(), "pi-mono");
        assert_eq!(AgentType::OhMyPi.to_string(), "oh-my-pi");
    }

    #[test]
    fn test_agent_type_parse() {
        assert_eq!(AgentType::parse("claude-code"), Some(AgentType::ClaudeCode));
        assert_eq!(AgentType::parse("claude"), Some(AgentType::ClaudeCode));
        assert_eq!(AgentType::parse("pi"), Some(AgentType::PiMono));
        assert_eq!(AgentType::parse("omp"), Some(AgentType::OhMyPi));
        assert_eq!(AgentType::parse("unknown"), None);
    }

    #[test]
    fn test_agent_type_detection_layer() {
        assert_eq!(
            AgentType::ClaudeCode.detection_layer(),
            DetectionLayer::Native
        );
        assert_eq!(AgentType::PiMono.detection_layer(), DetectionLayer::Native);
        assert_eq!(AgentType::Gemini.detection_layer(), DetectionLayer::Monitor);
        assert_eq!(AgentType::Qwen.detection_layer(), DetectionLayer::Monitor);
        assert_eq!(AgentType::Amp.detection_layer(), DetectionLayer::CLI);
    }

    #[test]
    fn test_agent_type_process_names() {
        let names = AgentType::PiMono.process_names();
        assert!(names.contains(&"pi"));
        assert!(names.contains(&"pi-mono"));
    }

    #[test]
    fn test_agent_type_config_dir() {
        assert_eq!(AgentType::PiMono.config_dir(), ".pi");
        assert_eq!(AgentType::OhMyPi.config_dir(), ".omp");
        assert_eq!(AgentType::ClaudeCode.config_dir(), ".claude");
    }

    #[test]
    fn test_detection_layer_display() {
        assert_eq!(DetectionLayer::Native.to_string(), "native");
        assert_eq!(DetectionLayer::Buffer.to_string(), "buffer");
    }

    #[test]
    fn test_session_activity() {
        let activity = SessionActivity::new(AgentType::ClaudeCode)
            .with_active(true)
            .with_session_id("test-123");

        assert!(activity.is_active);
        assert_eq!(activity.session_id, Some("test-123".to_string()));
    }

    #[test]
    fn test_support_tier_display() {
        assert_eq!(SupportTier::NativeLifecycle.to_string(), "native-lifecycle");
        assert_eq!(
            SupportTier::WrapperLifecycle.to_string(),
            "wrapper-lifecycle"
        );
        assert_eq!(SupportTier::MonitorOnly.to_string(), "monitor-only");
    }

    #[test]
    fn test_agent_support_tier_honest_mapping() {
        // Native lifecycle — dedicated hook + skill installation
        assert_eq!(
            AgentType::ClaudeCode.support_tier(),
            SupportTier::NativeLifecycle
        );
        assert_eq!(
            AgentType::PiMono.support_tier(),
            SupportTier::NativeLifecycle
        );
        assert_eq!(
            AgentType::OhMyPi.support_tier(),
            SupportTier::NativeLifecycle
        );
        assert_eq!(
            AgentType::PiSkills.support_tier(),
            SupportTier::NativeLifecycle
        );
        assert_eq!(
            AgentType::Droid.support_tier(),
            SupportTier::NativeLifecycle
        );

        // Monitor-only — process detection, no native hooks
        assert_eq!(AgentType::Gemini.support_tier(), SupportTier::MonitorOnly);
        assert_eq!(AgentType::Qwen.support_tier(), SupportTier::MonitorOnly);

        // Wrapper lifecycle — generic CLIHook
        assert_eq!(
            AgentType::Codex.support_tier(),
            SupportTier::WrapperLifecycle
        );
        assert_eq!(AgentType::Amp.support_tier(), SupportTier::WrapperLifecycle);
        assert_eq!(
            AgentType::Generic.support_tier(),
            SupportTier::WrapperLifecycle
        );
    }

    #[test]
    fn test_all_agents_have_valid_detection_layer() {
        // Every agent type should have a non-empty process_names() or a
        // non-empty skills_dir() — this ensures factory hooks can actually
        // detect the agent.
        for agent_type in &[
            AgentType::ClaudeCode,
            AgentType::Gemini,
            AgentType::Qwen,
            AgentType::PiMono,
            AgentType::OhMyPi,
            AgentType::PiSkills,
            AgentType::OpenCode,
            AgentType::Codex,
            AgentType::Amp,
            AgentType::Droid,
            AgentType::Hermes,
        ] {
            assert!(
                !agent_type.config_dir().is_empty(),
                "{} must have a config_dir",
                agent_type
            );
            assert!(
                !agent_type.to_string().is_empty(),
                "{} must have a display name",
                agent_type
            );
        }
    }

    #[test]
    fn test_wrapper_agents_have_wrapper_lifecycle_tier() {
        // WrapperLifecycle agents should report session_end capability
        // since CLIHook registers an atexit callback.
        let wrapper_agents = [
            AgentType::OpenCode,
            AgentType::Codex,
            AgentType::Amp,
            AgentType::Hermes,
        ];
        for agent in &wrapper_agents {
            assert_eq!(
                agent.support_tier(),
                SupportTier::WrapperLifecycle,
                "{} should be WrapperLifecycle",
                agent
            );
        }
    }
}