ta-submit 0.15.15-alpha.3

Submit adapters for VCS integration in Trusted Autonomy
Documentation
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
//! Core SourceAdapter trait and result types
//!
//! The `SourceAdapter` trait (formerly `SubmitAdapter`) is the unified abstraction
//! for VCS operations. It combines submit operations (commit, push, open review)
//! with sync operations (fetch upstream, detect conflicts).

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use ta_changeset::DraftPackage;
use ta_goal::GoalRun;
use thiserror::Error;

use crate::config::SubmitConfig;

/// Errors that can occur during source operations
#[derive(Debug, Error)]
pub enum SubmitError {
    #[error("Adapter not configured: {0}")]
    NotConfigured(String),

    #[error("VCS operation failed: {0}")]
    VcsError(String),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Configuration error: {0}")]
    ConfigError(String),

    #[error("Review creation failed: {0}")]
    ReviewError(String),

    #[error("Invalid state: {0}")]
    InvalidState(String),

    #[error("Sync failed: {0}")]
    SyncError(String),

    #[error("Sync conflict: {conflicts} file(s) in conflict")]
    SyncConflict { conflicts: usize },
}

pub type Result<T> = std::result::Result<T, SubmitError>;

/// Result of a commit operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitResult {
    /// Commit identifier (hash, changelist number, etc.)
    pub commit_id: String,

    /// Human-readable message
    pub message: String,

    /// Adapter-specific metadata
    #[serde(default)]
    pub metadata: HashMap<String, String>,

    /// Gitignored artifacts that were dropped from this commit (v0.13.17.5).
    /// Known-safe paths (.mcp.json, *.local.toml, .ta/ runtime files) are
    /// dropped silently; unexpected-ignored paths trigger a warning.
    #[serde(default)]
    pub ignored_artifacts: Vec<ta_changeset::IgnoredArtifact>,
}

/// Result of a push operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushResult {
    /// Remote reference (branch name, changelist URL, etc.)
    pub remote_ref: String,

    /// Human-readable message
    pub message: String,

    /// Adapter-specific metadata
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

/// Result of opening a review request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReviewResult {
    /// Review URL (GitHub PR, Perforce review, etc.)
    pub review_url: String,

    /// Review identifier
    pub review_id: String,

    /// Human-readable message
    pub message: String,

    /// Adapter-specific metadata
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

/// Result of a sync operation — pulling upstream changes into the local workspace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncResult {
    /// Whether upstream had new changes that were incorporated.
    pub updated: bool,

    /// Files with merge conflicts (empty if none).
    pub conflicts: Vec<String>,

    /// Number of new upstream commits incorporated.
    pub new_commits: u32,

    /// Human-readable summary of what happened.
    pub message: String,

    /// Adapter-specific metadata.
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

impl SyncResult {
    /// Whether the sync completed without conflicts.
    pub fn is_clean(&self) -> bool {
        self.conflicts.is_empty()
    }
}

/// Opaque saved VCS state for save/restore around apply operations.
///
/// Each adapter stores its own state (e.g., Git saves the current branch name,
/// Perforce saves the current changelist). The state is passed back to
/// `restore_state()` after the apply operation completes.
pub struct SavedVcsState {
    /// Adapter name that created this state (for safety checks).
    pub adapter: String,
    /// Opaque state data — only the creating adapter knows how to interpret this.
    pub data: Box<dyn std::any::Any + Send>,
}

/// Pluggable adapter for source control operations (submit + sync).
///
/// The staging->review->apply loop is VCS-agnostic. This trait allows
/// different implementations for Git, Perforce, SVN, or custom workflows.
///
/// Renamed from `SubmitAdapter` in v0.11.1 to reflect the unified scope
/// (submit + sync). The old name is available as a type alias.
pub trait SourceAdapter: Send + Sync {
    /// Create a working branch/changelist/workspace for this goal
    ///
    /// For Git: creates a feature branch
    /// For Perforce: creates a changelist
    /// For "none": no-op
    fn prepare(&self, goal: &GoalRun, config: &SubmitConfig) -> Result<()>;

    /// Commit the approved changes from staging
    ///
    /// For Git: `git add` + `git commit`
    /// For Perforce: shelve files
    /// For "none": no-op
    fn commit(&self, goal: &GoalRun, pr: &DraftPackage, message: &str) -> Result<CommitResult>;

    /// Push the committed changes
    ///
    /// For Git: `git push`
    /// For Perforce: submit changelist
    /// For "none": no-op
    fn push(&self, goal: &GoalRun) -> Result<PushResult>;

