a3s-ahp 2.4.0

Agent Harness Protocol v2.4 — Universal, transport-agnostic protocol for supervising autonomous AI agents
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Idle event fired when the agent has been idle for a threshold duration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdleEvent {
    /// Duration of idle time in milliseconds.
    pub idle_duration_ms: u64,
    /// Reason for becoming idle.
    pub idle_reason: String,
    /// Last event type before becoming idle.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_event_type: Option<String>,
    /// Suggested action for the idle period.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suggested_action: Option<String>,
}

/// Heartbeat event for periodic status updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatEvent {
    /// Agent uptime in milliseconds.
    pub uptime_ms: u64,
    /// Total events processed since start.
    pub total_events_processed: u64,
    /// Current agent state.
    pub current_state: String,
    /// CPU usage percentage (0-100).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu_percent: Option<f32>,
    /// Memory usage in bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_bytes: Option<u64>,
    /// Currently active tool count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_tools: Option<usize>,
    /// Pending actions in queue.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pending_actions: Option<usize>,
    /// Queue depth.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub queue_depth: Option<usize>,
    /// Tokens used in current session.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokens_used: Option<i32>,
}

/// Structured context passed with events for context-aware control decisions.
///
/// The client exposes capabilities it chooses. The server can use exposed
/// capabilities and should ignore unrecognized or unneeded entries.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EventContext {
    /// Recent facts or knowledge retrieved.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recent_facts: Option<Vec<Fact>>,
    /// Memory/knowledge base state summary.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_summary: Option<MemorySummary>,
    /// Session statistics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_stats: Option<SessionStats>,
    /// Current task/goal description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_task: Option<String>,
    /// Client-defined capabilities as arbitrary key-value pairs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capabilities: Option<HashMap<String, serde_json::Value>>,
}

/// A factual memory item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fact {
    pub content: String,
    pub source: String,
    pub confidence: f32,
}

/// Memory state summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemorySummary {
    pub memory_type: String,
    pub total_items: usize,
    pub recent_topics: Vec<String>,
}

/// Session statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStats {
    pub total_actions: usize,
    pub total_tokens: i32,
    pub duration_ms: u64,
    pub error_count: usize,
}

/// Decision for idle/dream events.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "decision", rename_all = "lowercase")]
pub enum IdleDecision {
    /// Allow background consolidation/dream.
    Allow,
    /// Defer idle processing.
    Defer {
        #[serde(skip_serializing_if = "Option::is_none")]
        reason: Option<String>,
    },
}

/// Perception intent - why the model needs to perceive context.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionIntent {
    /// Identify or classify an entity.
    Recognize,
    /// Understand semantics, logic, or behavior.
    Understand,
    /// Find the location or path of a resource.
    Locate,
    /// Recall relevant information from accumulated knowledge.
    Retrieve,
    /// Understand the overall structure of an environment or system.
    Explore,
    /// Infer causality or logic based on existing information.
    Reason,
    /// Confirm whether an assumption or state is correct.
    Validate,
    /// Compare similarities and differences.
    Compare,
    /// Get current status or history of a process.
    Track,
}

impl std::fmt::Display for PerceptionIntent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PerceptionIntent::Recognize => write!(f, "recognize"),
            PerceptionIntent::Understand => write!(f, "understand"),
            PerceptionIntent::Locate => write!(f, "locate"),
            PerceptionIntent::Retrieve => write!(f, "retrieve"),
            PerceptionIntent::Explore => write!(f, "explore"),
            PerceptionIntent::Reason => write!(f, "reason"),
            PerceptionIntent::Validate => write!(f, "validate"),
            PerceptionIntent::Compare => write!(f, "compare"),
            PerceptionIntent::Track => write!(f, "track"),
        }
    }
}

/// Perception target - what the model wants to perceive.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionTarget {
    /// A specific object or concept.
    Entity {
        name: String,
        /// Entity type, such as function, file, person, document, config, or API.
        entity_type: String,
        /// Identifier attribute.
        identifier: Option<String>,
    },
    /// A path or place.
    Location {
        path: String,
        /// Location type, such as file, directory, URL, endpoint, or region.
        location_type: String,
    },
    /// Something that happened or will happen.
    Event {
        description: String,
        /// Event type, such as change, action, decision, error, or meeting.
        event_type: String,
        /// Related time range.
        time_range: Option<TimeRange>,
    },
    /// Connections between multiple entities.
    Relation {
        entities: Vec<String>,
        /// Relation type, such as dependency, ownership, sequence, or conflict.
        relation_type: String,
    },
    /// A policy, strategy, or convention.
    Rule {
        name: String,
        /// Rule type, such as policy, convention, constraint, or requirement.
        rule_type: String,
        /// Applicable scope.
        scope: Option<String>,
    },
    /// Current condition of a system or entity.
    State {
        target: String,
        aspect: String,
        include_history: bool,
    },
    /// An available capability or asset.
    Resource {
        name: String,
        /// Resource type, such as tool, skill, API, data, or personnel.
        resource_type: String,
        constraints: Option<serde_json::Value>,
    },
    /// A recurring phenomenon or rule.
    Pattern {
        pattern: String,
        /// Pattern type, such as text, regex, structure, or behavior.
        pattern_type: String,
    },
}

