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
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
//! Social media adapter plugin discovery and external plugin wrapper.
//!
//! ## Plugin discovery
//!
//! Plugins are searched in order:
//! 1. `~/.config/ta/plugins/social/` — user-global
//! 2. `.ta/plugins/social/` — project-local
//! 3. `$PATH` — bare executable with prefix `ta-social-`
//!
//! The first matching plugin for the given platform name is used.
//!
//! ## ExternalSocialAdapter
//!
//! Wraps an external plugin process and translates calls into
//! JSON-over-stdio request/response exchanges. Each method call spawns
//! a fresh process (plugins are stateless per-call).
//!
//! ## Credentials
//!
//! Credentials (OAuth2 tokens) are stored in the OS keychain under the
//! key `ta-social:<platform>:<handle>`. Plugins retrieve them via
//! `ta adapter credentials get <key>`.

use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::social_plugin_protocol::{
    CreateScheduledParams, CreateSocialDraftParams, SocialDraftStatusParams, SocialHealthParams,
    SocialPluginError, SocialPluginRequest, SocialPluginResponse, SocialPostContent,
    SocialPostState, SOCIAL_PROTOCOL_VERSION,
};

// ---------------------------------------------------------------------------
// Plugin manifest
// ---------------------------------------------------------------------------

/// Parsed `plugin.toml` manifest for a social media adapter plugin.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SocialPluginManifest {
    /// Platform name (e.g., "linkedin", "x", "buffer").
    pub name: String,

    /// Plugin version (semver).
    #[serde(default = "default_version")]
    pub version: String,

    /// Plugin type — must be `"social"`.
    #[serde(rename = "type", default = "default_type")]
    pub plugin_type: String,

    /// Executable command to spawn.
    pub command: String,

    /// Additional arguments passed on every invocation.
    #[serde(default)]
    pub args: Vec<String>,

    /// Capabilities this plugin exposes.
    ///
    /// Standard values: `"create_draft"`, `"create_scheduled"`, `"draft_status"`, `"health"`.
    #[serde(default)]
    pub capabilities: Vec<String>,

    /// Human-readable description.
    #[serde(default)]
    pub description: Option<String>,

    /// Per-call timeout in seconds.
    #[serde(default = "default_timeout_secs")]
    pub timeout_secs: u64,

    /// Protocol version this plugin implements.
    #[serde(default = "default_protocol_version")]
    pub protocol_version: u32,
}

fn default_version() -> String {
    "0.1.0".to_string()
}

fn default_type() -> String {
    "social".to_string()
}

fn default_timeout_secs() -> u64 {
    60
}

fn default_protocol_version() -> u32 {
    SOCIAL_PROTOCOL_VERSION
}

impl SocialPluginManifest {
    /// Load a manifest from a `plugin.toml` file.
    pub fn load(path: &Path) -> Result<Self, SocialPluginError> {
        let content = std::fs::read_to_string(path)?;
        let manifest: Self = toml::from_str(&content).map_err(|e| {
            SocialPluginError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("invalid manifest at {}: {}", path.display(), e),
            ))
        })?;
        Ok(manifest)
    }
}

// ---------------------------------------------------------------------------
// Discovery
// ---------------------------------------------------------------------------

/// Where a social plugin was discovered from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SocialPluginSource {
    /// `~/.config/ta/plugins/social/` (user-global).
    UserGlobal,
    /// `.ta/plugins/social/` in the project root.
    ProjectLocal,
    /// Bare executable on `$PATH` (prefix `ta-social-`).
    Path,
}

impl std::fmt::Display for SocialPluginSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SocialPluginSource::UserGlobal => write!(f, "global"),
            SocialPluginSource::ProjectLocal => write!(f, "project"),
            SocialPluginSource::Path => write!(f, "PATH"),
        }
    }
}

/// A discovered social media plugin with its manifest and origin.
#[derive(Debug, Clone)]
pub struct DiscoveredSocialPlugin {
    /// Parsed manifest.
    pub manifest: SocialPluginManifest,
    /// Directory containing `plugin.toml` (None for PATH-discovered plugins).
    pub plugin_dir: Option<PathBuf>,
    /// Discovery source.
    pub source: SocialPluginSource,
}

