shepherd-core 6.6.0

The harness-agnostic shepherd engine: domain types, configuration schema, and run state. Knows nothing about any CLI, harness, or process.
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
528
529
530
531
//! Versioned dispatch record creation, stop, and cross-harness resume.

#[cfg(feature = "alloc")]
use alloc::{
    format,
    string::{String, ToString},
    vec::Vec,
};

use crate::Harness;

use super::{
    AgentId, AgentType, CapabilityContract, CapabilityProbe, CapabilityReadiness, CapabilityReport,
    DispatchError, DispatchResult, LaneId, ProjectId, Role, RunId, SessionId, StartupAttachment,
    validate_parent_child, validate_write_scope_pattern,
};

pub const DISPATCH_SCHEMA: &str = "shepherd.dispatch/3";

#[derive(
    Clone,
    Copy,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    serde::Deserialize,
    serde::Serialize,
    strum::AsRefStr,
    strum::Display,
    strum::EnumCount,
    strum::EnumIs,
    strum::EnumString,
    strum::IntoStaticStr,
    strum::VariantNames,
)]
#[serde(rename_all = "snake_case")]
#[strum(ascii_case_insensitive, serialize_all = "snake_case")]
pub enum DispatchState {
    Active,
    CapabilityBlocked,
    Malignant,
    Stopped,
}

