rig-review 4.9.0 → 4.10.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.
Files changed (43) hide show
  1. package/docs/DESIGN.md +4 -4
  2. package/docs/REVIEW_PACKAGE.md +90 -16
  3. package/docs/STABILITY.md +100 -3
  4. package/package.json +2 -2
  5. package/src/chest-landmarks.ts +9 -4
  6. package/src/compare-html.ts +1 -1
  7. package/src/feedback-export.ts +2 -2
  8. package/src/feedback-validation.ts +11 -6
  9. package/src/frame-clock.ts +20 -4
  10. package/src/job-act.ts +60 -71
  11. package/src/job-api.ts +286 -40
  12. package/src/job-cli.ts +3 -2
  13. package/src/job-contract.ts +92 -5
  14. package/src/job-front.ts +19 -8
  15. package/src/job-host.ts +7 -1
  16. package/src/job-session.ts +136 -26
  17. package/src/job-source.ts +109 -14
  18. package/src/job-store.ts +7 -4
  19. package/src/manifest.ts +59 -7
  20. package/src/rebuild.ts +1 -1
  21. package/src/session.ts +9 -4
  22. package/src/stage-comment.ts +2 -1
  23. package/web/catalog-en.ts +26 -6
  24. package/web/catalog-ko.ts +49 -29
  25. package/web/chest-editor.ts +236 -31
  26. package/web/compare.css +2 -2
  27. package/web/i18n.ts +1 -3
  28. package/web/inspection-persistence.ts +19 -6
  29. package/web/interview-panel.ts +13 -80
  30. package/web/job-rig-screen.ts +59 -33
  31. package/web/job-session.ts +175 -40
  32. package/web/label-placement.ts +25 -8
  33. package/web/main.ts +314 -89
  34. package/web/part-selection.ts +2 -1
  35. package/web/parts-viewport-provider.ts +19 -6
  36. package/web/rebuild-client.ts +2 -4
  37. package/web/rig-host.ts +5 -4
  38. package/web/rig-overlays.ts +3 -2
  39. package/web/rig-viewport-provider.ts +6 -1
  40. package/web/stage-comments.ts +22 -21
  41. package/web/styles.css +21 -49
  42. package/web/workspace-layout.ts +4 -4
  43. package/web/workspace-ui.ts +5 -3
package/src/job-api.ts CHANGED
@@ -5,16 +5,17 @@ import {
5
5
  JOB_ACT_SCHEMA, JOB_API_SCHEMA, JOB_API_V3_SCHEMA, JOB_ENABLED_ACT_KINDS, JOB_PARTS_ENUMERATOR_RANGE,
6
6
  JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RECORD_SIGNOFF_STAGES, JOB_RECORD_ACT_SCHEMA, JOB_RECORD_INSTANCE_SCHEMA, JOB_RECORD_SOURCE_SCHEMA,
7
7
  JOB_RESERVED_ACT_KINDS,
8
- JOB_MESH_EDITS_ACT_SCHEMA,
8
+ JOB_MESH_EDITS_ACT_SCHEMA, JOB_CHEST_LANDMARKS_ACT_SCHEMA,
9
9
  type AgentRequestV3, type FetchActRequestV3, type JobAgentRequest, type JobApiSchema,
10
10
  type JobCheckpointEvidence, type JobDisplayAcknowledgment, type JobErrorCode, type JobFetchActRequest, type JobFileIdentity,
11
- type JobHostPorts, type JobHumanAuthority, type JobInterviewDiagnostic, type JobInterviewDiagnostics,
11
+ type JobHostPorts, type JobHumanAuthority, type JobInterviewDiagnostic, type JobInterviewDiagnostics, type JobProgressRequestV3,
12
12
  type JobInterviewJson, type JobInterviewRecord, type JobPlanIdentity, type JobPublicationMode,
13
13
  type JobAct, type JobMeshEdit, type JobMeshEditsAct, type JobMeshEditValue,
14
- type JobPublishRequest, type JobResult, type JobSelector, type JobSha256, type JobSubject,
14
+ type JobChestLandmarkEdit, type JobChestLandmarksAct, type JobChestLandmarkValue, type JobPublishedChestLandmark,
15
+ type JobPublishRequest, type JobResult, type JobSelector, type JobSha256, type JobSourceBinding, type JobSubject,
15
16
  type OptionContext, type OptionSource, type PlanContext, type PlanSource, type ProjectRecord,
16
17
  type PublishRequestV3, type PublishResultV3, type RecordAct, type RecordFreeze,
17
- type RecordOptionResult, type RecordPartsIdentity, type RecordPicksSubject, type RecordPlanResult, type RecordPlanSubject,
18
+ type RecordOptionResult, type RecordOptionSetBinding, type RecordOptionSetRef, type RecordPartsIdentity, type RecordPicksSubject, type RecordPlanResult, type RecordPlanSubject,
18
19
  type RecordResult, type RecordSignoffStage, type RecordSource, type RecordSubject,
19
20
  jobCaptureBodyLimit, jobRawActMediaType,
20
21
  } from './job-contract.ts';
@@ -31,6 +32,7 @@ import {parseJobPartsIdentity} from './job-source.ts';
31
32
  import {parseControlPart, parseControlView} from './control.ts';
32
33
  import {canonicalJson} from './canonical-json.ts';
33
34
  import {MESH_EDIT_MAX_OPS, type Vec} from './mesh-edit.ts';
35
+ import {parseBustSpec} from './chest-landmarks.ts';
34
36
 