/// Discover all social media adapter plugins.
///
/// Resolution order:
/// 1. `~/.config/ta/plugins/social/` — user-global (highest priority)
/// 2. `.ta/plugins/social/` — project-local
///
/// PATH discovery (`ta-social-<name>`) is performed on-demand in
/// [`find_social_plugin`] when a named plugin is not found above.
pub fn discover_social_plugins(project_root: &Path) -> Vec<DiscoveredSocialPlugin> {
    let mut plugins = Vec::new();

    // 1. User-global
    if let Some(config_dir) = user_config_dir() {
        let global_dir = config_dir.join("ta").join("plugins").join("social");
        scan_social_plugin_dir(&global_dir, SocialPluginSource::UserGlobal, &mut plugins);
    }

    // 2. Project-local
    let project_dir = project_root.join(".ta").join("plugins").join("social");
    scan_social_plugin_dir(&project_dir, SocialPluginSource::ProjectLocal, &mut plugins);

    plugins
}

/// Scan a directory for social plugin subdirectories containing `plugin.toml`.
fn scan_social_plugin_dir(
    dir: &Path,
    source: SocialPluginSource,
    out: &mut Vec<DiscoveredSocialPlugin>,
) {
    if !dir.is_dir() {
        return;
    }

    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(e) => {
            tracing::warn!(
                dir = %dir.display(),
                error = %e,
                "Failed to read social plugin directory"
            );
            return;
        }
    };

    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }

        let manifest_path = path.join("plugin.toml");
        if !manifest_path.exists() {
            continue;
        }

        match SocialPluginManifest::load(&manifest_path) {
            Ok(manifest) => {
                tracing::debug!(
                    plugin = %manifest.name,
                    source = %source,
                    "Discovered social plugin"
                );
                out.push(DiscoveredSocialPlugin {
                    manifest,
                    plugin_dir: Some(path),
                    source: source.clone(),
                });
            }
            Err(e) => {
                tracing::warn!(
                    path = %manifest_path.display(),
                    error = %e,
                    "Skipping invalid social plugin manifest"
                );
            }
        }
    }
}

/// Find a social plugin by platform name.
///
/// Searches user-global → project-local → PATH.
/// Returns `None` if no plugin is found for the given platform.
pub fn find_social_plugin(platform: &str, project_root: &Path) -> Option<DiscoveredSocialPlugin> {
    // Search manifest-based plugins.
    let all = discover_social_plugins(project_root);
    if let Some(p) = all.into_iter().find(|p| p.manifest.name == platform) {
        return Some(p);
    }

    // Fall back to bare PATH executable: `ta-social-<name>`.
    let bare_cmd = format!("ta-social-{}", platform);
    if which_on_path(&bare_cmd) {
        tracing::info!(
            platform = %platform,
            command = %bare_cmd,
            "Found social plugin as bare executable on PATH"
        );
        return Some(DiscoveredSocialPlugin {
            manifest: SocialPluginManifest {
                name: platform.to_string(),
                version: "unknown".to_string(),
                plugin_type: "social".to_string(),
                command: bare_cmd,
                args: vec![],
                capabilities: vec![
                    "create_draft".to_string(),
                    "create_scheduled".to_string(),
                    "draft_status".to_string(),
                    "health".to_string(),
                ],
                description: None,
                timeout_secs: 60,
                protocol_version: SOCIAL_PROTOCOL_VERSION,
            },
            plugin_dir: None,
            source: SocialPluginSource::Path,
        });
    }

    None
}

// ---------------------------------------------------------------------------
// ExternalSocialAdapter
// ---------------------------------------------------------------------------

/// Social media adapter that delegates all operations to an external plugin process.
///
/// Each method call spawns a fresh process, sends one JSON request line to
/// stdin, reads one JSON response line from stdout, then waits for exit.
#[derive(Debug)]
pub struct ExternalSocialAdapter {
    /// Plugin command to spawn.
    command: String,
    /// Additional pre-configured args.
    args: Vec<String>,
    /// Platform name (from manifest).
    platform: String,
    /// Per-call timeout.
    timeout: Duration,
}

