halo-agent 0.1.1

Halo Agent and Workflow execution kernel
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
//! Resource persistence, not hostile-process isolation. A local root must be
//! trusted against concurrent symlink/path replacement by other processes.

use sha2::{Digest, Sha256};
use std::{
    path::{Component, Path, PathBuf},
    sync::Arc,
    sync::atomic::{AtomicU64, Ordering},
};

use tokio::{io::AsyncWriteExt, sync::RwLock};

use crate::{
    Cancellation, Error,
    components::{
        mcp::Server,
        skill::{Definition as SkillDefinition, Skill},
    },
    workspace::{Definition, Mode},
};

use super::dispatch::{Dispatch, ToolInputs};
use super::operation::{cancellable, check};

pub mod docker;
pub mod es;
pub use docker::Docker;
mod tools;

static TEMP: AtomicU64 = AtomicU64::new(0);

struct Temporary(PathBuf);
impl Drop for Temporary {
    fn drop(&mut self) {
        // Also cleans up if the caller drops the write Future. This is a
        // single exact path created by this operation, never recursive.
        let _ = std::fs::remove_file(&self.0);
    }
}

#[derive(Clone)]
pub struct Local {
    root: PathBuf,
    definition: Definition,
    dispatch: Dispatch,
    sandbox: Option<crate::core::sandbox::Local>,
    pub(super) access: Arc<RwLock<()>>,
    pub(super) skill_cache: tools::SkillCache,
}

impl Local {
    pub(super) fn skill_catalog(&self) -> Vec<SkillDefinition> {
        let mut definitions = self.dispatch.skill_catalog();
        let root = self.root.join("skills");
        let Ok(entries) = std::fs::read_dir(root) else {
            return definitions;
        };
        definitions.extend(
            entries
                .filter_map(Result::ok)
                .filter_map(|entry| {
                    let path = entry.path();
                    if !entry.file_type().ok()?.is_dir() {
                        return None;
                    }
                    let markdown = std::fs::read_to_string(path.join("SKILL.md")).ok()?;
                    let skill = crate::core::skill::Skill::from_markdown(&markdown).ok()?;
                    let name = entry.file_name().to_str()?.to_owned();
                    (skill.definition().name == name).then(|| skill.definition().clone())
                })
                .collect::<Vec<_>>(),
        );
        definitions
    }

    /// Opens an existing application-owned directory. Does not invent or
    /// initialize an application workspace layout.
    pub fn new(root: impl AsRef<Path>) -> Result<Self, Error> {
        let root = std::fs::canonicalize(root).map_err(io_error)?;
        if !root.is_dir() {
            return Err(Error::new(
                "INVALID_ARGUMENTS",
                "workspace root must be a directory",
            ));
        }
        let path = root
            .to_str()
            .ok_or_else(|| Error::new("INVALID_ARGUMENTS", "workspace root must be UTF-8"))?
            .to_owned();
        Ok(Self {
            root,
            definition: Definition {
                mode: Mode::Local,
                path: Some(path),
                metadata: Default::default(),
            },
            dispatch: Dispatch::new(),
            sandbox: None,
            access: Arc::new(RwLock::new(())),
            skill_cache: Arc::new(RwLock::new(None)),
        })
    }

    /// Register the application's Tools, MCP servers and Skills in this
    /// workspace. This only stores configuration; it performs no tool calls
    /// or discovery. Standard local file tools
    /// are added when the Workspace is consumed by an Agent or Harness.
    #[allow(private_bounds)]
    pub fn tools<I>(mut self, tools: I) -> Self
    where
        I: ToolInputs,
    {
        self.dispatch = self.dispatch.tools(tools);
        self
    }

    /// Register already connected and discovered MCP servers on this Workspace.
    #[allow(private_bounds)]
    pub fn mcps<S>(mut self, servers: impl IntoIterator<Item = S>) -> Self
    where
        S: Server + 'static,
    {
        self.dispatch = self.dispatch.mcps(servers);
        self
    }

    /// Register in-process Skills on this Workspace. Local directory discovery
    /// remains owned by the Workspace backend and is cached there.
    #[allow(private_bounds)]
    pub fn skills<S>(mut self, skills: impl IntoIterator<Item = S>) -> Result<Self, Error>
    where
        S: Skill + 'static,
    {
        self.dispatch = self.dispatch.skills(skills)?;
        Ok(self)
    }