    /// Open a review request
    ///
    /// For Git: create GitHub/GitLab PR via API or `gh pr create`
    /// For Perforce: create Swarm review
    /// For "none": no-op
    fn open_review(&self, goal: &GoalRun, pr: &DraftPackage) -> Result<ReviewResult>;

    /// Sync the local workspace with upstream changes.
    ///
    /// For Git: `git fetch` + merge/rebase/ff per `source.git.sync_strategy`
    /// For SVN: `svn update`
    /// For Perforce: `p4 sync`
    /// For "none": no-op (always returns updated=false)
    ///
    /// Returns a `SyncResult` describing what happened. If conflicts are
    /// detected, `SyncResult.conflicts` is non-empty but the method still
    /// returns `Ok` — the caller decides how to handle conflicts. Only
    /// returns `Err` for infrastructure failures (network, permissions).
    fn sync_upstream(&self) -> Result<SyncResult> {
        Ok(SyncResult {
            updated: false,
            conflicts: vec![],
            new_commits: 0,
            message: "No sync operation (default implementation)".to_string(),
            metadata: HashMap::new(),
        })
    }

    /// Adapter display name (for CLI output)
    fn name(&self) -> &str;

    /// Patterns to exclude from staging copy (VCS metadata dirs, etc.)
    ///
    /// Returns patterns in .taignore format: "dirname/", "*.ext", "name".
    /// These are merged with user .taignore patterns and built-in defaults
    /// during overlay workspace creation and diffing.
    fn exclude_patterns(&self) -> Vec<String> {
        vec![]
    }

    /// Save working state before apply operations.
    ///
    /// Git: saves the current branch name so it can be restored after commit.
    /// Perforce: saves the current changelist context.
    /// Default: no-op (returns None).
    fn save_state(&self) -> Result<Option<SavedVcsState>> {
        Ok(None)
    }

    /// Restore working state after apply operations.
    ///
    /// Git: switches back to the original branch.
    /// Perforce: reverts to saved client state.
    /// Default: no-op.
    fn restore_state(&self, _state: Option<SavedVcsState>) -> Result<()> {
        Ok(())
    }

    /// Get the current branch or workspace name.
    ///
    /// Git: current branch name (e.g., "main", "feature/abc-def")
    /// SVN: working copy URL path
    /// Perforce: current client/workspace name
    /// Default: "unknown"
    fn current_branch(&self) -> Result<String> {
        Ok("unknown".to_string())
    }

    /// Get the current revision identifier for the working directory.
    ///
    /// Git: short commit hash (e.g., "abc1234")
    /// SVN: revision number (e.g., "r1234")
    /// Perforce: changelist number (e.g., "@1234")
    /// Default: "unknown"
    fn revision_id(&self) -> Result<String> {
        Ok("unknown".to_string())
    }

    /// Check the status of a review/PR by its review ID (e.g., PR number).
    ///
    /// Git: uses `gh pr view --json state` to check PR status.
    /// Returns the current state as a string: "open", "merged", "closed".
    /// Default: returns None (not supported).
    fn check_review(&self, _review_id: &str) -> Result<Option<ReviewStatus>> {
        Ok(None)
    }

    /// Merge a review/PR into the target branch and sync the local workspace.
    ///
    /// Git: calls `gh pr merge` to merge the PR immediately.
    /// Perforce: calls `p4 submit -c <CL>` to submit the shelved changelist.
    /// SVN: no-op (SVN commits directly; no separate merge step).
    /// Default: no-op, returns a guidance message telling the user what to do.
    ///
    /// Returns a `MergeResult` describing what happened. `merged = true` means
    /// the merge was completed immediately; `merged = false` means auto-merge is
    /// pending (CI must pass first).
    fn merge_review(&self, _review_id: &str) -> Result<MergeResult> {
        Ok(MergeResult {
            merged: false,
            merge_commit: None,
            message: "This adapter does not support automatic merging. \
                      Merge the PR manually in your VCS platform, then run `ta sync`."
                .to_string(),
            metadata: HashMap::new(),
        })
    }

    /// Auto-detect whether this adapter applies to the given project root.
    ///
    /// Git: checks for .git/ directory
    /// SVN: checks for .svn/ directory
    /// Perforce: checks for P4CONFIG env var or .p4config
    fn detect(project_root: &Path) -> bool
    where
        Self: Sized,
    {
        let _ = project_root;
        false
    }