impl ExternalSocialAdapter {
    /// Create a new adapter from a discovered plugin manifest.
    pub fn new(manifest: &SocialPluginManifest) -> Self {
        Self {
            command: manifest.command.clone(),
            args: manifest.args.clone(),
            platform: manifest.name.clone(),
            timeout: Duration::from_secs(manifest.timeout_secs),
        }
    }

    /// Platform name (e.g., "linkedin", "x", "buffer").
    pub fn platform(&self) -> &str {
        &self.platform
    }

    /// Create a draft in the platform's native draft state.
    ///
    /// Returns the platform-assigned draft ID (e.g., "linkedin-draft-abc123").
    ///
    /// NOTE: There is intentionally no `publish` method on this type.
    /// TA never publishes social media posts on behalf of the user.
    pub fn create_draft(&self, post: SocialPostContent) -> Result<String, SocialPluginError> {
        let req = SocialPluginRequest::CreateDraft(CreateSocialDraftParams { post });
        let resp = self.call_plugin(&req, "create_draft")?;
        resp.draft_id
            .ok_or_else(|| SocialPluginError::InvalidResponse {
                name: self.platform.clone(),
                op: "create_draft".to_string(),
                reason: "response missing draft_id".to_string(),
            })
    }

    /// Schedule a post at a future time.
    ///
    /// Returns `(scheduled_id, confirmed_scheduled_at)`.
    ///
    /// The platform (or its scheduler) controls the actual publication.
    pub fn create_scheduled(
        &self,
        post: SocialPostContent,
        scheduled_at: &str,
    ) -> Result<(String, String), SocialPluginError> {
        let req = SocialPluginRequest::CreateScheduled(CreateScheduledParams {
            post,
            scheduled_at: scheduled_at.to_string(),
        });
        let resp = self.call_plugin(&req, "create_scheduled")?;
        let id = resp
            .scheduled_id
            .ok_or_else(|| SocialPluginError::InvalidResponse {
                name: self.platform.clone(),
                op: "create_scheduled".to_string(),
                reason: "response missing scheduled_id".to_string(),
            })?;
        let at = resp
            .scheduled_at
            .unwrap_or_else(|| scheduled_at.to_string());
        Ok((id, at))
    }

    /// Poll the current state of a previously created draft or scheduled post.
    pub fn draft_status(&self, draft_id: &str) -> Result<SocialPostState, SocialPluginError> {
        let req = SocialPluginRequest::DraftStatus(SocialDraftStatusParams {
            draft_id: draft_id.to_string(),
        });
        let resp = self.call_plugin(&req, "draft_status")?;
        Ok(resp.state.unwrap_or(SocialPostState::Unknown))
    }

    /// Run a health check: verify credentials and connectivity.
    ///
    /// Returns `(handle, provider_name)` on success.
    pub fn health(&self) -> Result<(String, String), SocialPluginError> {
        let req = SocialPluginRequest::Health(SocialHealthParams {});
        let resp = self.call_plugin(&req, "health")?;
        let handle = resp.handle.unwrap_or_else(|| "<unknown>".to_string());
        let provider = resp.provider.unwrap_or_else(|| self.platform.clone());
        Ok((handle, provider))
    }

    // -----------------------------------------------------------------------
    // Internal
    // -----------------------------------------------------------------------

