-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtools.rs
More file actions
558 lines (518 loc) · 19.3 KB
/
Copy pathtools.rs
File metadata and controls
558 lines (518 loc) · 19.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
//! Tool classification for GitHub operations
//!
//! This module provides functions to classify GitHub MCP tools
//! by their operation type (read, write, merge, delete, etc.)
/// Write operations that modify data
pub const WRITE_OPERATIONS: &[&str] = &[
// Keep sorted for binary_search correctness.
"actions_run_trigger",
"add_comment_to_pending_review",
"add_deploy_key",
"add_gpg_key", // gh gpg-key add — adds a user GPG signing key
"add_issue_comment",
"add_project_item", // deprecated alias for projects_write (addProjectV2ItemById)
"add_reply_to_pull_request_comment",
"add_ssh_key", // gh ssh-key add — adds a user SSH auth/signing key
"archive_repository", // gh repo archive — blocked: repo settings change unsupported
"assign_copilot_to_issue",
"cancel_workflow_run", // gh run cancel — cancels an in-progress workflow run
"create_branch",
"create_discussion", // gh discussion create — creates a discussion in a repository
"create_gist",
"create_issue",
"create_or_update_file",
"create_pull_request",
"create_pull_request_with_copilot",
"create_release", // POST /repos/.../releases
"create_repository",
"delete_deploy_key",
"delete_file",
"delete_gist", // DELETE /gists/{gist_id}
"delete_issue_comment", // DELETE /repos/.../issues/comments/{id}
"delete_project_item", // deprecated alias for projects_write (deleteProjectV2Item)
"delete_release", // DELETE /repos/.../releases/{id}
"delete_workflow_run_logs", // deprecated alias for actions_run_trigger (DELETE run logs)
"disable_workflow", // gh workflow disable
"discussion_comment_write", // creates or edits GitHub Discussion comments
"dismiss_notification",
"edit_discussion", // gh discussion edit — edits title/body/labels of a discussion
"edit_release", // PATCH /repos/.../releases/{id}
"edit_repository", // gh repo edit — can change visibility, security settings
"enable_toolset", // Dynamically enables additional toolsets, expanding the agent's capability set
"enable_workflow", // gh workflow enable
"force_cancel_workflow_run", // gh run cancel --force — force-cancels a workflow run
"fork_repository",
"label_write",
"manage_notification_subscription",
"manage_repository_notification_subscription",
"mark_all_notifications_read",
"pin_issue", // gh issue pin
"projects_write",
"push_files",
"rename_repository", // gh repo rename — blocked: breaks clone URLs and integrations
"request_copilot_review",
"rerun_failed_jobs", // gh run rerun --failed — reruns only failed jobs
"rerun_workflow_job", // gh run rerun --job — reruns a specific job
"rerun_workflow_run", // gh run rerun — reruns a completed workflow run
"revert_pull_request", // gh pr revert — creates revert branch + PR
"run_workflow", // deprecated alias for actions_run_trigger (POST workflow dispatch)
"set_secret", // gh secret set
"set_variable", // gh variable set
"star_repository",
"sync_fork", // gh repo sync
"transfer_issue", // gh issue transfer
"transfer_repository", // gh repo transfer — blocked: repo ownership transfer is irreversible
"unarchive_repository", // gh repo unarchive — blocked: symmetric to archive_repository
"unpin_issue", // gh issue unpin
"unstar_repository",
"update_issue_comment", // PATCH /repos/.../issues/comments/{id}
"upload_release_asset", // gh release upload
];
/// Read-write operations that both read and modify data
pub const READ_WRITE_OPERATIONS: &[&str] = &[
// Keep sorted for binary_search correctness.
"add_pull_request_review_comment", // POST /repos/.../pulls/{number}/comments
"add_sub_issue", // POST /repos/.../issues/{number}/sub_issues
"create_agent_task", // gh agent-task create — creates a Copilot coding-agent job (branch + PR); blocked as unsupported
"create_pull_request_review", // POST /repos/.../pulls/{number}/reviews
"delete_pending_pull_request_review", // DELETE /repos/.../pulls/{number}/reviews/{id}
"issue_write",
"issue_write_ff_remote_mcp_issue_fields", // feature-flag variant of issue_write
"merge_pull_request",
"pull_request_review_write",
"remove_sub_issue", // DELETE/POST — remove sub-issue link
"reprioritize_sub_issue", // PATCH — reorder sub-issues
"request_pull_request_reviewers", // POST /repos/.../pulls/{number}/requested_reviewers
"resolve_review_thread", // PUT /graphql — resolveReviewThread
"set_issue_fields", // GraphQL — sets custom field values on a specific repository issue
"sub_issue_write",
"submit_pending_pull_request_review", // POST /repos/.../pulls/{number}/reviews/{id}/events
"unresolve_review_thread", // PUT /graphql — unresolveReviewThread
"update_gist",
"update_issue_assignees", // PATCH — modifies issue assignees
"update_issue_body", // PATCH — modifies issue body
"update_issue_labels", // PATCH — modifies issue labels
"update_issue_milestone", // PATCH — modifies issue milestone
"update_issue_state", // PATCH — opens or closes an issue
"update_issue_title", // PATCH — modifies issue title
"update_issue_type", // PATCH — modifies issue type
"update_project_item", // deprecated alias for projects_write (updateProjectV2ItemFieldValue)
"update_pull_request",
"update_pull_request_body", // PATCH — modifies PR body
"update_pull_request_branch",
"update_pull_request_draft_state", // PATCH — converts to/from draft
"update_pull_request_state", // PATCH — opens or closes a PR
"update_pull_request_title", // PATCH — modifies PR title
];
/// Check if a tool is a write operation
pub fn is_write_operation(tool_name: &str) -> bool {
WRITE_OPERATIONS.binary_search(&tool_name).is_ok()
|| is_lock_operation(tool_name)
|| is_unlock_operation(tool_name)
}
/// Check if a tool is a read-write operation
pub fn is_read_write_operation(tool_name: &str) -> bool {
READ_WRITE_OPERATIONS.binary_search(&tool_name).is_ok()
}
/// Check if a tool is a merge operation
pub fn is_merge_operation(tool_name: &str) -> bool {
tool_name.starts_with("merge_")
}
/// Check if a tool is a delete operation
pub fn is_delete_operation(tool_name: &str) -> bool {
tool_name.starts_with("delete_")
}
/// Check if a tool is a lock operation
pub fn is_lock_operation(tool_name: &str) -> bool {
tool_name.starts_with("lock_")
}
/// Check if a tool is an unlock operation
pub fn is_unlock_operation(tool_name: &str) -> bool {
tool_name.starts_with("unlock_")
}
/// Tools that are unconditionally blocked regardless of agent integrity.
///
/// Keep sorted for `binary_search` correctness (see `blocked_tools_are_sorted` test).
/// Entries here should also appear in `WRITE_OPERATIONS` or `READ_WRITE_OPERATIONS`.
pub const BLOCKED_TOOLS: &[&str] = &[
"archive_repository", // repo settings change; unsupported
"create_agent_task", // unsupported agent-task creation
"rename_repository", // breaks clone URLs and integrations
"transfer_repository", // irreversible ownership transfer
"unarchive_repository", // symmetric to archive_repository
];
/// Returns `true` if `tool_name` is in [`BLOCKED_TOOLS`] — denied regardless of agent integrity.
pub fn is_blocked_tool(tool_name: &str) -> bool {
BLOCKED_TOOLS.binary_search(&tool_name).is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn blocked_tools_are_classified_as_write_or_read_write() {
for &tool in BLOCKED_TOOLS {
assert!(
WRITE_OPERATIONS.contains(&tool) || READ_WRITE_OPERATIONS.contains(&tool),
"blocked tool `{tool}` must also be classified in WRITE_OPERATIONS or READ_WRITE_OPERATIONS"
);
}
}
#[test]
fn write_operations_are_sorted() {
let mut sorted = WRITE_OPERATIONS.to_vec();
sorted.sort_unstable();
assert_eq!(
WRITE_OPERATIONS,
sorted.as_slice(),
"WRITE_OPERATIONS must be kept in sorted order for binary_search correctness"
);
}
#[test]
fn read_write_operations_are_sorted() {
let mut sorted = READ_WRITE_OPERATIONS.to_vec();
sorted.sort_unstable();
assert_eq!(
READ_WRITE_OPERATIONS,
sorted.as_slice(),
"READ_WRITE_OPERATIONS must be kept in sorted order for binary_search correctness"
);
}
#[test]
fn blocked_tools_are_sorted() {
let mut sorted = BLOCKED_TOOLS.to_vec();
sorted.sort_unstable();
assert_eq!(
BLOCKED_TOOLS,
sorted.as_slice(),
"BLOCKED_TOOLS must be kept in sorted order for binary_search correctness"
);
}
#[test]
fn test_is_blocked_tool_transfer_repository() {
assert!(
is_blocked_tool("transfer_repository"),
"transfer_repository must be unconditionally blocked"
);
}
#[test]
fn test_is_blocked_tool_repo_modifying_operations() {
for op in &[
"archive_repository",
"unarchive_repository",
"rename_repository",
] {
assert!(
is_blocked_tool(op),
"{} must be unconditionally blocked (modifying gh repo operation)",
op
);
}
}
#[test]
fn test_is_blocked_tool_other_write_ops_not_blocked() {
// Regular write operations should not be blocked
for op in &[
"create_issue",
"add_issue_comment",
"pin_issue",
"unpin_issue",
] {
assert!(!is_blocked_tool(op), "{} should not be blocked", op);
}
}
#[test]
fn test_transfer_repository_is_write_operation() {
assert!(
is_write_operation("transfer_repository"),
"transfer_repository must be classified as a write operation"
);
}
#[test]
fn test_repo_modifying_operations_are_write_operations() {
for op in &[
"archive_repository",
"unarchive_repository",
"rename_repository",
] {
assert!(
is_write_operation(op),
"{} must be classified as a write operation",
op
);
}
}
#[test]
fn test_pin_unpin_issue_are_write_operations() {
assert!(
is_write_operation("pin_issue"),
"pin_issue must be classified as a write operation"
);
assert!(
is_write_operation("unpin_issue"),
"unpin_issue must be classified as a write operation"
);
}
#[test]
fn test_workflow_run_cancel_rerun_are_write_operations() {
for op in &[
"cancel_workflow_run",
"force_cancel_workflow_run",
"rerun_workflow_run",
"rerun_failed_jobs",
"rerun_workflow_job",
] {
assert!(
is_write_operation(op),
"{} must be classified as a write operation",
op
);
}
}
#[test]
fn test_cli_gap_operations_are_write_operations() {
for op in &[
"edit_repository",
"revert_pull_request",
"add_deploy_key",
"delete_deploy_key",
"add_gpg_key",
"add_ssh_key",
] {
assert!(
is_write_operation(op),
"{} must be classified as a write operation",
op
);
}
}
#[test]
fn test_create_agent_task_is_read_write_and_blocked() {
assert!(
is_read_write_operation("create_agent_task"),
"create_agent_task must be classified as a read-write operation"
);
assert!(
is_blocked_tool("create_agent_task"),
"create_agent_task must be unconditionally blocked (unsupported agent operation)"
);
assert!(
!is_write_operation("create_agent_task"),
"create_agent_task should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)"
);
}
#[test]
fn test_deprecated_alias_write_operations() {
for op in &[
"run_workflow",
"delete_workflow_run_logs",
"add_project_item",
"delete_project_item",
] {
assert!(
is_write_operation(op),
"{} (deprecated alias) must be classified as a write operation",
op
);
}
}
#[test]
fn test_deprecated_alias_read_write_operations() {
assert!(
is_read_write_operation("update_project_item"),
"update_project_item (deprecated alias) must be classified as a read-write operation"
);
}
#[test]
fn test_preemptive_cli_write_operations() {
for op in &[
"update_issue_comment",
"delete_issue_comment",
"create_release",
"edit_release",
"delete_release",
"delete_gist",
] {
assert!(
is_write_operation(op),
"{} (pre-emptive CLI) must be classified as a write operation",
op
);
}
}
#[test]
fn test_granular_issue_update_tools_are_read_write_operations() {
for op in &[
"update_issue_assignees",
"update_issue_body",
"update_issue_labels",
"update_issue_milestone",
"update_issue_state",
"update_issue_title",
"update_issue_type",
] {
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
}
#[test]
fn test_set_issue_fields_is_read_write_operation() {
let op = "set_issue_fields";
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
#[test]
fn test_issue_write_ff_remote_mcp_issue_fields_is_read_write_operation() {
let op = "issue_write_ff_remote_mcp_issue_fields";
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
#[test]
fn test_sub_issue_tools_are_read_write_operations() {
for op in &[
"sub_issue_write",
"add_sub_issue",
"remove_sub_issue",
"reprioritize_sub_issue",
] {
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
}
#[test]
fn test_pr_review_tools_are_read_write_operations() {
for op in &[
"add_pull_request_review_comment",
"create_pull_request_review",
"delete_pending_pull_request_review",
"request_pull_request_reviewers",
"resolve_review_thread",
"submit_pending_pull_request_review",
"unresolve_review_thread",
] {
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
}
#[test]
fn test_is_merge_operation() {
assert!(is_merge_operation("merge_pull_request"));
assert!(is_merge_operation("merge_upstream"));
assert!(!is_merge_operation("create_pull_request"));
assert!(!is_merge_operation("update_pull_request"));
assert!(!is_merge_operation(""));
}
#[test]
fn test_is_delete_operation() {
assert!(is_delete_operation("delete_file"));
assert!(is_delete_operation("delete_branch"));
assert!(is_delete_operation("delete_release"));
assert!(!is_delete_operation("create_repository"));
assert!(!is_delete_operation(""));
}
#[test]
fn test_is_lock_operation() {
assert!(is_lock_operation("lock_issue"));
assert!(is_lock_operation("lock_pull_request"));
assert!(!is_lock_operation("unlock_issue"));
assert!(!is_lock_operation("create_issue"));
assert!(!is_lock_operation(""));
}
#[test]
fn test_is_unlock_operation() {
assert!(is_unlock_operation("unlock_issue"));
assert!(is_unlock_operation("unlock_pull_request"));
assert!(!is_unlock_operation("lock_issue"));
assert!(!is_unlock_operation("create_issue"));
assert!(!is_unlock_operation(""));
}
#[test]
fn test_lock_and_unlock_contribute_to_write_operations() {
// is_write_operation delegates to is_lock_operation and is_unlock_operation
assert!(is_write_operation("lock_issue"));
assert!(is_write_operation("unlock_issue"));
}
#[test]
fn test_discussion_comment_write_is_write_operation() {
assert!(
is_write_operation("discussion_comment_write"),
"discussion_comment_write must be classified as a write operation"
);
assert!(
!is_read_write_operation("discussion_comment_write"),
"discussion_comment_write should not be in READ_WRITE_OPERATIONS"
);
}
#[test]
fn test_create_and_edit_discussion_are_write_operations() {
for op in &["create_discussion", "edit_discussion"] {
assert!(
is_write_operation(op),
"{op} must be classified as a write operation"
);
assert!(
!is_read_write_operation(op),
"{op} should not be in READ_WRITE_OPERATIONS"
);
}
}
#[test]
fn test_granular_pr_update_tools_are_read_write_operations() {
for op in &[
"update_pull_request_body",
"update_pull_request_draft_state",
"update_pull_request_state",
"update_pull_request_title",
] {
assert!(
is_read_write_operation(op),
"{} must be classified as a read-write operation",
op
);
assert!(
!is_write_operation(op),
"{} should not be in WRITE_OPERATIONS (it is in READ_WRITE_OPERATIONS)",
op
);
}
}
}