dort-core 0.2.0

UI-independent planning and guarded filesystem services for Dort
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
//! Private install record storage.
use super::{Outcome, Scope, Sources, invalid};
use crate::{
    Result, adoption,
    context::HostContext,
    error::io_error,
    journal,
    observation::Fingerprint,
    state::{PrivateDir, WriterLock},
};
use rustix::fs::{self, Mode, OFlags};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::{fs::File, io::Read, path::PathBuf};

const MAX_ATTEMPTS: usize = 256;
const MAX_FILE: usize = 64 * 1024;
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Problem {
    MissingIntent,
    Malformed,
    Changed,
}
pub struct Attempt {
    pub id: String,
    pub scope: Option<Scope>,
    pub outcome: Option<Outcome>,
    pub observed_versions: Vec<String>,
    pub problem: Option<Problem>,
    pub unresolved: bool,
    pub explicitly_closed: bool,
    pub(super) head: String,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Intent {
    schema: u32,
    id: String,
    scope: Scope,
    sources: Sources,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Completed {
    schema: u32,
    id: String,
    intent_sha256: String,
    outcome: Outcome,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    observed_versions: Vec<String>,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct ProbeScope {
    pub executable: PathBuf,
    pub home: PathBuf,
    pub search_dirs: Vec<PathBuf>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub npm_binding: Option<super::inspection::NpmBinding>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub brew_prefix: Option<PathBuf>,
}
impl ProbeScope {
    pub(super) fn preview(&self) -> Result<super::inspection::Preview> {
        if let Some(prefix) = &self.brew_prefix {
            if self.npm_binding.is_some() || !super::inspection::absolute(prefix) {
                return Err(invalid("invalid Homebrew closure scope"));
            }
            super::inspection::Preview::new(
                super::inspection::Query::BrewFormulae,
                self.executable.clone(),
                self.home.clone(),
                self.search_dirs.clone(),
            )
        } else if let Some(b) = &self.npm_binding {
            if b.node != self.executable {
                return Err(invalid("closure Node binding differs"));
            }
            super::inspection::Preview::npm_probe(
                b.clone(),
                self.home.clone(),
                self.search_dirs.clone(),
            )
        } else {
            return Err(invalid("closure requires npm or Homebrew scope"));
        }
        .map_err(|_| invalid("invalid explicit Tools closure scope"))
    }
    fn schema(&self) -> u32 {
        if self.brew_prefix.is_some() { 3 } else { 2 }
    }
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Closure {
    schema: u32,
    id: String,
    head: String,
    scope: ProbeScope,
    observed_records: usize,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Envelope {
    sha256: String,
    payload: String,
}
pub(super) struct Handle {
    pub id: String,
    head: String,
}
fn root(host: &HostContext) -> PathBuf {
    host.state_dir.join("tools-attempts")
}
fn directory(host: &HostContext, id: &str) -> Result<PrivateDir> {
    if !journal::valid_id(id) {
        return Err(invalid("invalid Tools attempt identifier"));
    }
    PrivateDir::open(&root(host).join(id), false)?.ok_or_else(|| invalid("Tools attempt not found"))
}
fn names(dir: &PrivateDir, limit: usize) -> Result<Vec<String>> {
    let mut names = vec![];
    for item in
        fs::Dir::read_from(&dir.file).map_err(|e| io_error("list Tools state", &dir.path, e))?
    {
        let item = item.map_err(|e| io_error("list Tools state entry", &dir.path, e))?;
        let raw = item.file_name().to_bytes();
        if raw == b"." || raw == b".." {
            continue;
        }
        if names.len() >= limit {
            return Err(invalid("Tools state entry limit exceeded"));
        }
        names.push(String::from_utf8(raw.to_vec()).map_err(|_| {
            invalid("Tools state names must be UTF-8; explicit manual repair required")
        })?);
    }
    names.sort();
    Ok(names)
}
fn read_bytes(dir: &PrivateDir, name: &str) -> Result<Vec<u8>> {
    let mut file = File::from(
        fs::openat(
            &dir.file,
            name,
            OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(|e| io_error("open private Tools metadata", dir.path.join(name), e))?,
    );
    dir.check_file(&file, name)?;
    let stat = |file: &File| {
        fs::fstat(file)
            .map(|s| Fingerprint::from_stat(&s))
            .map_err(|e| io_error("inspect Tools metadata", &dir.path, e))
    };
    let before = stat(&file)?;
    if before.size < 0 || before.size as usize > MAX_FILE {
        return Err(invalid("Tools metadata exceeds bounded read limit"));
    }
    let mut bytes = vec![];
    (&mut file)
        .take(MAX_FILE as u64 + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| io_error("read Tools metadata", &dir.path, e))?;
    let named = fs::statat(&dir.file, name, fs::AtFlags::SYMLINK_NOFOLLOW)
        .map_err(|e| io_error("recheck Tools metadata", &dir.path, e))?;
    if bytes.len() > MAX_FILE || before != stat(&file)? || before != Fingerprint::from_stat(&named)
    {
        return Err(invalid("Tools metadata changed during bounded read"));
    }
    Ok(bytes)
}
fn encode<T: Serialize>(value: &T) -> Result<Vec<u8>> {
    let payload = journal::encode(value)?;
    let bytes = journal::encode(&Envelope {
        sha256: journal::digest(payload.as_bytes()),
        payload,
    })?
    .into_bytes();
    if bytes.len() > MAX_FILE {
        return Err(invalid("Tools metadata exceeds durable record limit"));
    }
    Ok(bytes)
}
fn read<T: DeserializeOwned>(dir: &PrivateDir, name: &str) -> Result<T> {
    let envelope: Envelope = serde_json::from_slice(&read_bytes(dir, name)?)
        .map_err(|_| invalid("malformed Tools metadata; contents withheld"))?;
    if journal::digest(envelope.payload.as_bytes()) != envelope.sha256 {
        return Err(invalid("Tools metadata checksum mismatch"));
    }
    serde_json::from_str(&envelope.payload)
        .map_err(|_| invalid("malformed Tools record; contents withheld"))
}
pub(super) fn valid_scope(s: &Scope) -> bool {
    let token = |s: &str| {
        !s.is_empty()
            && s.len() <= 256
            && s.as_bytes()[0].is_ascii_alphanumeric()
            && s.bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
    };
    let absolute = super::inspection::absolute;
    [s.npm.is_some(), s.brew.is_some()]
        .into_iter()
        .filter(|p| *p)
        .count()
        == 1
        && journal::valid_id(&s.repository_id)
        && [s.tool.as_str(), &s.binary].into_iter().all(token)
        && if s.brew.is_some() {
            super::brew::formula(&s.package)
        } else if s.npm.is_some() {
            valid_npm_package(&s.package)
        } else {
            false
        }
        && (s.brew.is_some()
            || matches!(
                super::VersionPolicy::parse(crate::model::Provider::Npm, Some(&s.version)),
                Ok(super::VersionPolicy::Exact { .. })
            ))
        && s.version.len() <= 256
        && s.profiles.contains("base")
        && s.profiles.iter().all(|p| token(p))
        && s.bound_tools.contains(&s.tool)
        && s.bound_tools.len() <= 256
        && s.bound_tools.iter().all(|id| token(id))
        && [&s.repository, &s.executable, &s.home]
            .into_iter()
            .all(|p| absolute(p))
        && !s.search_dirs.is_empty()
        && s.search_dirs.len() <= 256
        && s.search_dirs.iter().all(|p| absolute(p))
        && !s.tool_search_dirs.is_empty()
        && s.tool_search_dirs.len() <= super::binaries::MAX_DIRECTORIES
        && s.tool_search_dirs.iter().all(|p| absolute(p))
        && (s.npm.is_none() || (s.brew.is_none() && super::npm::valid(s)))
        && (s.brew.is_none() || super::brew::valid(s))
}
fn valid_npm_package(s: &str) -> bool {
    let name = |p: &str| {
        !p.is_empty()
            && p.len() <= 214
            && p.as_bytes()[0].is_ascii_alphanumeric()
            && p.bytes()
                .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(&b))
    };
    if let Some(s) = s.strip_prefix('@') {
        s.split_once('/').is_some_and(|(a, b)| name(a) && name(b))
    } else {
        name(s)
    }
}
fn schema(scope: &Scope) -> u32 {
    if scope.brew.is_some() { 3 } else { 2 }
}
fn intent(dir: &PrivateDir, id: &str) -> Result<Intent> {
    let i: Intent = read(dir, "intent.json")?;
    if i.schema != schema(&i.scope)
        || i.scope.npm.is_some() != i.sources.npm.is_some()
        || i.scope.brew.is_some() != i.sources.brew.is_some()
        || match (&i.scope.brew, &i.sources.brew_probe) {
            (Some(b), Some(p)) => !p.valid(b),
            (None, None) => false,
            _ => true,
        }
        || i.id != id
        || !valid_scope(&i.scope)
        || i.sources.manifest.path != i.scope.repository.join("dort.toml")
        || i.sources.executable.path != i.scope.executable
        || !journal::valid_id(&i.sources.host_sha256)
        || i.sources
            .manifest
            .sha256
            .as_deref()
            .is_none_or(|h| !journal::valid_id(h))
    {
        return Err(invalid("invalid Tools attempt scope"));
    }
    Ok(i)
}
// Binds immutable record bytes, all unexpected entries and directory identity.
// Unsafe/unreadable artifacts are never followed/read; their stable metadata is
// still part of the explicit closure review. This is not authenticity vs same user.
fn head(dir: &PrivateDir, exclude: Option<&str>) -> Result<String> {
    let stat = fs::fstat(&dir.file)
        .map_err(|e| io_error("inspect Tools attempt directory", &dir.path, e))?;
    let identity = (
        stat.st_dev,
        stat.st_ino,
        stat.st_uid,
        stat.st_gid,
        stat.st_mode,
    );
    let mut entries = vec![];
    for name in names(dir, 32)?
        .into_iter()
        .filter(|n| Some(n.as_str()) != exclude)
    {
        let stat = fs::statat(&dir.file, name.as_str(), fs::AtFlags::SYMLINK_NOFOLLOW)
            .map_err(|e| io_error("inspect Tools attempt entry", &dir.path, e))?;
        let fp = Fingerprint::from_stat(&stat);
        let digest = read_bytes(dir, &name)
            .ok()
            .map(|bytes| journal::digest(&bytes));
        let named = fs::statat(&dir.file, name.as_str(), fs::AtFlags::SYMLINK_NOFOLLOW)
            .map_err(|e| io_error("recheck Tools attempt entry", &dir.path, e))?;
        if fp != Fingerprint::from_stat(&named) {
            return Err(invalid("Tools attempt changed during diagnosis"));
        }
        entries.push((name, fp, digest));
    }
    Ok(journal::digest(
        journal::encode(&(identity, entries))?.as_bytes(),
    ))
}
fn closure_index(name: &str) -> Option<usize> {
    let digits = name.strip_prefix("close-")?.strip_suffix(".json")?;
    if digits.len() != 4 || !digits.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    digits.parse().ok()
}
fn scan(dir: &PrivateDir, id: &str) -> Result<Attempt> {
    let before = head(dir, None)?;
    let files = names(dir, 32)?;
    let i = intent(dir, id).ok();
    let mut a = Attempt {
        id: id.into(),
        scope: i.as_ref().map(|i| i.scope.clone()),
        outcome: None,
        observed_versions: Vec::new(),
        problem: None,
        unresolved: true,
        explicitly_closed: false,
        head: before.clone(),
    };
    if i.is_none() {
        a.problem = Some(if files.iter().any(|n| n == "intent.json") {
            Problem::Malformed
        } else {
            Problem::MissingIntent
        });
    }
    if files.iter().any(|n| {
        !["intent.json", "outcome.json"].contains(&n.as_str()) && closure_index(n).is_none()
    }) {
        a.problem = Some(Problem::Malformed);
    }
    if files.iter().any(|n| n == "outcome.json") {
        let completed = read::<Completed>(dir, "outcome.json").ok();
        if let (Some(i), Some(c)) = (&i, completed)
            && c.schema == i.schema
            && c.id == id
            && (i.schema >= 3 || c.outcome.package != super::PackageState::ProviderCurrentPresent)
            && (i.schema < 3 || c.outcome.package != super::PackageState::ExactPresent)
            && (i.schema >= 3 || c.observed_versions.is_empty())
            && c.observed_versions.len() <= 256
            && c.observed_versions
                .iter()
                .all(|v| super::super::valid_observed_version(v) && !v.starts_with("HEAD"))
            && (c.outcome.package != super::PackageState::ProviderCurrentPresent
                || !c.observed_versions.is_empty())
            && c.intent_sha256 == journal::digest(&read_bytes(dir, "intent.json")?)
        {
            a.outcome = Some(c.outcome);
            a.observed_versions = c.observed_versions;
        } else {
            a.problem = Some(Problem::Malformed);
        }
    }
    if a.problem.is_none() && a.outcome.is_some() {
        a.unresolved = false;
    }
    if let Some(name) = files.iter().rev().find(|n| closure_index(n).is_some()) {
        if let Ok(c) = read::<Closure>(dir, name)
            && c.schema == c.scope.schema()
            && c.id == id
            && c.head == head(dir, Some(name))?
            && c.scope.preview().is_ok()
            && c.observed_records <= super::planning::MAX_VERSION_CLAIMS
        {
            a.explicitly_closed = true;
            a.unresolved = false;
        } else {
            a.problem = Some(Problem::Malformed);
            a.unresolved = true;
        }
    }
    if before != head(dir, None)? {
        a.problem = Some(Problem::Changed);
        a.unresolved = true;
        a.explicitly_closed = false;
    }
    Ok(a)
}
/// Read-only; unavailable storage is an error, never a healthy empty inventory.
pub fn diagnose(host: &HostContext) -> Result<Vec<Attempt>> {
    let Some(dir) = PrivateDir::open(&root(host), false)? else {
        return Ok(vec![]);
    };
    let mut out = vec![];
    for id in names(&dir, MAX_ATTEMPTS)? {
        if !journal::valid_id(&id) {
            return Err(invalid(
                "unexpected Tools state entry; explicit manual repair required",
            ));
        }
        out.push(scan(&directory(host, &id)?, &id)?);
    }
    Ok(out)
}
pub(super) fn ensure_clear(host: &HostContext) -> Result<()> {
    let attempts = diagnose(host)?;
    if attempts.iter().any(|a| a.unresolved) {
        return Err(invalid(
            "unresolved Tools attempt; inspect and close it before another installation",
        ));
    }
    if attempts.len() >= MAX_ATTEMPTS {
        return Err(invalid(
            "Tools attempt storage limit reached; no automatic history deletion",
        ));
    }
    Ok(())
}
pub(super) fn begin(
    host: &HostContext,
    lock: &WriterLock,
    scope: Scope,
    sources: Sources,
) -> Result<Handle> {
    lock.check_context(host)?;
    ensure_clear(host)?;
    if !valid_scope(&scope) {
        return Err(invalid("invalid Tools installation scope"));
    }
    let id = adoption::transaction_id();
    let bytes = encode(&Intent {
        schema: schema(&scope),
        id: id.clone(),
        scope,
        sources,
    })?;
    let parent = PrivateDir::open(&root(host), true)?.expect("created Tools state");
    fs::mkdirat(&parent.file, id.as_str(), Mode::from_raw_mode(0o700))
        .map_err(|e| io_error("allocate Tools attempt", &parent.path, e))?;
    parent.sync()?;
    let dir = directory(host, &id)?;
    dir.create("intent.json", &bytes)?;
    Ok(Handle {
        id,
        head: head(&dir, None)?,
    })
}
pub(super) fn verify_pending(host: &HostContext, handle: &Handle) -> Result<()> {
    let dir = directory(host, &handle.id)?;
    let a = scan(&dir, &handle.id)?;
    if a.head != handle.head
        || a.problem.is_some()
        || !a.unresolved
        || a.outcome.is_some()
        || a.explicitly_closed
    {
        return Err(invalid("Tools attempt changed; installer/outcome refused"));
    }
    Ok(())
}
pub(super) fn finish(
    host: &HostContext,
    lock: &WriterLock,
    handle: &Handle,
    outcome: Outcome,
    observed_versions: Vec<String>,
) -> Result<()> {
    lock.check_context(host)?;
    verify_pending(host, handle)?;
    let dir = directory(host, &handle.id)?;
    let completed = Completed {
        schema: intent(&dir, &handle.id)?.schema,
        id: handle.id.clone(),
        intent_sha256: journal::digest(&read_bytes(&dir, "intent.json")?),
        outcome,
        observed_versions,
    };
    dir.create("outcome.json", &encode(&completed)?)
}
pub(super) fn get(host: &HostContext, id: &str) -> Result<Attempt> {
    scan(&directory(host, id)?, id)
}
pub(super) fn close(
    host: &HostContext,
    lock: &WriterLock,
    id: &str,
    expected_head: &str,
    scope: ProbeScope,
    records: usize,
) -> Result<Attempt> {
    lock.check_context(host)?;
    let dir = directory(host, id)?;
    let a = scan(&dir, id)?;
    if a.head != expected_head || !a.unresolved {
        return Err(invalid(
            "Tools closure scope changed; request a fresh review",
        ));
    }
    let c = Closure {
        schema: scope.schema(),
        id: id.into(),
        head: expected_head.into(),
        scope,
        observed_records: records,
    };
    // A later explicit closure binds every earlier artifact, including damaged
    // closure records. Nothing is overwritten or called a successful install.
    let next = names(&dir, 32)?
        .iter()
        .filter_map(|n| closure_index(n))
        .max()
        .map_or(0, |i| i + 1);
    if next >= 32 {
        return Err(invalid("Tools closure record limit reached"));
    }
    dir.create(&format!("close-{next:04}.json"), &encode(&c)?)?;
    scan(&dir, id)
}