agent-sdk-core 0.1.0-alpha.4

Product-neutral primitive kernel and contracts for a Rust-first Agent SDK.
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
475
476
477
//! Durable and observable SDK records. Use these DTOs for events, journals, effects,
//! context, output, and feature evidence. Constructing records is data-only;
//! persistence, publication, and external actions happen through ports or application
//! coordinators. This file contains the hooks portion of that contract.
//!
use serde::{Deserialize, Serialize};

use crate::{
    domain::{
        AgentId, AttemptId, EffectId, EntityKind, EntityRef, PolicyRef, PrivacyClass, RunId,
        SessionId, SourceKind, SourceRef, TurnId,
    },
    effect::{EffectIntent, EffectKind, EffectResult},
    journal::{
        EventIndexProjection, JOURNAL_SCHEMA_VERSION, JournalRecord, JournalRecordBase,
        JournalRecordKind, JournalRecordPayload,
    },
    package_hooks::{HookId, HookPoint, HookResponseClass, HookSpec},
};

/// Constant value for the records::hooks contract. Use it to keep SDK
/// records and tests aligned on the same stable value.
pub const HOOK_RECORD_SCHEMA_VERSION: u16 = 1;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
/// Carries the hook record record payload for journal, event, or fixture surfaces.
/// Creating or cloning it only preserves serialized SDK state; append, publish, replay, or export effects are documented on the runtime and port methods that store it.
pub struct HookRecord {
    /// Wire schema version used for compatibility checks.
    pub schema_version: u16,
    /// Stable hook id used for typed lineage, lookup, or dedupe.
    pub hook_id: HookId,
    /// Point used by this record or request.
    pub point: HookPoint,
    /// Payload carried by this record.
    /// Use the surrounding policy and redaction fields to decide whether it can be exposed.
    pub payload: HookRecordPayload,
}