35
37
  export type {
36
38
  JobInterviewJson,
@@ -43,6 +45,7 @@ export const jobBodyLimit = 256 * 1024;
43
45
  export const jobWaitLimitMs = 30_000;
44
46
  /** Subjects one `ask-batch` request may list (#963). */
45
47
  export const jobAskBatchLimit = 64;
48
+ const jobProgressBodyLimit = 1024;
46
49
  const requestDeadlineMs = 10_000;
47
50
 
48
51
  export function jobHash(value: string | Uint8Array): string {
@@ -201,15 +204,18 @@ export function parseRecordFreeze(value: unknown): RecordFreeze {
201
204
  }
202
205
 
203
206
  export function parsePublishResultV3(value: unknown): PublishResultV3 {
204
- const item = jobObject(value, ['kind', 'path', 'selector', 'questionId', 'setId']);
207
+ const item = jobObject(value, ['kind', 'path', 'selector', 'questionId', 'setId', 'sets']);
205
208
  switch (item.kind) {
206
209
  case 'package':
207
210
  jobObject(item, ['kind', 'path', 'selector']);
208
211
  if (!('selector' in item) || item.selector === undefined) throw new JobError('JOB_REQUEST_INVALID');
209
212
  return { kind: 'package', path: jobPath(item.path), selector: parseJobSelector(item.selector) };
210
- case 'option-set':
211
- jobObject(item, ['kind', 'questionId', 'setId']);
212
- return { kind: 'option-set', questionId: jobId(item.questionId), setId: jobId(item.setId) };
213
+ case 'option-set': {
214
+ jobObject(item, ['kind', 'questionId', 'setId', 'sets']);
215
+ const first = { questionId: jobId(item.questionId), setId: jobId(item.setId) };
216
+ if (!('sets' in item)) return { kind: 'option-set', ...first };
217
+ return { kind: 'option-set', ...first, sets: parseOptionSetRefs(item.sets, first) };
218
+ }
213
219
  case 'plan':
214
220
  jobObject(item, ['kind']);
215
221
  return { kind: 'plan' };
@@ -217,6 +223,28 @@ export function parsePublishResultV3(value: unknown): PublishResultV3 {
217
223
  throw new JobError('JOB_REQUEST_INVALID');
218
224
  }
219
225
 
226
+ /** #1004: 2–64 distinct sets, `sets[0]` equal to the result's own `questionId`/`setId`. One set never uses `sets`,
227
+ * so a one-set publication keeps a single encoding. A set named twice refuses `JOB_OPTION_SET_DUPLICATE`. */
228
+ function parseOptionSetRefs(value: unknown, first: RecordOptionSetRef): readonly RecordOptionSetRef[] {
229
+ if (!Array.isArray(value) || value.length < 2 || value.length > jobAskBatchLimit) throw new JobError('JOB_REQUEST_INVALID');
230
+ const seen = new Set<string>();
231
+ const sets = value.map(raw => {
232
+ const row = exactObject(raw, ['questionId', 'setId'], 'JOB_REQUEST_INVALID');
233
+ const ref = { questionId: jobId(row.questionId), setId: jobId(row.setId) };
234
+ const key = JSON.stringify([ref.questionId, ref.setId]);
235
+ if (seen.has(key)) throw new JobError('JOB_OPTION_SET_DUPLICATE', `set ${ref.questionId}/${ref.setId} is named twice`);
236
+ seen.add(key);
237
+ return ref;
238
+ });
239
+ if (sets[0]!.questionId !== first.questionId || sets[0]!.setId !== first.setId) throw new JobError('JOB_REQUEST_INVALID');
240
+ return sets;
241
+ }
242
+
243
+ /** The sets a record option-set result publishes, in order. */
244
+ export function optionSetRefs(result: RecordOptionResult): readonly RecordOptionSetRef[] {
245
+ return result.sets ?? [{ questionId: result.questionId, setId: result.setId }];
246
+ }
247
+
220
248
  export function parseRecordResult(value: unknown): RecordResult {
221
249
  const result = parsePublishResultV3(value);
222
250
  if (result.kind === 'package') throw new JobError('JOB_REQUEST_INVALID');
@@ -283,7 +311,7 @@ export function parseJobRequest(
283
311
  ): JobAgentRequest | JobFetchActRequest | AgentRequestV3 | FetchActRequestV3 {
284
312
  const item = jobObject(value, [
285
313
  'schema', 'jobId', 'requestId', 'verb', 'expectedGeneration', 'idempotencyKey',
286
- 'result', 'freeze', 'publicationId', 'subject', 'subjects', 'after', 'waitMs', 'reason', 'actId',
314
+ 'result', 'freeze', 'publicationId', 'subject', 'subjects', 'after', 'waitMs', 'reason', 'actId', 'seq', 'phase', 'text', 'step',
287
315
  ]);
288
316
  const targetSchema = mode === 'package-only' || mode === 'record-and-package'
289
317
  ? jobApiSchemaForMode(mode)
@@ -299,6 +327,9 @@ export function parseJobRequest(
299
327
  jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'actId']);
300
328
  return { ...base, verb: 'fetch-act', actId: jobId(item.actId) };
301
329
  }
330
+ if (item.verb === 'progress') {
331
+ return { ...base, verb: 'progress', ...parseJobProgress(item) };
332
+ }
302
333
  const mutation = { ...base, expectedGeneration: jobInteger(item.expectedGeneration), idempotencyKey: jobId(item.idempotencyKey) };
303
334
  switch (item.verb) {
304
335
  case 'publish': {
@@ -392,6 +423,24 @@ export function parseJobRequest(
392
423
  throw new JobError('JOB_REQUEST_INVALID');
393
424
  }
394
425
 
426
+ function parseJobProgress(item: Record<string, unknown>): Omit<JobProgressRequestV3, 'schema' | 'jobId' | 'requestId' | 'verb'> {
427
+ jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'seq', 'phase', 'text', 'step']);
428
+ const seq = jobInteger(item.seq), phase = item.phase;
429
+ if (seq < 1 || (phase !== 'ingest' && phase !== 'build' && phase !== 'measure' && phase !== 'publish' && phase !== 'other')) {
430
+ throw new JobError('JOB_REQUEST_INVALID');
431
+ }
432
+ let text: string | undefined;
433
+ if (Object.hasOwn(item, 'text')) {
434
+ text = item.text === '' ? '' : jobText(item.text, 120);
435
+ if (/[\u0085\u2028\u2029]/u.test(text)) throw new JobError('JOB_REQUEST_INVALID');
436
+ }
437
+ if (!Object.hasOwn(item, 'step')) return { seq, phase, ...(text !== undefined ? { text } : {}) };
438
+ const step = jobObject(item.step, ['i', 'n']);
439
+ const n = jobInteger(step.n, 1000), i = jobInteger(step.i, n);
440
+ if (i < 1) throw new JobError('JOB_REQUEST_INVALID');
441
+ return { seq, phase, ...(text !== undefined ? { text } : {}), step: { i, n } };
442
+ }
443
+
395
444
  /** The single V3 `ask` subject parser; `ask-batch` parses each listed subject through it. */
396
445
  function parseAskSubjectV3(value: unknown): JobSubject | RecordSubject {
397
446
  const isReopenSubject =
@@ -644,21 +693,32 @@ function parsePlanSubjects(
644
693
  });
645
694
  }
646
695
 
696
+ /** One `interview-picks` subject per published set, in set order. Two subjects for one set refuse by name. */
647
697
  function parseOptionSubjects(
648
698
  rawSubjects: unknown,
649
699
  selection: RecordOptionResult,
650
700
  code: JobErrorCode,
651
- ): readonly [RecordPicksSubject] {
652
- if (!Array.isArray(rawSubjects) || rawSubjects.length !== 1) {
701
+ ): readonly RecordPicksSubject[] {
702
+ const sets = optionSetRefs(selection);
703
+ if (!Array.isArray(rawSubjects) || rawSubjects.length > jobAskBatchLimit) {
653
704
  throw new JobError(code);
654
705
  }
655
- const parsed = asCode(() => parseRecordSubject(rawSubjects[0]), code);
656
- if (parsed.kind !== 'interview-picks' ||
657
- parsed.questionId !== selection.questionId ||
658
- parsed.setId !== selection.setId) {
706
+ const seen = new Set<string>();
707
+ const subjects = rawSubjects.map(raw => {
708
+ const parsed = asCode(() => parseRecordSubject(raw), code);
709
+ if (parsed.kind !== 'interview-picks') throw new JobError(code);
710
+ const key = JSON.stringify([parsed.questionId, parsed.setId]);
711
+ if (seen.has(key)) {
712
+ throw new JobError('JOB_OPTION_SET_DUPLICATE', `two subjects for set ${parsed.questionId}/${parsed.setId}`);
713
+ }
714
+ seen.add(key);
715
+ return { kind: 'interview-picks' as const, questionId: parsed.questionId, setId: parsed.setId };
716
+ });
717
+ if (subjects.length !== sets.length ||
718
+ subjects.some((subject, index) => subject.questionId !== sets[index]!.questionId || subject.setId !== sets[index]!.setId)) {
659
719
  throw new JobError(code);
660
720
  }
661
- return [{ kind: 'interview-picks', questionId: parsed.questionId, setId: parsed.setId }];
721
+ return subjects;
662
722
  }
663
723
 
664
724
  export function parseRecordPartsIdentity(
@@ -679,7 +739,7 @@ type ParsedRecordPayload =
679
739
  readonly inputs: readonly JobFileIdentity[];
680
740
  readonly files: readonly JobFileIdentity[];
681
741
  readonly plan: null;
682
- readonly subjects: readonly [RecordPicksSubject];
742
+ readonly subjects: readonly RecordPicksSubject[];
683
743
  }
684
744
  | {
685
745
  readonly kind: 'plan';
@@ -761,7 +821,10 @@ export function assertEqualRecordContexts(
761
821
  if (ctx.plan === null) {
762
822
  const first = Array.isArray(ctx.subjects) ? ctx.subjects[0] : undefined;
763
823
  if (!first || first.kind !== 'interview-picks') throw new JobError('JOB_SOURCE_MISMATCH');
764
- return { kind: 'option-set', questionId: first.questionId, setId: first.setId };
824
+ if (ctx.subjects.length < 2) return { kind: 'option-set', questionId: first.questionId, setId: first.setId };
825
+ // Several sets: the subjects name them; parseOptionSubjects still refuses a set named twice.
826
+ return { kind: 'option-set', questionId: first.questionId, setId: first.setId,
827
+ sets: ctx.subjects.map(subject => ({ questionId: subject.questionId, setId: subject.setId })) };
765
828
  }
766
829
  return { kind: 'plan' };
767
830
  };
@@ -787,6 +850,7 @@ export interface RecordSourceDigestInput {
787
850
  readonly plan: JobPlanIdentity | null;
788
851
  readonly subjects: readonly RecordSubject[];
789
852
  readonly files: readonly JobFileIdentity[];
853
+ readonly optionSets?: readonly RecordOptionSetBinding[];
790
854
  }
791
855
 
792
856
  export function serializeRecordSourceDigestPayload(input: RecordSourceDigestInput): string {
@@ -806,6 +870,7 @@ export function serializeRecordSourceDigestPayload(input: RecordSourceDigestInpu
806
870
  'subjects',
807
871
  'files',
808
872
  'checkpointEvidence',
873
+ 'optionSets',
809
874
  ]),
810
875
  'JOB_SOURCE_MISMATCH',
811
876
  );
@@ -845,19 +910,24 @@ export function serializeRecordSourceDigestPayload(input: RecordSourceDigestInpu
845
910
  throw new JobError('JOB_SOURCE_MISMATCH');
846
911
  }
847
912
  const subjects = parseOptionSubjects(item.subjects, selection, 'JOB_SOURCE_MISMATCH');
913
+ const optionSets = parseOptionSetBindings(item.optionSets, selection, optionSetFile, byPath);
914
+ // One set keeps the original payload bytes; several sets add `sets` to the selection and `optionSets` last.
848
915
  return JSON.stringify({
849
916
  schema: JOB_RECORD_SOURCE_SCHEMA,
850
917
  record: { path: 'project.json', sha256: record.sha256 },
851
- selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId },
918
+ selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId,
919
+ ...(selection.sets ? { sets: selection.sets.map(set => ({ questionId: set.questionId, setId: set.setId })) } : {}) },
852
920
  inputs,
853
921
  optionSet,
854
922
  optionSetFile,
855
923
  plan: null,
856
924
  subjects,
857
925
  files,
926
+ ...(optionSets ? { optionSets } : {}),
858
927
  });
859
928
  }
860
929
 
930
+ if (item.optionSets !== undefined) throw new JobError('JOB_SOURCE_MISMATCH');
861
931
  if (item.optionSet !== null || item.optionSetFile !== null || item.plan === null) {
862
932
  throw new JobError('JOB_SOURCE_MISMATCH');
863
933
  }
@@ -877,6 +947,36 @@ export function serializeRecordSourceDigestPayload(input: RecordSourceDigestInpu
877
947
  });
878
948
  }
