agentics-domain 0.3.0

Domain types and validation models for the Agentics challenge platform.
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
//! Web authentication and human identity API models.

use std::borrow::Cow;
use std::fmt;

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::ids::{AdminServiceTokenId, CreatorApiTokenId, HumanId};
use super::pioneer_codes::PioneerCodeInput;
use super::urls::GithubSignInAuthorizationUrl;

/// Validation failure for [`GithubUserId`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GithubUserIdError;

impl fmt::Display for GithubUserIdError {
    /// Format the user-facing GitHub user id validation error.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("github_user_id must be a positive integer")
    }
}

impl std::error::Error for GithubUserIdError {}

/// Positive numeric GitHub user id returned by GitHub sign-in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GithubUserId(i64);

impl GithubUserId {
    /// Parse a positive GitHub user id from an external boundary.
    pub fn try_new(value: i64) -> Result<Self, GithubUserIdError> {
        if value <= 0 {
            return Err(GithubUserIdError);
        }
        Ok(Self(value))
    }

    /// Borrow the numeric value for database and wire boundaries.
    pub fn as_i64(self) -> i64 {
        self.0
    }
}

impl fmt::Display for GithubUserId {
    /// Handles fmt for this module.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Serialize for GithubUserId {
    /// Serialize as the existing JSON integer contract.
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_i64(self.0)
    }
}

impl<'de> Deserialize<'de> for GithubUserId {
    /// Deserialize from the existing JSON integer contract.
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = i64::deserialize(deserializer)?;
        Self::try_new(value).map_err(serde::de::Error::custom)
    }
}

impl JsonSchema for GithubUserId {
    /// Keep the generated schema inline so DTO wire shape stays a number.
    fn inline_schema() -> bool {
        true
    }

    /// Handles schema name for this module.
    fn schema_name() -> Cow<'static, str> {
        "GithubUserId".into()
    }

    /// Handles json schema for this module.
    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "integer",
            "minimum": 1
        })
    }
}

/// Role granted to a human account.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum HumanRole {
    Creator,
    Admin,
}

impl HumanRole {
    /// Stable database and wire string for a human role.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Creator => "creator",
            Self::Admin => "admin",
        }
    }

    /// Parse a stable database string for a human role.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "creator" => Some(Self::Creator),
            "admin" => Some(Self::Admin),
            _ => None,
        }
    }
}

/// Persistent lifecycle state for a human account.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum HumanStatus {
    Active,
    SetupRequired,
    Disabled,
    Deleted,
}

impl HumanStatus {
    /// Stable database and wire string for a human status.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Active => "active",
            Self::SetupRequired => "setup_required",
            Self::Disabled => "disabled",
            Self::Deleted => "deleted",
        }
    }

    /// Parse a stable database string for a human status.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "active" => Some(Self::Active),
            "setup_required" => Some(Self::SetupRequired),
            "disabled" => Some(Self::Disabled),
            "deleted" => Some(Self::Deleted),
            _ => None,
        }
    }
}

/// Browser-submitted request to start GitHub sign-in.
#[derive(Debug, Clone, Deserialize, garde::Validate, schemars::JsonSchema)]
#[garde(allow_unvalidated)]
#[serde(deny_unknown_fields)]
pub struct GithubSignInLoginRequest {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub return_to: Option<String>,
}

/// Browser-submitted request to finish setup for a signed-in human.
#[derive(Debug, Clone, Deserialize, garde::Validate, schemars::JsonSchema)]
#[garde(allow_unvalidated)]
#[serde(deny_unknown_fields)]
pub struct CompleteHumanSetupRequest {
    pub pioneer_code: PioneerCodeInput,
}

/// URL returned to a browser or CLI so it can start GitHub sign-in.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GithubSignInLoginResponse {
    pub authorization_url: GithubSignInAuthorizationUrl,
}

/// Browser-submitted request that completes GitHub sign-in.
#[derive(Debug, Clone, Deserialize, garde::Validate, schemars::JsonSchema)]
#[garde(allow_unvalidated)]
#[serde(deny_unknown_fields)]
pub struct GithubSignInCallbackRequest {
    #[garde(custom(crate::validation::trimmed_non_empty))]
    pub code: String,
    #[garde(custom(crate::validation::trimmed_non_empty))]
    pub state: String,
}

/// Current human browser session identity.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct HumanSessionResponse {
    pub human_id: HumanId,
    pub status: HumanStatus,
    pub github_user_id: GithubUserId,
    pub github_login: String,
    pub roles: Vec<HumanRole>,
    pub csrf_token: String,
    pub expires_at: String,
}

/// Response returned after finishing human account setup.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct CompleteHumanSetupResponse {
    pub session: HumanSessionResponse,
}

/// Response returned after completing GitHub sign-in.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GithubSignInCallbackResponse {
    pub session: HumanSessionResponse,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub return_to: Option<String>,
}

/// Admin-visible human identity row.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminHumanDto {
    pub human_id: HumanId,
    pub status: HumanStatus,
    pub github_user_id: GithubUserId,
    pub github_login: String,
    pub roles: Vec<HumanRole>,
    pub created_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deleted_at: Option<String>,
}

/// Admin list response for human identities.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminHumanListResponse {
    pub items: Vec<AdminHumanDto>,
}

