-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlearning_activations.rs
More file actions
145 lines (132 loc) · 4.42 KB
/
Copy pathlearning_activations.rs
File metadata and controls
145 lines (132 loc) · 4.42 KB
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
//! Memory/skill activation tracking (spec §7.6 / §22).
//!
//! Records when knowledge is injected so utility can be measured beyond
//! `memory get`. Fail-open; capped JSONL under the project learning dir.
#![allow(dead_code)]
use serde::{Deserialize, Serialize};
use crate::learning_store::{self, ProjectLearningPaths, MAX_ACTIVATIONS};
/// Retrieval stage labels (spec §7.6 / §14.4).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RetrievalStage {
PrePlan,
Implementation,
ErrorRecovery,
PreValidation,
Reflection,
}
impl RetrievalStage {
pub fn as_str(self) -> &'static str {
match self {
Self::PrePlan => "pre_plan",
Self::Implementation => "implementation",
Self::ErrorRecovery => "error_recovery",
Self::PreValidation => "pre_validation",
Self::Reflection => "reflection",
}
}
pub fn parse(s: &str) -> Self {
match s.trim().to_lowercase().as_str() {
"implementation" => Self::Implementation,
"error_recovery" | "error" => Self::ErrorRecovery,
"pre_validation" | "validation" => Self::PreValidation,
"reflection" => Self::Reflection,
_ => Self::PrePlan,
}
}
}
/// Compact activation record (spec §7.6).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LearningActivation {
pub id: String,
pub project_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub episode_id: Option<String>,
pub item_kind: String,
pub item_id: String,
pub stage: String,
pub rank: usize,
pub retrieval_score: f32,
pub tokens_injected: usize,
#[serde(default)]
pub explicitly_opened: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub followed_by_agent: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub outcome: Option<String>,
}
fn now_secs() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
/// Append one activation (fail-open).
pub fn record_activation(act: &LearningActivation) {
let paths = ProjectLearningPaths::resolve(&act.project_id);
let _ = paths.ensure();
learning_store::append_jsonl(&paths.activations, act, MAX_ACTIVATIONS);
}
/// Record a batch of pack-injected items for a stage.
pub fn record_pack_activations(
project_id: &str,
stage: RetrievalStage,
episode_id: Option<&str>,
items: &[(
/*kind*/ &str,
/*id*/ &str,
/*rank*/ usize,
/*score*/ f32,
/*tokens*/ usize,
)],
) {
let ts = now_secs();
for (i, (kind, id, rank, score, tokens)) in items.iter().enumerate() {
let act = LearningActivation {
id: format!("act-{ts}-{i}"),
project_id: project_id.to_string(),
episode_id: episode_id.map(|s| s.to_string()),
item_kind: (*kind).to_string(),
item_id: (*id).to_string(),
stage: stage.as_str().to_string(),
rank: *rank,
retrieval_score: *score,
tokens_injected: *tokens,
explicitly_opened: false,
followed_by_agent: None,
outcome: None,
};
record_activation(&act);
}
}
/// Load activations (skips malformed lines).
pub fn load_activations(project_id: &str) -> Vec<LearningActivation> {
let paths = ProjectLearningPaths::resolve(project_id);
learning_store::read_jsonl(&paths.activations)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::learning_store::{learning_test_serial, override_learning_root};
#[test]
fn record_and_load_activations() {
let _serial = learning_test_serial()
.lock()
.unwrap_or_else(|e| e.into_inner());
let root = std::env::temp_dir().join(format!("act-{}-{}", std::process::id(), now_secs()));
let _ = std::fs::remove_dir_all(&root);
let _g = override_learning_root(root);
record_pack_activations(
"project-test",
RetrievalStage::PrePlan,
None,
&[
("memory", "foo", 0, 0.8, 40),
("episode", "ep-1", 1, 0.5, 20),
],
);
let loaded = load_activations("project-test");
assert_eq!(loaded.len(), 2);
assert_eq!(loaded[0].item_id, "foo");
assert_eq!(loaded[0].stage, "pre_plan");
}
}