rig-review 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/job-act.ts CHANGED
@@ -1,12 +1,21 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import {
3
- JOB_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, jobStillByteLimit, JOB_ENABLED_ACT_KINDS, JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RESERVED_ACT_KINDS,
4
- type JobAct, type JobActValues, type JobPendingAsk, type JobReviewerBinding,
3
+ JOB_ACT_SCHEMA, JOB_RECORD_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, jobStillByteLimit, JOB_ENABLED_ACT_KINDS, JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RESERVED_ACT_KINDS,
4
+ type ActV3, type JobAct, type JobActValues, type JobPendingAsk, type JobReviewerBinding,
5
5
  type JobSourceBinding, type JobSubject, type JobUISubmission, type JobRigCapture, type JobRigStill,
6
+ type PendingAskV3, type RecordAct, type RecordSource, type RecordSubject, type SourceV3,
6
7
  } from './job-contract.ts';
7
- import { jobHash, jobId, jobInteger, jobObject, requestDigest } from './job-api.ts';
8
+ import {
9
+ isRecordSource, jobHash, jobId, jobInteger, jobObject,
10
+ parseRecordAct, parseRecordSource, requestDigest, validateRecordAskSubject,
11
+ } from './job-api.ts';
8
12
  import { validatePicks } from './interview-picks.ts';
9
- import type { JobRetainedSource } from './job-source.ts';
13
+ import {
14
+ isRetainedRecordSource,
15
+ type JobRetainedRecordSource,
16
+ type JobRetainedSource,
17
+ type JobRetainedSourceV3,
18
+ } from './job-source.ts';
10
19
  import { JobError } from './job-store.ts';
11
20
  import { checkedPath, readRegular } from './interview-project.ts';
12
21
  import { prepareJobImageSet } from './job-image-set.ts';
@@ -26,7 +35,56 @@ function requirePlan(source: JobSourceBinding): void {
26
35
  if (!source.plan?.steps.length) throw new JobError('JOB_ASK_INVALID', 'Published plan step identities are required');
27
36
  }
28
37
 