/// Response returned after granting or revoking a human admin role.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminHumanRoleResponse {
    pub human: AdminHumanDto,
}

/// Browser-submitted request to create an admin service token.
#[derive(Debug, Clone, Deserialize, garde::Validate, schemars::JsonSchema)]
#[garde(allow_unvalidated)]
#[serde(deny_unknown_fields)]
pub struct CreateAdminServiceTokenRequest {
    #[garde(custom(crate::validation::trimmed_non_empty))]
    pub label: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
}

/// Admin service-token metadata.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminServiceTokenDto {
    pub id: AdminServiceTokenId,
    pub label: String,
    pub status: String,
    pub created_by_human_id: HumanId,
    pub created_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_used_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revoked_by_human_id: Option<HumanId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revoked_at: Option<String>,
}

/// Response returned after creating an admin service token.
#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminServiceTokenCreatedResponse {
    pub token: String,
    pub token_record: AdminServiceTokenDto,
}

impl fmt::Debug for AdminServiceTokenCreatedResponse {
    /// Redacts the one-time raw admin service token from debug output.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AdminServiceTokenCreatedResponse")
            .field("token", &"<redacted>")
            .field("token_record", &self.token_record)
            .finish()
    }
}

/// Admin list response for admin service tokens.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AdminServiceTokenListResponse {
    pub items: Vec<AdminServiceTokenDto>,
}

/// Response returned after revoking an admin service token.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct RevokeAdminServiceTokenResponse {
    pub token_record: AdminServiceTokenDto,
}

/// Browser-submitted request to create a creator API token.
#[derive(Debug, Clone, Deserialize, garde::Validate, schemars::JsonSchema)]
#[garde(allow_unvalidated)]
#[serde(deny_unknown_fields)]
pub struct CreateCreatorApiTokenRequest {
    #[garde(custom(crate::validation::trimmed_non_empty))]
    pub label: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
}

/// Creator API-token metadata visible to the owning creator.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct CreatorApiTokenDto {
    pub id: CreatorApiTokenId,
    pub label: String,
    pub status: String,
    pub created_by_human_id: HumanId,
    pub created_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_used_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revoked_at: Option<String>,
}

/// Response returned after creating a creator API token.
#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct CreatorApiTokenCreatedResponse {
    pub token: String,
    pub token_record: CreatorApiTokenDto,
}

impl fmt::Debug for CreatorApiTokenCreatedResponse {
    /// Redacts the one-time raw creator API token from debug output.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CreatorApiTokenCreatedResponse")
            .field("token", &"<redacted>")
            .field("token_record", &self.token_record)
            .finish()
    }
}

/// Creator list response for API tokens.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct CreatorApiTokenListResponse {
    pub items: Vec<CreatorApiTokenDto>,
}

/// Response returned after revoking a creator API token.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct RevokeCreatorApiTokenResponse {
    pub token_record: CreatorApiTokenDto,
}

#[cfg(test)]
mod tests {
    use super::{
        AdminServiceTokenCreatedResponse, AdminServiceTokenDto, CreatorApiTokenCreatedResponse,
        CreatorApiTokenDto, GithubUserId,
    };
    use crate::models::ids::{AdminServiceTokenId, CreatorApiTokenId, HumanId};

    /// Verifies GitHub user ids keep the integer wire contract while rejecting invalid ids.
    #[test]
    fn github_user_ids_are_positive_wire_integers() {
        let id = GithubUserId::try_new(42).expect("positive id should parse");
        assert_eq!(id.as_i64(), 42);
        assert_eq!(
            serde_json::to_value(id).expect("id should serialize"),
            serde_json::json!(42)
        );
        assert!(GithubUserId::try_new(0).is_err());
        assert!(serde_json::from_value::<GithubUserId>(serde_json::json!(-1)).is_err());
    }

    /// Verifies one-time bearer tokens cannot leak through accidental debug output.
    #[test]
    fn token_creation_debug_output_redacts_raw_tokens() {
        let human_id = HumanId::generate();
        let admin = AdminServiceTokenCreatedResponse {
            token: "agentics_admin_secret".to_string(),
            token_record: AdminServiceTokenDto {
                id: AdminServiceTokenId::generate(),
                label: "admin".to_string(),
                status: "active".to_string(),
                created_by_human_id: human_id.clone(),
                created_at: "2026-06-01T00:00:00Z".to_string(),
                last_used_at: None,
                expires_at: None,
                revoked_by_human_id: None,
                revoked_at: None,
            },
        };
        let creator = CreatorApiTokenCreatedResponse {
            token: "agentics_creator_secret".to_string(),
            token_record: CreatorApiTokenDto {
                id: CreatorApiTokenId::generate(),
                label: "creator".to_string(),
                status: "active".to_string(),
                created_by_human_id: human_id,
                created_at: "2026-06-01T00:00:00Z".to_string(),
                last_used_at: None,
                expires_at: None,
                revoked_at: None,
            },
        };

        let admin_debug = format!("{admin:?}");
        let creator_debug = format!("{creator:?}");

        assert!(!admin_debug.contains("agentics_admin_secret"));
        assert!(!creator_debug.contains("agentics_creator_secret"));
        assert!(admin_debug.contains("<redacted>"));
        assert!(creator_debug.contains("<redacted>"));
    }
}