edda-pack 0.1.0

Context generation and budget controls for Edda
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
use edda_index::{fetch_store_line, read_index_tail, IndexRecordV1};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::Path;

const DEFAULT_INDEX_TAIL_LINES: usize = 5000;
const DEFAULT_INDEX_TAIL_MAX_BYTES: u64 = 8 * 1024 * 1024; // 8MB
const DEFAULT_PACK_TURNS: usize = 12;
const DEFAULT_PACK_BUDGET_CHARS: usize = 12000;

// ── Turn + ToolUse structs ──

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Turn {
    pub user_uuid: String,
    pub assistant_uuid: String,
    pub user_text: String,
    pub assistant_texts: Vec<String>,
    pub tool_uses: Vec<ToolUse>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolUse {
    pub id: Option<String>,
    pub name: String,
    pub command: Option<String>,
    pub description: Option<String>,
    pub file_path: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackMetadata {
    pub project_id: String,
    pub session_id: String,
    pub git_branch: String,
    pub turn_count: usize,
    pub budget_chars: usize,
}

// ── Turn alignment via uuid/parentUuid ──

/// Build turns from index records by matching assistant.parentUuid → user.uuid.
pub fn build_turns(
    project_dir: &Path,
    session_id: &str,
    max_turns: usize,
) -> anyhow::Result<Vec<Turn>> {
    let tail_lines: usize = std::env::var("EDDA_INDEX_TAIL_LINES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_INDEX_TAIL_LINES);
    let tail_bytes: u64 = std::env::var("EDDA_INDEX_TAIL_MAX_BYTES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_INDEX_TAIL_MAX_BYTES);
    let pack_turns: usize = std::env::var("EDDA_PACK_TURNS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_PACK_TURNS);
    let max_turns = max_turns.min(pack_turns);

    let index_path = project_dir
        .join("index")
        .join(format!("{session_id}.jsonl"));
    let records = read_index_tail(&index_path, tail_lines, tail_bytes)?;

    if records.is_empty() {
        return Ok(vec![]);
    }

    // Build lookup by uuid
    let by_uuid: HashMap<String, &IndexRecordV1> =
        records.iter().map(|r| (r.uuid.clone(), r)).collect();

    // Collect assistant records in order
    let assistants: Vec<&IndexRecordV1> = records
        .iter()
        .filter(|r| r.record_type == "assistant")
        .collect();

    let store_path = project_dir
        .join("transcripts")
        .join(format!("{session_id}.jsonl"));

    let mut turns = Vec::new();
    let mut seen_user_uuids = HashSet::new();

    // Process newest assistant first
    for asst_rec in assistants.iter().rev() {
        if turns.len() >= max_turns {
            break;
        }

        // Walk UP the parentUuid chain to find the real user prompt.
        // Claude Code transcript structure:
        //   user(STRING) → assistant(tool_use) → user(tool_result) → assistant(tool_use) → ... → assistant(text)
        // We start from the leaf assistant and walk up to find the root user with STRING content.
        let mut current_parent = asst_rec.parent_uuid.as_deref();
        let mut chain_tool_uses: Vec<ToolUse> = Vec::new();
        let mut real_user_uuid = String::new();
        let mut real_user_text = String::new();

        let mut depth = 0;
        const MAX_CHAIN_DEPTH: usize = 50;

        while let Some(parent_id) = current_parent {
            if depth >= MAX_CHAIN_DEPTH {
                break;
            }
            depth += 1;

            let parent_rec = match by_uuid.get(parent_id) {
                Some(r) => r,
                None => break,
            };

            if parent_rec.record_type == "user" {
                // Try to extract user text from this record
                if let Ok(raw) =
                    fetch_store_line(&store_path, parent_rec.store_offset, parent_rec.store_len)
                {
                    if let Ok(json) = serde_json::from_slice::<serde_json::Value>(&raw) {
                        let text = extract_user_text(&json);
                        if !text.is_empty() {
                            real_user_uuid = parent_rec.uuid.clone();
                            real_user_text = text;
                            break; // Found the real user prompt
                        }
                    }
                }
                // Content is array (tool_result) or empty → keep walking up
                current_parent = parent_rec.parent_uuid.as_deref();
            } else if parent_rec.record_type == "assistant" {
                // Intermediate assistant → collect its tool_uses
                if let Ok(raw) =
                    fetch_store_line(&store_path, parent_rec.store_offset, parent_rec.store_len)
                {
                    if let Ok(json) = serde_json::from_slice::<serde_json::Value>(&raw) {
                        let (_, tus) = parse_assistant_content(&json);
                        chain_tool_uses.extend(tus);
                    }
                }
                current_parent = parent_rec.parent_uuid.as_deref();
            } else {
                break; // unexpected record type
            }
        }

        if real_user_text.is_empty() || real_user_uuid.is_empty() {
            continue;
        }

        // Dedup: only one turn per real user prompt
        if !seen_user_uuids.insert(real_user_uuid.clone()) {
            continue;
        }

        // Parse final (leaf) assistant content
        let asst_raw =
            match fetch_store_line(&store_path, asst_rec.store_offset, asst_rec.store_len) {
                Ok(r) => r,
                Err(_) => continue,
            };
        let asst_json: serde_json::Value = match serde_json::from_slice(&asst_raw) {
            Ok(v) => v,
            Err(_) => continue,
        };
        let (assistant_texts, final_tool_uses) = parse_assistant_content(&asst_json);

        // Merge tool_uses: chain (reversed to chronological) + final assistant's
        chain_tool_uses.reverse();
        chain_tool_uses.extend(final_tool_uses);

        turns.push(Turn {
            user_uuid: real_user_uuid,
            assistant_uuid: asst_rec.uuid.clone(),
            user_text: real_user_text,
            assistant_texts,
            tool_uses: chain_tool_uses,
        });
    }

    Ok(turns)
}

/// Extract user text from a transcript user record.
/// Returns non-empty string only for real user prompts (STRING content).
/// Returns empty for tool_result arrays (these are tool execution results, not user input).
fn extract_user_text(user_json: &serde_json::Value) -> String {
    let content = match user_json.get("message").and_then(|m| m.get("content")) {
        Some(c) => c,
        None => return String::new(),
    };

    // String content → real user prompt
    if let Some(s) = content.as_str() {
        return s.to_string();
    }

    // Array content → check block types
    if let Some(arr) = content.as_array() {
        // If any block is tool_result, this is NOT a real user prompt
        let has_tool_result = arr
            .iter()
            .any(|b| b.get("type").and_then(|t| t.as_str()) == Some("tool_result"));
        if has_tool_result {
            return String::new();
        }

        // Extract text from text blocks (handles ARRAY(text) format)
        let texts: Vec<&str> = arr
            .iter()
            .filter_map(|b| {
                if b.get("type").and_then(|t| t.as_str()) == Some("text") {
                    b.get("text").and_then(|t| t.as_str())
                } else {
                    None
                }
            })
            .collect();
        if !texts.is_empty() {
            return texts.join(" ");
        }
    }

    String::new()
}

fn parse_assistant_content(asst_json: &serde_json::Value) -> (Vec<String>, Vec<ToolUse>) {
    let mut texts = Vec::new();
    let mut tool_uses = Vec::new();

    let content = asst_json.get("message").and_then(|m| m.get("content"));

    if let Some(arr) = content.and_then(|c| c.as_array()) {
        for block in arr {
            let block_type = block.get("type").and_then(|v| v.as_str()).unwrap_or("");
            match block_type {
                "text" => {
                    if let Some(text) = block.get("text").and_then(|v| v.as_str()) {
                        texts.push(text.to_string());
                    }
                }
                "tool_use" => {
                    let name = block
                        .get("name")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    let id = block.get("id").and_then(|v| v.as_str()).map(|s| s.into());
                    let input = block.get("input");
                    let command = input
                        .and_then(|i| i.get("command"))
                        .and_then(|c| c.as_str())
                        .map(|s| s.into());
                    let description = input
                        .and_then(|i| i.get("description"))
                        .and_then(|d| d.as_str())
                        .map(|s| s.into());
                    let file_path = input
                        .and_then(|i| i.get("file_path"))
                        .and_then(|f| f.as_str())
                        .map(|s| s.into());

                    tool_uses.push(ToolUse {
                        id,
                        name,
                        command,
                        description,
                        file_path,
                    });
                }
                _ => {}
            }
        }
    } else if let Some(text) = content.and_then(|c| c.as_str()) {
        texts.push(text.to_string());
    }

    (texts, tool_uses)
}

// ── Pack rendering ──

/// Render turns into a markdown pack string with budget truncation.
pub fn render_pack(turns: &[Turn], metadata: &PackMetadata, budget_chars: usize) -> String {
    let budget = if budget_chars == 0 {
        std::env::var("EDDA_PACK_BUDGET_CHARS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(DEFAULT_PACK_BUDGET_CHARS)
    } else {
        budget_chars
    };

    let mut out = String::new();
    out.push_str("# edda memory pack (hot)\n\n");
    out.push_str(&format!("- project_id: {}\n", metadata.project_id));
    out.push_str(&format!("- session_id: {}\n", metadata.session_id));
    out.push_str(&format!("- git_branch: {}\n", metadata.git_branch));
    out.push_str(&format!("- turns: {}\n\n", turns.len()));
    out.push_str("## Recent Turns (deterministic)\n\n");

    // Render turns, newest first, truncate from oldest if over budget
    for (i, turn) in turns.iter().enumerate() {
        let mut section = String::new();
        let user_preview = truncate_str(&turn.user_text, 200);
        section.push_str(&format!("### Turn {} (newest first)\n", i + 1));
        section.push_str(&format!("- User: {user_preview}\n"));

        for tu in &turn.tool_uses {
            let cmd_str = tu
                .command
                .as_deref()
                .map(|c| format!(" `{}`", truncate_str(c, 80)))
                .unwrap_or_default();
            let desc_str = tu
                .description
                .as_deref()
                .map(|d| format!(" ({})", truncate_str(d, 60)))
                .unwrap_or_default();
            let file_str = tu
                .file_path
                .as_deref()
                .map(|f| format!(" file={f}"))
                .unwrap_or_default();
            section.push_str(&format!(
                "  - ToolUse: {}{}{}{}\n",
                tu.name, cmd_str, desc_str, file_str
            ));
        }

        for text in &turn.assistant_texts {
            let preview = truncate_str(text, 300);
            section.push_str(&format!("  - Assistant: {preview}\n"));
        }
        section.push('\n');

        if out.len() + section.len() > budget {
            out.push_str(&format!(
                "... ({} more turns truncated by budget)\n",
                turns.len() - i
            ));
            break;
        }
        out.push_str(&section);
    }

    out
}

fn truncate_str(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.replace('\n', " ")
    } else {
        // Find the last char boundary at or before `max` bytes
        let mut end = max;
        while end > 0 && !s.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}...", s[..end].replace('\n', " "))
    }
}

/// Write hot.md and hot.meta.json to the packs directory.
pub fn write_pack(project_dir: &Path, pack_md: &str, meta: &PackMetadata) -> anyhow::Result<()> {
    let packs_dir = project_dir.join("packs");
    std::fs::create_dir_all(&packs_dir)?;

    edda_store::write_atomic(&packs_dir.join("hot.md"), pack_md.as_bytes())?;

    let meta_json = serde_json::to_string_pretty(meta)?;
    edda_store::write_atomic(&packs_dir.join("hot.meta.json"), meta_json.as_bytes())?;

    Ok(())
}

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

    #[test]
    fn render_pack_basic() {
        let turns = vec![Turn {
            user_uuid: "u1".into(),
            assistant_uuid: "a1".into(),
            user_text: "How do I sort a list?".into(),
            assistant_texts: vec!["Use the sort() method.".into()],
            tool_uses: vec![ToolUse {
                id: Some("tu1".into()),
                name: "Bash".into(),
                command: Some("ls -la".into()),
                description: Some("List files".into()),
                file_path: None,
            }],
        }];

        let meta = PackMetadata {
            project_id: "abc123".into(),
            session_id: "s1".into(),
            git_branch: "main".into(),
            turn_count: 1,
            budget_chars: 12000,
        };

        let md = render_pack(&turns, &meta, 12000);
        assert!(md.contains("# edda memory pack (hot)"));
        assert!(md.contains("How do I sort a list?"));
        assert!(md.contains("ToolUse: Bash"));
        assert!(md.contains("Use the sort() method."));
    }

    #[test]
    fn render_pack_budget_truncation() {
        let turns: Vec<Turn> = (0..20)
            .map(|i| Turn {
                user_uuid: format!("u{i}"),
                assistant_uuid: format!("a{i}"),
                user_text: format!("Question {i} with some extra text padding to fill space"),
                assistant_texts: vec![format!(
                    "Answer {i} with a reasonably long response text to consume budget"
                )],
                tool_uses: vec![],
            })
            .collect();

        let meta = PackMetadata {
            project_id: "abc".into(),
            session_id: "s1".into(),
            git_branch: "main".into(),
            turn_count: 20,
            budget_chars: 500,
        };

        let md = render_pack(&turns, &meta, 500);
        assert!(md.contains("truncated by budget"));
        assert!(md.len() <= 600); // some slack for the truncation message
    }

    #[test]
    fn write_pack_creates_files() {
        let tmp = tempfile::tempdir().unwrap();
        let meta = PackMetadata {
            project_id: "test".into(),
            session_id: "s1".into(),
            git_branch: "main".into(),
            turn_count: 0,
            budget_chars: 12000,
        };

        write_pack(tmp.path(), "# pack content", &meta).unwrap();

        let hot = tmp.path().join("packs").join("hot.md");
        assert!(hot.exists());
        assert_eq!(std::fs::read_to_string(&hot).unwrap(), "# pack content");

        let meta_path = tmp.path().join("packs").join("hot.meta.json");
        assert!(meta_path.exists());
    }
}