    fn call_plugin(
        &self,
        req: &SocialPluginRequest,
        op: &str,
    ) -> Result<SocialPluginResponse, SocialPluginError> {
        let req_json = serde_json::to_string(req)?;

        let mut parts = self.command.split_whitespace();
        let program = parts.next().ok_or_else(|| SocialPluginError::SpawnFailed {
            command: self.command.clone(),
            reason: "command string is empty".to_string(),
        })?;

        let mut cmd = Command::new(program);
        for arg in parts {
            cmd.arg(arg);
        }
        for arg in &self.args {
            cmd.arg(arg);
        }
        cmd.stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = cmd.spawn().map_err(|e| SocialPluginError::SpawnFailed {
            command: self.command.clone(),
            reason: e.to_string(),
        })?;

        // Write request to stdin.
        if let Some(mut stdin) = child.stdin.take() {
            stdin
                .write_all(req_json.as_bytes())
                .and_then(|_| stdin.write_all(b"\n"))
                .map_err(|e| {
                    SocialPluginError::Io(std::io::Error::new(
                        e.kind(),
                        format!("failed to write to plugin stdin: {}", e),
                    ))
                })?;
        }

        // Wait with timeout.
        let timeout_ms = self.timeout.as_millis() as u64;
        let output =
            wait_with_timeout(child, timeout_ms).map_err(|_| SocialPluginError::Timeout {
                name: self.platform.clone(),
                op: op.to_string(),
                timeout_secs: self.timeout.as_secs(),
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(SocialPluginError::OpFailed {
                name: self.platform.clone(),
                op: op.to_string(),
                reason: format!(
                    "plugin exited with status {}. stderr: {}",
                    output.status,
                    stderr.trim()
                ),
            });
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let first_line = stdout.lines().next().unwrap_or("").trim();

        if first_line.is_empty() {
            return Err(SocialPluginError::InvalidResponse {
                name: self.platform.clone(),
                op: op.to_string(),
                reason: "plugin produced no output (expected one JSON line)".to_string(),
            });
        }

        let resp: SocialPluginResponse =
            serde_json::from_str(first_line).map_err(|e| SocialPluginError::InvalidResponse {
                name: self.platform.clone(),
                op: op.to_string(),
                reason: format!(
                    "invalid JSON: {}. Got: '{}'",
                    e,
                    if first_line.len() > 200 {
                        &first_line[..200]
                    } else {
                        first_line
                    }
                ),
            })?;

        if !resp.ok {
            return Err(SocialPluginError::OpFailed {
                name: self.platform.clone(),
                op: op.to_string(),
                reason: resp
                    .error
                    .unwrap_or_else(|| "plugin returned ok=false".to_string()),
            });
        }

        Ok(resp)
    }
}

// ---------------------------------------------------------------------------
// Supervisor check for social content
// ---------------------------------------------------------------------------

/// Result of a social content supervisor check.
#[derive(Debug, Clone)]
pub struct SocialSupervisorResult {
    /// Whether the content passed all checks.
    pub passed: bool,
    /// Human-readable reason for flagging (None if passed).
    pub flag_reason: Option<String>,
    /// Confidence score [0.0, 1.0].
    pub confidence: f64,
}

/// Social supervisor configuration.
#[derive(Debug, Clone, Default)]
pub struct SocialSupervisorConfig {
    /// Confidence below this threshold → review queue.
    pub min_confidence: f64,
    /// Substrings that trigger a flag if found in the post body.
    pub flag_if_contains: Vec<String>,
    /// Whether to check for patterns that look like unverified claims.
    pub check_unverified_claims: bool,
    /// Client names that must NOT appear unless explicitly allowed.
    pub blocked_client_names: Vec<String>,
}

/// Check social media post content against the supervisor policy.
///
/// Checks:
/// - `confidence >= min_confidence`
/// - No `flag_if_contains` substring appears in the post body
/// - No blocked client names in the post body (unless `allow_client_names` is true)
/// - Optionally checks for patterns that look like unverified claims
pub fn social_supervisor_check(
    body: &str,
    confidence: f64,
    config: &SocialSupervisorConfig,
    allow_client_names: bool,
) -> SocialSupervisorResult {
    // 1. Confidence threshold check.
    if confidence < config.min_confidence {
        return SocialSupervisorResult {
            passed: false,
            flag_reason: Some(format!(
                "supervisor confidence {:.2} below threshold {:.2}",
                confidence, config.min_confidence
            )),
            confidence,
        };
    }

    // 2. flag_if_contains checks.
    let body_lower = body.to_lowercase();
    for phrase in &config.flag_if_contains {
        if body_lower.contains(&phrase.to_lowercase()) {
            return SocialSupervisorResult {
                passed: false,
                flag_reason: Some(format!("post body contains flagged phrase: '{}'", phrase)),
                confidence,
            };
        }
    }

    // 3. Blocked client names.
    if !allow_client_names {
        for client in &config.blocked_client_names {
            if body_lower.contains(&client.to_lowercase()) {
                return SocialSupervisorResult {
                    passed: false,
                    flag_reason: Some(format!(
                        "post body contains client name '{}' (not allowed without explicit permission)",
                        client
                    )),
                    confidence,
                };
            }
        }
    }

    // 4. Unverified claims check (heuristic).
    if config.check_unverified_claims {
        let claim_patterns = [
            "guaranteed to",
            "100% proven",
            "scientifically proven",
            "always works",
            "never fails",
            "zero risk",
        ];
        for pattern in &claim_patterns {
            if body_lower.contains(pattern) {
                return SocialSupervisorResult {
                    passed: false,
                    flag_reason: Some(format!(
                        "post body contains potentially unverified claim: '{}'",
                        pattern
                    )),
                    confidence,
                };
            }
        }
    }

    SocialSupervisorResult {
        passed: true,
        flag_reason: None,
        confidence,
    }
}

// ---------------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------------

/// Check whether a binary exists on PATH.
fn which_on_path(name: &str) -> bool {
    std::env::var_os("PATH")
        .map(|path_var| std::env::split_paths(&path_var).any(|dir| dir.join(name).is_file()))
        .unwrap_or(false)
}

/// Get the user's config directory.
fn user_config_dir() -> Option<PathBuf> {
    if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
        return Some(PathBuf::from(xdg));
    }
    std::env::var("HOME")
        .ok()
        .map(|home| PathBuf::from(home).join(".config"))
}

/// Wait for a child process to exit, killing it after `timeout_ms` milliseconds.
fn wait_with_timeout(
    child: std::process::Child,
    timeout_ms: u64,
) -> std::result::Result<std::process::Output, String> {
    use std::sync::mpsc;

    let child_id = child.id();
    let (tx, rx) = mpsc::channel::<()>();

    let watchdog =
        std::thread::spawn(
            move || match rx.recv_timeout(Duration::from_millis(timeout_ms)) {
                Ok(()) => {}
                Err(_) => {
                    #[cfg(unix)]
                    unsafe {
                        libc::kill(child_id as libc::pid_t, libc::SIGKILL);
                    }
                    #[cfg(not(unix))]
                    let _ = child_id;
                }
            },
        );

    let output = child
        .wait_with_output()
        .map_err(|e| format!("wait_with_output failed: {}", e))?;

    let _ = tx.send(());
    let _ = watchdog.join();

    Ok(output)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn write_manifest(dir: &Path, content: &str) {
        std::fs::write(dir.join("plugin.toml"), content).unwrap();
    }

    #[test]
    fn discover_social_plugins_finds_manifests() {
        let root = tempfile::tempdir().unwrap();
        let social_dir = root.path().join(".ta").join("plugins").join("social");

        let linkedin_dir = social_dir.join("linkedin");
        std::fs::create_dir_all(&linkedin_dir).unwrap();
        write_manifest(
            &linkedin_dir,
            r#"
name = "linkedin"
version = "0.1.0"
type = "social"
command = "ta-social-linkedin"
capabilities = ["create_draft", "create_scheduled", "draft_status", "health"]
description = "LinkedIn social media adapter"
"#,
        );

        let plugins = discover_social_plugins(root.path());
        assert_eq!(plugins.len(), 1);
        assert_eq!(plugins[0].manifest.name, "linkedin");
        assert_eq!(plugins[0].source, SocialPluginSource::ProjectLocal);
    }

    #[test]
    fn discover_social_plugins_skips_invalid_manifest() {
        let root = tempfile::tempdir().unwrap();
        let social_dir = root.path().join(".ta").join("plugins").join("social");

        let good_dir = social_dir.join("linkedin");
        std::fs::create_dir_all(&good_dir).unwrap();
        write_manifest(
            &good_dir,
            r#"name = "linkedin"
type = "social"
command = "ta-social-linkedin"
"#,
        );

        let bad_dir = social_dir.join("bad");
        std::fs::create_dir_all(&bad_dir).unwrap();
        std::fs::write(bad_dir.join("plugin.toml"), "{{not valid toml}}").unwrap();

        let plugins = discover_social_plugins(root.path());
        assert_eq!(plugins.len(), 1);
        assert_eq!(plugins[0].manifest.name, "linkedin");
    }

    #[test]
    fn discover_social_plugins_empty_dir_returns_empty() {
        let root = tempfile::tempdir().unwrap();
        let plugins = discover_social_plugins(root.path());
        assert!(plugins.is_empty());
    }

    #[test]
    fn find_social_plugin_project_local() {
        let root = tempfile::tempdir().unwrap();
        let social_dir = root.path().join(".ta").join("plugins").join("social");

        let x_dir = social_dir.join("x");
        std::fs::create_dir_all(&x_dir).unwrap();
        write_manifest(
            &x_dir,
            r#"name = "x"
type = "social"
command = "ta-social-x"
"#,
        );

        let found = find_social_plugin("x", root.path());
        assert!(found.is_some());
        assert_eq!(found.unwrap().manifest.name, "x");
    }

    #[test]
    fn find_social_plugin_missing_returns_none() {
        let root = tempfile::tempdir().unwrap();
        let found = find_social_plugin("nonexistent-platform", root.path());
        assert!(found.is_none());
    }

    #[test]
    fn social_plugin_source_display() {
        assert_eq!(SocialPluginSource::UserGlobal.to_string(), "global");
        assert_eq!(SocialPluginSource::ProjectLocal.to_string(), "project");
        assert_eq!(SocialPluginSource::Path.to_string(), "PATH");
    }

    #[test]
    fn supervisor_check_passes_clean_content() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.8,
            flag_if_contains: vec!["I promise".to_string()],
            check_unverified_claims: true,
            blocked_client_names: vec!["AcmeCorp".to_string()],
        };
        let result = social_supervisor_check(
            "Excited to share our new AI pipeline feature!",
            0.95,
            &config,
            false,
        );
        assert!(result.passed);
        assert!(result.flag_reason.is_none());
    }

    #[test]
    fn supervisor_check_fails_low_confidence() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.8,
            ..Default::default()
        };
        let result = social_supervisor_check("Good content here", 0.5, &config, false);
        assert!(!result.passed);
        assert!(result.flag_reason.unwrap().contains("below threshold"));
    }

    #[test]
    fn supervisor_check_fails_flag_phrase() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.0,
            flag_if_contains: vec!["I promise".to_string()],
            ..Default::default()
        };
        let result =
            social_supervisor_check("I promise this will work perfectly.", 1.0, &config, false);
        assert!(!result.passed);
        assert!(result.flag_reason.unwrap().contains("I promise"));
    }

    #[test]
    fn supervisor_check_fails_client_name() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.0,
            blocked_client_names: vec!["SecretClient".to_string()],
            ..Default::default()
        };
        let result = social_supervisor_check(
            "Working with SecretClient on this amazing project!",
            1.0,
            &config,
            false,
        );
        assert!(!result.passed);
        assert!(result.flag_reason.unwrap().contains("client name"));
    }

    #[test]
    fn supervisor_check_allows_client_name_when_permitted() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.0,
            blocked_client_names: vec!["SecretClient".to_string()],
            ..Default::default()
        };
        let result = social_supervisor_check(
            "Working with SecretClient on this amazing project!",
            1.0,
            &config,
            true, // explicitly allowed
        );
        assert!(result.passed);
    }

    #[test]
    fn supervisor_check_fails_unverified_claim() {
        let config = SocialSupervisorConfig {
            min_confidence: 0.0,
            check_unverified_claims: true,
            ..Default::default()
        };
        let result = social_supervisor_check(
            "This is guaranteed to increase your revenue by 500%!",
            1.0,
            &config,
            false,
        );
        assert!(!result.passed);
        assert!(result.flag_reason.unwrap().contains("unverified claim"));
    }

    /// Return the path to a shared mock social plugin binary.
    #[cfg(unix)]
    fn shared_mock_social_plugin_path() -> &'static std::path::Path {
        use std::io::Write as W;
        use std::os::unix::fs::PermissionsExt;
        use std::sync::OnceLock;

        static MOCK_PATH: OnceLock<std::path::PathBuf> = OnceLock::new();
        MOCK_PATH.get_or_init(|| {
            let pid = std::process::id();
            let name = format!("ta-social-mock-shared-{}", pid);

            #[cfg(target_os = "linux")]
            let path = {
                let shm = std::path::Path::new("/dev/shm");
                if shm.exists() {
                    shm.join(&name)
                } else {
                    std::path::PathBuf::from("/tmp").join(&name)
                }
            };
            #[cfg(not(target_os = "linux"))]
            let path = std::env::temp_dir().join(&name);

            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(
                br#"#!/bin/sh
read -r line
case "$line" in
  *create_draft*)    echo '{"ok":true,"draft_id":"linkedin-draft-abc123"}' ;;
  *create_scheduled*) echo '{"ok":true,"scheduled_id":"buffer-post-xyz","scheduled_at":"2026-04-07T14:00:00Z"}' ;;
  *)                 echo '{"ok":true,"handle":"@testuser","provider":"mock"}' ;;
