-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathghost_surface_inputs.rs
More file actions
294 lines (277 loc) · 9.99 KB
/
Copy pathghost_surface_inputs.rs
File metadata and controls
294 lines (277 loc) · 9.99 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
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
//! Pure aggregations that build the per-source inputs the ghost-surface
//! detector consumes. Rust port of `packages/analyze/src/ghost-surface-inputs.ts`
//! — see AgentWorkforce/burn#273.
//!
//! These functions take a slice of `TurnRecord` (the shape that lands in
//! the burn ledger). The TS counterpart takes `EnrichedTurn[]`, but none of
//! the read fields live on the stamp enrichment, so we operate on the
//! underlying `TurnRecord` directly to avoid pulling the ledger crate into
//! `relayburn-analyze`'s dependency surface.
//!
//! The optional `userTurnTextBySession` map (used by Claude /
//! Codex slash-command miners) is sourced from the ledger's content
//! sidecar in the query layer; loading it lives outside this module so the
//! analyze crate stays I/O-free.
use std::collections::{HashMap, HashSet};
use crate::reader::{SourceKind, TurnRecord};
use crate::analyze::ghost_surface::GhostSurfaceInputs;
use crate::analyze::pricing::PricingTable;
/// Per-source set of *observed invocation names* in this slice. Each turn
/// contributes its tool-call names, any `skillName` set on a tool call, and
/// the subagent type (when present).
pub fn build_observed_names_by_source(
turns: &[TurnRecord],
) -> HashMap<SourceKind, HashSet<String>> {
let mut out: HashMap<SourceKind, HashSet<String>> = HashMap::new();
for t in turns {
let entry = out.entry(t.source).or_default();
for call in &t.tool_calls {
entry.insert(call.name.clone());
if let Some(skill) = &call.skill_name {
entry.insert(skill.clone());
}
}
if let Some(sub) = &t.subagent {
if let Some(stype) = &sub.subagent_type {
entry.insert(stype.clone());
}
}
}
out
}
/// Per-source count of distinct sessionIds seen in this slice. Drives the
/// cost multiplier (a ghost file rides in every one of those sessions).
pub fn build_session_count_by_source(turns: &[TurnRecord]) -> HashMap<SourceKind, u64> {
let mut seen: HashMap<SourceKind, HashSet<String>> = HashMap::new();
for t in turns {
seen.entry(t.source)
.or_default()
.insert(t.session_id.clone());
}
seen.into_iter()
.map(|(s, set)| (s, set.len() as u64))
.collect()
}
/// Pick a representative dollar-per-token rate for ghost-surface costing.
/// User-installed surface rides in the CACHED prefix on every call after
/// the first, so the cacheRead rate is the right basis. Pricing values
/// in `PricingTable` are per million tokens, hence the `/ 1e6` conversion.
/// Falls back to 0 (which produces $0 cost but still surfaces ghosts) when
/// no priced model is available.
pub fn pick_representative_cache_read_rate(turns: &[TurnRecord], pricing: &PricingTable) -> f64 {
let mut counts: HashMap<&str, u64> = HashMap::new();
let mut order: Vec<&str> = Vec::new();
for t in turns {
let m = t.model.as_str();
if !counts.contains_key(m) {
order.push(m);
}
*counts.entry(m).or_insert(0) += 1;
}
let mut best_model: Option<&str> = None;
let mut best_count: i64 = -1;
// Iterate in first-seen order so ties go to the earliest-seen model
// (matches the TS `Map` iteration order).
for m in order {
let c = counts[m] as i64;
if c > best_count && pricing.contains_key(m) {
best_model = Some(m);
best_count = c;
}
}
match best_model {
Some(m) => pricing[m].cache_read / 1_000_000.0,
None => 0.0,
}
}
/// Assemble a `GhostSurfaceInputs` from a slice of turns plus a pricing
/// table. The optional `user_turn_text_by_session` map should be loaded by
/// the caller (typically the CLI) from the content sidecar; pass `None`
/// to fall back to v1 (tool-call only) behaviour.
pub fn build_ghost_surface_inputs(
turns: &[TurnRecord],
pricing: &PricingTable,
user_turn_text_by_session: Option<HashMap<SourceKind, HashMap<String, Vec<String>>>>,
) -> GhostSurfaceInputs {
let observed = build_observed_names_by_source(turns);
let counts = build_session_count_by_source(turns);
let dollar_per_token = pick_representative_cache_read_rate(turns, pricing);
let user_turn_text_by_session = user_turn_text_by_session.filter(|m| !m.is_empty());
GhostSurfaceInputs {
observed_names_by_source: observed,
session_count_by_source: counts,
dollar_per_token,
claude_home: None,
codex_home: None,
opencode_projects: None,
user_turn_text_by_session,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::reader::{Subagent, ToolCall, Usage};
use crate::analyze::pricing::{ModelCost, ReasoningMode};
fn make_turn(
source: SourceKind,
session_id: &str,
model: &str,
tool_calls: Vec<ToolCall>,
subagent: Option<Subagent>,
) -> TurnRecord {
TurnRecord {
v: 1,
source,
session_id: session_id.to_string(),
session_path: None,
message_id: format!("msg-{session_id}"),
turn_index: 0,
ts: "2026-04-20T00:00:00.000Z".to_string(),
model: model.to_string(),
project: None,
project_key: None,
usage: Usage {
input: 0,
output: 0,
reasoning: 0,
cache_read: 0,
cache_create_5m: 0,
cache_create_1h: 0,
},
tool_calls,
files_touched: None,
subagent,
stop_reason: None,
activity: None,
retries: None,
has_edits: None,
fidelity: None,
}
}
fn tool_call(name: &str, skill: Option<&str>) -> ToolCall {
ToolCall {
id: "id".to_string(),
name: name.to_string(),
target: None,
args_hash: "h".to_string(),
is_error: None,
edit_pre_hash: None,
edit_post_hash: None,
skill_name: skill.map(|s| s.to_string()),
replaced_tools: None,
collapsed_calls: None,
}
}
fn subagent(stype: &str) -> Subagent {
Subagent {
is_sidechain: true,
parent_tool_use_id: None,
agent_id: None,
parent_agent_id: None,
subagent_type: Some(stype.to_string()),
description: None,
}
}
#[test]
fn observed_names_unions_calls_skills_and_subagents() {
let turns = vec![
make_turn(
SourceKind::ClaudeCode,
"s1",
"claude-sonnet-4-6",
vec![tool_call("Read", None), tool_call("Skill", Some("init"))],
Some(subagent("code-reviewer")),
),
make_turn(
SourceKind::Codex,
"s2",
"gpt",
vec![tool_call("read_file", None)],
None,
),
];
let observed = build_observed_names_by_source(&turns);
let claude = observed.get(&SourceKind::ClaudeCode).unwrap();
assert!(claude.contains("Read"));
assert!(claude.contains("Skill"));
assert!(claude.contains("init"));
assert!(claude.contains("code-reviewer"));
let codex = observed.get(&SourceKind::Codex).unwrap();
assert!(codex.contains("read_file"));
}
#[test]
fn session_count_dedups_by_session_id() {
let turns = vec![
make_turn(SourceKind::ClaudeCode, "s1", "m", vec![], None),
make_turn(SourceKind::ClaudeCode, "s1", "m", vec![], None),
make_turn(SourceKind::ClaudeCode, "s2", "m", vec![], None),
make_turn(SourceKind::Codex, "s3", "m", vec![], None),
];
let counts = build_session_count_by_source(&turns);
assert_eq!(counts.get(&SourceKind::ClaudeCode).copied(), Some(2));
assert_eq!(counts.get(&SourceKind::Codex).copied(), Some(1));
}
#[test]
fn picks_cache_read_rate_of_most_used_priced_model() {
let mut pricing: PricingTable = HashMap::new();
pricing.insert(
"claude-sonnet-4-6".to_string(),
ModelCost {
input: 0.0,
output: 0.0,
cache_read: 0.3,
cache_write: 0.0,
reasoning: None,
reasoning_mode: ReasoningMode::IncludedInOutput,
},
);
pricing.insert(
"claude-opus-4-7".to_string(),
ModelCost {
input: 0.0,
output: 0.0,
cache_read: 1.5,
cache_write: 0.0,
reasoning: None,
reasoning_mode: ReasoningMode::IncludedInOutput,
},
);
let turns = vec![
make_turn(
SourceKind::ClaudeCode,
"s1",
"claude-sonnet-4-6",
vec![],
None,
),
make_turn(
SourceKind::ClaudeCode,
"s1",
"claude-sonnet-4-6",
vec![],
None,
),
make_turn(
SourceKind::ClaudeCode,
"s2",
"claude-opus-4-7",
vec![],
None,
),
];
let rate = pick_representative_cache_read_rate(&turns, &pricing);
assert!((rate - 0.3 / 1_000_000.0).abs() < 1e-15);
}
#[test]
fn picks_zero_when_no_priced_model() {
let pricing: PricingTable = HashMap::new();
let turns = vec![make_turn(SourceKind::Codex, "s1", "unknown", vec![], None)];
assert_eq!(pick_representative_cache_read_rate(&turns, &pricing), 0.0);
}
#[test]
fn build_inputs_drops_empty_user_turn_text() {
let pricing: PricingTable = HashMap::new();
let inputs = build_ghost_surface_inputs(&[], &pricing, Some(HashMap::new()));
assert!(inputs.user_turn_text_by_session.is_none());
}
}