impl HookRecord {
    /// Registered.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn registered(spec: &HookSpec) -> Result<Self, crate::domain::AgentError> {
        Ok(Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::Registered {
                spec_hash: spec.spec_hash()?,
                executor_ref: spec.executor_ref.as_str().to_string(),
                policy_ref: spec.policy_ref.clone(),
            },
        })
    }

    /// Builds the invocation started value.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    pub fn invocation_started(spec: &HookSpec, invocation_id: impl Into<String>) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::InvocationStarted {
                invocation_id: invocation_id.into(),
            },
        }
    }

    /// Returns an updated value with completed configured.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn completed(spec: &HookSpec, invocation_id: impl Into<String>, elapsed_ms: u64) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::Completed {
                invocation_id: invocation_id.into(),
                elapsed_ms,
            },
        }
    }

    /// Builds the timeout record or result value.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn timeout(spec: &HookSpec, invocation_id: impl Into<String>, elapsed_ms: u64) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::TimedOut {
                invocation_id: invocation_id.into(),
                elapsed_ms,
                failure_policy: format!("{:?}", spec.failure),
            },
        }
    }

    /// Cancelled.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn cancelled(spec: &HookSpec, invocation_id: impl Into<String>) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::Cancelled {
                invocation_id: invocation_id.into(),
            },
        }
    }

    /// Returns an updated value with failed configured.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn failed(
        spec: &HookSpec,
        invocation_id: impl Into<String>,
        redacted_summary: impl Into<String>,
    ) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::Failed {
                invocation_id: invocation_id.into(),
                failure_policy: format!("{:?}", spec.failure),
                redacted_summary: redacted_summary.into(),
            },
        }
    }

    /// Returns the response decision currently held by this value.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn response_decision(
        spec: &HookSpec,
        invocation_id: impl Into<String>,
        decision: HookResponseDecision,
        response_class: HookResponseClass,
        target_domain_refs: Vec<EntityRef>,
    ) -> Self {
        Self {
            schema_version: HOOK_RECORD_SCHEMA_VERSION,
            hook_id: spec.hook_id.clone(),
            point: spec.point.clone(),
            payload: HookRecordPayload::ResponseDecision {
                invocation_id: invocation_id.into(),
                decision,
                response_class,
                mutation_right_decision: "checked_against_point_and_spec_rights".to_string(),
                target_domain_refs,
            },
        }
    }

    /// Builds a rejected response decision journal record.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    #[expect(
        clippy::too_many_arguments,
        reason = "hook response records are durable audit DTOs and keep their lineage fields explicit until a record-builder pass"
    )]
    pub fn rejected_response_journal_record(
        journal_seq: u64,
        record_id: impl Into<String>,
        run_id: RunId,
        agent_id: AgentId,
        session_id: Option<SessionId>,
        turn_id: Option<TurnId>,
        attempt_id: Option<AttemptId>,
        source: SourceRef,
        spec: &HookSpec,
        invocation_id: impl Into<String>,
        decision: HookResponseDecision,
        response_class: HookResponseClass,
        runtime_package_fingerprint: impl Into<String>,
    ) -> (Self, JournalRecord) {
        let hook_record =
            Self::response_decision(spec, invocation_id, decision, response_class, Vec::new());
        let mut base = JournalRecordBase::new(
            journal_seq,
            format!("{}.response", record_id.into()),
            run_id,
            agent_id,
            source,
        );
        base.session_id = session_id;
        base.turn_id = turn_id;
        base.attempt_id = attempt_id;
        base.runtime_package_fingerprint = runtime_package_fingerprint.into();
        base.privacy = PrivacyClass::ContentRefsOnly;
        base.redaction_policy_id = "policy.redaction.hook.default".to_string();
        let journal_record = hook_journal_record(
            base,
            hook_record.clone(),
            SourceRef::with_kind(SourceKind::Hook, spec.hook_id.as_str()),
            "hook_response_decision",
        );
        (hook_record, journal_record)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
/// Enumerates the finite hook record payload cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum HookRecordPayload {
    /// Use this variant when the contract needs to represent registered; selecting it has no side effect by itself.
    Registered {
        /// Deterministic spec hash used for stale checks, package evidence,
        /// or replay comparisons.
        spec_hash: String,
        /// Typed executor ref reference. Resolving or executing it is a
        /// separate policy-gated step.
        executor_ref: String,
        /// Policy reference that must be resolved by the host or runtime
        /// before execution.
        policy_ref: PolicyRef,
    },
    /// Use this variant when the contract needs to represent invocation started; selecting it has no side effect by itself.
    InvocationStarted {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
    },
    /// Use this variant when the contract needs to represent completed; selecting it has no side effect by itself.
    Completed {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
        /// elapsed ms duration in milliseconds.
        elapsed_ms: u64,
    },
    /// Use this variant when the contract needs to represent timed out; selecting it has no side effect by itself.
    TimedOut {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
        /// elapsed ms duration in milliseconds.
        elapsed_ms: u64,
        /// Failure policy used by this record or request.
        failure_policy: String,
    },
    /// Use this variant when the contract needs to represent cancelled; selecting it has no side effect by itself.
    Cancelled {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
    },
    /// Use this variant when the contract needs to represent failed; selecting it has no side effect by itself.
    Failed {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
        /// Failure policy used by this record or request.
        failure_policy: String,
        /// Redacted human-readable summary safe for events, telemetry, and
        /// logs.
        redacted_summary: String,
    },
    /// Use this variant when the contract needs to represent response decision; selecting it has no side effect by itself.
    ResponseDecision {
        /// Stable invocation id used for typed lineage, lookup, or dedupe.
        invocation_id: String,
        /// Decision used by this record or request.
        decision: HookResponseDecision,
        /// Classification value for response class.
        /// Policy and projection paths use it for finite routing decisions.
        response_class: HookResponseClass,
        /// Decision explaining why a hook response mutation class was accepted or rejected.
        /// Use it as audit evidence when applying hook responses that can affect run state.
        mutation_right_decision: String,
        /// Typed target domain refs references. Resolving them is separate
        /// from constructing this record.
        target_domain_refs: Vec<EntityRef>,
    },
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite hook response decision cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum HookResponseDecision {
    /// Use this variant when the contract needs to represent accepted journaled before apply; selecting it has no side effect by itself.
    AcceptedJournaledBeforeApply,
    /// Use this variant when the contract needs to represent rejected mutation right; selecting it has no side effect by itself.
    RejectedMutationRight,
    /// Use this variant when the contract needs to represent rejected point matrix; selecting it has no side effect by itself.
    RejectedPointMatrix,
    /// Use this variant when the contract needs to represent rejected policy; selecting it has no side effect by itself.
    RejectedPolicy,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
/// Carries the hook mutation journal plan record payload for journal, event, or fixture surfaces.
/// Creating or cloning it only preserves serialized SDK state; append, publish, replay, or export effects are documented on the runtime and port methods that store it.
pub struct HookMutationJournalPlan {
    /// Hook record used by this record or request.
    pub hook_record: HookRecord,
    /// Effect intent used by this record or request.
    pub effect_intent: EffectIntent,
    /// Hook journal record used by this record or request.
    pub hook_journal_record: JournalRecord,
    /// Intent journal record used by this record or request.
    pub intent_journal_record: JournalRecord,
    /// Result journal record used by this record or request.
    pub result_journal_record: JournalRecord,
}

impl HookMutationJournalPlan {
    /// Builds the accepted response value.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    #[expect(
        clippy::too_many_arguments,
        reason = "hook response records are durable audit DTOs and keep their lineage fields explicit until a record-builder pass"
    )]
    pub fn accepted_response(
        journal_seq: u64,
        record_id: impl Into<String>,
        run_id: RunId,
        agent_id: AgentId,
        session_id: Option<SessionId>,
        turn_id: Option<TurnId>,
        attempt_id: Option<AttemptId>,
        source: SourceRef,
        spec: &HookSpec,
        invocation_id: impl Into<String>,
        response_class: HookResponseClass,
        runtime_package_fingerprint: impl Into<String>,
    ) -> Self {
        let invocation_id = invocation_id.into();
        let hook_record = HookRecord::response_decision(
            spec,
            invocation_id.clone(),
            HookResponseDecision::AcceptedJournaledBeforeApply,
            response_class.clone(),
            vec![hook_entity_ref(&spec.hook_id)],
        );
        let effect_id = EffectId::new(format!(
            "effect.hook.{}.{}",
            spec.hook_id.as_str(),
            invocation_id
        ));
        let mut intent = EffectIntent::new(
            effect_id,
            EffectKind::HookMutation,
            hook_entity_ref(&spec.hook_id),
            SourceRef::with_kind(SourceKind::Hook, spec.hook_id.as_str()),
            format!("accepted hook response {:?}", response_class),
        );
        intent.policy_refs = vec![spec.policy_ref.clone()];

        let record_id = record_id.into();
        let runtime_package_fingerprint = runtime_package_fingerprint.into();
        let mut hook_base = JournalRecordBase::new(
            journal_seq,
            format!("{record_id}.response"),
            run_id.clone(),
            agent_id.clone(),
            source.clone(),
        );
        hook_base.session_id = session_id.clone();
        hook_base.turn_id = turn_id.clone();
        hook_base.attempt_id = attempt_id.clone();
        hook_base.runtime_package_fingerprint = runtime_package_fingerprint.clone();
        hook_base.privacy = PrivacyClass::ContentRefsOnly;
        hook_base.redaction_policy_id = "policy.redaction.hook.default".to_string();
        let mut intent_base = JournalRecordBase::new(
            journal_seq + 1,
            format!("{record_id}.intent"),
            run_id.clone(),
            agent_id.clone(),
            source.clone(),
        );
        intent_base.session_id = session_id.clone();
        intent_base.turn_id = turn_id.clone();
        intent_base.attempt_id = attempt_id.clone();
        intent_base.runtime_package_fingerprint = runtime_package_fingerprint.clone();
        intent_base.privacy = PrivacyClass::ContentRefsOnly;
        intent_base.redaction_policy_id = "policy.redaction.hook.default".to_string();
        let mut result_base = JournalRecordBase::new(
            journal_seq + 2,
            format!("{record_id}.result"),
            run_id,
            agent_id,
            source,
        );
        result_base.session_id = session_id;
        result_base.turn_id = turn_id;
        result_base.attempt_id = attempt_id;
        result_base.runtime_package_fingerprint = runtime_package_fingerprint;
        result_base.privacy = PrivacyClass::ContentRefsOnly;
        result_base.redaction_policy_id = "policy.redaction.hook.default".to_string();

        let hook_journal_record = hook_journal_record(
            hook_base,
            hook_record.clone(),
            SourceRef::with_kind(SourceKind::Hook, spec.hook_id.as_str()),
            "hook_response_decision",
        );
        let intent_journal_record = JournalRecord::effect_intent(intent_base, intent.clone());
        let result = EffectResult::completed(
            intent.effect_id.clone(),
            format!(
                "accepted hook response {:?} journaled before apply",
                response_class
            ),
        );
        let result_journal_record = JournalRecord::effect_result(result_base, result);
        Self {
            hook_record,
            effect_intent: intent,
            hook_journal_record,
            intent_journal_record,
            result_journal_record,
        }
    }
}

