artifactize-core 0.26.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
//! Explicit Workspace Trust gate for ACP agents (`$STATE/chat/trust.json`).
//!
//! Because ACP agents run with `cwd = session.worktree` and native coding tools, they can read
//! repository-committed hooks, settings and MCP configs on startup before any permission prompt.
//! Consent binds `(agent_id, TrustScope, LauncherFingerprint)` and hashes the resolved
//! executable's bytes directly at the Trust gate before every spawn or warm reuse.

use std::{
    io::Read,
    path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::{
    auth::storage::{Storage, Tokens},
    platform, repository,
    types::Timestamp,
};

use super::AgentId;

const TRUST_FILE: &str = "trust.json";
const TRUST_LOCK: &str = "trust";
/// Streaming buffer for direct SHA-256 hashing of resolved ACP executables.
const HASH_CHUNK_BYTES: usize = 64 * 1024;
/// Current schema version of `$STATE/chat/trust.json`.
const TRUST_SCHEMA_VERSION: u32 = 1;

/// What repository or directory the user explicitly trusted for an ACP agent.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum TrustScope {
    /// Canonical git `common_dir`: covers all branches and worktrees of this repository.
    GitRepo {
        #[serde(with = "crate::platform::path_serde")]
        common_dir: PathBuf,
    },
    /// Canonical absolute path for non-git folders (never collapses into `Identity::default()`).
    Directory {
        #[serde(with = "crate::platform::path_serde")]
        canonical_path: PathBuf,
    },
}

impl TrustScope {
    /// Derive the exact trust scope for a worktree and its git identity.
    pub fn for_worktree(worktree: &Path, identity: &repository::Identity) -> Result<Self, String> {
        let canonical =
            crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
        if let Some(common_dir) = &identity.common_dir {
            let common = crate::workspace::canonical_target(common_dir)
                .map_err(|error| error.to_string())?;
            Ok(Self::GitRepo { common_dir: common })
        } else {
            Ok(Self::Directory {
                canonical_path: canonical,
            })
        }
    }

    pub fn display_label(&self) -> String {
        match self {
            Self::GitRepo { common_dir } => {
                format!("Git repository ({})", platform::path_text(common_dir))
            }
            Self::Directory { canonical_path } => platform::path_text(canonical_path),
        }
    }

    pub fn disclosure(&self) -> &'static str {
        match self {
            Self::GitRepo { .. } => {
                "Trusting a Git repository covers all branches and worktrees of that repository; checking out an untrusted branch will not re-prompt."
            }
            Self::Directory { .. } => {
                "Trusting this directory allows the ACP agent to run natively inside it and read any local configuration files before prompting."
            }
        }
    }
}

/// Complete executable + argument identity bound to a workspace trust entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LauncherFingerprint {
    #[serde(with = "crate::platform::path_serde")]
    pub resolved_executable: PathBuf,
    pub executable_sha256: String,
    pub args_hash: String,
}

impl LauncherFingerprint {
    /// Compute the launcher fingerprint by hashing the executable bytes directly (no mtime/size cache).
    pub fn compute(resolved_executable: &Path, args: &[String]) -> Result<Self, String> {
        let canonical = platform::canonicalize(resolved_executable).map_err(|error| {
            format!(
                "Cannot resolve ACP executable {}: {error}.",
                platform::path_text(resolved_executable)
            )
        })?;
        let executable_sha256 = hash_executable(&canonical)?;
        let args_hash = hash_args(args);
        Ok(Self {
            resolved_executable: canonical,
            executable_sha256,
            args_hash,
        })
    }

    /// Combine with `auth_generation` into the `AcpBinding::runtime_fingerprint` key.
    pub fn runtime_fingerprint(&self, auth_generation: u64) -> String {
        let mut hasher = Sha256::new();
        hasher.update(platform::path_text(&self.resolved_executable).as_bytes());
        hasher.update([0]);
        hasher.update(self.executable_sha256.as_bytes());
        hasher.update([0]);
        hasher.update(self.args_hash.as_bytes());
        hasher.update([0]);
        hasher.update(auth_generation.to_le_bytes());
        hex_digest(hasher.finalize().as_slice())
    }
}

/// One persisted consent record in `$STATE/chat/trust.json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustRecord {
    pub agent_id: AgentId,
    pub scope: TrustScope,
    pub fingerprint: LauncherFingerprint,
    pub granted_at: Timestamp,
}

/// Details returned when a turn or warm reuse requires explicit workspace trust consent.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustRequirement {
    pub agent_id: AgentId,
    pub agent_name: String,
    pub scope: TrustScope,
    pub fingerprint: LauncherFingerprint,
    pub disclosure: String,
    pub reason: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SavedTrust {
    version: u32,
    #[serde(default)]
    entries: Vec<TrustRecord>,
}

