- 1
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] - 2
- 3
//! Capacity activities are ledger entries but never model-visible - 4
//! (docs/design/68-context-engine.md §1, §4): `SessionLog::latest_capacity_profile` - 5
//! reconstructs the most recent profile for a key from `CapacityProbe` / - 6
//! `CapacityFeedback` activities, and a session carrying them projects - 7
//! exactly as it would without them. - 8
//! - 9
//! `CapacityProfile`/`ProfileKey` live in vak-agent (which depends on - 10
//! vak-session, not the reverse), so this test stands in with small local - 11
//! types that have the same JSON shape a real caller would serialize — - 12
//! `latest_capacity_profile` only round-trips JSON, it never interprets it. - 13
- 14
use std::path::PathBuf; - 15
- 16
use serde::{Deserialize, Serialize}; - 17
use tempfile::tempdir; - 18
- 19
use vak_llm::Message; - 20
use vak_session::SessionLog; - 21
use vak_session::types::{ - 22
ActivityKind, ActivityRecord, ActivityStatus, FrozenContract, MessageRecord, SessionHeader, - 23
}; - 24
- 25
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] - 26
struct FakeKey { - 27
provider: String, - 28
model: String, - 29
quantisation: Option<String>, - 30
} - 31
- 32
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] - 33
struct FakeProfile { - 34
instruction_horizon_tokens: u64, - 35
needs_reprobe: bool, - 36
} - 37
- 38
fn header() -> SessionHeader { - 39
SessionHeader { - 40
agent: None, - 41
session_id: "s-capacity".into(), - 42
created_at: chrono::Utc::now(), - 43
cwd: PathBuf::from("/tmp/proj"), - 44
parent_session_id: None, - 45
contract_id: None, - 46
work_item_id: None, - 47
conversation: None, - 48
contract: FrozenContract { - 49
app_version: "0.1.0".into(), - 50
provider: "ollama".into(), - 51
model: "gemma4:e2b-mlx".into(), - 52
route_ladder: Vec::new(), - 53
route_objective: String::new(), - 54
route_annotations: Vec::new(), - 55
system_prompt: "system prompt v1".into(), - 56
permission_mode: "workspace-write".into(), - 57
capabilities: Vec::new(), - 58
prompt_layers: Vec::new(), - 59
}, - 60
} - 61
} - 62
- 63
fn capacity_activity( - 64
kind: ActivityKind, - 65
key: &FakeKey, - 66
profile: &FakeProfile, - 67
id: &str, - 68
) -> ActivityRecord { - 69
let mut data = std::collections::BTreeMap::new(); - 70
data.insert("key".into(), serde_json::to_string(key).unwrap()); - 71
data.insert("profile".into(), serde_json::to_string(profile).unwrap()); - 72
ActivityRecord { - 73
activity_id: id.into(), - 74
turn: None, - 75
kind, - 76
status: ActivityStatus::Succeeded, - 77
label: "capacity".into(), - 78
detail: None, - 79
data, - 80
} - 81
} - 82
- 83
#[test] - 84
fn latest_capacity_profile_returns_the_most_recent_matching_entry() { - 85
let dir = tempdir().unwrap(); - 86
let path = dir.path().join("capacity.jsonl"); - 87
let mut log = SessionLog::create(path, header()).unwrap(); - 88
- 89
let key = FakeKey { - 90
provider: "ollama".into(), - 91
model: "gemma4:e2b-mlx".into(), - 92
quantisation: None, - 93
}; - 94
let other_key = FakeKey { - 95
provider: "anthropic".into(), - 96
model: "claude-sonnet-5".into(), - 97
quantisation: None, - 98
}; - 99
- 100
let probed = FakeProfile { - 101
instruction_horizon_tokens: 20_000, - 102
needs_reprobe: false, - 103
}; - 104
let fed_back = FakeProfile { - 105
instruction_horizon_tokens: 18_000, - 106
needs_reprobe: true, - 107
}; - 108
let unrelated = FakeProfile { - 109
instruction_horizon_tokens: 100_000, - 110
needs_reprobe: false, - 111
}; - 112
- 113
log.append_activity(capacity_activity( - 114
ActivityKind::CapacityProbe, - 115
&key, - 116
&probed, - 117
"a1", - 118
)) - 119
.unwrap(); - 120
log.append_activity(capacity_activity( - 121
ActivityKind::CapacityProbe, - 122
&other_key, - 123
&unrelated, - 124
"a2", - 125
)) - 126
.unwrap(); - 127
log.append_activity(capacity_activity( - 128
ActivityKind::CapacityFeedback, - 129
&key, - 130
&fed_back, - 131
"a3", - 132
)) - 133
.unwrap(); - 134
- 135
let latest: FakeProfile = log - 136
.latest_capacity_profile(&key) - 137
.expect("a profile must be found for the matching key"); - 138
assert_eq!( - 139
latest, fed_back, - 140
"feedback activity must win over the earlier probe" - 141
); - 142
- 143
let latest_other: FakeProfile = log - 144
.latest_capacity_profile(&other_key) - 145
.expect("the other key's profile must be found independently"); - 146
assert_eq!(latest_other, unrelated); - 147
- 148
let missing_key = FakeKey { - 149
provider: "openai".into(), - 150
model: "gpt-5.6".into(), - 151
quantisation: None, - 152
}; - 153
assert!( - 154
log.latest_capacity_profile::<_, FakeProfile>(&missing_key) - 155
.is_none() - 156
); - 157
} - 158
- 159
#[test] - 160
fn capacity_activities_never_change_the_model_visible_projection() { - 161
let dir = tempdir().unwrap(); - 162
let path = dir.path().join("derive_parity.jsonl"); - 163
let mut log = SessionLog::create(path, header()).unwrap(); - 164
log.append_message(MessageRecord { - 165
message: Message::user_text("what's the weather"), - 166
meta: None, - 167
}) - 168
.unwrap(); - 169
let before = log.derive_messages(); - 170
- 171
let key = FakeKey { - 172
provider: "ollama".into(), - 173
model: "gemma4:e2b-mlx".into(), - 174
quantisation: None, - 175
}; - 176
let profile = FakeProfile { - 177
instruction_horizon_tokens: 13_000, - 178
needs_reprobe: false, - 179
}; - 180
log.append_activity(capacity_activity( - 181
ActivityKind::CapacityProbe, - 182
&key, - 183
&profile, - 184
"p1", - 185
)) - 186
.unwrap(); - 187
log.append_activity(capacity_activity( - 188
ActivityKind::CapacityFeedback, - 189
&key, - 190
&profile, - 191
"p2", - 192
)) - 193
.unwrap(); - 194
- 195
let after = log.derive_messages(); - 196
assert_eq!( - 197
before, after, - 198
"capacity-probe/capacity-feedback activities must never reach the model" - 199
); - 200
} - 201
Indexing the workspace…
Vakyartha documentation is discovering safe artifacts, anchors, and source references.