    /// Protected submit targets for this adapter (§15 VCS Submit Invariant).
    ///
    /// Returns the list of refs/branches/paths that agents must never commit
    /// directly to. `prepare()` must create an isolation mechanism (feature
    /// branch, shelved CL, etc.) before `verify_not_on_protected_target()` is
    /// called.
    ///
    /// Default: empty list (no protected targets — applies to adapters that
    /// handle isolation entirely through their `prepare()` implementation).
    fn protected_submit_targets(&self) -> Vec<String> {
        vec![]
    }

    /// Assert the post-`prepare()` invariant: the adapter must not be
    /// positioned to commit directly to a protected target (§15).
    ///
    /// Called immediately after `prepare()` succeeds, before any commit or
    /// push. Hard failure aborts the apply workflow.
    ///
    /// Default implementation: if `protected_submit_targets()` returns a
    /// non-empty list, subclasses should override this to check the current
    /// position. The base implementation is a no-op (safe for adapters whose
    /// `prepare()` guarantees isolation without needing an extra check).
    fn verify_not_on_protected_target(&self) -> Result<()> {
        Ok(())
    }

    /// Produce environment variables to inject into the agent process so it
    /// operates on the staging directory instead of the developer's real VCS
    /// workspace (v0.13.17.3).
    ///
    /// Called by `ta run` before spawning the agent. The returned vars are
    /// merged into the agent's process environment.
    ///
    /// Default: no-op (returns empty map). VCS adapters that support isolation
    /// should override this method.
    fn stage_env(
        &self,
        _staging_dir: &Path,
        _config: &crate::config::VcsAgentConfig,
    ) -> Result<HashMap<String, String>> {
        Ok(HashMap::new())
    }
}

/// Result of merging a review (PR, shelved CL, etc.) into the target branch.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MergeResult {
    /// Whether the merge was completed (false = pending CI, auto-merge enabled).
    pub merged: bool,
    /// Merge commit SHA or changelist number (if available).
    pub merge_commit: Option<String>,
    /// Human-readable message about what happened.
    pub message: String,
    /// Adapter-specific metadata.
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

/// Status of a VCS review/PR (v0.11.2.3).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReviewStatus {
    /// Current state: "open", "merged", "closed", "draft".
    pub state: String,
    /// Whether CI checks are passing.
    pub checks_passing: Option<bool>,
}

/// Backward-compatible alias: `SubmitAdapter` is the old name for `SourceAdapter`.
///
/// Deprecated in v0.11.1. Use `SourceAdapter` instead.
pub use SourceAdapter as SubmitAdapter;

#[cfg(test)]
mod tests {
    use super::*;

    struct MockAdapter;
    impl SourceAdapter for MockAdapter {
        fn prepare(&self, _: &GoalRun, _: &SubmitConfig) -> Result<()> {
            Ok(())
        }
        fn commit(&self, _: &GoalRun, _: &DraftPackage, _: &str) -> Result<CommitResult> {
            unimplemented!()
        }
        fn push(&self, _: &GoalRun) -> Result<PushResult> {
            unimplemented!()
        }
        fn open_review(&self, _: &GoalRun, _: &DraftPackage) -> Result<ReviewResult> {
            unimplemented!()
        }
        fn name(&self) -> &str {
            "mock"
        }
    }

    #[test]
    fn default_protected_targets_empty() {
        let adapter = MockAdapter;
        assert!(adapter.protected_submit_targets().is_empty());
    }

    #[test]
    fn default_verify_not_on_protected_target_ok() {
        let adapter = MockAdapter;
        assert!(adapter.verify_not_on_protected_target().is_ok());
    }

    #[test]
    fn sync_result_is_clean_when_no_conflicts() {
        let result = SyncResult {
            updated: true,
            conflicts: vec![],
            new_commits: 3,
            message: "ok".to_string(),
            metadata: HashMap::new(),
        };
        assert!(result.is_clean());
    }

    #[test]
    fn sync_result_is_not_clean_with_conflicts() {
        let result = SyncResult {
            updated: true,
            conflicts: vec!["src/main.rs".to_string()],
            new_commits: 3,
            message: "conflict".to_string(),
            metadata: HashMap::new(),
        };
        assert!(!result.is_clean());
    }

    #[test]
    fn sync_result_serialization_roundtrip() {
        let result = SyncResult {
            updated: true,
            conflicts: vec!["a.rs".to_string()],
            new_commits: 5,
            message: "synced".to_string(),
            metadata: [("branch".to_string(), "main".to_string())]
                .into_iter()
                .collect(),
        };
        let json = serde_json::to_string(&result).unwrap();
        let restored: SyncResult = serde_json::from_str(&json).unwrap();
        assert!(restored.updated);
        assert_eq!(restored.conflicts, vec!["a.rs"]);
        assert_eq!(restored.new_commits, 5);
    }
}