@rig-c/parts 2.37.0 → 2.38.1

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/parts.ts CHANGED
@@ -592,7 +592,14 @@ function checkOwnership(raw: unknown, object: string, fail: (code: string, objec
592
592
  function orderedProvenance(p: PixelProvenance): PixelProvenance {
593
593
  return { unchanged_layer_px: p.unchanged_layer_px, hand_px: p.hand_px, automatic_px: p.automatic_px,
594
594
  edits: p.edits.map(e => ({ edit: e.edit, px: e.px })), automatic: p.automatic.map(e => ({ stage: e.stage, px: e.px })),
595
- ...(p.depth === undefined ? {} : { depth: { kind: p.depth.kind, values: p.depth.values.map(v => ({ group: v.group, ...(v.bone === undefined ? {} : { bone: v.bone }), value: v.value })) } }) };
595
+ ...(p.depth === undefined ? {} : { depth: { kind: p.depth.kind, values: p.depth.values.map(v => ({ group: v.group, ...(v.bone === undefined ? {} : { bone: v.bone }), value: v.value })) } }),
596
+ ...(p.edited_part === undefined ? {} : { edited_part: {
597
+ edit: p.edited_part.edit, dir: p.edited_part.dir,
598
+ receipt: { part: p.edited_part.receipt.part, file: p.edited_part.receipt.file,
599
+ sha256: p.edited_part.receipt.sha256, from_sha256: p.edited_part.receipt.from_sha256, editor: p.edited_part.receipt.editor },
600
+ changed_px: p.edited_part.changed_px, changed_counted_px: p.edited_part.changed_counted_px,
601
+ changed_excluded_px: p.edited_part.changed_excluded_px, removed_opaque_px: p.edited_part.removed_opaque_px,
602
+ } }) };
596
603
  }
597
604
 
598
605
  function orderedTakenSource(source: TakenLayerSource): TakenLayerSource {
@@ -783,13 +790,14 @@ function show(v: unknown): string {
783
790
  }
784
791
 
785
792
  /** The same optional measurement contract for base and alternate records. */
786
- function checkProvenance(raw: unknown, opaque: unknown, object: string, fail: (code: string, object: string, detail: string) => void, hasScalar = false): void {
793
+ function checkProvenance(raw: unknown, opaque: unknown, object: string, fail: (code: string, object: string, detail: string) => void, hasScalar = false, identity?: { name: unknown; w: unknown; h: unknown }): void {
787
794
  const v = raw as PixelProvenance | null;
788
795
  const count = (n: unknown): boolean => Number.isSafeInteger(n) && (n as number) >= 0;
789
796
  const edit = (name: string): boolean => /^(cuts\[\d+\]|patches\[\d+\])$/.test(name) ||
797
+ (/^edited_parts\[\d+\]$/.test(name) && name === v?.edited_part?.edit) ||
790
798
  (/^(full|head):author:/.test(name) && readFrom(name) !== null);
791
799
  const shape = v !== null && typeof v === 'object' && !Array.isArray(v) &&
792
- Object.keys(v).filter(k => k !== 'depth').sort().join(',') === 'automatic,automatic_px,edits,hand_px,unchanged_layer_px' &&
800
+ Object.keys(v).filter(k => k !== 'depth' && k !== 'edited_part').sort().join(',') === 'automatic,automatic_px,edits,hand_px,unchanged_layer_px' &&
793
801
  [v.unchanged_layer_px, v.hand_px, v.automatic_px].every(count) && Array.isArray(v.edits) && Array.isArray(v.automatic) &&
794
802
  v.edits.every(e => e !== null && typeof e === 'object' && Object.keys(e).sort().join(',') === 'edit,px' && typeof e.edit === 'string' && edit(e.edit) && count(e.px) && e.px > 0) &&
795
803
  v.automatic.every(e => e !== null && typeof e === 'object' && Object.keys(e).sort().join(',') === 'px,stage' && typeof e.stage === 'string' && Object.hasOwn(AUTOMATIC_WRITERS, e.stage) && count(e.px) && e.px > 0) &&
@@ -804,6 +812,28 @@ function checkProvenance(raw: unknown, opaque: unknown, object: string, fail: (c
804
812
  const hand = p.edits.reduce((n, e) => n + e.px, 0), automatic = p.automatic.reduce((n, e) => n + e.px, 0);
805
813
  fail('PARTS_COUNTS_ADD_UP', object, `unchanged_layer_px ${p.unchanged_layer_px} + hand_px ${p.hand_px} + automatic_px ${p.automatic_px} = ${total}; opaque_px ${opaque} is required; edit subtotal ${hand} (hand_px ${p.hand_px} required), stage subtotal ${automatic} (automatic_px ${p.automatic_px} required)`);
806
814
  }
815
+ if (v !== null && typeof v === 'object' && 'edited_part' in v) {
816
+ const e = v.edited_part;
817
+ const keys = 'changed_counted_px,changed_excluded_px,changed_px,dir,edit,receipt,removed_opaque_px';
818
+ const nonempty = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0;
819
+ const relativePath = (value: unknown): boolean => nonempty(value) && !/^(?:[\\/]|[A-Za-z]:[\\/])/.test(value);
820
+ const valid = e !== null && e !== undefined && typeof e === 'object' && !Array.isArray(e) && Object.keys(e).sort().join(',') === keys &&
821
+ typeof e.edit === 'string' && /^edited_parts\[\d+\]$/.test(e.edit) && relativePath(e.dir) &&
822
+ [e.changed_px, e.changed_counted_px, e.changed_excluded_px, e.removed_opaque_px].every(count) &&
823
+ e.receipt !== null && typeof e.receipt === 'object' && !Array.isArray(e.receipt) &&
824
+ Object.keys(e.receipt).sort().join(',') === 'editor,file,from_sha256,part,sha256' &&
825
+ nonempty(e.receipt.part) && e.receipt.part === identity?.name && relativePath(e.receipt.file) && nonempty(e.receipt.editor) &&
826
+ [e.receipt.sha256, e.receipt.from_sha256].every(s => typeof s === 'string' && /^[a-f0-9]{64}$/.test(s));
827
+ if (!valid) fail('PARTS_FIELD_TYPE', object + ' edited_part', 'requires edited_parts[i], a config-relative dir, a receipt with the enclosing part name, dir-relative file, two lowercase sha256 hashes and editor, and four nonnegative safe integer change counts');
828
+ else {
829
+ const area = typeof identity?.w === 'number' && typeof identity.h === 'number' ? identity.w * identity.h : NaN;
830
+ if (!Number.isSafeInteger(area) || !count(opaque) || e.changed_px !== e.changed_counted_px + e.changed_excluded_px ||
831
+ e.changed_px > area || e.changed_counted_px > (opaque as number) || e.changed_excluded_px > area - (opaque as number) ||
832
+ e.removed_opaque_px > e.changed_excluded_px) {
833
+ fail('PARTS_COUNTS_ADD_UP', object + ' edited_part', `changed_px ${e.changed_px} must equal counted ${e.changed_counted_px} + excluded ${e.changed_excluded_px}; counted must fit opaque_px ${show(opaque)}, excluded must fit the remaining box pixels (area ${area}), and removed_opaque_px ${e.removed_opaque_px} must be at most changed_excluded_px`);
834
+ }
835
+ }
836
+ }
807
837
  if (v !== null && typeof v === 'object' && 'depth' in v) {
808
838
  const d = v.depth;
809
839
  const valid = !hasScalar && d !== null && typeof d === 'object' && !Array.isArray(d) &&
@@ -890,7 +920,7 @@ function checkParts(raw: unknown, path: string): PartsFile {
890
920
  if (k in r && !(Number.isInteger(r[k]) && (r[k] as number) >= 0)) fail('PARTS_FIELD_TYPE', `${label} field "${k}"`, `is ${show(r[k])}; a non-negative integer is required`);
891
921
  }
892
922
  const count = (k: string): number | null => (Number.isInteger(r[k]) && (r[k] as number) >= 0 ? (r[k] as number) : null);
893
- if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, label + ' provenance', fail, 'depth' in r);
923
+ if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, label + ' provenance', fail, 'depth' in r, { name: r.name, w: r.w, h: r.h });
894
924
  checkPaintingExtensionCount(r, label, fail);
895
925
  const [op, vis, occ, vnp] = ['opaque_px', 'visible_px', 'occluded_px', 'visible_not_projected_px'].map(count);
896
926
  if (held.length === VISIBILITY_KEYS.length && op !== null && vis !== null && occ !== null && vnp !== null) {
@@ -1126,7 +1156,7 @@ function checkParts(raw: unknown, path: string): PartsFile {
1126
1156
  if (!isPartName(r.name)) fail('PARTS_FIELD_TYPE', `${recordAt} field "name"`, `is ${show(r.name)}; a file-name-safe part name is required`);
1127
1157
  if ('depth' in r) problems.push(...partDepthProblems(r.depth, `${recordAt} depth`));
1128
1158
  const part817 = typeof r.name === 'string' ? `part "${r.name}"` : `records[${j}]`;
1129
- if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, `${at} ${part817} provenance`, fail, 'depth' in r);
1159
+ if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, `${at} ${part817} provenance`, fail, 'depth' in r, { name: r.name, w: r.w, h: r.h });
1130
1160
  checkPaintingExtensionCount(r, `${at} ${part817}`, fail);
1131
1161
  const l = r[LIMB_KEY];
1132
1162
  if (typeof l !== 'object' || l === null || Array.isArray(l) || !('cap' in l)) return;
package/src/provenance.ts CHANGED
@@ -3,6 +3,7 @@ import { OPAQUE_ALPHA_ABOVE } from './layers.ts';
3
3
  import type { PartRecord } from './parts.ts';
4
4
  import type { AuthoredUnitDepth } from './authoredunit.ts';
5
5
  import { crop, type Raster } from './raster/index.ts';
6
+ import type { EditedPartProvenance } from './editedparts.ts';
6
7
 
7
8
  /** Negative IDs distinguish automatic writes from positive, config-indexed hand edits. */
8
9
  export const AUTOMATIC_WRITERS = {
@@ -16,6 +17,8 @@ export const EXCLUDED_PIXEL = -2147483648;
16
17
  export const NOT_OVERRIDDEN_PIXEL = -2147483647;
17
18
  export const STAGE_MAP_FILE = 'stage-map.json';
18
19
  export interface PartStageMap {
20
+ /** Whole-file receipt, independent of the final-opaque writer partition. */
21
+ edited_part?: EditedPartProvenance;
19
22
  name: string;
20
23
  x: number;
21
24
  y: number;
@@ -38,6 +41,8 @@ export interface StageMap {
38
41
  }
39
42
 
40
43
  export interface PixelProvenance {
44
+ /** Receipt covers unchanged and transparent pixels as well as changed ones. */
45
+ edited_part?: EditedPartProvenance;
41
46
  /** Scalar transport omitted only for fully explicit unit consumers (#877). */
42
47
  depth?: AuthoredUnitDepth;
43
48
  unchanged_layer_px: number;