clawshell 0.2.1

A security privileged process for the OpenClaw ecosystem.
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
use std::path::{Path, PathBuf};

use crate::config::Config;
use crate::migration::core::{AmbiguityResolver, AmbiguousChoice, ConfigVersion, MigrationIssue};
use crate::migration::target::{MigrationTarget, TargetError, TargetMigrationOutput};

mod versions;

#[derive(Debug, Clone)]
pub struct ClawshellTomlTarget {
    path: PathBuf,
}

impl ClawshellTomlTarget {
    pub fn new(path: PathBuf) -> Self {
        Self { path }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionGateStatus {
    Current(ConfigVersion),
    Missing,
    Mismatch { found: ConfigVersion },
}

pub fn version_gate_status(path: &Path) -> Result<VersionGateStatus, TargetError> {
    let content = std::fs::read_to_string(path).map_err(|e| TargetError::Validation {
        details: format!("failed to read '{}': {}", path.display(), e),
    })?;
    version_gate_status_from_content(&content)
}

pub fn version_gate_status_from_content(content: &str) -> Result<VersionGateStatus, TargetError> {
    let expected = ConfigVersion::current();
    let found = detect_version_from_content(content)?;

    let status = match found {
        Some(version) if version == expected => VersionGateStatus::Current(version),
        Some(version) => VersionGateStatus::Mismatch { found: version },
        None => VersionGateStatus::Missing,
    };

    Ok(status)
}

pub fn ensure_current_version(path: &Path) -> Result<(), TargetError> {
    let expected = ConfigVersion::current();
    match version_gate_status(path)? {
        VersionGateStatus::Current(_) => Ok(()),
        VersionGateStatus::Missing => Err(TargetError::MigrationRequiredMissingVersion {
            expected,
            path: path.to_path_buf(),
        }),
        VersionGateStatus::Mismatch { found } => {
            Err(TargetError::MigrationRequiredVersionMismatch {
                found,
                expected,
                path: path.to_path_buf(),
            })
        }
    }
}

pub fn detect_version_from_content(content: &str) -> Result<Option<ConfigVersion>, TargetError> {
    let parsed: toml::Value = toml::from_str(content)?;
    let table = parsed
        .as_table()
        .ok_or(TargetError::TopLevelTableExpected)?;

    let Some(version_value) = table.get("version") else {
        return Ok(None);
    };

    let Some(version_str) = version_value.as_str() else {
        return Err(TargetError::VersionMustBeString {
            found: version_value.type_str().to_string(),
        });
    };

    let parsed =
        version_str
            .parse::<ConfigVersion>()
            .map_err(|e| TargetError::InvalidVersionValue {
                value: version_str.to_string(),
                source: e,
            })?;

    Ok(Some(parsed))
}

impl MigrationTarget for ClawshellTomlTarget {
    fn name(&self) -> &'static str {
        "clawshell"
    }

    fn path(&self) -> &Path {
        &self.path
    }

    fn detect_version(&self, content: &str) -> Result<Option<ConfigVersion>, TargetError> {
        detect_version_from_content(content)
    }

    fn migrate(
        &self,
        content: &str,
        from: &ConfigVersion,
        to: &ConfigVersion,
        resolver: &mut dyn AmbiguityResolver,
    ) -> Result<TargetMigrationOutput, TargetError> {
        if from > to {
            let issue = MigrationIssue {
                target: self.name().to_string(),
                step_id: "future-version".to_string(),
                message: format!(
                    "source version {} is newer than target {} (downgrade is unsupported)",
                    from, to
                ),
                recommended: AmbiguousChoice::Abort,
            };
            return match resolver.resolve(&issue)? {
                AmbiguousChoice::ApplyRecommended | AmbiguousChoice::Abort => {
                    Err(TargetError::DowngradeAborted {
                        from: from.clone(),
                        to: to.clone(),
                    })
                }
                AmbiguousChoice::Skip => Ok(TargetMigrationOutput {
                    content: content.to_string(),
                    applied_steps: vec!["skip-future-version".to_string()],
                    warnings: vec![format!(
                        "skipped migration because source version {} is newer than target {}",
                        from, to
                    )],
                }),
            };
        }

        let detected = detect_version_from_content(content)?;
        let mut value: toml::Value = toml::from_str(content)?;
        let table = value
            .as_table_mut()
            .ok_or(TargetError::TopLevelTableExpected)?;
        let version_step_output =
            versions::apply_versioned_steps(self.name(), table, from, to, resolver)?;
        let mut applied_steps = version_step_output.applied_steps;
        let warnings = version_step_output.warnings;

        if detected.as_ref() != Some(to) {
            table.insert("version".to_string(), toml::Value::String(to.to_string()));
            let step = match detected {
                Some(found) => format!("set-version:{}->{}", found, to),
                None => format!("set-version:missing->{}", to),
            };
            applied_steps.push(step);
        }

        let migrated = toml::to_string_pretty(&value)?;

        Ok(TargetMigrationOutput {
            content: migrated,
            applied_steps,
            warnings,
        })
    }

    fn validate(&self, content: &str) -> Result<(), TargetError> {
        Config::from_str_with_validation(content).map_err(|e| TargetError::Validation {
            details: e.to_string(),
        })?;
        Ok(())
    }
}

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