879
949
 
950
+ /** #1004: the per-set bindings of a several-set publication. Absent for one set; row 0 repeats the singular fields. */
951
+ function parseOptionSetBindings(
952
+ value: unknown,
953
+ selection: RecordOptionResult,
954
+ first: JobFileIdentity,
955
+ byPath: ReadonlyMap<string, string>,
956
+ ): readonly RecordOptionSetBinding[] | undefined {
957
+ if (!selection.sets) {
958
+ if (value !== undefined) throw new JobError('JOB_SOURCE_MISMATCH');
959
+ return undefined;
960
+ }
961
+ const sets = selection.sets;
962
+ if (!Array.isArray(value) || value.length !== sets.length) throw new JobError('JOB_SOURCE_MISMATCH');
963
+ const paths = new Set<string>();
964
+ return value.map((raw, index) => {
965
+ const row = exactObject(raw, ['questionId', 'setId', 'optionSet', 'optionSetFile'], 'JOB_SOURCE_MISMATCH');
966
+ const rawSet = exactObject(row.optionSet, ['id', 'sha256'], 'JOB_SOURCE_MISMATCH');
967
+ const file = parseFileRow(row.optionSetFile, 'JOB_SOURCE_MISMATCH');
968
+ const set = sets[index]!;
969
+ if (row.questionId !== set.questionId || row.setId !== set.setId || rawSet.id !== set.setId ||
970
+ rawSet.sha256 !== file.sha256 || file.path === 'project.json' || byPath.get(file.path) !== file.sha256 ||
971
+ paths.has(file.path) || (index === 0 && (file.path !== first.path || file.sha256 !== first.sha256))) {
972
+ throw new JobError('JOB_SOURCE_MISMATCH');
973
+ }
974
+ paths.add(file.path);
975
+ return { questionId: set.questionId, setId: set.setId, optionSet: { id: set.setId, sha256: file.sha256 },
976
+ optionSetFile: { path: file.path, sha256: file.sha256 } };
977
+ });
978
+ }
979
+
880
980
  export function recordSourceDigest(input: RecordSourceDigestInput): JobSha256 {
881
981
  return jobHash(serializeRecordSourceDigestPayload(input));
882
982
  }
