use std::collections::HashMap;
use std::fs::{self, OpenOptions};
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
use std::path::Path;
use tracevault_core::hooks::{parse_hook_event, HookResponse};
use tracevault_core::streaming::{
extract_is_error_from_transcript, StreamEventRequest, StreamEventType,
};
#[allow(clippy::type_complexity)]
pub fn apply_context(
ctx: crate::context::Context,
) -> (
Option<String>,
Option<Vec<String>>,
Option<HashMap<String, String>>,
) {
let flow_id = ctx.flow_id;
let labels = if ctx.labels.is_empty() {
None
} else {
Some(ctx.labels)
};
let params = if ctx.params.is_empty() {
None
} else {
Some(ctx.params.into_iter().collect())
};
(flow_id, labels, params)
}
pub fn read_new_transcript_lines(
transcript_path: &Path,
offset_path: &Path,
) -> Result<(Vec<serde_json::Value>, i64), io::Error> {
if !transcript_path.exists() {
return Ok((vec![], 0));
}
let offset: i64 = if offset_path.exists() {
let content = fs::read_to_string(offset_path)?;
content
.trim()
.parse::<i64>()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
} else {
0
};
let mut file = fs::File::open(transcript_path)?;
file.seek(SeekFrom::Start(offset as u64))?;
let reader = io::BufReader::new(file);
let mut lines = Vec::new();
let mut bytes_read = offset;
for line_result in reader.lines() {
let line = line_result?;
bytes_read += line.len() as i64 + 1;
if line.trim().is_empty() {
continue;
}
if let Ok(value) = serde_json::from_str::<serde_json::Value>(&line) {
lines.push(value);
}
}
Ok((lines, bytes_read))
}
pub fn append_pending(pending_path: &Path, json: &str) -> Result<(), io::Error> {
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(pending_path)?;
writeln!(file, "{json}")?;
Ok(())
}
pub fn drain_pending(pending_path: &Path) -> Result<Vec<String>, io::Error> {
if !pending_path.exists() {
return Ok(vec![]);
}
let content = fs::read_to_string(pending_path)?;
let lines: Vec<String> = content
.lines()
.filter(|l| !l.trim().is_empty())
.map(String::from)
.collect();
fs::remove_file(pending_path)?;
Ok(lines)
}
pub fn resolve_session_paths(
hook_cwd: &Path,
session_id: &str,
) -> (std::path::PathBuf, std::path::PathBuf) {
let project_root = crate::paths::resolve_project_root(hook_cwd).root;
let session_dir = project_root
.join(".tracevault")
.join("sessions")
.join(session_id);
(project_root, session_dir)
}
pub async fn run_stream(event_type: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let hook_event = parse_hook_event(&input)?;
let hook_cwd = Path::new(&hook_event.cwd);
let (project_root, session_dir) = resolve_session_paths(hook_cwd, &hook_event.session_id);
fs::create_dir_all(&session_dir)?;
let worktree_top = crate::paths::worktree_toplevel(hook_cwd);
let _ = fs::write(session_dir.join("origin"), &worktree_top);
let event_uuid = uuid::Uuid::now_v7();
let transcript_path = Path::new(&hook_event.transcript_path);
let offset_path = session_dir.join(".stream_offset");
let (transcript_lines, new_offset) = read_new_transcript_lines(transcript_path, &offset_path)?;
let stream_event_type = match event_type {
"notification" => StreamEventType::SessionStart,
"stop" => StreamEventType::SessionEnd,
_ => StreamEventType::ToolUse,
};
let tool_is_error = hook_event
.tool_use_id
.as_deref()
.and_then(|uid| extract_is_error_from_transcript(uid, &transcript_lines));
let ctx = crate::context::Context::effective(hook_cwd);
let (ctx_flow_id, ctx_labels, ctx_params) = apply_context(ctx);
let mut req = StreamEventRequest {
protocol_version: 1,
tool: Some("claude-code".to_string()),
event_type: stream_event_type,
session_id: hook_event.session_id.clone(),
timestamp: chrono::Utc::now(),
hook_event_name: Some(hook_event.hook_event_name.clone()),
tool_name: hook_event.tool_name.clone(),
tool_use_id: hook_event.tool_use_id.clone(),
tool_input: hook_event.tool_input.clone(),
tool_response: hook_event.tool_response.clone(),
tool_is_error,
event_index: None,
event_uuid: Some(event_uuid),
transcript_lines: if transcript_lines.is_empty() {
None
} else {
Some(transcript_lines)
},
transcript_offset: Some(new_offset),
model: None,
cwd: Some(hook_event.cwd.clone()),
final_stats: None,
flow_id: ctx_flow_id,
labels: ctx_labels,
params: ctx_params,
};
req.truncate_large_fields();
let (server_url, token) = crate::api_client::resolve_credentials(&project_root);
let config =
crate::config::TracevaultConfig::load(&project_root).ok_or("TracevaultConfig not found")?;
let org_slug = config
.org_slug
.as_deref()
.ok_or("org_slug not configured")?;
let repo_id = config.repo_id.as_deref().ok_or("repo_id not configured")?;
let server_url = server_url.ok_or("server_url not configured")?;
let client = crate::api_client::ApiClient::new(&server_url, token.as_deref());
let pending_path = session_dir.join("pending.jsonl");
let pending_events = drain_pending(&pending_path)?;
let mut send_failed = false;
for pending_json in &pending_events {
if let Ok(pending_req) = serde_json::from_str::<StreamEventRequest>(pending_json) {
if client
.stream_event(org_slug, repo_id, &pending_req)
.await
.is_err()
{
for evt in &pending_events {
append_pending(&pending_path, evt)?;
}
send_failed = true;
break;
}
}
}
let req_json = serde_json::to_string(&req)?;
if send_failed {
append_pending(&pending_path, &req_json)?;
} else {
match client.stream_event(org_slug, repo_id, &req).await {
Ok(_) => {
fs::write(&offset_path, new_offset.to_string())?;
}
Err(_) => {
append_pending(&pending_path, &req_json)?;
}
}
}
let response = HookResponse::allow();
println!("{}", serde_json::to_string(&response)?);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context::Context;
use std::collections::BTreeMap;
#[test]
fn apply_context_stamps_all_fields() {
let dir = tempfile::tempdir().unwrap();
let mut params = BTreeMap::new();
params.insert("env".to_string(), "prod".to_string());
params.insert("region".to_string(), "eu-west-1".to_string());
let ctx = Context {
flow_id: Some("flow-xyz".to_string()),
labels: vec!["backend".to_string(), "urgent".to_string()],
params,
};
let (flow_id, labels, params_out) = apply_context(ctx);
assert_eq!(flow_id, Some("flow-xyz".to_string()));
assert_eq!(
labels,
Some(vec!["backend".to_string(), "urgent".to_string()])
);
let p = params_out.expect("params should be Some");
assert_eq!(p.get("env").map(String::as_str), Some("prod"));
assert_eq!(p.get("region").map(String::as_str), Some("eu-west-1"));
let written = Context {
flow_id: Some("flow-xyz".to_string()),
labels: vec!["backend".to_string(), "urgent".to_string()],
params: {
let mut m = BTreeMap::new();
m.insert("env".to_string(), "prod".to_string());
m.insert("region".to_string(), "eu-west-1".to_string());
m
},
};
let ctx_path = dir.path().join(".tracevault").join("context.json");
written.save_to(&ctx_path).unwrap();
let loaded = Context::load_from(&ctx_path);
let (flow_id2, labels2, params2) = apply_context(loaded);
assert_eq!(flow_id2, Some("flow-xyz".to_string()));
assert!(labels2.is_some());
assert!(params2.is_some());
}
#[test]
fn apply_context_missing_file_all_none() {
let dir = tempfile::tempdir().unwrap();
let missing_path = dir.path().join(".tracevault").join("context.json");
let ctx = Context::load_from(&missing_path); let (flow_id, labels, params) = apply_context(ctx);
assert!(flow_id.is_none(), "flow_id should be None");
assert!(labels.is_none(), "labels should be None");
assert!(params.is_none(), "params should be None");
}
#[test]
fn apply_context_empty_collections_are_none() {
let ctx = Context {
flow_id: None,
labels: vec![],
params: BTreeMap::new(),
};
let (flow_id, labels, params) = apply_context(ctx);
assert!(flow_id.is_none());
assert!(
labels.is_none(),
"empty labels should be None, not Some([])"
);
assert!(
params.is_none(),
"empty params should be None, not Some({{}})"
);
}
#[test]
fn apply_context_flow_id_only() {
let ctx = Context {
flow_id: Some("my-flow".to_string()),
labels: vec![],
params: BTreeMap::new(),
};
let (flow_id, labels, params) = apply_context(ctx);
assert_eq!(flow_id, Some("my-flow".to_string()));
assert!(labels.is_none());
assert!(params.is_none());
}
use crate::test_helpers::{add_worktree, init_git_repo};
#[test]
fn resolve_session_paths_primary_checkout() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
std::fs::create_dir_all(repo.join(".tracevault")).unwrap();
let (project_root, session_dir) = resolve_session_paths(&repo, "sess-primary-123");
assert_eq!(
project_root.canonicalize().unwrap(),
repo.canonicalize().unwrap(),
"primary checkout: project_root must be repo root"
);
assert_eq!(
session_dir.canonicalize().unwrap_or(session_dir.clone()),
repo.join(".tracevault")
.join("sessions")
.join("sess-primary-123"),
"primary checkout: session_dir must be <repo>/.tracevault/sessions/<id>/"
);
assert!(
session_dir.starts_with(&repo),
"session_dir must be under the primary repo root"
);
}
#[test]
fn resolve_session_paths_sibling_worktree_uses_primary_root() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
let wt = tmp.path().join("sibling-wt");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
std::fs::create_dir_all(repo.join(".tracevault")).unwrap();
add_worktree(&repo, &wt);
let (project_root, session_dir) = resolve_session_paths(&wt, "sess-sibling-456");
assert_eq!(
project_root.canonicalize().unwrap(),
repo.canonicalize().unwrap(),
"sibling worktree: project_root must be primary repo root (not worktree dir)"
);
assert!(
session_dir.starts_with(&repo),
"sibling worktree: session_dir must be under the PRIMARY repo root"
);
assert!(
!session_dir.starts_with(&wt),
"sibling worktree: session_dir must NOT be under the sibling worktree dir"
);
assert!(
session_dir.ends_with(
std::path::Path::new(".tracevault")
.join("sessions")
.join("sess-sibling-456")
),
"session_dir must end with .tracevault/sessions/<session_id>"
);
}
#[test]
fn resolve_session_paths_non_git_fallback() {
let tmp = tempfile::tempdir().unwrap();
let (project_root, session_dir) = resolve_session_paths(tmp.path(), "sess-fallback-789");
assert_eq!(
project_root,
tmp.path(),
"non-git: project_root must fall back to hook_cwd"
);
assert_eq!(
session_dir,
tmp.path()
.join(".tracevault")
.join("sessions")
.join("sess-fallback-789"),
"non-git: session_dir must be relative to the fallback root"
);
}
#[test]
fn origin_marker_written_in_session_dir() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
let session_dir = repo
.join(".tracevault")
.join("sessions")
.join("test-sess-001");
std::fs::create_dir_all(&session_dir).unwrap();
let hook_cwd = &repo;
let worktree_top = crate::paths::worktree_toplevel(hook_cwd);
let _ = std::fs::write(session_dir.join("origin"), &worktree_top);
let origin_content = std::fs::read_to_string(session_dir.join("origin")).unwrap();
let expected = repo.canonicalize().unwrap().to_string_lossy().into_owned();
assert_eq!(
origin_content.trim(),
expected.as_str(),
"origin marker must contain the canonicalized worktree toplevel"
);
}
}