    #[test]
    fn test_detect_version_from_content_missing() {
        let content = r#"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://api.openai.com"
"#;

        let version = detect_version_from_content(content).unwrap();
        assert!(version.is_none());
    }

    #[test]
    fn test_detect_version_from_content_present() {
        let content = r#"
version = "0.0.1"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://api.openai.com"
"#;

        let version = detect_version_from_content(content).unwrap().unwrap();
        assert_eq!(version.to_string(), "0.0.1");
    }

    #[test]
    fn test_detect_version_from_content_prerelease() {
        let content = r#"
version = "0.1.0-alpha.0"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
openai_base_url = "https://api.openai.com"
"#;

        let version = detect_version_from_content(content).unwrap().unwrap();
        assert_eq!(version.to_string(), "0.1.0-alpha.0");
    }

    #[test]
    fn test_version_gate_status_missing() {
        let content = r#"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://api.openai.com"
"#;

        let status = version_gate_status_from_content(content).unwrap();
        assert_eq!(status, VersionGateStatus::Missing);
    }

    #[test]
    fn test_version_gate_status_current() {
        let current = env!("CARGO_PKG_VERSION");
        let content = format!(
            r#"
version = "{current}"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
openai_base_url = "https://api.openai.com"
"#,
        );

        let status = version_gate_status_from_content(&content).unwrap();
        let expected = ConfigVersion::current();
        assert_eq!(status, VersionGateStatus::Current(expected));
    }

    #[test]
    fn test_migrate_stamps_version_when_missing() {
        let target = ClawshellTomlTarget::new(PathBuf::from("/tmp/clawshell.toml"));
        let content = r#"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://api.openai.com"
"#;

        #[derive(Debug)]
        struct NoopResolver;

        impl AmbiguityResolver for NoopResolver {
            fn resolve(
                &mut self,
                _issue: &crate::migration::core::MigrationIssue,
            ) -> Result<
                crate::migration::core::AmbiguousChoice,
                crate::migration::core::AmbiguityResolutionError,
            > {
                Ok(crate::migration::core::AmbiguousChoice::ApplyRecommended)
            }
        }

        let mut resolver = NoopResolver;
        let from = ConfigVersion::legacy_baseline();
        let to = ConfigVersion::current();
        let result = target.migrate(content, &from, &to, &mut resolver).unwrap();
        assert!(result.content.contains("version = \""));
        assert!(
            result
                .content
                .contains("openai_base_url = \"https://api.openai.com\"")
        );
        assert!(!result.content.contains("\nbase_url = "));
        assert!(
            result
                .applied_steps
                .iter()
                .any(|s| s == "rename `upstream.base_url` to `upstream.openai_base_url`")
        );
    }

    #[test]
    fn test_migrate_with_both_upstream_keys_aborts_by_default() {
        let target = ClawshellTomlTarget::new(PathBuf::from("/tmp/clawshell.toml"));
        let content = r#"
version = "0.0.1"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://legacy.openai.com"
openai_base_url = "https://api.openai.com"
"#;

        #[derive(Debug)]
        struct AbortResolver;

        impl AmbiguityResolver for AbortResolver {
            fn resolve(
                &mut self,
                _issue: &crate::migration::core::MigrationIssue,
            ) -> Result<
                crate::migration::core::AmbiguousChoice,
                crate::migration::core::AmbiguityResolutionError,
            > {
                Ok(crate::migration::core::AmbiguousChoice::ApplyRecommended)
            }
        }

        let mut resolver = AbortResolver;
        let from: ConfigVersion = "0.0.1".parse().unwrap();
        let to = ConfigVersion::current();
        let err = target
            .migrate(content, &from, &to, &mut resolver)
            .expect_err("expected ambiguity to abort migration");
        assert!(
            err.to_string()
                .contains("both `upstream.base_url` and `upstream.openai_base_url` are present")
        );
    }

    #[test]
    fn test_migrate_with_both_upstream_keys_skip_drops_legacy() {
        let target = ClawshellTomlTarget::new(PathBuf::from("/tmp/clawshell.toml"));
        let content = r#"
version = "0.0.1"
log_level = "info"

[server]
host = "127.0.0.1"
port = 18790

[upstream]
base_url = "https://legacy.openai.com"
openai_base_url = "https://api.openai.com"
"#;

        #[derive(Debug)]
        struct SkipResolver;

        impl AmbiguityResolver for SkipResolver {
            fn resolve(
                &mut self,
                _issue: &crate::migration::core::MigrationIssue,
            ) -> Result<
                crate::migration::core::AmbiguousChoice,
                crate::migration::core::AmbiguityResolutionError,
            > {
                Ok(crate::migration::core::AmbiguousChoice::Skip)
            }
        }

        let mut resolver = SkipResolver;
        let from: ConfigVersion = "0.0.1".parse().unwrap();
        let to = ConfigVersion::current();
        let result = target.migrate(content, &from, &to, &mut resolver).unwrap();
        assert!(
            result
                .content
                .contains("openai_base_url = \"https://api.openai.com\"")
        );
        assert!(!result.content.contains("\nbase_url = "));
        assert!(
            result
                .warnings
                .iter()
                .any(|w| w.contains("Dropped legacy upstream.base_url"))
        );
    }
}