-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
634 lines (588 loc) · 22.3 KB
/
main.rs
File metadata and controls
634 lines (588 loc) · 22.3 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
mod agent_stats;
mod allowed_hosts;
mod compile;
mod configure;
mod detect;
mod ecosystem_domains;
mod engine;
mod execute;
mod fuzzy_schedule;
mod hash;
mod init;
mod logging;
mod mcp;
mod ndjson;
pub mod runtimes;
pub mod sanitize;
mod safeoutputs;
mod tools;
pub mod validate;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::path::{Path, PathBuf};
#[derive(Subcommand, Debug)]
enum Commands {
/// Compile markdown to pipeline definition (or recompile all detected pipelines)
Compile {
/// Path to the input markdown file. If omitted, auto-discovers and
/// recompiles all existing agentic pipelines in the current directory.
path: Option<String>,
/// Optional output path for the generated YAML file. If the path
/// refers to an existing directory, the compiled YAML is written
/// inside that directory using the default filename derived from
/// the input markdown (e.g. `foo.md` -> `<dir>/foo.lock.yml`).
#[arg(short, long)]
output: Option<String>,
/// Bypass the GitHub-remote guard (use when running inside a
/// GitHub-hosted repository like `githubnext/ado-aw` itself).
#[arg(long)]
force: bool,
/// Omit the "Verify pipeline integrity" step from the generated pipeline.
/// Only available in debug builds.
#[cfg(debug_assertions)]
#[arg(long)]
skip_integrity: bool,
/// Include MCPG debug diagnostics in the generated pipeline (debug
/// logging, stderr streaming, backend probe step).
/// Only available in debug builds.
#[cfg(debug_assertions)]
#[arg(long)]
debug_pipeline: bool,
},
/// Check that a compiled pipeline matches its source markdown
Check {
/// Path to the pipeline YAML file to verify (source auto-detected from header)
pipeline: String,
},
/// Run as an MCP server
Mcp {
// Specify the location where out.json should be placed.
output_directory: String,
/// Guard against directory traversal attacks by specifying the agent cannot influence folders outside this path
bounding_directory: String,
/// Only expose these safe output tools (can be repeated). If omitted, all tools are exposed.
#[arg(long = "enabled-tools")]
enabled_tools: Vec<String>,
},
/// Execute safe outputs from Stage 1 (Stage 3 of the pipeline)
Execute {
/// Path to the source markdown file (used to read tool configs from front matter)
#[arg(short, long)]
source: PathBuf,
/// Directory containing safe output NDJSON file
#[arg(long, default_value = ".")]
safe_output_dir: PathBuf,
/// Output directory for processed artifacts (e.g., agent memory)
#[arg(long)]
output_dir: Option<PathBuf>,
/// Azure DevOps organization URL (overrides AZURE_DEVOPS_ORG_URL env var)
#[arg(long)]
ado_org_url: Option<String>,
/// Azure DevOps project name (overrides SYSTEM_TEAMPROJECT env var)
#[arg(long)]
ado_project: Option<String>,
/// Dry run: validate inputs but skip ADO API calls
#[arg(long)]
dry_run: bool,
},
/// Run SafeOutputs MCP server over HTTP (for MCPG integration)
McpHttp {
/// Port to listen on
#[arg(long, default_value = "8100")]
port: u16,
/// API key for authentication (if not provided, one is generated)
#[arg(long)]
api_key: Option<String>,
/// Directory for safe output files
output_directory: String,
/// Guard against directory traversal attacks
bounding_directory: String,
/// Only expose these safe output tools (can be repeated). If omitted, all tools are exposed.
#[arg(long = "enabled-tools")]
enabled_tools: Vec<String>,
},
/// Initialize a repository for AI-first agentic pipeline authoring
Init {
/// Target directory (defaults to current directory)
#[arg(long)]
path: Option<PathBuf>,
/// Bypass the GitHub-remote guard (use when running inside a
/// GitHub-hosted repository like `githubnext/ado-aw` itself)
#[arg(long)]
force: bool,
},
/// Detect agentic pipelines and update GITHUB_TOKEN on their ADO definitions
Configure {
/// The new GITHUB_TOKEN value (defaults to GITHUB_TOKEN env var; prompted if omitted)
#[arg(long, env = "GITHUB_TOKEN")]
token: Option<String>,
/// Override: Azure DevOps organization (URL like `https://dev.azure.com/myorg`,
/// or just the org name `myorg`). Inferred from git remote by default.
#[arg(long)]
org: Option<String>,
/// Override: Azure DevOps project name (inferred from git remote by default)
#[arg(long)]
project: Option<String>,
/// PAT for ADO API authentication (prefer setting AZURE_DEVOPS_EXT_PAT env var; prompted if omitted)
#[arg(long, env = "AZURE_DEVOPS_EXT_PAT")]
pat: Option<String>,
/// Path to the repository root (defaults to current directory)
#[arg(long)]
path: Option<PathBuf>,
/// Preview changes without applying them
#[arg(long)]
dry_run: bool,
/// Explicit pipeline definition IDs to update (skips auto-detection)
#[arg(long, value_delimiter = ',')]
definition_ids: Option<Vec<u64>>,
},
}
#[derive(Parser, Debug)]
#[command(version, about = "Compiler for Azure DevOps agentic pipelines")]
struct Args {
/// Enable verbose logging (info level)
#[arg(short, long, global = true)]
verbose: bool,
/// Enable debug logging (debug level, implies verbose)
#[arg(short, long, global = true)]
debug: bool,
/// Output directory for ado-aw log files (overrides ADO_AW_LOG_DIR)
#[arg(long, global = true)]
log_output_dir: Option<PathBuf>,
#[command(subcommand)]
command: Option<Commands>,
}
async fn run_compile(
path: Option<String>,
output: Option<String>,
skip_integrity: bool,
debug_pipeline: bool,
) -> Result<()> {
if skip_integrity {
eprintln!("Warning: pipeline integrity check step omitted (--skip-integrity)");
}
if debug_pipeline {
eprintln!("Warning: debug diagnostics enabled in generated pipeline (--debug-pipeline)");
}
match path {
Some(p) => compile::compile_pipeline(&p, output.as_deref(), skip_integrity, debug_pipeline).await,
None => {
if output.is_some() {
anyhow::bail!(
"--output cannot be used with auto-discovery mode. \
Specify a path to compile a single file with a custom output."
);
}
compile::compile_all_pipelines(skip_integrity, debug_pipeline).await
}
}
}
fn is_github_remote(remote_url: &str) -> bool {
let url = remote_url.trim();
if url.starts_with("git@github.com:") || url.starts_with("ssh://git@github.com/") {
return true;
}
url::Url::parse(url)
.ok()
.and_then(|u| u.host_str().map(str::to_string))
.is_some_and(|host| host.eq_ignore_ascii_case("github.com"))
}
async fn ensure_non_github_remote_for_ado_aw(command_name: &str, repo_path: &Path) -> Result<()> {
// Integration tests invoke this binary from the ado-aw repository itself,
// which is intentionally hosted on GitHub.
if std::env::var_os("CARGO_BIN_EXE_ado-aw").is_some()
|| std::env::var_os("CARGO_BIN_EXE_ado_aw").is_some()
{
return Ok(());
}
let Ok(remote_url) = configure::get_git_remote_url(repo_path).await else {
return Ok(());
};
if is_github_remote(&remote_url) {
anyhow::bail!(
"Cannot run `ado-aw {}` in a GitHub repository (origin: {}). \
`ado-aw` is for Azure DevOps repositories. \
For GitHub repositories, use gh-aw instead: https://github.com/github/gh-aw\n\
\n\
If you are working inside `githubnext/ado-aw` itself (or a fork), \
pass `--force` to bypass this check.",
command_name,
remote_url
);
}
Ok(())
}
async fn run_execute(
source: PathBuf,
safe_output_dir: PathBuf,
output_dir: Option<PathBuf>,
ado_org_url: Option<String>,
ado_project: Option<String>,
dry_run: bool,
) -> Result<()> {
// Read and parse source markdown to get tool configs.
// Use parse_markdown_detailed so Stage 3 benefits from in-memory
// codemod fixes when a source has deprecated shapes. Stage 3 must
// NOT rewrite the source file (the executor's working tree is not
// the source-of-truth tree), so we just emit a log warning.
let content = tokio::fs::read_to_string(&source)
.await
.with_context(|| format!("Failed to read source file: {}", source.display()))?;
let parsed = compile::parse_markdown_detailed(&content)
.with_context(|| format!("Failed to parse source file: {}", source.display()))?;
if parsed.codemods.changed() {
log::warn!(
"front matter at {} contains deprecated shapes; running with in-memory codemod fixes applied. Run `ado-aw compile {}` to update the source.",
source.display(),
source.display(),
);
}
let mut front_matter = parsed.front_matter;
// Sanitize before lowering repos, mirroring compile_pipeline_inner
// and check_pipeline so unsanitized fields never flow into the
// execution context.
use crate::sanitize::SanitizeConfig;
front_matter.sanitize_config_fields();
// Resolve compact repos: syntax into the legacy fields for execution
let (resolved_repos, resolved_checkout) = compile::resolve_repos(&front_matter)
.with_context(|| "Failed to resolve repository configuration")?;
front_matter.repositories = resolved_repos;
front_matter.checkout = resolved_checkout;
println!("Loaded tool configs from: {}", source.display());
// Extract tools config before moving front_matter into build_execution_context
let tools = front_matter.tools.clone();
// Build execution context from front matter, CLI args, and environment
let mut ctx = build_execution_context(
front_matter,
&safe_output_dir,
ado_org_url,
ado_project,
dry_run,
)
.await;
// Discover the last author of the agent source file for use as a
// fallback assignee in create-work-item.
ctx.agent_last_author = discover_last_author(&source).await;
if let Some(ref email) = ctx.agent_last_author {
log::info!("Agent source last author: {}", email);
}
let results = execute::execute_safe_outputs(&safe_output_dir, &ctx).await?;
// Process agent memory if cache-memory tool is enabled
process_cache_memory(tools.as_ref(), &safe_output_dir, output_dir).await?;
print_execution_summary(&results);
let failure_count = results.iter().filter(|r| !r.success).count();
let warning_count = results.iter().filter(|r| r.is_warning()).count();
if failure_count > 0 {
std::process::exit(1);
} else if warning_count > 0 {
// Exit code 2 signals "succeeded with issues" — the pipeline
// step wraps this to emit ##vso[task.complete result=SucceededWithIssues;]
std::process::exit(2);
}
Ok(())
}
async fn build_execution_context(
front_matter: compile::FrontMatter,
safe_output_dir: &PathBuf,
ado_org_url: Option<String>,
ado_project: Option<String>,
dry_run: bool,
) -> crate::safeoutputs::ExecutionContext {
// Map checkout aliases to ADO repo names from the repositories list
let allowed_repositories = front_matter
.checkout
.iter()
.filter_map(|alias| {
front_matter
.repositories
.iter()
.find(|r| &r.repository == alias)
.map(|repo| (alias.clone(), repo.name.clone()))
})
.collect();
let mut ctx = crate::safeoutputs::ExecutionContext::default();
// Only override env-derived values when CLI args are explicitly provided;
// otherwise keep the defaults from SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /
// SYSTEM_TEAMPROJECT that ExecutionContext::default() already resolved.
if let Some(url) = ado_org_url {
ctx.ado_organization = crate::safeoutputs::org_from_url(&url);
ctx.ado_org_url = Some(url);
}
if let Some(project) = ado_project {
ctx.ado_project = Some(project);
}
ctx.working_directory = safe_output_dir.clone();
ctx.tool_configs = front_matter.safe_outputs.clone();
// Merge ado-aw-debug.create-issue config under the same tool_configs map
// so Stage 3's `ctx.get_tool_config::<CreateIssueConfig>("create-issue")`
// works exactly like every other safe-output. Without this merge the
// executor would only ever see Default::default().
//
// Crucially, also record `create-issue` in `debug_enabled_tools` so the
// Stage 3 executor can independently enforce the `ado-aw-debug` gate
// — without this, a forged NDJSON entry whose tool name is `create-issue`
// could bypass the MCP-layer default-deny.
if let Some(d) = front_matter.ado_aw_debug.as_ref()
&& let Some(ci) = d.create_issue.as_ref()
{
match serde_json::to_value(ci) {
Ok(v) => {
ctx.tool_configs.insert("create-issue".to_string(), v);
ctx.debug_enabled_tools.insert("create-issue".to_string());
}
Err(e) => log::warn!(
"Failed to serialize ado-aw-debug.create-issue config: {e}"
),
}
}
ctx.allowed_repositories = allowed_repositories;
ctx.dry_run = dry_run;
// Load agent stats from OTel JSONL if available
let otel_path = safe_output_dir.join(agent_stats::OTEL_FILENAME);
if otel_path.exists() {
match agent_stats::AgentStats::from_otel_file(&otel_path, &front_matter.name).await {
Ok(stats) => {
log::info!(
"Agent stats: {} input / {} output tokens, {}s duration, {} tool calls, {} turns",
stats.input_tokens, stats.output_tokens,
stats.duration_seconds as u64, stats.tool_calls, stats.turns
);
ctx.agent_stats = Some(stats);
}
Err(e) => log::warn!("Failed to parse OTel stats file: {}", e),
}
} else {
log::debug!("No OTel stats file found at {}", otel_path.display());
}
ctx
}
/// Look up the email of the person who last authored changes to `path`.
///
/// Runs `git log -1 --format='%ae' -- <path>` in the file's parent directory.
/// Returns `None` (with a debug log) when the lookup fails — e.g. shallow
/// clone with no relevant history, or git is unavailable.
///
/// Note: we pass the bare filename (not a full path) so git resolves it
/// relative to `cwd`. This means renames in history are not followed
/// (`--follow` has its own edge-cases with merge commits and is not worth
/// the complexity here).
async fn discover_last_author(path: &Path) -> Option<String> {
let dir = path.parent().unwrap_or(Path::new("."));
let output = tokio::process::Command::new("git")
.args(["log", "-1", "--format=%ae", "--"])
.arg(path.file_name()?)
.current_dir(dir)
.output()
.await;
match output {
Ok(o) if o.status.success() => {
let email = String::from_utf8_lossy(&o.stdout).trim().to_string();
if email.is_empty() {
log::debug!("git log returned no committer for {}", path.display());
None
} else {
// Sanitize the email: git committer values can contain
// arbitrary text (e.g. ADO pipeline log commands like
// ##vso[task.setvariable ...]). Apply the same config-level
// sanitization used for operator-supplied fields.
Some(crate::sanitize::sanitize_config(&email))
}
}
Ok(o) => {
log::debug!(
"git log failed for {}: {}",
path.display(),
String::from_utf8_lossy(&o.stderr).trim()
);
None
}
Err(e) => {
log::debug!("Failed to run git log for {}: {}", path.display(), e);
None
}
}
}
async fn process_cache_memory(
tools: Option<&compile::types::ToolsConfig>,
safe_output_dir: &PathBuf,
output_dir: Option<PathBuf>,
) -> Result<()> {
let Some(cm) = tools.and_then(|t| t.cache_memory.as_ref()) else {
return Ok(());
};
if !cm.is_enabled() {
return Ok(());
}
let memory_config = execute::MemoryConfig {
allowed_extensions: cm.allowed_extensions().to_vec(),
};
let memory_output = output_dir.unwrap_or_else(|| safe_output_dir.clone());
let result =
execute::process_agent_memory(safe_output_dir, &memory_output, &memory_config).await?;
println!(
"Memory: {} - {}",
if result.success { "✓" } else { "✗" },
result.message
);
Ok(())
}
fn print_execution_summary(results: &[crate::safeoutputs::ExecutionResult]) {
let success_count = results.iter().filter(|r| r.success && !r.is_warning()).count();
let warning_count = results.iter().filter(|r| r.is_warning()).count();
let failure_count = results.iter().filter(|r| !r.success).count();
println!("\n--- Execution Summary ---");
println!(
"Total: {} | Success: {} | Warnings: {} | Failed: {}",
results.len(),
success_count,
warning_count,
failure_count
);
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
// Determine command name for logging
let command_name = match &args.command {
Some(Commands::Compile { .. }) => "compile",
Some(Commands::Check { .. }) => "check",
Some(Commands::Mcp { .. }) => "mcp",
Some(Commands::Execute { .. }) => "execute",
Some(Commands::McpHttp { .. }) => "mcp-http",
Some(Commands::Init { .. }) => "init",
Some(Commands::Configure { .. }) => "configure",
None => "ado-aw",
};
// Initialize file-based logging to a daily log file.
let _log_path = logging::init_logging(
command_name,
args.debug,
args.verbose,
args.log_output_dir.as_deref(),
);
let Some(command) = args.command else {
println!("No subcommand was used. Try `compile <path>`");
return Ok(());
};
match command {
Commands::Compile {
path,
output,
force,
#[cfg(debug_assertions)]
skip_integrity,
#[cfg(debug_assertions)]
debug_pipeline,
} => {
#[cfg(not(debug_assertions))]
let skip_integrity = false;
#[cfg(not(debug_assertions))]
let debug_pipeline = false;
// `--force` bypasses the GitHub-remote guard so maintainers can
// run `ado-aw compile` inside this repository (or other
// GitHub-hosted forks) for development and example regeneration.
if !force {
ensure_non_github_remote_for_ado_aw("compile", Path::new(".")).await?;
}
run_compile(path, output, skip_integrity, debug_pipeline).await?;
}
Commands::Check { pipeline } => {
compile::check_pipeline(&pipeline).await?;
}
Commands::Mcp {
output_directory,
bounding_directory,
enabled_tools,
} => {
let filter = if enabled_tools.is_empty() { None } else { Some(enabled_tools) };
mcp::run(&output_directory, &bounding_directory, filter.as_deref()).await?;
}
Commands::Execute {
source,
safe_output_dir,
output_dir,
ado_org_url,
ado_project,
dry_run,
} => {
run_execute(source, safe_output_dir, output_dir, ado_org_url, ado_project, dry_run)
.await?;
}
Commands::McpHttp {
port,
api_key,
output_directory,
bounding_directory,
enabled_tools,
} => {
let filter = if enabled_tools.is_empty() { None } else { Some(enabled_tools) };
mcp::run_http(
&output_directory,
&bounding_directory,
port,
api_key.as_deref(),
filter.as_deref(),
)
.await?;
}
Commands::Init { path, force } => {
let init_path = path.as_deref().unwrap_or(Path::new("."));
// `--force` bypasses the GitHub-remote guard so maintainers can
// run `ado-aw init` inside this repository (or other GitHub-hosted
// forks) for development and example regeneration.
if !force {
ensure_non_github_remote_for_ado_aw("init", init_path).await?;
}
init::run(path.as_deref()).await?;
}
Commands::Configure {
token,
org,
project,
pat,
path,
dry_run,
definition_ids,
} => {
configure::run(
token.as_deref(),
org.as_deref(),
project.as_deref(),
pat.as_deref(),
path.as_deref(),
dry_run,
definition_ids.as_deref(),
)
.await?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::is_github_remote;
#[test]
fn detects_github_https_remote() {
assert!(is_github_remote("https://github.com/owner/repo.git"));
}
#[test]
fn detects_github_ssh_remote() {
assert!(is_github_remote("git@github.com:owner/repo.git"));
}
#[test]
fn does_not_flag_ado_https_remote() {
assert!(!is_github_remote(
"https://dev.azure.com/myorg/myproject/_git/myrepo"
));
}
#[test]
fn does_not_flag_ado_ssh_remote() {
assert!(!is_github_remote(
"git@ssh.dev.azure.com:v3/myorg/myproject/myrepo"
));
}
#[test]
fn does_not_flag_non_github_remote() {
assert!(!is_github_remote("https://gitlab.com/owner/repo.git"));
}
}