workspace-mgr 0.1.0

Fixed-policy repository workspace manager for coding agents
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
use std::cmp::Ordering;
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use fs2::FileExt;
use semver::Version;
use serde::{Deserialize, Serialize};

const CACHE_SCHEMA: u32 = 1;
const CACHE_FILE: &str = "update-check.json";
const LOCK_FILE: &str = "update-check.lock";
const REGISTRY_URL: &str = "https://crates.io/api/v1/crates/workspace-mgr";
const SUCCESS_INTERVAL: Duration = Duration::from_secs(6 * 60 * 60);
const FAILURE_INTERVAL: Duration = Duration::from_secs(60 * 60);
const REQUEST_TIMEOUT: Duration = Duration::from_millis(750);
const RESPONSE_LIMIT: u64 = 1024 * 1024;
const CACHE_LIMIT: u64 = 64 * 1024;
const DISABLE_ENV: &str = "WORKSPACE_MGR_UPDATE_CHECK_DISABLE";

#[cfg(debug_assertions)]
const TEST_URL_ENV: &str = "WORKSPACE_MGR_UPDATE_TEST_URL";
#[cfg(debug_assertions)]
const TEST_CACHE_ENV: &str = "WORKSPACE_MGR_UPDATE_TEST_CACHE";

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
struct UpdateCache {
    schema_version: u32,
    last_attempt: Option<u64>,
    last_success: Option<u64>,
    latest_any: Option<String>,
    latest_stable: Option<String>,
}

impl UpdateCache {
    fn needs_refresh(&self, now: u64) -> bool {
        if self.schema_version != CACHE_SCHEMA {
            return true;
        }
        match (self.last_attempt, self.last_success) {
            (Some(attempt), success) if success.is_none_or(|value| attempt > value) => {
                !is_recent(now, attempt, FAILURE_INTERVAL)
            }
            (_, Some(success)) => !is_recent(now, success, SUCCESS_INTERVAL),
            _ => true,
        }
    }

    fn candidate_for(&self, current: &Version) -> Option<Version> {
        let candidate = if current.pre.is_empty() {
            self.latest_stable.as_deref()
        } else {
            self.latest_any.as_deref()
        }?;
        let candidate = Version::parse(candidate).ok()?;
        (candidate.cmp_precedence(current) == Ordering::Greater).then_some(candidate)
    }
}

#[derive(Debug, Clone)]
struct LatestVersions {
    any: Version,
    stable: Option<Version>,
}

#[derive(Debug, Deserialize)]
struct RegistryResponse {
    versions: Vec<RegistryVersion>,
}

#[derive(Debug, Deserialize)]
struct RegistryVersion {
    num: String,
    yanked: bool,
}

pub fn check_and_warn() {
    if std::env::var_os(DISABLE_ENV).is_some() {
        return;
    }
    let Some(cache_path) = cache_path() else {
        return;
    };
    let Ok(current) = Version::parse(env!("CARGO_PKG_VERSION")) else {
        return;
    };
    let endpoint = registry_url();
    let now = unix_seconds(SystemTime::now());
    if let Some(latest) = check(&cache_path, &current, now, || {
        fetch_registry(&endpoint, REQUEST_TIMEOUT)
    }) {
        eprintln!(
            "workspace-mgr: update available: {current} -> {latest}; agent: notify the user and ask before updating"
        );
    }
}

fn check<F>(cache_path: &Path, current: &Version, now: u64, fetch: F) -> Option<Version>
where
    F: FnOnce() -> io::Result<LatestVersions>,
{
    let initial = read_cache(cache_path).unwrap_or_default();
    if !initial.needs_refresh(now) {
        return initial.candidate_for(current);
    }

    let parent = cache_path.parent()?;
    if fs::create_dir_all(parent).is_err() {
        return initial.candidate_for(current);
    }
    let lock_path = parent.join(LOCK_FILE);
    let Ok(lock) = OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(&lock_path)
    else {
        return initial.candidate_for(current);
    };
    if lock.try_lock_exclusive().is_err() {
        return initial.candidate_for(current);
    }

    let current_cache = read_cache(cache_path).unwrap_or(initial);
    if !current_cache.needs_refresh(now) {
        return current_cache.candidate_for(current);
    }

    let mut updated = current_cache;
    updated.schema_version = CACHE_SCHEMA;
    updated.last_attempt = Some(now);
    if let Ok(latest) = fetch() {
        updated.last_success = Some(now);
        updated.latest_any = Some(latest.any.to_string());
        updated.latest_stable = latest.stable.map(|version| version.to_string());
    }
    let candidate = updated.candidate_for(current);
    let _ = write_cache(cache_path, &updated);
    candidate
}