/// Time range for events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeRange {
    pub from: Option<i64>,
    pub to: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relative: Option<String>,
}

/// Perception domain - the domain of the current task.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionDomain {
    Coding,
    Writing,
    DataAnalysis,
    Research,
    ProjectManagement,
    Conversation,
    Operations,
    Security,
    General,
}

impl Default for PerceptionDomain {
    fn default() -> Self {
        PerceptionDomain::General
    }
}

/// Perception modality - the form of information needed.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionModality {
    Text,
    Code,
    StructuredData,
    Table,
    Chart,
    Image,
    Audio,
    Video,
    Any,
}

impl Default for PerceptionModality {
    fn default() -> Self {
        PerceptionModality::Any
    }
}

/// Quality requirement for perception.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionUrgency {
    /// Must have - cannot proceed without this context.
    Critical,
    /// Important - significantly improves response quality.
    High,
    /// Helpful but not essential.
    Normal,
    /// Can be cached or delayed.
    Low,
}

impl Default for PerceptionUrgency {
    fn default() -> Self {
        PerceptionUrgency::Normal
    }
}

/// Freshness requirement for perceived data.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PerceptionFreshness {
    /// Need realtime data, such as git status or filesystem state.
    Realtime,
    /// Need recent data, such as recent policy changes.
    Recent,
    /// Static data is sufficient, such as code structure or archives.
    Static,
}

impl Default for PerceptionFreshness {
    fn default() -> Self {
        PerceptionFreshness::Static
    }
}

/// Constraints for perception.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerceptionConstraints {
    #[serde(default)]
    pub max_results: Option<usize>,
    #[serde(default)]
    pub max_context_length: Option<usize>,
    #[serde(default = "default_include_sources")]
    pub include_sources: bool,
}

fn default_include_sources() -> bool {
    true
}

impl Default for PerceptionConstraints {
    fn default() -> Self {
        Self {
            max_results: None,
            max_context_length: None,
            include_sources: true,
        }
    }
}

/// Current perception context.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerceptionContext {
    pub workspace: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_task: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relevant_history: Option<Vec<HistoryItem>>,
}

/// History item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryItem {
    pub item_type: String,
    pub content: String,
    pub timestamp: i64,
}

/// Context perception event fired when the model needs workspace knowledge.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextPerceptionEvent {
    pub session_id: String,
    pub intent: PerceptionIntent,
    pub target: PerceptionTarget,
    #[serde(default)]
    pub domain: PerceptionDomain,
    #[serde(default)]
    pub preferred_modality: PerceptionModality,
    #[serde(default)]
    pub urgency: PerceptionUrgency,
    #[serde(default)]
    pub freshness: PerceptionFreshness,
    pub context: PerceptionContext,
    #[serde(default)]
    pub constraints: PerceptionConstraints,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

/// Injected context from harness.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InjectedContext {
    pub facts: Vec<Fact>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_contents: Option<Vec<FileContentSnippet>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_summary: Option<ProjectSummary>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub knowledge: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suggestions: Option<Vec<String>>,
}

/// File content snippet.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileContentSnippet {
    pub path: String,
    pub snippet: String,
    pub relevance_score: f32,
}

/// Project summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectSummary {
    pub project_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_files: Option<Vec<String>>,
    pub structure_description: String,
}

/// Decision for context perception events.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "decision", rename_all = "lowercase")]
pub enum ContextPerceptionDecision {
    /// Provide context and continue.
    Allow {
        injected_context: InjectedContext,
        #[serde(skip_serializing_if = "Option::is_none")]
        metadata: Option<HashMap<String, serde_json::Value>>,
    },
    /// Skip context injection.
    Block {
        reason: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        metadata: Option<HashMap<String, serde_json::Value>>,
    },
    /// Request more specific context.
    Refine {
        refined_intent: Option<PerceptionIntent>,
        refined_target: Option<PerceptionTarget>,
        scope_hints: Vec<String>,
    },
}