@@ -898,11 +998,14 @@ function assembleRecordSource(
898
998
  selection: RecordResult,
899
999
  body: ParsedRecordPayload,
900
1000
  rawOptionSetFile?: unknown,
1001
+ rawOptionSets?: unknown,
901
1002
  ): OptionSource | PlanSource {
902
1003
  if (selection.kind === 'option-set') {
903
1004
  if (body.kind !== 'option-set' || !rawOptionSetFile) throw new JobError('JOB_SOURCE_MISMATCH');
904
1005
  const optionSetFile = parseFileRow(rawOptionSetFile, 'JOB_SOURCE_MISMATCH');
905
1006
  const optionSet = { id: selection.setId, sha256: optionSetFile.sha256 };
1007
+ const optionSets = selection.sets === undefined && rawOptionSets === undefined ? undefined
1008
+ : parseOptionSetBindings(rawOptionSets, selection, optionSetFile, new Map(body.files.map(file => [file.path, file.sha256])));
906
1009
  const sourceHash = recordSourceDigest({
907
1010
  record,
908
1011
  selection,
@@ -912,6 +1015,7 @@ function assembleRecordSource(
912
1015
  plan: null,
913
1016
  subjects: body.subjects,
914
1017
  files: body.files,
1018
+ ...(optionSets ? { optionSets } : {}),
915
1019
  });
916
1020
  return {
917
1021
  sourceHash,
@@ -920,13 +1024,16 @@ function assembleRecordSource(
920
1024
  inputs: body.inputs,
921
1025
  files: body.files,
922
1026
  subject: 'option-set',
923
- selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId },
1027
+ selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId,
1028
+ ...(selection.sets ? { sets: selection.sets.map(set => ({ questionId: set.questionId, setId: set.setId })) } : {}) },
924
1029
  optionSet,
925
1030
  optionSetFile,
926
1031
  plan: null,
927
1032
  subjects: body.subjects,
1033
+ ...(optionSets ? { optionSets } : {}),
928
1034
  };
929
1035
  }
1036
+ if (rawOptionSets !== undefined) throw new JobError('JOB_SOURCE_MISMATCH');
930
1037
  if (body.kind !== 'plan' || (rawOptionSetFile !== undefined && rawOptionSetFile !== null)) {
931
1038
  throw new JobError('JOB_SOURCE_MISMATCH');
932
1039
  }
@@ -962,6 +1069,8 @@ export function buildRecordSource(input: {
962
1069
  readonly selection: RecordOptionResult;
963
1070
  readonly context: OptionContext;
964
1071
  readonly optionSetFile: JobFileIdentity;
1072
+ /** #1004: one sealed file per published set, in set order; required exactly when `selection.sets` is present. */
1073
+ readonly optionSetFiles?: readonly JobFileIdentity[];
965
1074
  }): OptionSource;
966
1075
  export function buildRecordSource(input: {
967
1076
  readonly record: ProjectRecord;
@@ -974,6 +1083,7 @@ export function buildRecordSource(input: {
974
1083
  readonly selection: RecordResult;
975
1084
  readonly context: OptionContext | PlanContext;
976
1085
  readonly optionSetFile?: JobFileIdentity | null;
1086
+ readonly optionSetFiles?: readonly JobFileIdentity[];
977
1087
  }): OptionSource | PlanSource {
978
1088
  const record = parseProjectRecord(input.record, 'JOB_SOURCE_MISMATCH');
979
1089
  const selection = asCode(() => parseRecordResult(input.selection), 'JOB_SOURCE_MISMATCH');
@@ -995,7 +1105,13 @@ export function buildRecordSource(input: {
995
1105
  subjects: context.subjects,
996
1106
  ...(context.checkpointEvidence ? {checkpointEvidence: context.checkpointEvidence} : {}),
997
1107
  };
998
- return assembleRecordSource(record, selection, body, input.optionSetFile);
1108
+ const sets = selection.kind === 'option-set' ? selection.sets : undefined;
1109
+ if ((sets === undefined) !== (input.optionSetFiles === undefined)) throw new JobError('JOB_SOURCE_MISMATCH');
1110
+ const optionSets = sets && input.optionSetFiles!.length === sets.length
1111
+ ? sets.map((set, index) => ({ questionId: set.questionId, setId: set.setId,
1112
+ optionSet: { id: set.setId, sha256: input.optionSetFiles![index]!.sha256 }, optionSetFile: input.optionSetFiles![index]! }))
1113
+ : sets ? [] : undefined;
1114
+ return assembleRecordSource(record, selection, body, input.optionSetFile, optionSets);
999
1115
  }
1000
1116
 
1001
1117
  export function parseRecordSource(value: unknown): RecordSource {
@@ -1015,7 +1131,7 @@ export function parseRecordSource(value: unknown): RecordSource {
1015
1131
  'subjects',
1016
1132
  ],
1017
1133
  'JOB_SOURCE_MISMATCH',
1018
- ['checkpointEvidence'],
1134
+ ['checkpointEvidence', 'optionSets'],
1019
1135
  );
1020
1136
  const record = parseProjectRecord(item.record, 'JOB_SOURCE_MISMATCH');
1021
1137
  const projectHash = asCode(() => jobSha(item.projectHash), 'JOB_SOURCE_MISMATCH');
@@ -1034,7 +1150,7 @@ export function parseRecordSource(value: unknown): RecordSource {
1034
1150
  );
1035
1151
  if (selection.kind === 'option-set') {
1036
1152
  const rawOptionSet = exactObject(item.optionSet, ['id', 'sha256'], 'JOB_SOURCE_MISMATCH');
1037
- const built = assembleRecordSource(record, selection, body, item.optionSetFile) as OptionSource;
1153
+ const built = assembleRecordSource(record, selection, body, item.optionSetFile, item.optionSets) as OptionSource;
1038
1154
  if (rawOptionSet.id !== built.optionSet.id ||
1039
1155
  rawOptionSet.sha256 !== built.optionSet.sha256 ||
1040
1156
  sourceHash !== built.sourceHash) {
@@ -1042,7 +1158,7 @@ export function parseRecordSource(value: unknown): RecordSource {
1042
1158
  }
1043
1159
  return built;
1044
1160
  }
1045
- if (item.optionSet !== null || item.optionSetFile !== null) {
1161
+ if (item.optionSet !== null || item.optionSetFile !== null || 'optionSets' in item) {
1046
1162
  throw new JobError('JOB_SOURCE_MISMATCH');
1047
1163
  }
1048
1164
  const built = assembleRecordSource(record, selection, body, null) as PlanSource;
@@ -1060,10 +1176,8 @@ export function validateRecordAskSubject(subject: unknown, source: RecordSource)
1060
1176
  const checkedSource = parseRecordSource(source);
1061
1177
  const parsed = parseRecordSubject(subject);
1062
1178
  if (checkedSource.subject === 'option-set') {
1063
- const expected = checkedSource.subjects[0];
1064
1179
  if (parsed.kind !== 'interview-picks' ||
1065
- parsed.questionId !== expected.questionId ||
1066
- parsed.setId !== expected.setId) {
1180
+ !checkedSource.subjects.some(expected => parsed.questionId === expected.questionId && parsed.setId === expected.setId)) {
1067
1181
  throw new JobError('JOB_ASK_INVALID');
1068
1182
  }
1069
1183
  return parsed;
@@ -1102,6 +1216,52 @@ function parseActWords(value: unknown, code: JobErrorCode): string {
1102
1216
  return value;
1103
1217
  }
1104
1218
 
1219
+ /** #1004: an act's source names that act's own set. At K >= 2 the singular `selection` (`questionId`/`setId`, with
1220
+ * `sets` kept), `optionSet` and `optionSetFile` are re-pointed to the act's `optionSets` row; `subjects`, `optionSets`
1221
+ * and `sourceHash` stay the publication's. One set is returned unchanged. */
1222
+ export function recordActSource(source: RecordSource, set: RecordOptionSetRef): RecordSource {
1223
+ if (source.subject !== 'option-set' || !source.optionSets) return source;
1224
+ const row = source.optionSets.find(item => item.questionId === set.questionId && item.setId === set.setId);
1225
+ if (!row) throw new JobError('JOB_ACT_INVALID', `act set ${set.questionId}/${set.setId} is not published`);
1226
+ return {
1227
+ ...source,
1228
+ selection: { kind: 'option-set', questionId: row.questionId, setId: row.setId, sets: source.selection.sets! },
1229
+ optionSet: { id: row.optionSet.id, sha256: row.optionSet.sha256 },
1230
+ optionSetFile: { path: row.optionSetFile.path, sha256: row.optionSetFile.sha256 },
1231
+ };
1232
+ }
1233
+
1234
+ /** The inverse for a received act at K >= 2: check that the per-act singular fields agree with the act's own
1235
+ * `optionSets` row, then put set 0 back so the publication source (and its `sourceHash`) can be re-derived. */
1236
+ function publicationFormOfActSource(raw: unknown): { readonly raw: unknown; readonly set?: RecordOptionSetRef } {
1237
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return { raw };
1238
+ const item = raw as Record<string, unknown>;
1239
+ const selection = item.selection as Record<string, unknown> | null | undefined;
1240
+ const rows = item.optionSets;
1241
+ if (item.subject !== 'option-set' || !Array.isArray(rows) || rows.length < 2 || !selection || typeof selection !== 'object' ||
1242
+ typeof selection.questionId !== 'string' || typeof selection.setId !== 'string') return { raw };
1243
+ const set = { questionId: selection.questionId, setId: selection.setId };
1244
+ const named = `${set.questionId}/${set.setId}`;
1245
+ const row = rows.find(entry => entry && typeof entry === 'object' &&
1246
+ (entry as RecordOptionSetBinding).questionId === set.questionId && (entry as RecordOptionSetBinding).setId === set.setId) as
1247
+ RecordOptionSetBinding | undefined;
1248
+ if (!row) throw new JobError('JOB_ACT_INVALID', `act set ${named} is not published`);
1249
+ const optionSet = item.optionSet as { id?: unknown; sha256?: unknown } | null;
1250
+ const file = item.optionSetFile as { path?: unknown; sha256?: unknown } | null;
1251
+ if (!optionSet || optionSet.id !== set.setId || optionSet.sha256 !== row.optionSet?.sha256) {
1252
+ throw new JobError('JOB_ACT_INVALID', `act optionSet does not name set ${named}`);
1253
+ }
1254
+ if (!file || file.path !== row.optionSetFile?.path || file.sha256 !== row.optionSetFile?.sha256) {
1255
+ throw new JobError('JOB_ACT_INVALID', `act optionSetFile is not the file of set ${named}`);
1256
+ }
1257
+ const first = rows[0] as RecordOptionSetBinding;
1258
+ return {
1259
+ set,
1260
+ raw: { ...item, selection: { ...selection, questionId: first.questionId, setId: first.setId },
1261
+ optionSet: first.optionSet, optionSetFile: first.optionSetFile },
1262
+ };
1263
+ }
1264
+
1105
1265
  export function parseRecordAct(value: unknown): RecordAct {
1106
1266
  const item = exactObject(
1107
1267
  value,
@@ -1116,14 +1276,19 @@ export function parseRecordAct(value: unknown): RecordAct {
1116
1276
  const authority = parseHumanAuthority(item.authority, 'JOB_ACT_INVALID');
1117
1277
  const at = asCode(() => jobText(item.at, 128), 'JOB_ACT_INVALID');
1118
1278
  if (Number.isNaN(Date.parse(at))) throw new JobError('JOB_ACT_INVALID');
1119
- const source = asCode(() => parseRecordSource(item.source), 'JOB_ACT_INVALID');
1279
+ const received = publicationFormOfActSource(item.source);
1280
+ const published = asCode(() => parseRecordSource(received.raw), 'JOB_ACT_INVALID');
1281
+ const source = received.set ? recordActSource(published, received.set) : published;
1120
1282
 
1121
1283
  switch (item.kind) {
1122
1284
  case 'interview-picks': {
1123
1285
  if (source.subject !== 'option-set') throw new JobError('JOB_ACT_INVALID');
1124
1286
  const rawValue = exactObject(item.value, ['questionId', 'selections', 'note'], 'JOB_ACT_INVALID');
1125
1287
  const questionId = asCode(() => jobId(rawValue.questionId), 'JOB_ACT_INVALID');
1126
- if (questionId !== source.selection.questionId || !Array.isArray(rawValue.selections) ||
1288
+ if (received.set && questionId !== received.set.questionId) {
1289
+ throw new JobError('JOB_ACT_INVALID', `value.questionId ${questionId} is not the act set ${received.set.questionId}/${received.set.setId}`);
1290
+ }
1291
+ if (!optionSetRefs(source.selection).some(set => set.questionId === questionId) || !Array.isArray(rawValue.selections) ||
1127
1292
  rawValue.selections.length > maxRecordFiles) {
1128
1293
  throw new JobError('JOB_ACT_INVALID');
1129
1294
  }
@@ -1425,6 +1590,10 @@ function commentsSource(raw: unknown): Exclude<SourceV3, OptionSource> {
1425
1590
  return structuredClone(item) as unknown as Exclude<SourceV3, OptionSource>;
1426
1591
  }
1427
1592
 
1593
+ export function isRigDraftVerdictSource(source: JobSourceBinding): boolean {
1594
+ return source.selector?.kind === 'draft' && !!source.model && !!source.projectHash && source.direction === null;
1595
+ }
1596
+
1428
1597
  /** Public @6 decoder. The UI finalizers write checkpoint, parts, rig/post verdict and signoff acts at @6 (#713). */
1429
1598
  export function parseCommentsAct(raw: unknown): JobCommentsAct {
1430
1599
  return decodeCommentsAct(raw, JOB_COMMENTS_ACT_SCHEMA) as JobCommentsAct;
@@ -1440,6 +1609,10 @@ export function parseMeshEditsAct(raw: unknown): JobMeshEditsAct {
1440
1609
  return decodeCommentsAct(raw, JOB_MESH_EDITS_ACT_SCHEMA) as JobMeshEditsAct;
1441
1610
  }
1442
1611
 
1612
+ export function parseChestLandmarksAct(raw: unknown): JobChestLandmarksAct {
1613
+ return decodeCommentsAct(raw, JOB_CHEST_LANDMARKS_ACT_SCHEMA) as JobChestLandmarksAct;
1614
+ }
1615
+
1443
1616
  export function meshEditDigest(edit: JobMeshEditValue): JobSha256 {
1444
1617
  return jobHash(canonicalJson(edit));
1445
1618
  }
@@ -1527,6 +1700,63 @@ export function parseMeshEdits(raw: unknown): readonly JobMeshEdit[] {
1527
1700
  }, 'JOB_ACT_INVALID');
1528
1701
  }
1529
1702
 
1703
+ function parseChestLandmarkValue(raw: unknown, code: JobErrorCode): JobChestLandmarkValue;
1704
+ function parseChestLandmarkValue(raw: unknown, code: JobErrorCode, published: true): JobPublishedChestLandmark;
1705
+ function parseChestLandmarkValue(raw: unknown, code: JobErrorCode, published = false): JobChestLandmarkValue | JobPublishedChestLandmark {
1706
+ const value = exactObject(raw, ['slot', 'skin', 'attachment', 'basis', 'source', 'bust'], code);
1707
+ if (value.basis !== 'setup') throw new JobError(code);
1708
+ const source = exactObject(value.source, published ? ['hash', 'frame', 'configHash'] : ['hash', 'frame'], code);
1709
+ const frame = exactObject(source.frame, ['width', 'height'], code);
1710
+ const width = meshNumber(frame.width), height = meshNumber(frame.height);
1711
+ if (width <= 0 || height <= 0) throw new JobError(code);
1712
+ const bust = parseBustSpec(value.bust, 'bust', true);
1713
+ for (const point of [bust.apex_r, bust.apex_l, bust.cleavage]) {
1714
+ if (point && (point[0] < 0 || point[0] > width || point[1] < 0 || point[1] > height)) throw new JobError(code);
1715
+ }
1716
+ return {
1717
+ slot: jobText(value.slot), skin: jobText(value.skin), attachment: jobText(value.attachment), basis: 'setup',
1718
+ source: {hash: jobSha(source.hash), frame: {width, height}, ...(published ? {configHash: jobSha(source.configHash)} : {})}, bust,
1719
+ };
1720
+ }
1721
+
1722
+ export function chestLandmarkIdentity(edit: JobChestLandmarkValue): string {
1723
+ return JSON.stringify([edit.skin, edit.slot, edit.attachment]);
1724
+ }
1725
+
1726
+ /** The seed and act use the same bounds and identity order. The act wraps each row in a digest. */
1727
+ function readChestLandmarkRows<Row>(raw: unknown, code: JobErrorCode,
1728
+ read: (item: unknown) => {edit: JobChestLandmarkValue; row: Row}): readonly Row[] {
1729
+ return asCode(() => {
1730
+ if (!Array.isArray(raw) || raw.length < 1 || raw.length > 4) throw new JobError(code);
1731
+ const seen = new Set<string>();
1732
+ let previousOrder: string | undefined;
1733
+ return Array.from(raw, item => {
1734
+ const {edit, row} = read(item);
1735
+ const key = chestLandmarkIdentity(edit), order = [edit.skin, edit.slot, edit.attachment].join('/');
1736
+ if (seen.has(key) || (previousOrder !== undefined && order < previousOrder)) throw new JobError(code);
1737
+ seen.add(key);
1738
+ previousOrder = order;
1739
+ return row;
1740
+ });
1741
+ }, code);
1742
+ }
1743
+
1744
+ export function parsePublishedChestLandmarks(raw: unknown): readonly JobPublishedChestLandmark[] {
1745
+ return readChestLandmarkRows(raw, 'JOB_SOURCE_MISMATCH', item => {
1746
+ const edit = parseChestLandmarkValue(item, 'JOB_SOURCE_MISMATCH', true);
1747
+ return {edit, row: edit};
1748
+ });
1749
+ }
1750
+
1751
+ export function parseChestLandmarkEdits(raw: unknown): readonly JobChestLandmarkEdit[] {
1752
+ return readChestLandmarkRows(raw, 'JOB_ACT_INVALID', item => {
1753
+ const {edit: value, sha256} = commentsObject(item, ['edit', 'sha256']);
1754
+ const edit = parseChestLandmarkValue(value, 'JOB_ACT_INVALID');
1755
+ if (jobSha(sha256) !== requestDigest(value)) throw new JobError('JOB_ACT_INVALID');
1756
+ return {edit, row: structuredClone(item) as unknown as JobChestLandmarkEdit};
1757
+ });
1758
+ }
1759
+
1530
1760
  /** Write @7 when a comment carries context, otherwise keep the @6 envelope. */
1531
1761
  export function commentsActForEmission(act: JobCommentsAct | JobContextCommentsAct): JobCommentsAct | JobContextCommentsAct {
1532
1762
  const value = act.value as {comments: readonly JobContextComment[]};
@@ -1546,8 +1776,9 @@ export function meshEditsActForEmission(act: JobCommentsAct | (Extract<JobCommen
1546
1776
  return parseMeshEditsAct({...act, schema: JOB_MESH_EDITS_ACT_SCHEMA}) as Extract<JobAct, {schema: typeof JOB_MESH_EDITS_ACT_SCHEMA}>;
1547
1777
  }
1548
1778
 
1549
- function decodeCommentsAct(raw: unknown, schema: JobCommentsAct['schema'] | JobContextCommentsAct['schema'] | JobMeshEditsAct['schema']):
1550
- JobCommentsAct | JobContextCommentsAct | JobMeshEditsAct {
1779
+ type CommentsWireAct = JobCommentsAct | JobContextCommentsAct | JobMeshEditsAct | JobChestLandmarksAct;
1780
+
1781
+ function decodeCommentsAct(raw: unknown, schema: CommentsWireAct['schema']): CommentsWireAct {
1551
1782
  return asCode(() => {
1552
1783
  const item = commentsObject(raw, ['schema', 'actId', 'jobId', 'askId', 'publicationId', 'kind', 'authority', 'at', 'source', 'value'], ['still']);
1553
1784
  if (item.schema !== schema) throw new JobError('JOB_ACT_INVALID');
@@ -1562,8 +1793,10 @@ function decodeCommentsAct(raw: unknown, schema: JobCommentsAct['schema'] | JobC
1562
1793
  };
1563
1794
  const keys = typeof item.kind === 'string' && Object.hasOwn(fields, item.kind) ? fields[item.kind] : undefined;
1564
1795
  if (!keys) throw new JobError('JOB_ACT_INVALID');
1565
- const at8 = schema === JOB_MESH_EDITS_ACT_SCHEMA;
1566
- const value = commentsObject(item.value, keys, at8 && item.kind === 'review-verdict' ? ['meshEdits'] : []);
1796
+ const at9 = schema === JOB_CHEST_LANDMARKS_ACT_SCHEMA;
1797
+ const supportsMesh = schema === JOB_MESH_EDITS_ACT_SCHEMA || at9;
1798
+ const value = commentsObject(item.value, at9 ? [...keys, 'chestLandmarks'] : keys,
1799
+ supportsMesh && item.kind === 'review-verdict' ? ['meshEdits'] : []);
1567
1800
  const comments = schema === JOB_COMMENTS_ACT_SCHEMA ? parseComments(value.comments, source) : decodeComments(value.comments, source, true);
1568
1801
  if (comments.some(comment => comment.anchor && 'side' in comment.anchor)) throw new JobError('JOB_ACT_INVALID');
1569
1802
  if ((comments as readonly JobContextComment[]).some(comment => comment.context?.subject &&
@@ -1579,7 +1812,7 @@ function decodeCommentsAct(raw: unknown, schema: JobCommentsAct['schema'] | JobC
1579
1812
  if ((value.verdict === 'approved') !== (issues === 0)) throw new JobError('JOB_ACT_INVALID');
1580
1813
  } else if (item.kind === 'review-verdict') {
1581
1814
  if ('record' in source || typeof value.verdict !== 'string' || !['pending', 'approved', 'changes-requested'].includes(value.verdict)) throw new JobError('JOB_ACT_INVALID');
1582
- if (!source.model || (value.stage === 'rig-review' ? source.selector?.kind !== 'rig' : value.stage !== 'direction-review' || source.selector?.kind !== 'post' || !source.direction)) throw new JobError('JOB_ACT_INVALID');
1815
+ if (!source.model || (value.stage === 'rig-review' ? source.selector?.kind !== 'rig' && !isRigDraftVerdictSource(source) : value.stage !== 'direction-review' || source.selector?.kind !== 'post' || !source.direction)) throw new JobError('JOB_ACT_INVALID');
1583
1816
  } else {
1584
1817
  const stage = jobId(value.stage);
1585
1818
  if (item.kind === 'signoff' && value.decision !== 'approved') throw new JobError('JOB_ACT_INVALID');
@@ -1590,12 +1823,17 @@ function decodeCommentsAct(raw: unknown, schema: JobCommentsAct['schema'] | JobC
1590
1823
  if ('record' in source) validateRecordAskSubject(item.kind === 'checkpoint' ? {kind: item.kind, checkpointId: value.checkpointId} :
1591
1824
  {kind: item.kind, stage: value.stage}, source);
1592
1825
  const rig = item.kind === 'review-verdict' && value.stage === 'rig-review';
1826
+ let chestLandmarks: readonly JobChestLandmarkEdit[] | undefined;
1827
+ if (at9) {
1828
+ if (!rig || value.verdict !== 'changes-requested') throw new JobError('JOB_ACT_INVALID');
1829
+ chestLandmarks = parseChestLandmarkEdits(value.chestLandmarks);
1830
+ }
1593
1831
  let meshEdits: readonly JobMeshEdit[] | undefined;
1594
- if (at8 && Object.hasOwn(value, 'meshEdits')) {
1832
+ if (supportsMesh && Object.hasOwn(value, 'meshEdits')) {
1595
1833
  if (!rig || value.verdict !== 'changes-requested') throw new JobError('JOB_ACT_INVALID');
1596
1834
  meshEdits = parseMeshEdits(value.meshEdits);
1597
1835
  }
1598
- if (at8 && item.kind === 'review-verdict' && value.verdict === 'changes-requested' && !comments.length && !meshEdits) {
1836
+ if (supportsMesh && item.kind === 'review-verdict' && value.verdict === 'changes-requested' && !comments.length && !meshEdits && !chestLandmarks) {
1599
1837
  throw new JobError('JOB_ACT_INVALID');
1600
1838
  }
1601
1839
  if (rig) {
@@ -1604,12 +1842,12 @@ function decodeCommentsAct(raw: unknown, schema: JobCommentsAct['schema'] | JobC
1604
1842
  (still.frame === 'setup' ? 'animation' in still : typeof still.frame !== 'number' || !Number.isFinite(still.frame) ||
1605
1843
  still.frame < 0 || typeof still.animation !== 'string' || !still.animation)) throw new JobError('JOB_ACT_INVALID');
1606
1844
  } else if ('still' in item) throw new JobError('JOB_ACT_INVALID');
1607
- return {...structuredClone(item), source, value: {...value, comments, ...(meshEdits ? {meshEdits} : {})}} as unknown as
1608
- JobCommentsAct | JobContextCommentsAct | JobMeshEditsAct;
1845
+ return {...structuredClone(item), source, value: {...value, comments, ...(meshEdits ? {meshEdits} : {}),
1846
+ ...(chestLandmarks ? {chestLandmarks} : {})}} as unknown as CommentsWireAct;
1609
1847
  }, 'JOB_ACT_INVALID');
1610
1848
  }
1611
1849
 
1612
- export async function jobBody(request: Request, maximum = jobBodyLimit): Promise<unknown> {
1850
+ export async function jobBody(request: Request, maximum = jobBodyLimit, validateSize?: (value: unknown, size: number) => void): Promise<unknown> {
1613
1851
  if (!/^application\/json(?:\s*;|$)/i.test(request.headers.get('content-type') ?? '')) {
1614
1852
  throw new JobError('JOB_REQUEST_INVALID');
1615
1853
  }
@@ -1634,6 +1872,7 @@ export async function jobBody(request: Request, maximum = jobBodyLimit): Promise
1634
1872
  chunks.push(value);
1635
1873
  }
1636
1874
  const value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks)));
1875
+ validateSize?.(value, size);
1637
1876
  if (value?.value != null && Object.hasOwn(value.value, 'meshEdits') &&
1638
1877
  Buffer.byteLength(canonicalJson(value.value.meshEdits)) > jobBodyLimit) {
1639
1878
  throw new JobError('JOB_BODY_LIMIT');
@@ -1703,7 +1942,11 @@ export function createJobHostPorts(session: JobSession, origin: string): JobHost
1703
1942
  {headers: {...responseHeaders, 'Content-Type': 'image/png'}});
1704
1943
  }
1705
1944
  gate(request, base + '/api', false);
1706
- const body = await jobBody(request);
1945
+ const body = await jobBody(request, jobBodyLimit, (value, size) => {
1946
+ if (size > jobProgressBodyLimit && (value as { verb?: unknown } | null)?.verb === 'progress') {
1947
+ throw new JobError('JOB_REQUEST_INVALID');
1948
+ }
1949
+ });
1707
1950
  const mode: JobPublicationMode =
1708
1951
  session.publicationMode === 'record-and-package' &&
1709
1952
  body !== null &&
@@ -1738,6 +1981,9 @@ export function createJobHostPorts(session: JobSession, origin: string): JobHost
1738
1981
  }
1739
1982
  },
1740
1983
  subscribeJob: input => session.subscribe(input),
1984
+ ...(session.apiSchema === JOB_API_V3_SCHEMA ? {
1985
+ subscribeJobPage: (input: Parameters<JobHostPorts['subscribeJob']>[0]) => session.subscribe(input, true),
1986
+ } : {}),
1741
1987
  async readPublication(job, publication) {
1742
1988
  try {
1743
1989
  if (job !== session.jobId) throw new JobError('JOB_UNKNOWN');