ag_session/env.rs
1//! Execution locations, independent of agent conversations and permissions.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::{SessionId, session_branch};
8
9/// Most task worktrees one session may keep at once, including ones still
10/// being created or removed, so parallel writers cannot exhaust disk space.
11pub const TASK_WORKTREE_LIMIT: usize = 2;
12
13/// Stable identity and lifecycle owner of an execution environment.
14///
15/// The existing session worktree is shared by all tasks in that session.
16/// Its identity uses the full persisted session ID, never the shortened branch
17/// name or a filesystem path. Session setup and cleanup remain authoritative;
18/// finishing a task does not release the environment.
19#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
20pub enum EnvId {
21 /// A local worktree owned by one session and shared by its tasks.
22 SessionWorktree(SessionId),
23 /// A separate local worktree owned by one session for one task's writes.
24 TaskWorktree(TaskWorktreeId),
25}
26
27impl EnvId {
28 /// Returns the session that owns, provisions, and cleans up this Env.
29 pub fn session_id(&self) -> &SessionId {
30 match self {
31 Self::SessionWorktree(session_id) => session_id,
32 Self::TaskWorktree(worktree) => &worktree.session_id,
33 }
34 }
35}
36
37/// Identity of a task worktree inside its owning session.
38///
39/// The worktree branches from the session branch at a base commit the host
40/// records when it reserves the Env. Its branch and directory names derive
41/// from this identity, so cleanup needs no stored path.
42#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
43pub struct TaskWorktreeId {
44 /// Session that owns the worktree and every cleanup decision.
45 pub session_id: SessionId,
46 /// Identifier unique within the owning session.
47 pub worktree_id: String,
48}
49
50impl TaskWorktreeId {
51 /// Returns the worktree's own branch, distinct from the session branch.
52 ///
53 /// A `-` suffix rather than a `/` path keeps the name from colliding
54 /// with the session branch ref.
55 pub fn branch(&self) -> String {
56 format!(
57 "{}-{}",
58 session_branch(&self.session_id),
59 Self::short(&self.worktree_id)
60 )
61 }
62
63 /// Returns the worktree directory name, a sibling of the session's
64 /// shortened worktree directory.
65 pub fn directory_name(&self) -> String {
66 format!(
67 "{}-{}",
68 Self::short(&self.session_id),
69 Self::short(&self.worktree_id)
70 )
71 }
72
73 /// Shortens an identifier the way session branches and folders do.
74 fn short(id: &str) -> &str {
75 id.char_indices()
76 .nth(8)
77 .map_or(id, |(boundary, _)| &id[..boundary])
78 }
79}
80
81/// Host-resolved local environment for execution in a shared session worktree.
82///
83/// Hosts validate the workspace before constructing this value. Resolution
84/// neither provisions a checkout nor grants permissions. Paths describe the
85/// current host, not durable identity or a security sandbox. Task worktrees
86/// are not resolved for execution yet, and remote execution is not supported.
87#[derive(Clone, Debug, Eq, PartialEq)]
88pub struct ResolvedEnv {
89 /// Validated session branch in the shared repository.
90 pub branch: String,
91 /// Working directory passed to execution, preserving the host's path.
92 pub folder: PathBuf,
93 /// Stable environment identity and cleanup owner.
94 pub id: EnvId,
95 /// Canonical main checkout protected by isolation checks, if non-bare.
96 pub main_checkout: Option<PathBuf>,
97}