-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
1255 lines (1143 loc) · 44 KB
/
mod.rs
File metadata and controls
1255 lines (1143 loc) · 44 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Tool parameter and result structs for MCP tools
use crate::{all_safe_output_names, tool_names};
use anyhow::Context;
use log::{debug, warn};
use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
use serde::{Deserialize, Serialize};
use ado_aw_derive::SanitizeConfig;
/// Characters to percent-encode in a URL path segment.
/// Encodes the structural delimiters that would break URL parsing if left raw:
/// `#` (fragment), `?` (query), `/` (path separator), and space.
/// This hardens operator-controlled values (project names, wiki names, work item
/// types) against accidental corruption of the URL structure.
pub(crate) const PATH_SEGMENT: &AsciiSet = &CONTROLS.add(b'#').add(b'?').add(b'/').add(b' ');
/// Safe output tools that are always available regardless of filtering.
/// These are diagnostic/transparency tools that agents should always have access to.
///
/// Derived from diagnostic tool types — adding a new diagnostic tool means adding
/// its type here and the name is extracted automatically via `ToolResult::NAME`.
pub const ALWAYS_ON_TOOLS: &[&str] = tool_names![
NoopResult,
MissingDataResult,
MissingToolResult,
ReportIncompleteResult,
];
/// Safe-output tools that require write access to ADO.
/// Compile-time derived from tool types via `ToolResult::NAME`.
///
/// Adding a new write-requiring tool: create the struct with `tool_result!{ write = true, ... }`,
/// then add its type to this list.
pub const WRITE_REQUIRING_SAFE_OUTPUTS: &[&str] = tool_names![
CreateWorkItemResult,
CommentOnWorkItemResult,
UpdateWorkItemResult,
CreatePrResult,
CreateWikiPageResult,
UpdateWikiPageResult,
AddPrCommentResult,
LinkWorkItemsResult,
QueueBuildResult,
CreateGitTagResult,
AddBuildTagResult,
CreateBranchResult,
UpdatePrResult,
UploadBuildAttachmentResult,
UploadPipelineArtifactResult,
UploadWorkitemAttachmentResult,
SubmitPrReviewResult,
ReplyToPrCommentResult,
ResolvePrThreadResult,
];
/// Non-MCP safe-output keys handled by the compiler/executor, not the MCP server.
/// These must not appear in `--enabled-tools` or they cause real MCP tools to be
/// filtered out (the router has no route for them).
pub const NON_MCP_SAFE_OUTPUT_KEYS: &[&str] = &[];
/// Tools that are gated behind `ado-aw-debug:` front-matter sections and must
/// NOT be exposed to a regular pipeline. The SafeOutputs MCP filter strips
/// these even when `enabled_tools` is `None`, so they only become reachable
/// when the compiler explicitly lists them in `--enabled-tools`.
///
/// Adding a new debug-only tool: register its result type with
/// `tool_result! { write = true, ... }`, add it here, and gate the
/// compiler-side `--enabled-tools` injection on its corresponding
/// `ado-aw-debug.<tool>` front-matter section.
pub const DEBUG_ONLY_TOOLS: &[&str] = tool_names![CreateIssueResult];
/// All recognised safe-output keys accepted in front matter `safe-outputs:`.
/// This is the union of write-requiring tool types and diagnostic tool types.
///
/// Derived at compile time from tool types — no hand-maintained string lists.
///
/// Note: `memory` was removed — it is now a first-class tool configured via
/// `tools: cache-memory:` and is no longer a safe-output key.
pub const ALL_KNOWN_SAFE_OUTPUTS: &[&str] = all_safe_output_names![
// Write-requiring MCP tools
CreateWorkItemResult,
CommentOnWorkItemResult,
UpdateWorkItemResult,
CreatePrResult,
CreateWikiPageResult,
UpdateWikiPageResult,
AddPrCommentResult,
LinkWorkItemsResult,
QueueBuildResult,
CreateGitTagResult,
AddBuildTagResult,
CreateBranchResult,
UpdatePrResult,
UploadBuildAttachmentResult,
UploadPipelineArtifactResult,
UploadWorkitemAttachmentResult,
SubmitPrReviewResult,
ReplyToPrCommentResult,
ResolvePrThreadResult,
// Always-on diagnostics
NoopResult,
MissingDataResult,
MissingToolResult,
ReportIncompleteResult;
];
/// Resolve the effective branch for a wiki.
///
/// If `configured_branch` is `Some`, that value is returned directly.
/// Otherwise the wiki metadata API is queried: code wikis (type 1) return
/// the published branch from the `versions` array; project wikis (type 0)
/// return `Ok(None)` because the server handles branching internally.
///
/// Returns `Err` when a code wiki is detected but the branch cannot be
/// resolved — callers should surface this as a user-facing failure rather
/// than proceeding to a confusing ADO PUT error.
pub(crate) async fn resolve_wiki_branch(
client: &reqwest::Client,
org_url: &str,
project: &str,
wiki_name: &str,
token: &str,
configured_branch: Option<&str>,
) -> Result<Option<String>, String> {
// Explicit configuration always wins.
if let Some(b) = configured_branch {
return Ok(Some(b.to_owned()));
}
let url = format!(
"{}/{}/_apis/wiki/wikis/{}",
org_url.trim_end_matches('/'),
utf8_percent_encode(project, PATH_SEGMENT),
utf8_percent_encode(wiki_name, PATH_SEGMENT),
);
let resp = match client
.get(&url)
.query(&[("api-version", "7.0")])
.basic_auth("", Some(token))
.send()
.await
{
Ok(r) => r,
Err(e) => {
warn!("Wiki metadata request failed: {e} — skipping branch auto-detection");
return Ok(None);
}
};
if !resp.status().is_success() {
warn!(
"Wiki metadata request returned HTTP {} — skipping branch auto-detection",
resp.status()
);
return Ok(None);
}
let body: serde_json::Value = match resp.json().await {
Ok(b) => b,
Err(e) => {
warn!("Failed to parse wiki metadata response: {e}");
return Ok(None);
}
};
// Detect code wikis. ADO returns the type as a string enum ("codeWiki" /
// "projectWiki") rather than a numeric value, so we check both forms.
let is_code_wiki = match body.get("type") {
Some(serde_json::Value::String(s)) => s.eq_ignore_ascii_case("codewiki"),
Some(serde_json::Value::Number(n)) => n.as_u64() == Some(1),
_ => false,
};
if !is_code_wiki {
let type_val = body.get("type").cloned().unwrap_or(serde_json::Value::Null);
debug!("Wiki is a project wiki (type {type_val}) — no branch needed");
return Ok(None);
}
// Code wiki: extract the published branch from versions[0].version
let branch = body
.get("versions")
.and_then(|v| v.as_array())
.and_then(|arr| arr.first())
.and_then(|v| v.get("version"))
.and_then(|v| v.as_str())
.map(|s| s.to_owned());
match &branch {
Some(b) => {
debug!("Detected code wiki — resolved branch: {b}");
Ok(branch)
}
None => Err(format!(
"Wiki '{wiki_name}' is a code wiki but its published branch could not be \
determined. Set 'branch' explicitly in the safe-outputs config."
)),
}
}
/// Look up an ADO repo name in `allowed_repositories`, accepting either:
/// 1. an exact alias key (e.g. `repo-sdk-ftdidevicecontrol`),
/// 2. an exact value match against the configured `name` (e.g. `4x4/sdk-FtdiDeviceControl`), or
/// 3. a case-insensitive match against the trailing repo-name part of the value
/// (e.g. `sdk-FtdiDeviceControl` for `4x4/sdk-FtdiDeviceControl`).
///
/// Azure DevOps repository names are case-insensitive, so the trailing-name fallback
/// matches case-insensitively. Returns the resolved ADO repo name (the map value) on
/// success, or `None` if no entry matches.
pub(crate) fn lookup_allowed_repository<'a>(
input: &str,
allowed_repositories: &'a std::collections::HashMap<String, String>,
) -> Option<&'a String> {
// 1. Exact alias key match
if let Some(name) = allowed_repositories.get(input) {
return Some(name);
}
// 2. Case-insensitive value match (full "project/repo" or just "repo").
// ADO repo names are case-insensitive, so accept any case for the full path.
if let Some((_, name)) = allowed_repositories
.iter()
.find(|(_, v)| v.eq_ignore_ascii_case(input))
{
return Some(name);
}
// 3. Trailing repo-name part match (case-insensitive)
allowed_repositories.iter().find_map(|(_, v)| {
let trailing = v.rsplit('/').next().unwrap_or(v.as_str());
if trailing.eq_ignore_ascii_case(input) {
Some(v)
} else {
None
}
})
}
/// Return `true` if `input` refers to the pipeline's own repository — either the
/// literal string `"self"`, the empty string, or a case-insensitive match against
/// `ctx.repository_name` (full value or trailing repo-name part).
pub(crate) fn input_refers_to_self(input: &str, ctx: &ExecutionContext) -> bool {
if input == "self" || input.is_empty() {
if input.is_empty() {
debug!("Empty repository alias treated as 'self'");
}
return true;
}
if let Some(name) = ctx.repository_name.as_deref() {
if name.eq_ignore_ascii_case(input) {
return true;
}
let trailing = name.rsplit('/').next().unwrap_or(name);
if trailing.eq_ignore_ascii_case(input) {
return true;
}
}
false
}
/// Resolve a repository alias to its ADO repo name.
///
/// Accepts `"self"` (or `None`) → `ctx.repository_name`, an alias key from
/// `ctx.allowed_repositories`, an exact value match, or a case-insensitive match
/// against the trailing repo-name part of either `ctx.repository_name` or any
/// configured allowed repository. See [`lookup_allowed_repository`] for the
/// matching rules used against `ctx.allowed_repositories`.
pub(crate) fn resolve_repo_name(
repo_alias: Option<&str>,
ctx: &ExecutionContext,
) -> Result<String, ExecutionResult> {
let alias = repo_alias.unwrap_or("self");
if input_refers_to_self(alias, ctx) {
return ctx
.repository_name
.clone()
.ok_or_else(|| ExecutionResult::failure("BUILD_REPOSITORY_NAME not set"));
}
lookup_allowed_repository(alias, &ctx.allowed_repositories)
.cloned()
.ok_or_else(|| {
ExecutionResult::failure(format!(
"Repository '{}' is not in the allowed repository list",
alias
))
})
}
/// Match a `value` against a `pattern` where `*` matches zero or more of **any**
/// character (including `/`).
///
/// Unlike file-path glob matching, `/` is **not** treated as a segment separator —
/// these patterns are used for tags, artifact names, and similar non-path strings.
///
/// Only the `*` wildcard is supported; there is no `?`, `[…]`, or `**` syntax.
/// Literal `*` characters cannot be escaped — this is intentional since the values
/// being matched (ADO tags, artifact names) cannot contain `*`.
pub(crate) fn wildcard_match(pattern: &str, value: &str) -> bool {
let p = pattern.as_bytes();
let v = value.as_bytes();
let (pn, vn) = (p.len(), v.len());
let mut pi = 0;
let mut vi = 0;
// Saved positions for backtracking on `*`
let mut star_p = usize::MAX;
let mut star_v: usize = 0;
while vi < vn {
if pi < pn && p[pi] == b'*' {
star_p = pi;
star_v = vi;
pi += 1;
} else if pi < pn && p[pi] == v[vi] {
pi += 1;
vi += 1;
} else if star_p != usize::MAX {
// Backtrack: let the last `*` consume one more character
pi = star_p + 1;
star_v += 1;
vi = star_v;
} else {
return false;
}
}
// Consume any trailing `*`s in the pattern
while pi < pn && p[pi] == b'*' {
pi += 1;
}
pi == pn
}
/// Return `true` if `tag` is matched by `pattern`.
///
/// Uses [`wildcard_match`] with **case-insensitive** comparison. `*` in the
/// pattern matches zero or more of any character (including `/`), so
/// `copilot:repo=org/project/*@main` correctly matches
/// `copilot:repo=org/project/MyRepo@main`.
///
/// This is the shared matcher for `allowed-tags` in `create-work-item`,
/// `update-work-item`, and `add-build-tag`.
pub(crate) fn tag_matches_pattern(tag: &str, pattern: &str) -> bool {
wildcard_match(
&pattern.to_ascii_lowercase(),
&tag.to_ascii_lowercase(),
)
}
/// Return `true` if `name` is matched by `pattern` (**case-sensitive**).
///
/// Uses [`wildcard_match`] for artifact-name allow-lists where case matters.
pub(crate) fn name_matches_pattern(name: &str, pattern: &str) -> bool {
wildcard_match(pattern, name)
}
/// Validate a string against `git check-ref-format` rules.
///
/// Returns `Ok(())` if the name is valid, or an `Err` describing the violation.
/// This covers the structural rules that Azure DevOps also enforces — catching
/// them early gives clearer error messages than letting the API fail.
pub(crate) fn validate_git_ref_name(name: &str, label: &str) -> anyhow::Result<()> {
use anyhow::ensure;
ensure!(!name.is_empty(), "{label} must not be empty");
ensure!(!name.contains(".."), "{label} must not contain '..'");
ensure!(!name.contains("@{"), "{label} must not contain '@{{'");
ensure!(!name.ends_with('.'), "{label} must not end with '.'");
ensure!(!name.ends_with(".lock"), "{label} must not end with '.lock'");
ensure!(
!name.contains('\\'),
"{label} must not contain backslash"
);
ensure!(
!name.contains("//"),
"{label} must not contain consecutive slashes"
);
for ch in ['~', '^', ':', '?', '*', '['] {
ensure!(
!name.contains(ch),
"{label} must not contain '{ch}'"
);
}
for component in name.split('/') {
ensure!(
!component.starts_with('.'),
"{label} path component must not start with '.'"
);
}
Ok(())
}
fn work_item_report_default_type() -> String {
"Task".to_string()
}
/// Configuration for filing (or appending to) an Azure DevOps work item
/// when a diagnostic safe output (`noop`, `missing-tool`) is called.
///
/// If a work item with the same title already exists in the project in a non-closed
/// state, a comment is appended instead of creating a new work item.
///
/// Both `noop` and `missing-tool` default to always creating/appending a work item.
/// Override the defaults in front matter to customise the title, type, area path, etc.
/// Set `enabled: false` to disable work-item filing entirely and restore the old
/// pass-through behaviour.
///
/// Example:
/// ```yaml
/// safe-outputs:
/// noop:
/// work-item:
/// title: "[ado-aw] Agent reported no operation"
/// work-item-type: Task
/// area-path: "MyProject\\MyTeam"
/// tags:
/// - agent-noop
/// ```
#[derive(Debug, Clone, SanitizeConfig, Serialize, Deserialize)]
pub struct WorkItemReportConfig {
/// Whether work-item filing is enabled (default: `true`).
/// Set to `false` to disable work-item creation/appending entirely.
#[serde(default = "default_enabled")]
pub enabled: bool,
/// Title of the work item to file or append a comment to.
/// If a non-closed work item with this exact title already exists,
/// a comment is appended rather than creating a new work item.
///
/// When `None`, each caller supplies a context-appropriate default
/// (e.g. noop vs missing-tool). This can happen when a partial
/// `work-item:` block is provided in front matter (e.g. overriding
/// only `work-item-type:`) — serde deserializes `title` as `None`
/// because `#[serde(default)]` applies per-field, not via the
/// per-tool default function.
#[serde(default)]
pub title: Option<String>,
/// Work item type to create (default: "Task")
#[serde(default = "work_item_report_default_type", rename = "work-item-type")]
pub work_item_type: String,
/// Area path for the work item
#[serde(default, rename = "area-path")]
pub area_path: Option<String>,
/// Iteration path for the work item
#[serde(default, rename = "iteration-path")]
pub iteration_path: Option<String>,
/// Tags to apply to the work item
#[serde(default)]
pub tags: Vec<String>,
/// Whether to include agent execution stats in the work item description/comment (default: true)
#[serde(default = "crate::agent_stats::default_include_stats", rename = "include-stats")]
pub include_stats: bool,
}
fn default_enabled() -> bool {
true
}
/// Search for a non-closed work item by exact title using WIQL.
/// Returns the ID of the most-recently-changed matching work item, or `None` if none found.
async fn wiql_find_work_item_by_title(
client: &reqwest::Client,
org_url: &str,
project: &str,
token: &str,
title: &str,
) -> anyhow::Result<Option<i64>> {
// The WIQL API does not support parameterized queries; string literals must be
// manually escaped. Doubling single quotes is the standard WIQL escaping convention
// (analogous to SQL). This title value comes from operator-controlled front-matter
// configuration and is sanitized via `sanitize_config_fields()` before reaching
// here, so it is not agent-supplied content. No other characters are WIQL-special
// inside a single-quoted literal.
let escaped_title = title.replace('\'', "''");
// The state filter covers the three built-in ADO process templates:
// Agile: "Closed", Scrum: "Done", CMMI: "Closed" (also "Resolved" in Agile/CMMI).
// Work items in any other state are considered active and eligible for commenting.
let query = format!(
"SELECT [System.Id] FROM WorkItems \
WHERE [System.Title] = '{escaped_title}' \
AND [System.TeamProject] = @project \
AND [System.State] NOT IN ('Closed', 'Resolved', 'Done') \
ORDER BY [System.ChangedDate] DESC"
);
let url = format!(
"{}/{}/_apis/wit/wiql?api-version=7.0",
org_url.trim_end_matches('/'),
utf8_percent_encode(project, PATH_SEGMENT),
);
debug!("WIQL search URL: {}", url);
let body = serde_json::json!({ "query": query });
let response = client
.post(&url)
.header("Content-Type", "application/json")
.basic_auth("", Some(token))
.json(&body)
.send()
.await
.context("Failed to query work items via WIQL")?;
if !response.status().is_success() {
let status = response.status();
let error_body = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
anyhow::bail!("WIQL query failed (HTTP {}): {}", status, error_body);
}
let result: serde_json::Value = response
.json()
.await
.context("Failed to parse WIQL response")?;
let first_id = result
.get("workItems")
.and_then(|v| v.as_array())
.and_then(|arr| arr.first())
.and_then(|item| item.get("id"))
.and_then(|id| id.as_i64());
debug!("WIQL search found work item id: {:?}", first_id);
Ok(first_id)
}
/// File a new work item or append a comment to an existing one with the same title.
///
/// If a non-closed work item matching the title (from `config.title` or
/// `default_title` when the config omits it) exists in the project,
/// a comment with `body` is appended. Otherwise a new work item is created
/// with `body` as the description.
///
/// When ADO credentials are not available (e.g. the pipeline has no write token) the
/// function returns [`ExecutionResult::warning`] instead of a hard failure so that
/// always-on diagnostic tools (`noop`, `missing-tool`) do not break pipelines that
/// run without a write service connection.
///
/// Returns an [`ExecutionResult`] describing what was done.
pub(crate) async fn file_or_append_work_item(
config: &WorkItemReportConfig,
default_title: &str,
body: &str,
ctx: &ExecutionContext,
) -> anyhow::Result<ExecutionResult> {
if !config.enabled {
return Ok(ExecutionResult::success(
"Work-item filing disabled via enabled: false".to_string(),
));
}
let title = config.title.as_deref().unwrap_or(default_title);
let org_url = match &ctx.ado_org_url {
Some(u) => u,
None => {
return Ok(ExecutionResult::warning(
"AZURE_DEVOPS_ORG_URL not set; work item not filed".to_string(),
));
}
};
let project = match &ctx.ado_project {
Some(p) => p,
None => {
return Ok(ExecutionResult::warning(
"SYSTEM_TEAMPROJECT not set; work item not filed".to_string(),
));
}
};
let token = match &ctx.access_token {
Some(t) => t,
None => {
return Ok(ExecutionResult::warning(
"No access token available (SYSTEM_ACCESSTOKEN or AZURE_DEVOPS_EXT_PAT); \
work item not filed"
.to_string(),
));
}
};
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.unwrap_or_default();
// Search for an existing non-closed work item with the same title
let existing_id =
match wiql_find_work_item_by_title(&client, org_url, project, token, title).await {
Ok(id) => id,
Err(e) => {
warn!("WIQL search for existing work item failed: {e} — skipping work item filing");
return Ok(ExecutionResult::warning(format!(
"Work item not filed (WIQL search failed): {e}"
)));
}
};
let body_with_stats =
crate::agent_stats::append_stats_to_body(body, ctx, config.include_stats);
if let Some(work_item_id) = existing_id {
// Append a comment to the existing work item
debug!(
"Found existing work item #{}, appending comment",
work_item_id
);
let comment_payload = serde_json::json!({ "text": body_with_stats });
let url = format!(
"{}/{}/_apis/wit/workItems/{}/comments?api-version=7.1-preview.4",
org_url.trim_end_matches('/'),
utf8_percent_encode(project, PATH_SEGMENT),
work_item_id,
);
let resp = client
.post(&url)
.header("Content-Type", "application/json")
.basic_auth("", Some(token))
.json(&comment_payload)
.send()
.await
.context("Failed to add comment to work item")?;
if resp.status().is_success() {
let resp_body: serde_json::Value = resp
.json()
.await
.context("Failed to parse comment response")?;
let comment_id = resp_body.get("id").and_then(|v| v.as_i64());
let message = match comment_id {
Some(id) => format!(
"Appended comment #{} to existing work item #{}: {}",
id, work_item_id, title
),
None => format!(
"Appended comment to existing work item #{}: {}",
work_item_id, title
),
};
Ok(ExecutionResult::success_with_data(
message,
serde_json::json!({
"action": "appended",
"work_item_id": work_item_id,
"comment_id": comment_id,
}),
))
} else {
let status = resp.status();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
Ok(ExecutionResult::failure(format!(
"Failed to append comment to work item #{} (HTTP {}): {}",
work_item_id, status, error_body
)))
}
} else {
// Create a new work item
debug!("No existing work item found, creating new one");
let mut patch_doc = vec![
serde_json::json!({"op": "add", "path": "/fields/System.Title", "value": title}),
serde_json::json!({"op": "add", "path": "/fields/System.Description", "value": body_with_stats}),
serde_json::json!({"op": "add", "path": "/multilineFieldsFormat/System.Description", "value": "Markdown"}),
];
if let Some(area_path) = &config.area_path {
patch_doc.push(
serde_json::json!({"op": "add", "path": "/fields/System.AreaPath", "value": area_path}),
);
}
if let Some(iteration_path) = &config.iteration_path {
patch_doc.push(
serde_json::json!({"op": "add", "path": "/fields/System.IterationPath", "value": iteration_path}),
);
}
if !config.tags.is_empty() {
patch_doc.push(
serde_json::json!({"op": "add", "path": "/fields/System.Tags", "value": config.tags.join("; ")}),
);
}
let url = format!(
"{}/{}/_apis/wit/workitems/${}?api-version=7.0",
org_url.trim_end_matches('/'),
utf8_percent_encode(project, PATH_SEGMENT),
utf8_percent_encode(&config.work_item_type, PATH_SEGMENT),
);
let resp = client
.post(&url)
.header("Content-Type", "application/json-patch+json")
.basic_auth("", Some(token))
.json(&patch_doc)
.send()
.await
.context("Failed to create work item")?;
if resp.status().is_success() {
let resp_body: serde_json::Value = resp
.json()
.await
.context("Failed to parse work item response")?;
let work_item_id = resp_body.get("id").and_then(|v| v.as_i64());
let work_item_url = resp_body
.get("_links")
.and_then(|l| l.get("html"))
.and_then(|h| h.get("href"))
.and_then(|h| h.as_str())
.unwrap_or("")
.to_string();
let message = match work_item_id {
Some(id) => format!("Created work item #{}: {}", id, title),
None => format!("Created work item: {}", title),
};
Ok(ExecutionResult::success_with_data(
message,
serde_json::json!({
"action": "created",
"work_item_id": work_item_id,
"url": work_item_url,
}),
))
} else {
let status = resp.status();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
Ok(ExecutionResult::failure(format!(
"Failed to create work item (HTTP {}): {}",
status, error_body
)))
}
}
}
mod add_build_tag;
mod add_pr_comment;
mod comment_on_work_item;
mod create_branch;
mod create_git_tag;
mod create_issue;
mod create_pull_request;
mod create_wiki_page;
mod create_work_item;
mod link_work_items;
mod missing_data;
mod missing_tool;
mod noop;
mod queue_build;
mod reply_to_pr_comment;
mod report_incomplete;
mod resolve_pr_thread;
mod result;
mod submit_pr_review;
mod update_pr;
mod update_wiki_page;
mod update_work_item;
mod upload_build_attachment;
mod upload_pipeline_artifact;
mod upload_workitem_attachment;
pub use add_build_tag::*;
pub use add_pr_comment::*;
pub use comment_on_work_item::*;
pub use create_branch::*;
pub use create_git_tag::*;
pub use create_issue::*;
pub(crate) use create_issue::validate_target_repo;
pub use create_pull_request::*;
pub use create_wiki_page::*;
pub use create_work_item::*;
pub use link_work_items::*;
pub use missing_data::*;
pub use missing_tool::*;
pub use noop::*;
pub use queue_build::*;
pub use reply_to_pr_comment::*;
pub use report_incomplete::*;
pub use resolve_pr_thread::*;
pub use result::{
ExecutionContext, ExecutionResult, Executor, ToolResult, Validate, anyhow_to_mcp_error,
org_from_url,
};
pub use submit_pr_review::*;
pub use update_pr::*;
pub use update_wiki_page::*;
pub use update_work_item::*;
pub use upload_build_attachment::*;
pub use upload_pipeline_artifact::*;
pub use upload_workitem_attachment::*;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_write_requiring_subset_of_all_known() {
for name in WRITE_REQUIRING_SAFE_OUTPUTS {
assert!(
ALL_KNOWN_SAFE_OUTPUTS.contains(name),
"WRITE_REQUIRING_SAFE_OUTPUTS entry '{}' is missing from ALL_KNOWN_SAFE_OUTPUTS",
name
);
}
}
#[test]
fn test_always_on_subset_of_all_known() {
for name in ALWAYS_ON_TOOLS {
assert!(
ALL_KNOWN_SAFE_OUTPUTS.contains(name),
"ALWAYS_ON_TOOLS entry '{}' is missing from ALL_KNOWN_SAFE_OUTPUTS",
name
);
}
}
#[test]
fn test_non_mcp_keys_subset_of_all_known() {
for name in NON_MCP_SAFE_OUTPUT_KEYS {
assert!(
ALL_KNOWN_SAFE_OUTPUTS.contains(name),
"NON_MCP_SAFE_OUTPUT_KEYS entry '{}' is missing from ALL_KNOWN_SAFE_OUTPUTS",
name
);
}
}
/// Verify that every type in the write-requiring list actually has
/// `REQUIRES_WRITE == true`, and every diagnostic type has `false`.
#[test]
fn test_requires_write_consistency() {
// Write-requiring tools
assert!(CreateWorkItemResult::REQUIRES_WRITE);
assert!(CommentOnWorkItemResult::REQUIRES_WRITE);
assert!(UpdateWorkItemResult::REQUIRES_WRITE);
assert!(CreatePrResult::REQUIRES_WRITE);
assert!(CreateWikiPageResult::REQUIRES_WRITE);
assert!(UpdateWikiPageResult::REQUIRES_WRITE);
assert!(AddPrCommentResult::REQUIRES_WRITE);
assert!(LinkWorkItemsResult::REQUIRES_WRITE);
assert!(QueueBuildResult::REQUIRES_WRITE);
assert!(CreateGitTagResult::REQUIRES_WRITE);
assert!(AddBuildTagResult::REQUIRES_WRITE);
assert!(CreateBranchResult::REQUIRES_WRITE);
assert!(UpdatePrResult::REQUIRES_WRITE);
assert!(UploadBuildAttachmentResult::REQUIRES_WRITE);
assert!(UploadPipelineArtifactResult::REQUIRES_WRITE);
assert!(UploadWorkitemAttachmentResult::REQUIRES_WRITE);
assert!(SubmitPrReviewResult::REQUIRES_WRITE);
assert!(ReplyToPrCommentResult::REQUIRES_WRITE);
assert!(ResolvePrThreadResult::REQUIRES_WRITE);
// Diagnostic tools (should NOT require write)
assert!(!NoopResult::REQUIRES_WRITE);
assert!(!MissingDataResult::REQUIRES_WRITE);
assert!(!MissingToolResult::REQUIRES_WRITE);
assert!(!ReportIncompleteResult::REQUIRES_WRITE);
}
/// Verify ALL_KNOWN_SAFE_OUTPUTS has exactly the right count:
/// write tools + diagnostics + non-MCP keys.
#[test]
fn test_all_known_completeness() {
// The three sub-lists must be disjoint — a tool in multiple lists would
// be duplicated in ALL_KNOWN and the count would mismatch.
for name in WRITE_REQUIRING_SAFE_OUTPUTS {
assert!(
!ALWAYS_ON_TOOLS.contains(name),
"Tool '{}' appears in both WRITE_REQUIRING and ALWAYS_ON — lists must be disjoint",
name
);
assert!(
!NON_MCP_SAFE_OUTPUT_KEYS.contains(name),
"Tool '{}' appears in both WRITE_REQUIRING and NON_MCP — lists must be disjoint",
name
);
}
for name in ALWAYS_ON_TOOLS {
assert!(
!NON_MCP_SAFE_OUTPUT_KEYS.contains(name),
"Tool '{}' appears in both ALWAYS_ON and NON_MCP — lists must be disjoint",
name
);
}
let expected = WRITE_REQUIRING_SAFE_OUTPUTS.len()
+ ALWAYS_ON_TOOLS.len()
+ NON_MCP_SAFE_OUTPUT_KEYS.len();
assert_eq!(
ALL_KNOWN_SAFE_OUTPUTS.len(),
expected,
"ALL_KNOWN_SAFE_OUTPUTS should be the union of write + diagnostic + non-MCP lists"
);
}
// ─── validate_git_ref_name ──────────────────────────────────────────────
#[test]
fn test_validate_git_ref_name_rejects_at_brace() {
assert!(validate_git_ref_name("branch@{0}", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_dotlock_suffix() {
assert!(validate_git_ref_name("my-branch.lock", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_consecutive_slashes() {
assert!(validate_git_ref_name("feat//thing", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_backslash() {
assert!(validate_git_ref_name("feat\\evil", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_special_chars() {
for ch in ['~', '^', ':', '?', '*', '['] {
let name = format!("feat{ch}bad");
assert!(
validate_git_ref_name(&name, "b").is_err(),
"should reject '{ch}'"
);
}
}
#[test]
fn test_validate_git_ref_name_rejects_component_starting_with_dot() {
assert!(validate_git_ref_name("feat/.hidden", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_trailing_dot() {
assert!(validate_git_ref_name("my-branch.", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_double_dot() {
assert!(validate_git_ref_name("foo..bar", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_rejects_empty() {
assert!(validate_git_ref_name("", "b").is_err());
}
#[test]
fn test_validate_git_ref_name_accepts_valid_refs() {
assert!(validate_git_ref_name("feature/add-login", "b").is_ok());
assert!(validate_git_ref_name("v1.2.3", "b").is_ok());
assert!(validate_git_ref_name("release/2026-04-17", "b").is_ok());
}
// ─── wildcard_match ─────────────────────────────────────────────────
#[test]
fn test_wildcard_match_exact() {
assert!(wildcard_match("hello", "hello"));
assert!(!wildcard_match("hello", "world"));
}
#[test]
fn test_wildcard_match_star_any() {
assert!(wildcard_match("*", "anything"));
assert!(wildcard_match("*", ""));
assert!(wildcard_match("*", "a/b/c"));
}
#[test]
fn test_wildcard_match_trailing_star() {
assert!(wildcard_match("agent-*", "agent-created"));
assert!(wildcard_match("agent-*", "agent-"));
assert!(!wildcard_match("agent-*", "bot-created"));
}
#[test]
fn test_wildcard_match_middle_star() {
assert!(wildcard_match("a*z", "az"));
assert!(wildcard_match("a*z", "abcz"));
assert!(!wildcard_match("a*z", "abcy"));
}
#[test]
fn test_wildcard_match_star_crosses_slash() {
// Unlike file-path globs, * matches across /
assert!(wildcard_match("team/*", "team/sub/item"));
assert!(wildcard_match("prefix/*@main", "prefix/a/b/c@main"));
}