29
- export function bindJobAsk(subject: JobSubject, retained: JobRetainedSource): JobSourceBinding {
38
+ export function bindJobRecordAsk(subject: unknown, retained: JobRetainedRecordSource): RecordSource {
39
+ const source = structuredClone(parseRecordSource(retained.source));
40
+ const parsedSubject = validateRecordAskSubject(subject, source);
41
+ if (
42
+ requestDigest(retained.subjects) !== requestDigest(source.subjects) ||
43
+ !retained.subjects.some(candidate => requestDigest(candidate) === requestDigest(parsedSubject))
44
+ ) {
45
+ throw new JobError('JOB_ASK_INVALID');
46
+ }
47
+ switch (parsedSubject.kind) {
48
+ case 'interview-picks': {
49
+ if (source.subject !== 'option-set') throw new JobError('JOB_ASK_INVALID');
50
+ const question = retained.questions.find(
51
+ item => item.editable && item.set.question === parsedSubject.questionId && item.set.id === parsedSubject.setId,
52
+ );
53
+ if (
54
+ !question ||
55
+ question.ref.path !== source.optionSetFile.path ||
56
+ question.ref.sha256 !== source.optionSet.sha256
57
+ ) {
58
+ throw new JobError('JOB_ASK_INVALID');
59
+ }
60
+ return source;
61
+ }
62
+ case 'checkpoint': {
63
+ if (
64
+ source.subject !== 'plan' ||
65
+ !source.plan.steps.length ||
66
+ !source.plan.steps.some(step => step.id === parsedSubject.checkpointId)
67
+ ) {
68
+ throw new JobError('JOB_ASK_INVALID');
69
+ }
70
+ return source;
71
+ }
72
+ case 'signoff': {
73
+ if (source.subject !== 'plan' || !source.plan.steps.length) {
74
+ throw new JobError('JOB_ASK_INVALID');
75
+ }
76
+ return source;
77
+ }
78
+ }
79
+ }
80
+
81
+ export function bindJobAsk(subject: JobSubject, retained: JobRetainedSource): JobSourceBinding;
82
+ export function bindJobAsk(subject: RecordSubject | JobSubject, retained: JobRetainedRecordSource): RecordSource;
83
+ export function bindJobAsk(subject: JobSubject | RecordSubject, retained: JobRetainedSourceV3): SourceV3;
84
+ export function bindJobAsk(subject: JobSubject | RecordSubject, retained: JobRetainedSourceV3): SourceV3 {
85
+ if (isRetainedRecordSource(retained)) {
86
+ return bindJobRecordAsk(subject, retained);
87
+ }
30
88
  const source = structuredClone(retained.source);
31
89
  if (source.subject === 'image-set') {
32
90
  if (subject.kind !== 'review-verdict' || subject.stage !== 'image-review') throw new JobError('JOB_ASK_INVALID');
@@ -47,7 +105,7 @@ export function bindJobAsk(subject: JobSubject, retained: JobRetainedSource): Jo
47
105
  }
48
106
  case 'checkpoint': {
49
107
  requirePlan(source);
50
- const stepId = subject.planStepId ?? subject.checkpointId;
108
+ const stepId = ('planStepId' in subject ? subject.planStepId : undefined) ?? subject.checkpointId;
51
109
  if (!declared || !source.plan!.steps.some(step => step.id === stepId)) throw new JobError('JOB_ASK_INVALID');
52
110
  return source;
53
111
  }
@@ -123,6 +181,207 @@ export function prepareJobStill(input: JobUISubmission, ask: JobPendingAsk, reta
123
181
  return {still, bytes, binding: binding as unknown as JobRigCapture['binding']};
124
182
  }
125
183
 
184
+ function actObject<Key extends string>(value: unknown, allowed: readonly Key[]): Record<Key, unknown> {
185
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
186
+ throw new JobError('JOB_ACT_INVALID');
187
+ }
188
+ for (const key of Object.keys(value)) {
189
+ if (!allowed.includes(key as Key)) throw new JobError('JOB_ACT_INVALID');
190
+ }
191
+ return value as Record<Key, unknown>;
192
+ }
193
+
194
+ function actIdField(value: unknown): string {
195
+ try {
196
+ return jobId(value);
197
+ } catch {
198
+ throw new JobError('JOB_ACT_INVALID');
199
+ }
200
+ }
201
+
202
+ export function prepareJobRecordAct(options: {
203
+ readonly jobId: string;
204
+ readonly submission: JobUISubmission;
205
+ readonly ask: PendingAskV3;
206
+ readonly retained: JobRetainedRecordSource;
207
+ readonly reviewer: JobReviewerBinding;
208
+ readonly still?: unknown;
209
+ readonly actId?: string;
210
+ readonly at?: string;
211
+ }): RecordAct {
212
+ const { submission, ask, retained, reviewer } = options;
213
+ if (!submission || typeof submission !== 'object' || Array.isArray(submission)) {
214
+ throw new JobError('JOB_REQUEST_INVALID');
215
+ }
216
+ const rawSubmission = submission as unknown as Record<string, unknown>;
217
+ if (
218
+ options.still !== undefined ||
219
+ rawSubmission.capture !== undefined ||
220
+ 'authority' in rawSubmission ||
221
+ 'source' in rawSubmission
222
+ ) {
223
+ throw new JobError('JOB_REQUEST_INVALID');
224
+ }
225
+ if ((JOB_RESERVED_ACT_KINDS as readonly unknown[]).includes(rawSubmission.kind)) {
226
+ throw new JobError('JOB_ACT_KIND_DISABLED');
227
+ }
228
+ if (ask.status !== 'pending') throw new JobError('JOB_ASK_SUPERSEDED');
229
+ if (!isRecordSource(ask.source)) throw new JobError('JOB_ACT_SOURCE_STALE');
230
+ let askSubject: RecordSubject;
231
+ try {
232
+ askSubject = validateRecordAskSubject(ask.subject, ask.source);
233
+ } catch {
234
+ throw new JobError('JOB_ACT_SOURCE_STALE');
235
+ }
236
+ if (submission.kind !== askSubject.kind || submission.publicationId !== ask.publicationId) {
237
+ throw new JobError('JOB_ACT_SOURCE_STALE');
238
+ }
239
+ let boundSource: RecordSource;
240
+ try {
241
+ boundSource = bindJobRecordAsk(askSubject, retained);
242
+ } catch {
243
+ throw new JobError('JOB_ACT_SOURCE_STALE');
244
+ }
245
+ if (
246
+ boundSource.sourceHash !== ask.source.sourceHash ||
247
+ requestDigest(boundSource) !== requestDigest(ask.source)
248
+ ) {
249
+ throw new JobError('JOB_ACT_SOURCE_STALE');
250
+ }
251
+ if (
252
+ !reviewer ||
253
+ typeof reviewer !== 'object' ||
254
+ Array.isArray(reviewer) ||
255
+ Object.keys(reviewer).some(key => key !== 'id' && key !== 'role') ||
256
+ typeof reviewer.id !== 'string' ||
257
+ !reviewer.id ||
258
+ (reviewer.role !== 'owner' && reviewer.role !== 'delegate')
259
+ ) {
260
+ throw new JobError('JOB_AUTHORITY_REFUSED');
261
+ }
262
+ const at = options.at ?? new Date().toISOString();
263
+ const actId = options.actId ?? randomUUID();
264
+ const envelope = {
265
+ schema: JOB_RECORD_ACT_SCHEMA,
266
+ actId: actIdField(actId),
267
+ jobId: actIdField(options.jobId),
268
+ askId: actIdField(ask.askId),
269
+ publicationId: actIdField(ask.publicationId),
270
+ authority: {
271
+ actor: 'human' as const,
272
+ channel: 'review-ui' as const,
273
+ reviewer: { id: reviewer.id, role: reviewer.role },
274
+ },
275
+ at,
276
+ };
277
+
278
+ switch (askSubject.kind) {
279
+ case 'interview-picks': {
280
+ if (boundSource.subject !== 'option-set') throw new JobError('JOB_ACT_SOURCE_STALE');
281
+ const value = actObject(submission.value, ['questionId', 'selections', 'note']);
282
+ if (
283
+ !('questionId' in value) ||
284
+ !('selections' in value) ||
285
+ !('note' in value) ||
286
+ value.questionId !== askSubject.questionId ||
287
+ !Array.isArray(value.selections)
288
+ ) {
289
+ throw new JobError('JOB_ACT_INVALID');
290
+ }
291
+ const question = retained.questions.find(
292
+ item => item.editable && item.set.id === askSubject.setId && item.set.question === askSubject.questionId,
293
+ );
294
+ if (!question) throw new JobError('JOB_ACT_SOURCE_STALE');
295
+ const selections = value.selections.map(raw => {
296
+ const item = actObject(raw, ['itemId', 'optionId', 'otherText']);
297
+ if (!('itemId' in item) || !('optionId' in item) || !('otherText' in item)) {
298
+ throw new JobError('JOB_ACT_INVALID');
299
+ }
300
+ if (item.otherText !== null && typeof item.otherText !== 'string') {
301
+ throw new JobError('JOB_ACT_INVALID');
302
+ }
303
+ return {
304
+ itemId: actIdField(item.itemId),
305
+ optionId: actIdField(item.optionId),
306
+ otherText: item.otherText === null ? null : words(item.otherText),
307
+ };
308
+ });
309
+ const note = words(value.note);
310
+ try {
311
+ validatePicks(
312
+ {
313
+ schema: 'rig-review/interview-picks@1',
314
+ set: question.ref,
315
+ by: 'user',
316
+ at,
317
+ picks: selections.map(item => ({
318
+ ask: item.itemId,
319
+ option_id: item.optionId,
320
+ ...(item.otherText === null ? {} : { other_text: item.otherText }),
321
+ })),
322
+ note,
323
+ },
324
+ question,
325
+ );
326
+ } catch {
327
+ throw new JobError('JOB_ACT_INVALID');
328
+ }
329
+ return parseRecordAct({
330
+ ...envelope,
331
+ kind: 'interview-picks',
332
+ source: boundSource,
333
+ value: { questionId: askSubject.questionId, selections, note },
334
+ });
335
+ }
336
+ case 'checkpoint': {
337
+ if (boundSource.subject !== 'plan') throw new JobError('JOB_ACT_SOURCE_STALE');
338
+ const value = actObject(submission.value, ['checkpointId', 'outcome', 'words']);
339
+ if (
340
+ !('checkpointId' in value) ||
341
+ !('outcome' in value) ||
342
+ !('words' in value) ||
343
+ value.checkpointId !== askSubject.checkpointId
344
+ ) {
345
+ throw new JobError('JOB_ACT_INVALID');
346
+ }
347
+ return parseRecordAct({
348
+ ...envelope,
349
+ kind: 'checkpoint',
350
+ source: boundSource,
351
+ value: {
352
+ checkpointId: askSubject.checkpointId,
353
+ planStepId: askSubject.checkpointId,
354
+ outcome: choice(value.outcome, ['pass', 'fail'] as const),
355
+ words: words(value.words),
356
+ },
357
+ });
358
+ }
359
+ case 'signoff': {
360
+ if (boundSource.subject !== 'plan') throw new JobError('JOB_ACT_SOURCE_STALE');
361
+ const value = actObject(submission.value, ['stage', 'decision', 'words']);
362
+ if (
363
+ !('stage' in value) ||
364
+ !('decision' in value) ||
365
+ !('words' in value) ||
366
+ value.stage !== askSubject.stage ||
367
+ value.decision !== 'approved'
368
+ ) {
369
+ throw new JobError('JOB_ACT_INVALID');
370
+ }
371
+ return parseRecordAct({
372
+ ...envelope,
373
+ kind: 'signoff',
374
+ source: boundSource,
375
+ value: {
376
+ stage: askSubject.stage,
377
+ decision: 'approved',
378
+ words: words(value.words),
379
+ },
380
+ });
381
+ }
382
+ }
383
+ }
384
+
126
385
  /** Only the authenticated UI host invokes this adapter. All source and authority
127
386
  * fields are daemon-owned. Empty/whitespace strings remain exact human words.
128
387
  */
@@ -133,8 +392,57 @@ export function finalizeJobAct(options: {
133
392
  readonly retained: JobRetainedSource;
134
393
  readonly reviewer: JobReviewerBinding;
135
394
  readonly still?: JobRigStill;
136
- }): JobAct {
137
- const { submission, ask, retained, reviewer } = options;
395
+ }): JobAct;
396
+ export function finalizeJobAct(options: {
397
+ readonly jobId: string;
398
+ readonly submission: JobUISubmission;
399
+ readonly ask: PendingAskV3;
400
+ readonly retained: JobRetainedRecordSource;
401
+ readonly reviewer: JobReviewerBinding;
402
+ readonly still?: undefined;
403
+ readonly actId?: string;
404
+ readonly at?: string;
405
+ }): RecordAct;
406
+ export function finalizeJobAct(options: {
407
+ readonly jobId: string;
408
+ readonly submission: JobUISubmission;
409
+ readonly ask: JobPendingAsk | PendingAskV3;
410
+ readonly retained: JobRetainedSourceV3;
411
+ readonly reviewer: JobReviewerBinding;
412
+ readonly still?: JobRigStill;
413
+ readonly actId?: string;
414
+ readonly at?: string;
415
+ }): ActV3;
416
+ export function finalizeJobAct(options: {
417
+ readonly jobId: string;
418
+ readonly submission: JobUISubmission;
419
+ readonly ask: JobPendingAsk | PendingAskV3;
420
+ readonly retained: JobRetainedSourceV3;
421
+ readonly reviewer: JobReviewerBinding;
422
+ readonly still?: JobRigStill;
423
+ readonly actId?: string;
424
+ readonly at?: string;
425
+ }): ActV3 {
426
+ if (isRetainedRecordSource(options.retained)) {
427
+ return prepareJobRecordAct({
428
+ jobId: options.jobId,
429
+ submission: options.submission,
430
+ ask: options.ask,
431
+ retained: options.retained,
432
+ reviewer: options.reviewer,
433
+ still: options.still,
434
+ actId: options.actId,
435
+ at: options.at,
436
+ });
437
+ }
438
+ const { submission, ask, retained, reviewer } = options as {
439
+ readonly jobId: string;
440
+ readonly submission: JobUISubmission;
441
+ readonly ask: JobPendingAsk;
442
+ readonly retained: JobRetainedSource;
443
+ readonly reviewer: JobReviewerBinding;
444
+ readonly still?: JobRigStill;
445
+ };
138
446
  if (ask.status !== 'pending') throw new JobError('JOB_ASK_SUPERSEDED');
139
447
  if (submission.kind !== ask.subject.kind || submission.publicationId !== ask.publicationId) throw new JobError('JOB_ACT_SOURCE_STALE');
140
448
  const source = bindJobAsk(ask.subject, retained);