fn fetch_registry(endpoint: &str, timeout: Duration) -> io::Result<LatestVersions> {
    let user_agent = format!(
        "workspace-mgr/{} (https://github.com/S-kblogN/workspace-mgr)",
        env!("CARGO_PKG_VERSION")
    );
    let config = ureq::Agent::config_builder()
        .timeout_global(Some(timeout))
        .timeout_connect(Some(timeout))
        .max_redirects(3)
        .build();
    let agent = ureq::Agent::new_with_config(config);
    let mut response = agent
        .get(endpoint)
        .header("User-Agent", &user_agent)
        .call()
        .map_err(io::Error::other)?;
    let body = response
        .body_mut()
        .with_config()
        .limit(RESPONSE_LIMIT)
        .read_to_vec()
        .map_err(io::Error::other)?;
    let payload: RegistryResponse = serde_json::from_slice(&body)?;

    let versions = payload
        .versions
        .into_iter()
        .filter(|entry| !entry.yanked)
        .filter_map(|entry| Version::parse(&entry.num).ok())
        .collect::<Vec<_>>();
    let any = versions
        .iter()
        .max_by(|left, right| left.cmp_precedence(right))
        .cloned()
        .ok_or_else(|| io::Error::other("registry response has no usable versions"))?;
    let stable = versions
        .into_iter()
        .filter(|version| version.pre.is_empty())
        .max_by(|left, right| left.cmp_precedence(right));
    Ok(LatestVersions { any, stable })
}

fn cache_path() -> Option<PathBuf> {
    #[cfg(debug_assertions)]
    if let Some(path) = std::env::var_os(TEST_CACHE_ENV).filter(|value| !value.is_empty()) {
        return Some(PathBuf::from(path));
    }
    if let Some(root) = std::env::var_os("XDG_CACHE_HOME").filter(|value| !value.is_empty()) {
        return Some(PathBuf::from(root).join("workspace-mgr").join(CACHE_FILE));
    }
    let home = std::env::var_os("HOME")
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)?;
    #[cfg(target_os = "macos")]
    let root = home.join("Library").join("Caches");
    #[cfg(not(target_os = "macos"))]
    let root = home.join(".cache");
    Some(root.join("workspace-mgr").join(CACHE_FILE))
}

fn registry_url() -> String {
    #[cfg(debug_assertions)]
    if let Some(url) = std::env::var_os(TEST_URL_ENV).filter(|value| !value.is_empty()) {
        return url.to_string_lossy().into_owned();
    }
    REGISTRY_URL.to_owned()
}

fn read_cache(path: &Path) -> Option<UpdateCache> {
    if fs::metadata(path).ok()?.len() > CACHE_LIMIT {
        return None;
    }
    let bytes = fs::read(path).ok()?;
    let cache: UpdateCache = serde_json::from_slice(&bytes).ok()?;
    (cache.schema_version == CACHE_SCHEMA).then_some(cache)
}

fn write_cache(path: &Path, cache: &UpdateCache) -> io::Result<()> {
    let parent = path
        .parent()
        .ok_or_else(|| io::Error::other("update cache path has no parent"))?;
    fs::create_dir_all(parent)?;
    let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
    serde_json::to_writer(&mut temporary, cache)?;
    temporary.write_all(b"\n")?;
    temporary.as_file_mut().sync_all()?;
    temporary.persist(path).map_err(|error| error.error)?;
    Ok(())
}