    /// Explicitly select host command execution, automatically binding its
    /// Tool to this root. This does not start a process or provide isolation.
    /// Docker execution must use its own shared-environment Workspace instead.
    pub fn sandbox(mut self, sandbox: crate::core::sandbox::Local) -> Self {
        self.sandbox = Some(sandbox);
        self
    }

    async fn resolve(&self, path: &str, cancellation: &Cancellation) -> Result<PathBuf, Error> {
        validate_path(path)?;
        let target = self.root.join(path);
        // Reject all existing symlink components, including a final link. A
        // not-yet-existing suffix is allowed for writes but not read success.
        let mut current = self.root.clone();
        for component in Path::new(path).components() {
            check(cancellation)?;
            current.push(component.as_os_str());
            match tokio::fs::symlink_metadata(&current).await {
                Ok(metadata) if metadata.file_type().is_symlink() => {
                    return Err(Error::new(
                        "PERMISSION_DENIED",
                        "workspace symlinks are not allowed",
                    ));
                }
                Ok(_) => {}
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => break,
                Err(error) => return Err(io_error(error)),
            }
        }
        Ok(target)
    }

    /// Return a content fingerprint for the complete local Skill tree.
    ///
    /// Skill files can be replaced without changing their size or the
    /// filesystem timestamp resolution available to the process. Hashing the
    /// bytes makes cache invalidation depend on the actual Workspace content,
    /// matching the Docker-backed loader and preventing an Agent from using a
    /// stale Skill after an in-place update.
    pub(super) async fn skill_fingerprint(
        &self,
        cancellation: &Cancellation,
    ) -> Result<String, Error> {
        let mut paths = match self.list("skills", true, cancellation).await {
            Ok(paths) => paths,
            Err(error) if error.code == "NOT_FOUND" => Vec::new(),
            Err(error) => return Err(error),
        };
        paths.sort();
        let mut hasher = Sha256::new();
        for path in paths {
            let bytes = self.read(&path, cancellation).await?;
            hasher.update(path.as_bytes());
            hasher.update([0]);
            hasher.update((bytes.len() as u64).to_le_bytes());
            hasher.update(bytes);
            crate::core::operation::check(cancellation)?;
        }
        Ok(format!("sha256:{:x}", hasher.finalize()))
    }
}

impl From<Local> for Dispatch {
    fn from(mut workspace: Local) -> Self {
        let dispatch = std::mem::take(&mut workspace.dispatch);
        tools::attach(workspace.into(), dispatch)
    }
}

impl Local {
    /// Internal Tool catalog used to implement the Workspace contract.
    pub(super) fn tool_catalog(&self) -> Vec<crate::tool::ToolDefinition> {
        Dispatch::from(self.clone()).tool_catalog()
    }

    pub(super) fn mcp_catalog(&self) -> Vec<crate::mcp::Definition> {
        Dispatch::from(self.clone()).mcp_catalog()
    }

    /// Execute one configured or built-in Workspace Tool.
    pub async fn execute(
        &self,
        call: crate::tool::Call,
        cancellation: &Cancellation,
    ) -> Result<(crate::Message, crate::tool::Evaluation), Error> {
        Dispatch::from(self.clone())
            .execute(call, cancellation)
            .await
    }
}

impl crate::workspace::Workspace for Local {
    fn definition(&self) -> &Definition {
        &self.definition
    }

    fn tool(&self) -> Vec<crate::tool::ToolDefinition> {
        self.tool_catalog()
    }

    fn mcp(&self) -> Vec<crate::mcp::Definition> {
        self.mcp_catalog()
    }

    fn skill(&self) -> Vec<SkillDefinition> {
        self.skill_catalog()
    }

    fn execute<'a>(
        &'a self,
        call: crate::tool::Call,
        cancellation: &'a Cancellation,
    ) -> crate::workspace::ToolFuture<'a> {
        Box::pin(self.execute(call, cancellation))
    }
}

