返回 CodeWhale
report.rs
根目录 / crates / engine / src / tools / workflow / report.rs
1 use super::{SCHEMA_RAW_PREVIEW_CHARS, WorkflowRunRecord, WorkflowRunStatus};
2 use std::path::{Path, PathBuf};
3
4 /// Persist a durable per-run report under `.codewhale/reports/<run_id>.md`
5 /// so a settled background run leaves one synthesized artifact even after
6 /// the session ends. Best-effort: report IO never affects the run outcome.
7 pub(super) fn write_run_report_artifact(workspace: &Path, record: &WorkflowRunRecord) {
8 if !matches!(
9 record.status,
10 WorkflowRunStatus::Completed
11 | WorkflowRunStatus::Degraded
12 | WorkflowRunStatus::Failed
13 | WorkflowRunStatus::Cancelled
14 ) {
15 return;
16 }
17 let Some(relative) = run_report_relative_path(&record.run_id) else {
18 return;
19 };
20 let path = workspace.join(&relative);
21 // Written through the confined helper: a link in `.codewhale` (or at the
22 // report's own name) is refused instead of redirecting the write out of
23 // the workspace, and missing parents are created without following links.
24 if let Err(err) =
25 codewhale_util::fs_confined::write(workspace, &path, render_run_report(record).as_bytes())
26 {
27 codewhale_runtime::logging::warn(format!(
28 "workflow report {} not written: {err}",
29 path.display()
30 ));
31 }
32 }
33
34 /// Workspace-relative path of a run's report, `.codewhale/reports/<id>.md`.
35 /// `None` when the id has no path-safe characters.
36 fn run_report_relative_path(run_id: &str) -> Option<PathBuf> {
37 // Run ids are generated slugs, but never trust one as a path segment.
38 let safe_id: String = run_id
39 .chars()
40 .filter(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_'))
41 .collect();
42 (!safe_id.is_empty()).then(|| {
43 Path::new(".codewhale")
44 .join("reports")
45 .join(format!("{safe_id}.md"))
46 })
47 }
48
49 /// The run's report path relative to the workspace, only when the report
50 /// was actually written. Always `/`-separated: the path is shown to the model
51 /// and the user in receipts, and must read the same on every platform.
52 pub(super) fn written_run_report(workspace: &Path, run_id: &str) -> Option<String> {
53 let relative = run_report_relative_path(run_id)?;
54 workspace.join(&relative).is_file().then(|| {
55 relative
56 .components()
57 .map(|part| part.as_os_str().to_string_lossy())
58 .collect::<Vec<_>>()
59 .join("/")
60 })
61 }
62
63 /// Bounded preview of a raw `responseSchema` reply for run records and
64 /// reports (#5583): the first [`SCHEMA_RAW_PREVIEW_CHARS`] chars on a char
65 /// boundary, with an explicit marker when the text is longer.
66 pub(super) fn bounded_raw_preview(raw: &str) -> String {
67 if raw.chars().count() <= SCHEMA_RAW_PREVIEW_CHARS {
68 return raw.to_string();
69 }
70 let kept: String = raw.chars().take(SCHEMA_RAW_PREVIEW_CHARS).collect();
71 format!("{kept}\n…[preview truncated; full reply in the schema artifact]")
72 }
73
74 /// Write the full raw reply of a failed `responseSchema` attempt as a
75 /// durable artifact beside the run report (#5583), returning its path.
76 /// `None` (with a warning) when the write fails — the bounded preview
77 /// remains either way.
78 pub(super) fn write_schema_raw_artifact(
79 workspace: &Path,
80 run_id: &str,
81 task_id: &str,
82 attempt: u32,
83 raw: &str,
84 ) -> Option<String> {
85 // Run/task ids are generated slugs, but never trust one as a path segment.
86 let safe = |text: &str| {
87 text.chars()
88 .filter(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_'))
89 .collect::<String>()
90 };
91 let (safe_run, safe_task) = (safe(run_id), safe(task_id));
92 if safe_run.is_empty() || safe_task.is_empty() {
93 return None;
94 }
95 let path = workspace.join(".codewhale").join("reports").join(format!(
96 "{safe_run}.schema.{safe_task}.attempt{attempt}.txt"
97 ));
98 // Confined write: no link below the workspace is followed, and the missing
99 // `.codewhale/reports` parents are created without following one.
100 match codewhale_util::fs_confined::write(workspace, &path, raw.as_bytes()) {
101 Ok(()) => Some(path.display().to_string()),
102 Err(err) => {
103 codewhale_runtime::logging::warn(format!(
104 "workflow schema artifact {} not written: {err}",
105 path.display()
106 ));
107 None
108 }
109 }
110 }
111
112 pub(super) fn render_run_report(record: &WorkflowRunRecord) -> String {
113 let mut out = String::new();
114 out.push_str(&format!("# Workflow run {}\n\n", record.run_id));
115 out.push_str(&format!("- status: {:?}\n", record.status));
116 if let Some(goal) = record.workflow_goal.as_deref() {
117 out.push_str(&format!("- goal: {goal}\n"));
118 }
119 if let Some(source) = record.source_path.as_deref() {
120 out.push_str(&format!("- source: {}\n", source.display()));
121 }
122 out.push_str(&format!("- started_at_ms: {}\n", record.started_at_ms));
123 if let Some(completed) = record.completed_at_ms {
124 out.push_str(&format!("- completed_at_ms: {completed}\n"));
125 }
126 out.push_str(&format!("- child_agents: {}\n", record.child_ids.len()));
127 if let Some(error) = record.error.as_deref() {
128 out.push_str(&format!("- error: {error}\n"));
129 }
130 if record.dispatch_failure_count > 0 {
131 out.push_str(&format!(
132 "\n## Dispatch failures ({})\n\n",
133 record.dispatch_failure_count
134 ));
135 let omitted = record
136 .dispatch_failure_count
137 .saturating_sub(u64::try_from(record.dispatch_failures.len()).unwrap_or(u64::MAX));
138 if omitted > 0 {
139 out.push_str(&format!(
140 "- {omitted} older failure receipt(s) omitted from this bounded report; see the workflow journal\n"
141 ));
142 }
143 for failure in &record.dispatch_failures {
144 let slot = failure
145 .label
146 .as_deref()
147 .or(failure.phase.as_deref())
148 .unwrap_or("task");
149 out.push_str(&format!("- {slot}: {}\n", failure.message));
150 }
151 }
152 if !record.gate_status.is_empty() {
153 out.push_str("\n## Gates\n\n");
154 for line in &record.gate_status {
155 out.push_str(&format!("- {line:?}\n"));
156 }
157 }
158 if !record.progress.is_empty() {
159 out.push_str("\n## Progress\n\n");
160 for line in &record.progress {
161 out.push_str(&format!("- {line}\n"));
162 }
163 }
164 if !record.schema_errors.is_empty() {
165 out.push_str(&format!(
166 "\n## Schema errors ({})\n\n",
167 record.schema_errors.len()
168 ));
169 for error in &record.schema_errors {
170 out.push_str(&format!(
171 "- `{}` attempt {}: [{}] {}\n",
172 error.task_id, error.attempt, error.kind, error.message
173 ));
174 if !error.raw_preview.is_empty() {
175 out.push_str(&format!(
176 " - raw reply ({}):\n",
177 if error.raw_truncated {
178 "bounded preview; carried text was capped"
179 } else {
180 "bounded preview"
181 }
182 ));
183 for line in error.raw_preview.lines() {
184 out.push_str(&format!(" {line}\n"));
185 }
186 }
187 if let Some(artifact) = &error.artifact {
188 out.push_str(&format!(" - full reply: {artifact}\n"));
189 }
190 }
191 }
192 if !record.schema_repairs.is_empty() {
193 out.push_str(&format!(
194 "\n## Schema repairs ({}, including succeeded ones)\n\n",
195 record.schema_repair_count
196 ));
197 for repair in &record.schema_repairs {
198 out.push_str(&format!(
199 "- `{}` attempt {}: [{}] a bounded repair followed\n",
200 repair.task_id, repair.attempt, repair.kind
201 ));
202 if let Some(artifact) = &repair.artifact {
203 out.push_str(&format!(" - full reply: {artifact}\n"));
204 }
205 }
206 }
207 if let Some(result) = record.result.as_ref() {
208 out.push_str("\n## Result\n\n```json\n");
209 out.push_str(&serde_json::to_string_pretty(result).unwrap_or_else(|_| result.to_string()));
210 out.push_str("\n```\n");
211 }
212 if let Some(verification) = record.verification.as_ref() {
213 out.push_str("\n## Verification\n\n```json\n");
214 out.push_str(
215 &serde_json::to_string_pretty(verification)
216 .unwrap_or_else(|_| verification.to_string()),
217 );
218 out.push_str("\n```\n");
219 }
220 out
221 }
222
222 lines RUST