1use crate::{
4 ActivityId, AgentIdentity, AssetId, Error, ErrorKind, JobId, MediaRoot, MetadataAssertion,
5 RepresentationId, RepresentationKind, Result, Timestamp, ToolIdentity,
6};
7
8pub const MAX_JOB_KIND_BYTES: usize = 128;
10pub const MAX_JOB_INPUTS: usize = 100_000;
12pub const MAX_JOB_DIAGNOSTIC_BYTES: usize = 4_096;
14pub const MAX_REGENERATION_PLANS: usize = 100_000;
16
17#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
19pub struct JobKind(String);
20
21impl JobKind {
22 pub fn new(value: impl Into<String>) -> Result<Self> {
29 let value = value.into();
30 let valid_bytes = value
31 .bytes()
32 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-' | b':'));
33 let valid_namespace = value
34 .split_once(':')
35 .is_some_and(|(namespace, local)| !namespace.is_empty() && !local.is_empty());
36 if value.len() > MAX_JOB_KIND_BYTES || !valid_bytes || !valid_namespace {
37 return Err(Error::new(
38 ErrorKind::InvalidArgument,
39 format!(
40 "job kind must be a namespaced identifier of at most {MAX_JOB_KIND_BYTES} ASCII bytes"
41 ),
42 ));
43 }
44 Ok(Self(value))
45 }
46
47 #[must_use]
49 pub fn as_str(&self) -> &str {
50 &self.0
51 }
52}
53
54#[derive(Clone, Debug, Eq, PartialEq)]
56pub struct RequestedJobOutput {
57 asset_id: AssetId,
58 representation_kind: RepresentationKind,
59 target_root: Option<String>,
60}
61
62impl RequestedJobOutput {
63 pub fn new(
70 asset_id: AssetId,
71 representation_kind: RepresentationKind,
72 target_root: Option<String>,
73 ) -> Result<Self> {
74 if let Some(name) = target_root.as_deref() {
75 MediaRoot::validate_name(name)?;
76 }
77 Ok(Self {
78 asset_id,
79 representation_kind,
80 target_root,
81 })
82 }
83
84 #[must_use]
86 pub const fn asset_id(&self) -> AssetId {
87 self.asset_id
88 }
89
90 #[must_use]
92 pub const fn representation_kind(&self) -> RepresentationKind {
93 self.representation_kind
94 }
95
96 #[must_use]
98 pub fn target_root(&self) -> Option<&str> {
99 self.target_root.as_deref()
100 }
101}
102
103#[derive(Clone, Debug, Eq, PartialEq)]
105pub struct JobClaim {
106 tool: ToolIdentity,
107 agent: Option<AgentIdentity>,
108 expires_at: Timestamp,
109}
110
111impl JobClaim {
112 #[must_use]
115 pub const fn new(
116 tool: ToolIdentity,
117 agent: Option<AgentIdentity>,
118 expires_at: Timestamp,
119 ) -> Self {
120 Self {
121 tool,
122 agent,
123 expires_at,
124 }
125 }
126
127 #[must_use]
129 pub const fn tool(&self) -> &ToolIdentity {
130 &self.tool
131 }
132
133 #[must_use]
135 pub const fn agent(&self) -> Option<&AgentIdentity> {
136 self.agent.as_ref()
137 }
138
139 #[must_use]
141 pub const fn expires_at(&self) -> Timestamp {
142 self.expires_at
143 }
144}
145
146#[derive(Clone, Copy, Debug, Eq, PartialEq)]
148pub struct JobCompletion {
149 activity_id: ActivityId,
150 representation_id: RepresentationId,
151}
152
153impl JobCompletion {
154 #[must_use]
156 pub const fn new(activity_id: ActivityId, representation_id: RepresentationId) -> Self {
157 Self {
158 activity_id,
159 representation_id,
160 }
161 }
162
163 #[must_use]
165 pub const fn activity_id(self) -> ActivityId {
166 self.activity_id
167 }
168
169 #[must_use]
171 pub const fn representation_id(self) -> RepresentationId {
172 self.representation_id
173 }
174}
175
176#[derive(Clone, Debug, Eq, PartialEq)]
178pub struct JobFailure {
179 diagnostic: String,
180}
181
182impl JobFailure {
183 pub fn new(diagnostic: impl Into<String>) -> Result<Self> {
190 let diagnostic = diagnostic.into();
191 if diagnostic.is_empty()
192 || diagnostic.len() > MAX_JOB_DIAGNOSTIC_BYTES
193 || diagnostic.contains('\0')
194 {
195 return Err(Error::new(
196 ErrorKind::InvalidArgument,
197 format!(
198 "job failure diagnostic must contain 1-{MAX_JOB_DIAGNOSTIC_BYTES} UTF-8 bytes without NUL"
199 ),
200 ));
201 }
202 Ok(Self { diagnostic })
203 }
204
205 #[must_use]
207 pub fn diagnostic(&self) -> &str {
208 &self.diagnostic
209 }
210}
211
212#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
214#[non_exhaustive]
215pub enum JobStateKind {
216 Requested,
218 Claimed,
220 Succeeded,
222 Failed,
224 Cancelled,
226}
227
228#[derive(Clone, Debug, Default, Eq, PartialEq)]
230pub struct JobQuery {
231 state: Option<JobStateKind>,
232 kind: Option<JobKind>,
233}
234
235impl JobQuery {
236 #[must_use]
238 pub const fn new(state: Option<JobStateKind>, kind: Option<JobKind>) -> Self {
239 Self { state, kind }
240 }
241
242 #[must_use]
244 pub const fn state(&self) -> Option<JobStateKind> {
245 self.state
246 }
247
248 #[must_use]
250 pub const fn kind(&self) -> Option<&JobKind> {
251 self.kind.as_ref()
252 }
253}
254
255#[derive(Clone, Debug, Eq, PartialEq)]
257#[non_exhaustive]
258pub enum JobState {
259 Requested,
261 Claimed(JobClaim),
263 Succeeded(JobCompletion),
265 Failed(JobFailure),
267 Cancelled,
269}
270
271impl JobState {
272 #[must_use]
274 pub const fn kind(&self) -> JobStateKind {
275 match self {
276 Self::Requested => JobStateKind::Requested,
277 Self::Claimed(_) => JobStateKind::Claimed,
278 Self::Succeeded(_) => JobStateKind::Succeeded,
279 Self::Failed(_) => JobStateKind::Failed,
280 Self::Cancelled => JobStateKind::Cancelled,
281 }
282 }
283}
284
285#[derive(Clone, Debug, Eq, PartialEq)]
287pub struct Job {
288 id: JobId,
289 kind: JobKind,
290 inputs: Vec<RepresentationId>,
291 requested_output: RequestedJobOutput,
292 state: JobState,
293}
294
295#[derive(Clone, Debug, Eq, PartialEq)]
297pub struct RegenerationJobPlan {
298 artifact_representation_id: RepresentationId,
299 job: Job,
300 parameters: Vec<MetadataAssertion>,
301}
302
303impl RegenerationJobPlan {
304 #[doc(hidden)]
306 #[must_use]
307 pub fn new(
308 artifact_representation_id: RepresentationId,
309 job: Job,
310 parameters: Vec<MetadataAssertion>,
311 ) -> Self {
312 Self {
313 artifact_representation_id,
314 job,
315 parameters,
316 }
317 }
318
319 #[must_use]
321 pub const fn artifact_representation_id(&self) -> RepresentationId {
322 self.artifact_representation_id
323 }
324
325 #[must_use]
327 pub const fn job(&self) -> &Job {
328 &self.job
329 }
330
331 #[must_use]
333 pub fn parameters(&self) -> &[MetadataAssertion] {
334 &self.parameters
335 }
336}
337
338impl Job {
339 pub fn new(
345 id: JobId,
346 kind: JobKind,
347 mut inputs: Vec<RepresentationId>,
348 requested_output: RequestedJobOutput,
349 ) -> Result<Self> {
350 if inputs.len() > MAX_JOB_INPUTS {
351 return Err(Error::new(
352 ErrorKind::InvalidArgument,
353 format!("job must have at most {MAX_JOB_INPUTS} inputs"),
354 ));
355 }
356 let supplied_count = inputs.len();
357 inputs.sort_unstable();
358 inputs.dedup();
359 if inputs.len() != supplied_count {
360 return Err(Error::new(
361 ErrorKind::InvalidArgument,
362 "job input representations must be distinct",
363 ));
364 }
365 Ok(Self {
366 id,
367 kind,
368 inputs,
369 requested_output,
370 state: JobState::Requested,
371 })
372 }
373
374 #[must_use]
376 pub const fn id(&self) -> JobId {
377 self.id
378 }
379
380 #[must_use]
382 pub const fn kind(&self) -> &JobKind {
383 &self.kind
384 }
385
386 #[must_use]
388 pub fn inputs(&self) -> &[RepresentationId] {
389 &self.inputs
390 }
391
392 #[must_use]
394 pub const fn requested_output(&self) -> &RequestedJobOutput {
395 &self.requested_output
396 }
397
398 #[must_use]
400 pub const fn state(&self) -> &JobState {
401 &self.state
402 }
403
404 #[doc(hidden)]
406 #[must_use]
407 pub fn with_state(mut self, state: JobState) -> Self {
408 self.state = state;
409 self
410 }
411}
412
413#[cfg(test)]
414mod tests {
415 use super::*;
416
417 fn output() -> RequestedJobOutput {
418 RequestedJobOutput::new(
419 AssetId::from_bytes([1; 16]),
420 RepresentationKind::Proxy,
421 Some("proxies".to_owned()),
422 )
423 .expect("valid output")
424 }
425
426 #[test]
427 fn job_kind_is_open_world_namespaced_text() {
428 assert!(JobKind::new("org.postproject:generate-proxy").is_ok());
429 assert!(JobKind::new("studio.example:custom_work").is_ok());
430 assert!(JobKind::new("not-namespaced").is_err());
431 }
432
433 #[test]
434 fn requested_jobs_canonicalize_and_reject_duplicate_inputs() {
435 let first = RepresentationId::from_bytes([1; 16]);
436 let second = RepresentationId::from_bytes([2; 16]);
437 let job = Job::new(
438 JobId::from_bytes([3; 16]),
439 JobKind::new("org.postproject:render").expect("valid kind"),
440 vec![second, first],
441 output(),
442 )
443 .expect("valid job");
444 assert_eq!(job.inputs(), [first, second]);
445 assert_eq!(job.state().kind(), JobStateKind::Requested);
446 assert!(
447 Job::new(
448 JobId::new(),
449 JobKind::new("org.postproject:render").expect("valid kind"),
450 vec![first, first],
451 output(),
452 )
453 .is_err()
454 );
455 }
456
457 #[test]
458 fn diagnostics_and_output_roots_are_bounded() {
459 assert!(JobFailure::new("").is_err());
460 assert!(JobFailure::new("failed to encode").is_ok());
461 assert!(
462 RequestedJobOutput::new(
463 AssetId::new(),
464 RepresentationKind::Proxy,
465 Some("../machine-path".to_owned()),
466 )
467 .is_err()
468 );
469 }
470}