impl Local {
    /// List sorted workspace-relative paths. `.` is the root; shallow listing
    /// includes directories, recursive listing returns files without symlinks.
    pub async fn list(
        &self,
        path: &str,
        recursive: bool,
        cancellation: &Cancellation,
    ) -> Result<Vec<String>, Error> {
        check(cancellation)?;
        let base = if path == "." {
            self.root.clone()
        } else {
            self.resolve(path, cancellation).await?
        };
        cancellable(
            async {
                let mut pending = vec![base];
                let mut paths = Vec::new();
                while let Some(directory) = pending.pop() {
                    check(cancellation)?;
                    let mut entries = tokio::fs::read_dir(directory).await.map_err(io_error)?;
                    while let Some(entry) = entries.next_entry().await.map_err(io_error)? {
                        check(cancellation)?;
                        let kind = entry.file_type().await.map_err(io_error)?;
                        if kind.is_symlink() || (!kind.is_file() && !kind.is_dir()) {
                            continue;
                        }
                        let entry_path = entry.path();
                        if kind.is_dir() && recursive {
                            pending.push(entry_path);
                        } else {
                            let relative = entry_path.strip_prefix(&self.root).map_err(|_| {
                                Error::new("PERMISSION_DENIED", "workspace path escaped its root")
                            })?;
                            let relative = relative.to_str().ok_or_else(|| {
                                Error::new("INVALID_ARGUMENTS", "workspace path must be UTF-8")
                            })?;
                            paths.push(relative.replace(std::path::MAIN_SEPARATOR, "/"));
                        }
                    }
                }
                paths.sort();
                Ok(paths)
            },
            cancellation,
        )
        .await
    }

    pub async fn read(&self, path: &str, cancellation: &Cancellation) -> Result<Vec<u8>, Error> {
        check(cancellation)?;
        let path = self.resolve(path, cancellation).await?;
        cancellable(
            async { tokio::fs::read(path).await.map_err(io_error) },
            cancellation,
        )
        .await
    }

    pub async fn write(
        &self,
        path: &str,
        data: Vec<u8>,
        cancellation: &Cancellation,
    ) -> Result<(), Error> {
        check(cancellation)?;
        let target = self.resolve(path, cancellation).await?;
        let parent = target
            .parent()
            .expect("relative workspace resource has parent");
        tokio::fs::create_dir_all(parent).await.map_err(io_error)?;
        check(cancellation)?;
        let temp = parent.join(format!(
            ".halo-write-{}-{}",
            std::process::id(),
            TEMP.fetch_add(1, Ordering::Relaxed)
        ));
        // Open and arm cleanup in the same poll. Dropping a pending async
        // open could otherwise leave a file created later by its worker.
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temp)
            .map_err(io_error)?;
        let _cleanup = Temporary(temp.clone());
        let mut file = tokio::fs::File::from_std(file);
        let written = cancellable(
            async {
                file.write_all(&data).await.map_err(io_error)?;
                file.flush().await.map_err(io_error)
            },
            cancellation,
        )
        .await;
        drop(file);
        if let Err(error) = written {
            let _ = tokio::fs::remove_file(&temp).await;
            return Err(error);
        }
        if let Err(error) = check(cancellation) {
            let _ = tokio::fs::remove_file(&temp).await;
            return Err(error);
        }
        // Rename is the commit point. Once committed, report success; do not
        // turn a persisted successful write into CANCELLED after the fact.
        let committed = tokio::fs::rename(&temp, target).await.map_err(io_error);
        if committed.is_err() {
            let _ = tokio::fs::remove_file(&temp).await;
        }
        committed
    }

    pub async fn delete(&self, path: &str, cancellation: &Cancellation) -> Result<(), Error> {
        check(cancellation)?;
        let path = self.resolve(path, cancellation).await?;
        check(cancellation)?;
        // No recursive directory deletion.
        tokio::fs::remove_file(path).await.map_err(io_error)
    }
}

fn validate_path(path: &str) -> Result<(), Error> {
    if path.is_empty()
        || path.contains('\0')
        || path.contains('\\')
        || path
            .split('/')
            .any(|part| part.is_empty() || part == "." || part == "..")
        || Path::new(path)
            .components()
            .any(|part| !matches!(part, Component::Normal(_)))
    {
        return Err(Error::new(
            "INVALID_ARGUMENTS",
            "workspace resource requires a normalized relative path",
        ));
    }
    Ok(())
}

fn io_error(error: std::io::Error) -> Error {
    let code = match error.kind() {
        std::io::ErrorKind::NotFound => "NOT_FOUND",
        std::io::ErrorKind::PermissionDenied => "PERMISSION_DENIED",
        std::io::ErrorKind::AlreadyExists => "CONFLICT",
        std::io::ErrorKind::NotADirectory => "INVALID_ARGUMENTS",
        _ => "IO_ERROR",
    };
    // OS messages may reveal absolute paths; do not expose raw diagnostics.
    Error::new(code, "workspace resource operation failed")
}