impl Default for SavedTrust {
    fn default() -> Self {
        Self {
            version: TRUST_SCHEMA_VERSION,
            entries: Vec::new(),
        }
    }
}

/// Resolve an ACP program from an empty private tempdir (never from `worktree`) and, for
/// built-in presets, reject executables located inside `worktree`.
pub fn resolve_executable(
    program: &str,
    worktree: Option<&Path>,
    reject_inside_worktree: bool,
) -> Result<PathBuf, String> {
    let probe_dir =
        platform::private_tempdir_in("artifactize-resolve-", &platform::temporary_directory())
            .map_err(|error| format!("Cannot create executable resolution tempdir: {error}."))?;
    let program_path = platform::program_path(program)
        .map_err(|error| format!("Invalid ACP program path {program:?}: {error}."))?;
    let resolved = platform::program::resolve(&program_path, probe_dir.path())
        .map_err(|error| format!("Cannot find ACP executable {program:?}: {error}."))?;
    let canonical = platform::canonicalize(&resolved).map_err(|error| {
        format!(
            "Cannot canonicalize ACP executable {}: {error}.",
            platform::path_text(&resolved)
        )
    })?;
    if reject_inside_worktree && let Some(worktree) = worktree {
        let canonical_worktree =
            crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
        if platform::is_within(&canonical, &canonical_worktree) {
            return Err(format!(
                "Refusing to launch built-in ACP preset executable {} located inside the workspace {}.",
                platform::path_text(&canonical),
                platform::path_text(&canonical_worktree),
            ));
        }
    }
    Ok(canonical)
}

/// Hash the executable's bytes directly via SHA-256 (never `(mtime, size)`).
pub fn hash_executable(path: &Path) -> Result<String, String> {
    let mut file = platform::open_regular_input(path)
        .map_err(|error| {
            format!(
                "Cannot open ACP executable {}: {error}.",
                platform::path_text(path)
            )
        })?
        .ok_or_else(|| {
            format!(
                "ACP executable {} is not a regular file.",
                platform::path_text(path)
            )
        })?;
    let mut hasher = Sha256::new();
    let mut buffer = [0u8; HASH_CHUNK_BYTES];
    loop {
        let count = file.read(&mut buffer).map_err(|error| {
            format!(
                "Cannot read ACP executable {}: {error}.",
                platform::path_text(path)
            )
        })?;
        if count == 0 {
            break;
        }
        hasher.update(&buffer[..count]);
    }
    Ok(hex_digest(hasher.finalize().as_slice()))
}

/// Pure deterministic digest over an argument vector.
pub fn hash_args(args: &[String]) -> String {
    let mut hasher = Sha256::new();
    for arg in args {
        hasher.update((arg.len() as u64).to_le_bytes());
        hasher.update(arg.as_bytes());
    }
    hex_digest(hasher.finalize().as_slice())
}

fn hex_digest(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = String::with_capacity(bytes.len() * 2);
    for &byte in bytes {
        out.push(HEX[(byte >> 4) as usize] as char);
        out.push(HEX[(byte & 0x0f) as usize] as char);
    }
    out
}

/// Read all saved trust records from `$STATE/chat/trust.json`.
pub fn list_trust(state: Option<&Path>, repo: Option<&Path>) -> Result<Vec<TrustRecord>, String> {
    let storage = Storage::inspect(state, repo, Tokens::ChatTrust)?;
    Ok(storage
        .read::<SavedTrust>(TRUST_FILE)?
        .unwrap_or_default()
        .entries)
}

/// Check whether `(agent_id, scope, fingerprint)` already has explicit consent.
pub fn check_trust(
    state: Option<&Path>,
    repo: Option<&Path>,
    agent_id: &AgentId,
    agent_name: &str,
    scope: &TrustScope,
    fingerprint: &LauncherFingerprint,
) -> Result<Result<(), TrustRequirement>, String> {
    let entries = list_trust(state, repo)?;
    if entries.iter().any(|entry| {
        &entry.agent_id == agent_id && &entry.scope == scope && &entry.fingerprint == fingerprint
    }) {
        return Ok(Ok(()));
    }
    let changed_binary = entries
        .iter()
        .any(|entry| &entry.agent_id == agent_id && &entry.scope == scope);
    let reason = if changed_binary {
        format!(
            "The executable or arguments for {agent_name} changed since you last trusted this workspace."
        )
    } else {
        format!(
            "{agent_name} will run natively in this workspace and can read repository settings, hooks and MCP configs before any permission prompt."
        )
    };
    Ok(Err(TrustRequirement {
        agent_id: agent_id.clone(),
        agent_name: agent_name.to_owned(),
        scope: scope.clone(),
        fingerprint: fingerprint.clone(),
        disclosure: scope.disclosure().to_owned(),
        reason,
    }))
}