/// Builds the hook entity ref value.
/// This is data construction and performs no I/O, journal append, event publication, or process
pub fn hook_entity_ref(hook_id: &HookId) -> EntityRef {
    EntityRef::new(EntityKind::Hook, hook_id.as_str())
}

fn hook_journal_record(
    base: JournalRecordBase,
    record: HookRecord,
    source: SourceRef,
    event_kind: impl Into<String>,
) -> JournalRecord {
    let subject_ref = hook_entity_ref(&record.hook_id);
    JournalRecord {
        journal_schema_version: JOURNAL_SCHEMA_VERSION,
        journal_seq: base.journal_seq,
        record_id: base.record_id,
        record_kind: JournalRecordKind::Hook,
        run_id: base.run_id.clone(),
        session_id: base.session_id.clone(),
        agent_id: base.agent_id.clone(),
        turn_id: base.turn_id.clone(),
        attempt_id: base.attempt_id.clone(),
        subject_ref: subject_ref.clone(),
        related_refs: Vec::new(),
        causal_refs: base.causal_refs,
        source: source.clone(),
        destination: base.destination.clone(),
        correlation_keys: Vec::new(),
        tags: vec!["hook".to_string()],
        delivery_semantics: "journal_backed".to_string(),
        event_index: EventIndexProjection {
            run_id: base.run_id,
            session_id: base.session_id,
            agent_id: base.agent_id,
            turn_id: base.turn_id,
            event_family: "hook".to_string(),
            event_kind: event_kind.into(),
            source,
            destination: base.destination,
            subject_ref: subject_ref.clone(),
            related_refs: Vec::new(),
            correlation_keys: Vec::new(),
            tags: vec!["hook".to_string()],
            privacy_class: base.privacy,
            delivery_semantics: "journal_backed".to_string(),
        },
        timestamp_millis: base.timestamp_millis,
        runtime_package_fingerprint: base.runtime_package_fingerprint,
        privacy: base.privacy,
        content_refs: Vec::new(),
        redaction_policy_id: base.redaction_policy_id,
        idempotency_key: None,
        dedupe_key: None,
        checkpoint_ref: base.checkpoint_ref,
        payload: JournalRecordPayload::Hook(record),
    }
}