esac
"#,
            )
            .unwrap();
            f.sync_all().unwrap();
            drop(f);

            let mut perms = std::fs::metadata(&path).unwrap().permissions();
            perms.set_mode(0o755);
            std::fs::set_permissions(&path, perms).unwrap();
            let _ = std::fs::metadata(&path).unwrap();
            path
        })
    }

    #[cfg(unix)]
    #[test]
    fn external_adapter_health_returns_handle() {
        let plugin_path = shared_mock_social_plugin_path();
        let manifest = SocialPluginManifest {
            name: "mock".to_string(),
            version: "0.1.0".to_string(),
            plugin_type: "social".to_string(),
            command: plugin_path.display().to_string(),
            args: vec![],
            capabilities: vec!["health".to_string()],
            description: None,
            timeout_secs: 30,
            protocol_version: SOCIAL_PROTOCOL_VERSION,
        };

        let adapter = ExternalSocialAdapter::new(&manifest);
        let (handle, provider) = adapter.health().unwrap();
        assert_eq!(handle, "@testuser");
        assert_eq!(provider, "mock");
    }

    #[cfg(unix)]
    #[test]
    fn external_adapter_create_draft_returns_id() {
        let plugin_path = shared_mock_social_plugin_path();
        let manifest = SocialPluginManifest {
            name: "mock".to_string(),
            version: "0.1.0".to_string(),
            plugin_type: "social".to_string(),
            command: plugin_path.display().to_string(),
            args: vec![],
            capabilities: vec!["create_draft".to_string()],
            description: None,
            timeout_secs: 30,
            protocol_version: SOCIAL_PROTOCOL_VERSION,
        };

        let adapter = ExternalSocialAdapter::new(&manifest);
        let draft_id = adapter
            .create_draft(SocialPostContent {
                body: "Excited to share this!".to_string(),
                media_urls: vec![],
                reply_to_id: None,
            })
            .unwrap();
        assert_eq!(draft_id, "linkedin-draft-abc123");
    }

    #[cfg(unix)]
    #[test]
    fn external_adapter_create_scheduled_returns_id_and_time() {
        let plugin_path = shared_mock_social_plugin_path();
        let manifest = SocialPluginManifest {
            name: "mock".to_string(),
            version: "0.1.0".to_string(),
            plugin_type: "social".to_string(),
            command: plugin_path.display().to_string(),
            args: vec![],
            capabilities: vec!["create_scheduled".to_string()],
            description: None,
            timeout_secs: 30,
            protocol_version: SOCIAL_PROTOCOL_VERSION,
        };

        let adapter = ExternalSocialAdapter::new(&manifest);
        let (scheduled_id, scheduled_at) = adapter
            .create_scheduled(
                SocialPostContent {
                    body: "Scheduled post content".to_string(),
                    media_urls: vec![],
                    reply_to_id: None,
                },
                "2026-04-07T14:00:00Z",
            )
            .unwrap();
        assert_eq!(scheduled_id, "buffer-post-xyz");
        assert_eq!(scheduled_at, "2026-04-07T14:00:00Z");
    }
}