/// Grant (or update) consent for `(agent_id, scope, fingerprint)` under cross-window file lock.
pub async fn grant_trust(
    state: Option<&Path>,
    repo: Option<&Path>,
    agent_id: AgentId,
    scope: TrustScope,
    fingerprint: LauncherFingerprint,
) -> Result<TrustRecord, String> {
    let storage = Storage::new(state, repo, Tokens::ChatTrust)?;
    let _lock = storage.lock(TRUST_LOCK).await?;
    let mut saved: SavedTrust = storage.read(TRUST_FILE)?.unwrap_or_default();
    saved
        .entries
        .retain(|entry| !(entry.agent_id == agent_id && entry.scope == scope));
    let record = TrustRecord {
        agent_id,
        scope,
        fingerprint,
        granted_at: crate::broker::now(),
    };
    saved.entries.push(record.clone());
    storage.save(TRUST_FILE, &saved)?;
    Ok(record)
}

/// Revoke workspace trust for `(agent_id, scope)`. Returns whether a matching entry existed.
pub async fn revoke_trust(
    state: Option<&Path>,
    repo: Option<&Path>,
    agent_id: &AgentId,
    scope: &TrustScope,
) -> Result<bool, String> {
    let storage = Storage::new(state, repo, Tokens::ChatTrust)?;
    let _lock = storage.lock(TRUST_LOCK).await?;
    let mut saved: SavedTrust = storage.read(TRUST_FILE)?.unwrap_or_default();
    let before = saved.entries.len();
    saved
        .entries
        .retain(|entry| !(&entry.agent_id == agent_id && &entry.scope == scope));
    let removed = saved.entries.len() != before;
    if removed {
        storage.save(TRUST_FILE, &saved)?;
    }
    Ok(removed)
}

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

    #[tokio::test]
    async fn trust_requires_reconsent_when_executable_bytes_change() {
        let root = crate::test_os::tempdir();
        let state = root.path().join("state");
        let worktree = root.path().join("work");
        let bin_dir = root.path().join("bin");
        std::fs::create_dir_all(&worktree).unwrap();
        std::fs::create_dir_all(&bin_dir).unwrap();
        let exe = bin_dir.join("agent-bin");
        std::fs::write(&exe, b"version-1").unwrap();

        let agent: AgentId = "antigravity".parse().unwrap();
        let scope = TrustScope::for_worktree(&worktree, &repository::Identity::default()).unwrap();
        assert!(matches!(scope, TrustScope::Directory { .. }));

        let fp1 = LauncherFingerprint::compute(&exe, &["acp".to_owned()]).unwrap();
        assert!(
            check_trust(
                Some(&state),
                Some(&worktree),
                &agent,
                "Antigravity",
                &scope,
                &fp1
            )
            .unwrap()
            .is_err()
        );
        grant_trust(
            Some(&state),
            Some(&worktree),
            agent.clone(),
            scope.clone(),
            fp1.clone(),
        )
        .await
        .unwrap();
        assert!(
            check_trust(
                Some(&state),
                Some(&worktree),
                &agent,
                "Antigravity",
                &scope,
                &fp1
            )
            .unwrap()
            .is_ok()
        );

        // Same-path binary replacement forces re-consent even if size matches.
        std::fs::write(&exe, b"version-2").unwrap();
        let fp2 = LauncherFingerprint::compute(&exe, &["acp".to_owned()]).unwrap();
        assert_ne!(fp1.executable_sha256, fp2.executable_sha256);
        let req = check_trust(
            Some(&state),
            Some(&worktree),
            &agent,
            "Antigravity",
            &scope,
            &fp2,
        )
        .unwrap()
        .unwrap_err();
        assert!(req.reason.contains("changed"));

        assert!(
            revoke_trust(Some(&state), Some(&worktree), &agent, &scope)
                .await
                .unwrap()
        );
        assert!(
            list_trust(Some(&state), Some(&worktree))
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn builtin_preset_rejects_executable_inside_worktree() {
        let root = crate::test_os::tempdir();
        let worktree = root.path().join("repo");
        std::fs::create_dir_all(&worktree).unwrap();
        let malicious = crate::test_os::executable_named(&worktree, "agy");
        std::fs::write(&malicious, b"fake-acp-binary\n").unwrap();
        crate::test_os::make_executable(&malicious);
        let error = resolve_executable(&platform::path_text(&malicious), Some(&worktree), true)
            .unwrap_err();
        assert!(error.contains("inside the workspace"), "{error}");
    }
}