agents-kit 0.0.2

Rust agent control Kit for Codex, OpenCode, and provider-backed Custom runtimes
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
use std::path::{Path, PathBuf};

use storage_kit::StateStoreBackend;
use storage_kit::state::record::{StateKey, StateKeyPrefix, StateListRequest, StateRecordSnapshot};

use crate::{
    AgentId, ConversationId, ConversationInfo, KitError, KnowledgePolicy, ModelSelector,
    NewConversation, NewWorkspace, PersonalitySelector, ReasoningEffort, UpdateWorkspaceRequest,
    WorkspaceContext, WorkspaceDefaults, WorkspaceExecutionPolicy, WorkspaceId, WorkspaceInfo,
    WorkspaceNetworkAccess, WorkspaceRoot, WorkspaceRootAccess, WorkspaceSandboxMode,
};

const WORKSPACE_NAMESPACE: &str = "agents_kit_workspaces_v1";
const CONVERSATION_NAMESPACE: &str = "agents_kit_conversations_v1";
const RECORD_VERSION: u32 = 2;
const MAX_RECORD_PAGES: usize = 1_024;

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
enum RecordLifecycle {
    Open,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
struct StoredDefaults {
    model: Option<ModelSelector>,
    personality: Option<PersonalitySelector>,
    reasoning_effort: Option<ReasoningEffort>,
    knowledge_policy: Option<KnowledgePolicy>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
enum StoredWorkspaceRootAccess {
    ReadOnly,
    ReadWrite,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
struct StoredWorkspaceRoot {
    path: PathBuf,
    access: StoredWorkspaceRootAccess,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
enum StoredWorkspaceSandboxMode {
    ReadOnly,
    WorkspaceWrite,
    FullAccess,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
enum StoredWorkspaceNetworkAccess {
    Disabled,
    Enabled,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
struct StoredWorkspaceExecutionPolicy {
    sandbox_mode: StoredWorkspaceSandboxMode,
    network_access: StoredWorkspaceNetworkAccess,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct WorkspaceRecord {
    version: u32,
    id: WorkspaceId,
    agent_id: AgentId,
    name: String,
    cwd: PathBuf,
    roots: Vec<StoredWorkspaceRoot>,
    execution_policy: StoredWorkspaceExecutionPolicy,
    defaults: StoredDefaults,
    revision: u64,
    lifecycle: RecordLifecycle,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ConversationRecord {
    version: u32,
    id: ConversationId,
    workspace_id: WorkspaceId,
    workspace_revision: u64,
    title: Option<String>,
    defaults: Option<StoredDefaults>,
    lifecycle: RecordLifecycle,
}

impl StoredDefaults {
    fn from_public(defaults: &WorkspaceDefaults) -> Self {
        Self {
            model: defaults.model().cloned(),
            personality: defaults.personality().cloned(),
            reasoning_effort: defaults.reasoning_effort().cloned(),
            knowledge_policy: defaults.knowledge_policy().cloned(),
        }
    }

    fn to_public(&self) -> Result<WorkspaceDefaults, KitError> {
        let defaults = WorkspaceDefaults::new(
            self.model.clone(),
            self.reasoning_effort.clone(),
            self.knowledge_policy.clone(),
        )
        .map_err(|_| KitError::invalid_state("stored workspace defaults are malformed"))?;
        Ok(match &self.personality {
            Some(personality) => defaults.with_personality(personality.clone()),
            None => defaults,
        })
    }
}

impl StoredWorkspaceRoot {
    fn from_public(root: &WorkspaceRoot) -> Self {
        Self {
            path: root.path().to_path_buf(),
            access: match root.access() {
                WorkspaceRootAccess::ReadOnly => StoredWorkspaceRootAccess::ReadOnly,
                WorkspaceRootAccess::ReadWrite => StoredWorkspaceRootAccess::ReadWrite,
            },
        }
    }

    fn to_public(&self) -> Result<WorkspaceRoot, KitError> {
        let access = match self.access {
            StoredWorkspaceRootAccess::ReadOnly => WorkspaceRootAccess::ReadOnly,
            StoredWorkspaceRootAccess::ReadWrite => WorkspaceRootAccess::ReadWrite,
        };
        WorkspaceRoot::new(self.path.clone(), access)
            .map_err(|_| KitError::invalid_state("stored workspace root is malformed"))
    }
}

impl StoredWorkspaceExecutionPolicy {
    fn from_public(policy: &WorkspaceExecutionPolicy) -> Self {
        Self {
            sandbox_mode: match policy.sandbox_mode() {
                WorkspaceSandboxMode::ReadOnly => StoredWorkspaceSandboxMode::ReadOnly,
                WorkspaceSandboxMode::WorkspaceWrite => StoredWorkspaceSandboxMode::WorkspaceWrite,
                WorkspaceSandboxMode::FullAccess => StoredWorkspaceSandboxMode::FullAccess,
            },
            network_access: match policy.network_access() {
                WorkspaceNetworkAccess::Disabled => StoredWorkspaceNetworkAccess::Disabled,
                WorkspaceNetworkAccess::Enabled => StoredWorkspaceNetworkAccess::Enabled,
            },
        }
    }

    fn to_public(self) -> WorkspaceExecutionPolicy {
        let sandbox_mode = match self.sandbox_mode {
            StoredWorkspaceSandboxMode::ReadOnly => WorkspaceSandboxMode::ReadOnly,
            StoredWorkspaceSandboxMode::WorkspaceWrite => WorkspaceSandboxMode::WorkspaceWrite,
            StoredWorkspaceSandboxMode::FullAccess => WorkspaceSandboxMode::FullAccess,
        };
        let network_access = match self.network_access {
            StoredWorkspaceNetworkAccess::Disabled => WorkspaceNetworkAccess::Disabled,
            StoredWorkspaceNetworkAccess::Enabled => WorkspaceNetworkAccess::Enabled,
        };
        WorkspaceExecutionPolicy::new(sandbox_mode, network_access)
    }
}

impl WorkspaceRecord {
    pub(crate) fn new(agent_id: AgentId, id: WorkspaceId, request: &NewWorkspace) -> Self {
        Self {
            version: RECORD_VERSION,
            id,
            agent_id,
            name: request.name().to_owned(),
            cwd: request.context().cwd().to_path_buf(),
            roots: request
                .context()
                .roots()
                .iter()
                .map(StoredWorkspaceRoot::from_public)
                .collect(),
            execution_policy: StoredWorkspaceExecutionPolicy::from_public(
                request.context().execution_policy(),
            ),
            defaults: StoredDefaults::from_public(request.defaults()),
            revision: 1,
            lifecycle: RecordLifecycle::Open,
        }
    }

    pub(crate) fn id(&self) -> &WorkspaceId {
        &self.id
    }

    pub(crate) fn agent_id(&self) -> &AgentId {
        &self.agent_id
    }

    pub(crate) fn revision(&self) -> u64 {
        self.revision
    }

    pub(crate) fn cwd(&self) -> &Path {
        &self.cwd
    }

    pub(crate) fn info(&self) -> Result<WorkspaceInfo, KitError> {
        if self.version != RECORD_VERSION || self.lifecycle != RecordLifecycle::Open {
            return Err(KitError::invalid_state(
                "stored workspace lifecycle is malformed",
            ));
        }
        let roots = self
            .roots
            .iter()
            .map(StoredWorkspaceRoot::to_public)
            .collect::<Result<Vec<_>, _>>()?;
        let context =
            WorkspaceContext::new(self.cwd.clone(), roots, self.execution_policy.to_public())
                .map_err(|_| KitError::invalid_state("stored workspace context is malformed"))?;
        WorkspaceInfo::new(
            self.id.clone(),
            self.name.clone(),
            context,
            self.defaults.to_public()?,
            self.revision,
        )
        .map_err(|_| KitError::invalid_state("stored workspace state is malformed"))
    }

    pub(crate) fn replace(&self, request: &UpdateWorkspaceRequest) -> Result<Self, KitError> {
        if request.id() != &self.id || request.expected_revision() != self.revision {
            return Err(KitError::conflict("workspace revision is stale")
                .with_detail(crate::error::ErrorDetail::StaleRevision)
                .in_phase(crate::error::ErrorPhase::Admission));
        }
        let revision = self
            .revision
            .checked_add(1)
            .ok_or_else(|| KitError::invalid_state("workspace revision is exhausted"))?;
        Ok(Self {
            version: RECORD_VERSION,
            id: self.id.clone(),
            agent_id: self.agent_id.clone(),
            name: request.name().to_owned(),
            cwd: request.context().cwd().to_path_buf(),
            roots: request
                .context()
                .roots()
                .iter()
                .map(StoredWorkspaceRoot::from_public)
                .collect(),
            execution_policy: StoredWorkspaceExecutionPolicy::from_public(
                request.context().execution_policy(),
            ),
            defaults: StoredDefaults::from_public(request.defaults()),
            revision,
            lifecycle: RecordLifecycle::Open,
        })
    }

    pub(crate) fn key(agent_id: &AgentId, id: &WorkspaceId) -> Result<StateKey, KitError> {
        let key = format!("{}:{}", agent_id.as_str(), id.as_str());
        StateKey::new(WORKSPACE_NAMESPACE, key)
            .map_err(|_| KitError::invalid_state("workspace state key is invalid"))
    }

    pub(crate) fn list_request(agent_id: &AgentId) -> Result<StateListRequest, KitError> {
        let prefix = StateKeyPrefix::new(format!("{}:", agent_id.as_str()))
            .map_err(|_| KitError::invalid_state("workspace state prefix is invalid"))?;
        StateListRequest::new(WORKSPACE_NAMESPACE)
            .map(|request| request.with_key_prefix(prefix))
            .map_err(|_| KitError::invalid_state("workspace inventory request is invalid"))
    }

    pub(crate) async fn list(
        store: &impl StateStoreBackend,
        agent_id: &AgentId,
    ) -> Result<Vec<Self>, KitError> {
        let mut records = Vec::new();
        let mut cursor = None;
        for _ in 0..MAX_RECORD_PAGES {
            let mut request = Self::list_request(agent_id)?
                .with_limit(StateListRequest::MAX_LIMIT)
                .map_err(|_| KitError::invalid_state("workspace inventory limit is invalid"))?;
            if let Some(next) = cursor.take() {
                request = request.start_after(next).map_err(|_| {
                    KitError::invalid_state("workspace inventory cursor is invalid")
                })?;
            }
            let page = store.list(request).await.map_err(|error| {
                KitError::from_state_store(error, "workspace inventory could not be loaded")
                    .in_phase(crate::error::ErrorPhase::StateRead)
            })?;
            for snapshot in page.records {
                records.push(Self::decode(snapshot, agent_id, None)?);
            }
            let Some(next) = page.next_start_after else {
                records.sort_by(|left, right| left.id.cmp(&right.id));
                return Ok(records);
            };
            cursor = Some(next);
        }
        Err(KitError::invalid_state(
            "workspace inventory exceeds its private page bound",
        ))
    }

    pub(crate) fn encode(&self) -> Result<serde_json::Value, KitError> {
        serde_json::to_value(self)
            .map_err(|_| KitError::invalid_state("workspace state could not be encoded"))
    }

    pub(crate) fn decode(
        snapshot: StateRecordSnapshot,
        agent_id: &AgentId,
        expected: Option<&WorkspaceId>,
    ) -> Result<Self, KitError> {
        let record: Self = serde::Deserialize::deserialize(snapshot.record.value.as_ref())
            .map_err(|_| KitError::invalid_state("stored workspace state is malformed"))?;
        if &record.agent_id != agent_id || expected.is_some_and(|id| id != &record.id) {
            return Err(KitError::not_found("workspace does not exist"));
        }
        if Self::key(agent_id, &record.id)?.key() != snapshot.record.key.key() {
            return Err(KitError::invalid_state(
                "stored workspace ownership contradicts its key",
            ));
        }
        record.info()?;
        Ok(record)
    }
}

impl ConversationRecord {
    pub(crate) fn new(
        id: ConversationId,
        workspace_id: WorkspaceId,
        workspace_revision: u64,
        request: &NewConversation,
    ) -> Self {
        Self {
            version: RECORD_VERSION,
            id,
            workspace_id,
            workspace_revision,
            title: request.title().map(str::to_owned),
            defaults: request.defaults().map(StoredDefaults::from_public),
            lifecycle: RecordLifecycle::Open,
        }
    }

    pub(crate) fn id(&self) -> &ConversationId {
        &self.id
    }

    pub(crate) fn workspace_id(&self) -> &WorkspaceId {
        &self.workspace_id
    }

    pub(crate) fn workspace_revision(&self) -> u64 {
        self.workspace_revision
    }

    pub(crate) fn defaults(&self) -> Result<Option<WorkspaceDefaults>, KitError> {
        self.defaults
            .as_ref()
            .map(StoredDefaults::to_public)
            .transpose()
    }

    pub(crate) fn info(&self) -> Result<ConversationInfo, KitError> {
        if self.version != RECORD_VERSION
            || self.lifecycle != RecordLifecycle::Open
            || self.workspace_revision == 0
        {
            return Err(KitError::invalid_state(
                "stored conversation state is malformed",
            ));
        }
        let _ = self.defaults()?;
        ConversationInfo::new(
            self.id.clone(),
            self.workspace_id.clone(),
            self.title.clone(),
        )
        .map_err(|_| KitError::invalid_state("stored conversation state is malformed"))
    }

    pub(crate) fn with_workspace_revision(&self, revision: u64) -> Result<Self, KitError> {
        if revision == 0 {
            return Err(KitError::invalid_state(
                "conversation workspace revision must be nonzero",
            ));
        }
        let mut record = self.clone();
        record.workspace_revision = revision;
        Ok(record)
    }

    pub(crate) fn key(
        workspace_id: &WorkspaceId,
        id: &ConversationId,
    ) -> Result<StateKey, KitError> {
        let key = format!("{}:{}", workspace_id.as_str(), id.as_str());
        StateKey::new(CONVERSATION_NAMESPACE, key)
            .map_err(|_| KitError::invalid_state("conversation state key is invalid"))
    }

    pub(crate) fn list_request(workspace_id: &WorkspaceId) -> Result<StateListRequest, KitError> {
        let prefix = StateKeyPrefix::new(format!("{}:", workspace_id.as_str()))
            .map_err(|_| KitError::invalid_state("conversation state prefix is invalid"))?;
        StateListRequest::new(CONVERSATION_NAMESPACE)
            .map(|request| request.with_key_prefix(prefix))
            .map_err(|_| KitError::invalid_state("conversation inventory request is invalid"))
    }

    pub(crate) async fn list(
        store: &impl StateStoreBackend,
        workspace_id: &WorkspaceId,
    ) -> Result<Vec<Self>, KitError> {
        let mut records = Vec::new();
        let mut cursor = None;
        for _ in 0..MAX_RECORD_PAGES {
            let mut request = Self::list_request(workspace_id)?
                .with_limit(StateListRequest::MAX_LIMIT)
                .map_err(|_| KitError::invalid_state("conversation inventory limit is invalid"))?;
            if let Some(next) = cursor.take() {
                request = request.start_after(next).map_err(|_| {
                    KitError::invalid_state("conversation inventory cursor is invalid")
                })?;
            }
            let page = store.list(request).await.map_err(|error| {
                KitError::from_state_store(error, "conversation inventory could not be loaded")
                    .in_phase(crate::error::ErrorPhase::StateRead)
            })?;
            for snapshot in page.records {
                records.push(Self::decode(snapshot, workspace_id, None)?);
            }
            let Some(next) = page.next_start_after else {
                records.sort_by(|left, right| left.id.cmp(&right.id));
                return Ok(records);
            };
            cursor = Some(next);
        }
        Err(KitError::invalid_state(
            "conversation inventory exceeds its private page bound",
        ))
    }

    pub(crate) fn encode(&self) -> Result<serde_json::Value, KitError> {
        serde_json::to_value(self)
            .map_err(|_| KitError::invalid_state("conversation state could not be encoded"))
    }

    pub(crate) fn decode(
        snapshot: StateRecordSnapshot,
        workspace_id: &WorkspaceId,
        expected: Option<&ConversationId>,
    ) -> Result<Self, KitError> {
        let record: Self = serde::Deserialize::deserialize(snapshot.record.value.as_ref())
            .map_err(|_| KitError::invalid_state("stored conversation state is malformed"))?;
        if &record.workspace_id != workspace_id || expected.is_some_and(|id| id != &record.id) {
            return Err(KitError::not_found("conversation does not exist"));
        }
        if Self::key(workspace_id, &record.id)?.key() != snapshot.record.key.key() {
            return Err(KitError::invalid_state(
                "stored conversation ownership contradicts its key",
            ));
        }
        record.info()?;
        Ok(record)
    }
}