fn unix_seconds(time: SystemTime) -> u64 {
    time.duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn is_recent(now: u64, timestamp: u64, interval: Duration) -> bool {
    timestamp <= now && now - timestamp < interval.as_secs()
}

#[cfg(test)]
mod tests {
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
    use std::thread;

    use super::*;

    fn cache(any: &str, stable: Option<&str>) -> UpdateCache {
        UpdateCache {
            schema_version: CACHE_SCHEMA,
            last_attempt: Some(100),
            last_success: Some(100),
            latest_any: Some(any.to_owned()),
            latest_stable: stable.map(str::to_owned),
        }
    }

    fn latest(any: &str, stable: Option<&str>) -> LatestVersions {
        LatestVersions {
            any: Version::parse(any).unwrap(),
            stable: stable.map(|value| Version::parse(value).unwrap()),
        }
    }

    #[test]
    fn prerelease_installations_follow_the_newest_channel() {
        let current = Version::parse("1.0.0-alpha.1").unwrap();
        assert_eq!(
            cache("1.1.0-beta.1", Some("1.0.0"))
                .candidate_for(&current)
                .unwrap(),
            Version::parse("1.1.0-beta.1").unwrap()
        );
    }

    #[test]
    fn stable_installations_ignore_newer_prereleases() {
        let current = Version::parse("1.0.0").unwrap();
        assert!(
            cache("1.1.0-beta.1", Some("1.0.0"))
                .candidate_for(&current)
                .is_none()
        );
        assert_eq!(
            cache("1.2.0-beta.1", Some("1.1.0"))
                .candidate_for(&current)
                .unwrap(),
            Version::parse("1.1.0").unwrap()
        );
    }

    #[test]
    fn fresh_cache_avoids_a_registry_request() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(CACHE_FILE);
        write_cache(&path, &cache("2.0.0", Some("2.0.0"))).unwrap();
        let requests = Arc::new(AtomicUsize::new(0));
        let observed = Arc::clone(&requests);
        let current = Version::parse("1.0.0").unwrap();
        let candidate = check(&path, &current, 101, move || {
            observed.fetch_add(1, AtomicOrdering::SeqCst);
            Ok(latest("3.0.0", Some("3.0.0")))
        });
        assert_eq!(candidate.unwrap(), Version::parse("2.0.0").unwrap());
        assert_eq!(requests.load(AtomicOrdering::SeqCst), 0);
    }

    #[test]
    fn failed_request_is_silent_and_negative_cached() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(CACHE_FILE);
        let current = Version::parse("1.0.0").unwrap();
        assert!(check(&path, &current, 100, || Err(io::Error::other("offline"))).is_none());
        let second = check(&path, &current, 101, || {
            panic!("negative cache should suppress a second request")
        });
        assert!(second.is_none());
        let written = read_cache(&path).unwrap();
        assert_eq!(written.last_attempt, Some(100));
        assert_eq!(written.last_success, None);
    }

    #[test]
    fn corrupt_cache_is_replaced_atomically() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(CACHE_FILE);
        fs::write(&path, b"not json").unwrap();
        let current = Version::parse("1.0.0-alpha.1").unwrap();
        let candidate = check(&path, &current, 200, || Ok(latest("1.0.0-alpha.2", None)));
        assert_eq!(candidate.unwrap(), Version::parse("1.0.0-alpha.2").unwrap());
        assert_eq!(
            read_cache(&path).unwrap().latest_any.as_deref(),
            Some("1.0.0-alpha.2")
        );
    }

    #[test]
    fn oversized_cache_is_ignored_and_replaced() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(CACHE_FILE);
        fs::write(&path, vec![b'x'; CACHE_LIMIT as usize + 1]).unwrap();
        let current = Version::parse("1.0.0-alpha.1").unwrap();
        let candidate = check(&path, &current, 200, || Ok(latest("1.0.0-alpha.2", None)));
        assert_eq!(candidate.unwrap(), Version::parse("1.0.0-alpha.2").unwrap());
        assert!(fs::metadata(&path).unwrap().len() < CACHE_LIMIT);
    }

    #[test]
    fn lock_contention_never_blocks_the_command() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(CACHE_FILE);
        let lock_path = temp.path().join(LOCK_FILE);
        let lock = OpenOptions::new()
            .create(true)
            .truncate(false)
            .read(true)
            .write(true)
            .open(lock_path)
            .unwrap();
        lock.try_lock_exclusive().unwrap();
        let current = Version::parse("1.0.0").unwrap();
        assert!(
            check(&path, &current, 100, || {
                panic!("a contended checker must not access the network")
            })
            .is_none()
        );
    }

    #[test]
    fn registry_response_ignores_yanked_and_invalid_versions() {
        let body = r#"{"versions":[{"num":"9.0.0","yanked":true},{"num":"broken","yanked":false},{"num":"2.0.0-beta.1","yanked":false},{"num":"1.5.0","yanked":false}]}"#;
        let (url, server) = serve_once(body, Duration::ZERO);
        let result = fetch_registry(&url, Duration::from_secs(1)).unwrap();
        server.join().unwrap();
        assert_eq!(result.any, Version::parse("2.0.0-beta.1").unwrap());
        assert_eq!(result.stable, Some(Version::parse("1.5.0").unwrap()));
    }

    #[test]
    fn registry_timeout_is_a_nonfatal_error() {
        let body = r#"{"versions":[]}"#;
        let (url, server) = serve_once(body, Duration::from_millis(200));
        assert!(fetch_registry(&url, Duration::from_millis(30)).is_err());
        server.join().unwrap();
    }

    fn serve_once(body: &str, delay: Duration) -> (String, thread::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let address = listener.local_addr().unwrap();
        let body = body.to_owned();
        let server = thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            let mut request = [0_u8; 2048];
            let _ = stream.read(&mut request);
            thread::sleep(delay);
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                body.len(),
                body
            );
            let _ = stream.write_all(response.as_bytes());
        });
        (
            format!("http://{address}/api/v1/crates/workspace-mgr"),
            server,
        )
    }
}