impl DispatchState {
    #[must_use]
    pub const fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::CapabilityBlocked | Self::Malignant | Self::Stopped
        )
    }
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub struct DispatchStart {
    pub project_id: ProjectId,
    pub run: RunId,
    /// Root session whose current run binding owns this child capability.
    pub root_session_id: SessionId,
    pub run_incarnation: String,
    pub nonce: String,
    pub harness: Harness,
    pub agent_id: AgentId,
    pub agent_type: AgentType,
    pub role: Role,
    pub lane: Option<LaneId>,
    pub parent_agent_id: Option<AgentId>,
    pub session_id: SessionId,
    pub write_scope: Vec<String>,
    pub model: Option<String>,
    pub capability_contract: CapabilityContract,
    pub capability_probe: CapabilityProbe,
    pub startup_attachment: Option<StartupAttachment>,
    pub attachment_nonce: Option<String>,
    pub result_artifact: Option<String>,
    pub result_nonce: Option<String>,
    pub review_artifact: Option<String>,
    pub review_nonce: Option<String>,
    pub started_at: i64,
    pub lease_expires_at: i64,
    pub resumes_agent_id: Option<AgentId>,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub struct DispatchRecord {
    pub schema: String,
    pub revision: u64,
    pub project_id: ProjectId,
    pub run: RunId,
    pub root_session_id: SessionId,
    pub run_incarnation: String,
    pub nonce: String,
    pub harness: Harness,
    pub agent_id: AgentId,
    pub agent_type: AgentType,
    pub role: Role,
    pub lane: Option<LaneId>,
    pub parent_agent_id: Option<AgentId>,
    pub session_id: SessionId,
    pub write_scope: Vec<String>,
    pub model: Option<String>,
    pub capabilities: CapabilityReport,
    pub startup_attachment: Option<StartupAttachment>,
    pub attachment_nonce: Option<String>,
    pub state: DispatchState,
    pub started_at: i64,
    pub lease_expires_at: i64,
    pub stopped_at: Option<i64>,
    pub result_artifact: Option<String>,
    pub result_nonce: Option<String>,
    pub review_artifact: Option<String>,
    pub review_nonce: Option<String>,
    pub resumes_agent_id: Option<AgentId>,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub struct StopRequest {
    pub agent_id: AgentId,
    pub expected_revision: u64,
    pub stopped_at: i64,
    pub result_artifact: Option<String>,
}

impl DispatchRecord {
    pub fn start(input: DispatchStart) -> DispatchResult<Self> {
        validate_start(&input)?;
        let compiled_contract = input.role.dispatch_capability_contract()?;
        if input.capability_contract != compiled_contract {
            return Err(DispatchError::InvalidRecord(
                "capability contract does not match the compiled role profile".into(),
            ));
        }
        let capabilities = input.capability_contract.evaluate(input.capability_probe);
        let blocked = capabilities.readiness() == CapabilityReadiness::Blocked;
        let record = Self {
            schema: DISPATCH_SCHEMA.into(),
            revision: 1,
            project_id: input.project_id,
            run: input.run,
            root_session_id: input.root_session_id,
            run_incarnation: input.run_incarnation,
            nonce: input.nonce,
            harness: input.harness,
            agent_id: input.agent_id,
            agent_type: input.agent_type,
            role: input.role,
            lane: input.lane,
            parent_agent_id: input.parent_agent_id,
            session_id: input.session_id,
            write_scope: input.write_scope,
            model: input.model,
            capabilities,
            startup_attachment: input.startup_attachment,
            attachment_nonce: input.attachment_nonce,
            state: if blocked {
                DispatchState::CapabilityBlocked
            } else {
                DispatchState::Active
            },
            started_at: input.started_at,
            lease_expires_at: input.lease_expires_at,
            stopped_at: blocked.then_some(input.started_at),
            result_artifact: input.result_artifact,
            result_nonce: input.result_nonce,
            review_artifact: input.review_artifact,
            review_nonce: input.review_nonce,
            resumes_agent_id: input.resumes_agent_id,
        };
        record.validate_loaded()?;
        Ok(record)
    }

    pub fn validate_loaded(&self) -> DispatchResult<()> {
        if self.schema != DISPATCH_SCHEMA {
            return Err(DispatchError::InvalidRecord(format!(
                "unsupported schema `{}`",
                self.schema
            )));
        }
        if self.revision == 0 {
            return Err(DispatchError::InvalidRecord(
                "revision must be positive".into(),
            ));
        }
        self.capabilities.validate()?;
        if let Some(reference) = &self.result_artifact {
            validate_artifact(reference)?;
        }
        if let Some(nonce) = &self.result_nonce {
            validate_nonce(nonce, "result")?;
            if self.result_artifact.is_none() {
                return Err(DispatchError::InvalidRecord(
                    "result nonce has no native completion artifact".into(),
                ));
            }
        }
        if let Some(reference) = &self.review_artifact {
            if !matches!(self.role, Role::Auditor | Role::Critic) {
                return Err(DispatchError::InvalidArtifact(reference.clone()));
            }
            validate_artifact(reference)?;
        }
        if let Some(nonce) = &self.review_nonce {
            validate_nonce(nonce, "review")?;
            if self.review_artifact.is_none() {
                return Err(DispatchError::InvalidRecord(
                    "review nonce has no native review artifact".into(),
                ));
            }
        }
        if let Some(attachment) = &self.startup_attachment {
            attachment.validate()?;
            let nonce = self.attachment_nonce.as_deref().ok_or_else(|| {
                DispatchError::InvalidRecord("startup attachment nonce is missing".into())
            })?;
            if nonce.len() != 64
                || !nonce
                    .bytes()
                    .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
            {
                return Err(DispatchError::InvalidRecord(
                    "startup attachment nonce is invalid".into(),
                ));
            }
        } else if self.attachment_nonce.is_some() {
            return Err(DispatchError::InvalidRecord(
                "startup attachment nonce has no attachment".into(),
            ));
        }
        let expected_capabilities =
            self.role
                .dispatch_capability_contract()?
                .evaluate(CapabilityProbe {
                    probe_id: self.capabilities.probe_id.clone(),
                    observed: self.capabilities.observed.clone(),
                    observed_events: self.capabilities.observed_events.clone(),
                    source: self.capabilities.source.clone(),
                    harness_version: self.capabilities.harness_version.clone(),
                    provider_version: self.capabilities.provider_version.clone(),
                    binary_sha256: self.capabilities.binary_sha256.clone(),
                    package_sha256: self.capabilities.package_sha256.clone(),
                    probed_at: self.capabilities.probed_at,
                });
        if self.capabilities != expected_capabilities {
            return Err(DispatchError::InvalidRecord(
                "capability diff does not match the compiled role profile".into(),
            ));
        }
        let start = DispatchStart {
            project_id: self.project_id.clone(),
            run: self.run.clone(),
            root_session_id: self.root_session_id.clone(),
            run_incarnation: self.run_incarnation.clone(),
            nonce: self.nonce.clone(),
            harness: self.harness,
            agent_id: self.agent_id.clone(),
            agent_type: self.agent_type.clone(),
            role: self.role,
            lane: self.lane.clone(),
            parent_agent_id: self.parent_agent_id.clone(),
            session_id: self.session_id.clone(),
            write_scope: self.write_scope.clone(),
            model: self.model.clone(),
            capability_contract: CapabilityContract::default(),
            capability_probe: CapabilityProbe {
                probe_id: self.capabilities.probe_id.clone(),
                observed: self.capabilities.observed.clone(),
                observed_events: self.capabilities.observed_events.clone(),
                source: self.capabilities.source.clone(),
                harness_version: self.capabilities.harness_version.clone(),
                provider_version: self.capabilities.provider_version.clone(),
                binary_sha256: self.capabilities.binary_sha256.clone(),
                package_sha256: self.capabilities.package_sha256.clone(),
                probed_at: self.capabilities.probed_at,
            },
            started_at: self.started_at,
            lease_expires_at: self.lease_expires_at,
            startup_attachment: self.startup_attachment.clone(),
            attachment_nonce: self.attachment_nonce.clone(),
            result_artifact: self.result_artifact.clone(),
            result_nonce: self.result_nonce.clone(),
            review_artifact: self.review_artifact.clone(),
            review_nonce: self.review_nonce.clone(),
            resumes_agent_id: self.resumes_agent_id.clone(),
        };
        validate_start(&start)?;
        start.capability_probe.validate()?;
        match self.state {
            DispatchState::Active
                if self.revision == 1
                    && self.stopped_at.is_none()
                    && self.capabilities.readiness() != CapabilityReadiness::Blocked => {}
            DispatchState::CapabilityBlocked
                if self.revision == 1
                    && self.stopped_at == Some(self.started_at)
                    && self.capabilities.readiness() == CapabilityReadiness::Blocked => {}
            DispatchState::Malignant
                if self.revision >= 2
                    && self.stopped_at.is_some_and(|at| at >= self.started_at) => {}
            DispatchState::Stopped
                if self.revision >= 2
                    && self.stopped_at.is_some_and(|at| at >= self.started_at) => {}
            _ => {
                return Err(DispatchError::InvalidRecord(
                    "state, revision, timestamps, and capability readiness disagree".into(),
                ));
            }
        }
        Ok(())
    }

    pub fn stop(&mut self, request: StopRequest) -> DispatchResult<()> {
        if request.expected_revision != self.revision {
            return Err(DispatchError::RevisionMismatch {
                expected: request.expected_revision,
                found: self.revision,
            });
        }
        if request.agent_id != self.agent_id {
            return Err(DispatchError::AgentMismatch {
                expected: self.agent_id.to_string(),
                found: request.agent_id.to_string(),
            });
        }
        if self.state != DispatchState::Active {
            return Err(DispatchError::InvalidTransition {
                from: self.state,
                to: DispatchState::Stopped,
            });
        }
        if request.stopped_at < self.started_at {
            return Err(DispatchError::InvalidTime(
                "stop time precedes start time".into(),
            ));
        }
        if let Some(reference) = &request.result_artifact {
            validate_artifact(reference)?;
            if self
                .result_artifact
                .as_ref()
                .is_some_and(|expected| expected != reference)
            {
                return Err(DispatchError::InvalidArtifact(reference.clone()));
            }
        }
        self.state = DispatchState::Stopped;
        self.stopped_at = Some(request.stopped_at);
        if self.result_artifact.is_none() {
            self.result_artifact = request.result_artifact;
        }
        self.revision += 1;
        Ok(())
    }

    pub fn quarantine_malignant(&mut self, at: i64) -> DispatchResult<()> {
        if self.state != DispatchState::Active || at < self.started_at {
            return Err(DispatchError::ReviewCustodyTerminal);
        }
        self.state = DispatchState::Malignant;
        self.stopped_at = Some(at);
        self.revision = self
            .revision
            .checked_add(1)
            .ok_or_else(|| DispatchError::InvalidRecord("revision overflow".into()))?;
        self.validate_loaded()
    }

    pub fn resume(&self, input: DispatchStart) -> DispatchResult<Self> {
        if self.state == DispatchState::Malignant {
            return Err(DispatchError::ReviewCustodyTerminal);
        }
        if !self.state.is_terminal() {
            return Err(DispatchError::InvalidTransition {
                from: self.state,
                to: DispatchState::Active,
            });
        }
        if input.agent_id == self.agent_id {
            return Err(DispatchError::ReusedResumeIdentity);
        }
        if input.resumes_agent_id.as_ref() != Some(&self.agent_id) {
            return Err(DispatchError::ResumeMismatch {
                field: "resumes_agent_id",
                expected: self.agent_id.to_string(),
                found: input
                    .resumes_agent_id
                    .as_ref()
                    .map(ToString::to_string)
                    .unwrap_or_default(),
            });
        }
        require_resume_match("project_id", &self.project_id, &input.project_id)?;
        require_resume_match("run", &self.run, &input.run)?;
        require_resume_match(
            "root_session_id",
            &self.root_session_id,
            &input.root_session_id,
        )?;
        require_resume_match(
            "run_incarnation",
            &self.run_incarnation,
            &input.run_incarnation,
        )?;
        require_resume_match("role", &self.role, &input.role)?;
        require_resume_match("lane", &self.lane, &input.lane)?;
        require_resume_match(
            "parent_agent_id",
            &self.parent_agent_id,
            &input.parent_agent_id,
        )?;
        if self.write_scope != input.write_scope {
            return Err(DispatchError::ResumeMismatch {
                field: "write_scope",
                expected: format!("{:?}", self.write_scope),
                found: format!("{:?}", input.write_scope),
            });
        }
        Self::start(input)
    }
}

fn validate_start(input: &DispatchStart) -> DispatchResult<()> {
    input.capability_contract.validate()?;
    validate_token("run incarnation", &input.run_incarnation)?;
    validate_token("dispatch nonce", &input.nonce)?;
    input.capability_probe.validate()?;
    if input.harness == Harness::ClaudeCode
        && input.agent_type.as_str() != input.role.as_str()
        && input.agent_type.as_str() != input.role.carrier()
    {
        return Err(DispatchError::AgentTypeRoleMismatch {
            agent_type: input.agent_type.to_string(),
            role: input.role,
        });
    }
    if input.started_at < 0 || input.lease_expires_at <= input.started_at {
        return Err(DispatchError::InvalidTime(
            "lease must expire after a non-negative start".into(),
        ));
    }
    if input.role == Role::Conductor && input.lane.is_none() {
        return Err(DispatchError::InvalidRecord(
            "Conductor dispatches require a lane".into(),
        ));
    }
    validate_parent_child(input.parent_agent_id.as_ref(), &input.agent_id)?;
    if input.resumes_agent_id.as_ref() == Some(&input.agent_id) {
        return Err(DispatchError::ReusedResumeIdentity);
    }
    let mut scopes = input.write_scope.clone();
    scopes.sort();
    if scopes.windows(2).any(|pair| pair[0] == pair[1]) {
        return Err(DispatchError::InvalidWriteScope(
            "write_scope entries must be unique".into(),
        ));
    }
    for scope in &input.write_scope {
        validate_write_scope_pattern(scope)?;
    }
    if input.model.as_ref().is_some_and(|model| {
        model.is_empty() || model.len() > 256 || model.chars().any(char::is_control)
    }) {
        return Err(DispatchError::InvalidIdentifier {
            kind: "model",
            value: input.model.clone().unwrap_or_default(),
        });
    }
    Ok(())
}

fn validate_token(kind: &'static str, value: &str) -> DispatchResult<()> {
    if value.is_empty() || value.len() > 128 || value.chars().any(char::is_control) {
        return Err(DispatchError::InvalidIdentifier {
            kind,
            value: value.into(),
        });
    }
    Ok(())
}

fn validate_artifact(reference: &str) -> DispatchResult<()> {
    let valid = !reference.is_empty()
        && reference.len() <= 512
        && !reference.starts_with('/')
        && !reference.contains('\\')
        && !reference.contains('\0')
        && !reference.chars().any(char::is_control)
        && reference
            .split('/')
            .all(|part| !part.is_empty() && part != "." && part != "..");
    if valid {
        Ok(())
    } else {
        Err(DispatchError::InvalidArtifact(reference.into()))
    }
}

fn validate_nonce(value: &str, kind: &str) -> DispatchResult<()> {
    if value.len() != 64
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
    {
        return Err(DispatchError::InvalidRecord(format!(
            "{kind} nonce is not lowercase hexadecimal"
        )));
    }
    Ok(())
}

fn require_resume_match<T>(field: &'static str, expected: &T, found: &T) -> DispatchResult<()>
where
    T: Eq + core::fmt::Debug,
{
    if expected == found {
        Ok(())
    } else {
        Err(DispatchError::ResumeMismatch {
            field,
            expected: format!("{expected:?}"),
            found: format!("{found:?}"),
        })
    }
}