@wildshard/shard-format 0.69.1 → 0.70.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.
@@ -1,5 +1,6 @@
1
1
  import { platformFileFamily } from './platformFileFamilies.ts';
2
2
  import { MAX_COOK_BYTES } from './cook-preparation.ts';
3
+ import { PREPARATION_SPECS, preparationSpecOf } from './preparation-spec.ts';
3
4
  import type { Ktx2DeviceCosts } from './ktx2Cost.ts';
4
5
  import { AdmissionLateSchema, lateLibraryRootsValid, type AdmissionLate } from './admissionLate.ts';
5
6
  import { AdmissionServerSchema, serverFilesExclusive, type AdmissionServer } from './admissionServer.ts';
@@ -15,9 +16,10 @@ import { ClientPlanRuntimeSchema } from './runtime.ts';
15
16
  import { RuntimeSpawnsSchema } from './runtimeSpawns.ts';
16
17
  import { RowsSchema } from './rows.ts';
17
18
  import { ShardHashSchema, ShardNaturalSchema, IdentitySchema, RequiresSchema, FilesSchema, type ShardIdentityInput, type ShardRequiresInput, type ShardReferencesInput, type ShardIdentity, type ShardRequires, type ShardReferences } from './shardSections.ts';
18
- import { LEGACY_LAYOUT } from './version.ts';
19
+ import { LEGACY_LAYOUT, writesPreparationMarker } from './version.ts';
19
20
  import { IndexFormatVersionSchema, LegacyIndexVersionSchema } from './index-format-version.ts';
20
21
  import type { IndexFormatVersion, LegacyIndexVersion } from './version.ts';
22
+ import type { PreparationSpec } from './preparation-spec.ts';
21
23
  import { isJsonData, isJsonDataWithFileSchemas } from './json.ts';
22
24
  import { clientOwnershipRules } from './clientOwnership.ts';
23
25
  import { PLATFORM_DATA_SECTIONS } from './platformDataSections.ts';
@@ -42,7 +44,12 @@ export const OutputIndexSchema: v.GenericSchema<OutputIndexRowsInput, OutputInde
42
44
  format: v.literal('wildshard.shardfile'),
43
45
  /** SHARD-COMPAT CP14 (FP25): the format version itself from 0.1.0.2; the legacy envelope carries the frozen layout marker
44
46
  * `2` here and its version in `version`. `checkIndexEnvelope` holds each envelope to its own fields. */
45
- formatVersion: IndexFormatVersionSchema, sdk: v.picklist(['0.2', '0.3']), version: LegacyIndexVersionSchema,
47
+ formatVersion: IndexFormatVersionSchema, version: LegacyIndexVersionSchema,
48
+ /** The legacy preparation marker (`'0.2'` built, `'0.3'` prepared) up to format 0.1.0.2; 0.1.0.3 drops it (PR4). */
49
+ sdk: v.exactOptional(v.picklist(['0.2', '0.3'])),
50
+ /** SHARD-COMPAT PR4 (J487): from format 0.1.0.3, the preparation spec version a prepared file was made to, owned by
51
+ * this package (`cook.json`'s `version`); absent on an unprepared file. It names no engine release. */
52
+ preparation: v.exactOptional(v.picklist(PREPARATION_SPECS)),
46
53
  identity: IdentitySchema, build: v.strictObject({ sourceHash: ShardHashSchema, validator: v.pipe(v.string(), v.minLength(1), v.maxLength(160)) }), requires: RequiresSchema,
47
54
  admission: v.strictObject({ budgets: ShardfileFieldSchemas.budgets, serverBudget: ShardfileFieldSchemas.serverBudget, authorCaps: ShardfileFieldSchemas.authorCaps, critical: ShardfileFieldSchemas.critical, library: ShardfileFieldSchemas.library, late: v.exactOptional(AdmissionLateSchema), server: v.exactOptional(AdmissionServerSchema), audioWaiver: v.exactOptional(AudioWaiverSchema) }),
48
55
  sections: v.pipe(v.array(section), v.maxLength(256)), schemas: emptySchemas,
@@ -83,9 +90,21 @@ function legacyIndexFields(index: OutputIndex): readonly string[] {
83
90
  index.runtime === null || !Object.hasOwn(index.runtime, 'trust') ? undefined : 'runtime.trust',
84
91
  ].filter((field): field is string => field !== undefined);
85
92
  }
93
+ /** PR4: 0.1.0.3 on records `preparation` and drops `sdk`; below it `sdk` is required and `preparation` unknown. */
94
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot output, only read
95
+ function checkPreparationMarker(index: OutputIndex): void {
96
+ const version = index.formatVersion === LEGACY_LAYOUT ? undefined : index.formatVersion;
97
+ if (version !== undefined && writesPreparationMarker(version)) {
98
+ if (index.sdk !== undefined) { throw new Error(`Format ${version} drops sdk; it records preparation (shard.json)`); }
99
+ return;
100
+ }
101
+ if (index.sdk === undefined) { throw new Error(`Format ${version ?? index.version ?? 'legacy'} needs sdk (shard.json)`); }
102
+ if (index.preparation !== undefined) { throw new Error(`Format ${version ?? index.version ?? 'legacy'} has no preparation field; it records sdk (shard.json)`); }
103
+ }
86
104
  /** Hold each envelope to its own fields: the legacy one keeps every field its readers require, 0.1.0.2 carries none. */
87
105
  // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot output, only read
88
106
  function checkIndexEnvelope(index: OutputIndex): void {
107
+ checkPreparationMarker(index);
89
108
  const carried = legacyIndexFields(index);
90
109
  if (index.formatVersion !== LEGACY_LAYOUT) {
91
110
  if (carried.length > 0) {
@@ -102,8 +121,9 @@ function checkIndexEnvelope(index: OutputIndex): void {
102
121
  function parseOutputIndexRows(input: unknown): OutputIndex {
103
122
  const index = v.parse(OutputIndexSchema, input);
104
123
  checkIndexEnvelope(index);
105
- if (index.sdk === '0.3' && index.cook === undefined) { throw new Error(`Missing cook preparation: run wildshard prepare <folder> --base <exact-preview-url> (${index.identity.slug})`); }
106
- if (index.sdk === '0.3' && index.cook !== undefined && index.cook.bytes > MAX_COOK_BYTES) { throw new Error('Cook preparation exceeds byte cap'); }
124
+ const prepared = preparationSpecOf(index) !== undefined;
125
+ if (prepared && index.cook === undefined) { throw new Error(`Missing cook preparation: run wildshard prepare <folder> --base <exact-preview-url> (${index.identity.slug})`); }
126
+ if (prepared && index.cook !== undefined && index.cook.bytes > MAX_COOK_BYTES) { throw new Error('Cook preparation exceeds byte cap'); }
107
127
  const paths = new Set(['shard.json', 'validation.json']), names = new Set<string>();
108
128
  if (index.ktx2Costs !== undefined) {
109
129
  const textures = new Map(index.files.filter(row=>row.kind==='ktx2' && row.commons!==true).map(row=>[row.hash,row]));
@@ -198,7 +218,8 @@ export function assembleOutputShardfile(input: unknown, sections: ReadonlyMap<st
198
218
  interface OutputIndexRowsInputFields {
199
219
  format: "wildshard.shardfile";
200
220
  formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
201
- sdk: "0.2" | "0.3";
221
+ sdk?: "0.2" | "0.3";
222
+ preparation?: PreparationSpec;
202
223
  version?: LegacyIndexVersion;
203
224
  identity: ShardIdentityInput;
204
225
  build: {
@@ -296,7 +317,8 @@ export type OutputIndexRowsInput = { [Key in keyof OutputIndexRowsInputFields]:
296
317
  interface OutputIndexRowsFields {
297
318
  format: "wildshard.shardfile";
298
319
  formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
299
- sdk: "0.2" | "0.3";
320
+ sdk?: "0.2" | "0.3";
321
+ preparation?: PreparationSpec;
300
322
  version?: LegacyIndexVersion;
301
323
  identity: ShardIdentity;
302
324
  build: {
@@ -0,0 +1,100 @@
1
+ /** The checks `requireCookPreparation` runs on a prepared file (SHARD-COMPAT PR4 and PR5, J487): the preparation
2
+ * marker against `cook.json`'s spec, the fence and the programs mount. */
3
+ import { isPreparationSpec, preparationSpecOf } from './preparation-spec.ts'
4
+ import { validationSourceBytes } from './validationReceipt.ts'
5
+
6
+ import type { AnyPrograms } from './cook-preparation.ts'
7
+ import type { OutputIndex } from './outputIndex.ts'
8
+
9
+ type Frozen<Value> = Value extends object
10
+ ? { readonly [Key in keyof Value]: Frozen<Value[Key]> }
11
+ : Value
12
+
13
+ /** Shared by admission and the cook. Legacy sdk 0.2 is an explicit migration boundary. */
14
+ function fenceValid(
15
+ index: Frozen<OutputIndex>,
16
+ lists: Readonly<{
17
+ fence: readonly string[]
18
+ mounts: readonly Readonly<{ hash: string }>[]
19
+ }>,
20
+ fix: string,
21
+ ): void {
22
+ const known = new Set<string>()
23
+ for (const file of index.files) {
24
+ known.add(file.hash)
25
+ }
26
+ for (const mount of lists.mounts) {
27
+ known.add(mount.hash)
28
+ }
29
+ for (const row of lists.fence) {
30
+ if (!known.has(row) && !index.requires.commons.includes(row)) {
31
+ throw new Error(`stale fence preparation: run ${fix}`)
32
+ }
33
+ }
34
+ }
35
+ function programMountValid(
36
+ index: Frozen<OutputIndex>,
37
+ cook: {
38
+ readonly programs?: AnyPrograms
39
+ readonly mounts: readonly Readonly<{ url: string; hash: string }>[]
40
+ },
41
+ digest: (bytes: Readonly<Uint8Array>) => string,
42
+ ): void {
43
+ const url = `/programs/${index.identity.slug}.json`
44
+ const mount = cook.mounts.find(
45
+ (row: Readonly<{ url: string; hash: string }>): boolean => row.url === url,
46
+ )
47
+ if (
48
+ cook.programs === undefined ||
49
+ mount?.hash !== digest(validationSourceBytes(cook.programs))
50
+ ) {
51
+ throw new Error(
52
+ `stale GLSL program mount: run wildshard prepare <folder> --base <exact-preview-url> (${index.identity.slug})`,
53
+ )
54
+ }
55
+ }
56
+ function failureOf(run: () => void): string | undefined {
57
+ try {
58
+ run()
59
+ return undefined
60
+ } catch (error: unknown) {
61
+ return error instanceof Error ? error.message : String(error)
62
+ }
63
+ }
64
+ const MISMATCH = 'mismatch'
65
+ /** Why the marker and `cook.json` disagree, or `undefined` when they agree. */
66
+ function markerRefusal(
67
+ spec: number | undefined,
68
+ prepared: boolean,
69
+ noMarker: boolean,
70
+ ): string | undefined {
71
+ if (spec === undefined) {
72
+ return prepared && noMarker
73
+ ? 'cook.json is prepared but shard.json names no preparation spec'
74
+ : undefined
75
+ }
76
+ return prepared ? MISMATCH : 'Missing cook preparation'
77
+ }
78
+ /** PR4: whether the file is prepared, holding `shard.json`'s spec and `cook.json`'s version to each other. */
79
+ function preparationMarked(
80
+ index: Readonly<{ sdk?: unknown; preparation?: unknown }>,
81
+ version: number | undefined,
82
+ fix: string,
83
+ ): boolean {
84
+ const spec = preparationSpecOf(index)
85
+ const prepared = isPreparationSpec(version)
86
+ const refusal =
87
+ spec !== undefined && version === spec
88
+ ? undefined
89
+ : markerRefusal(spec, prepared, index.sdk === undefined)
90
+ if (refusal !== undefined) {
91
+ throw new Error(
92
+ refusal === MISMATCH
93
+ ? `shard.json names preparation spec ${String(spec)}, cook.json is spec ${String(version)}: run ${fix}`
94
+ : `${refusal}: run ${fix}`,
95
+ )
96
+ }
97
+ return spec !== undefined || prepared
98
+ }
99
+
100
+ export { failureOf, fenceValid, preparationMarked, programMountValid }
@@ -0,0 +1,45 @@
1
+ /** The preparation spec version (SHARD-COMPAT PR4, J487): one number owned by this package, beside the format
2
+ * version. A prepared file records it in `shard.json` `preparation` (format 0.1.0.3 on) and as `cook.json`'s
3
+ * `version`; it names no engine release. Spec 3 still carries the engine renderer fingerprint and program build;
4
+ * spec 4 drops both. See `docs/preparation.md`. */
5
+
6
+ /** `cook.json` version 3: the legacy spec, bound to the engine renderer that recorded it. */
7
+ const LEGACY_PREPARATION_SPEC = 3
8
+ /** The spec a freshly prepared file is made to: no renderer fingerprint, programs with no engine build. */
9
+ const PREPARATION_SPEC = 4
10
+ /** Every preparation spec version a reader of this package accepts. */
11
+ const PREPARATION_SPECS: readonly [
12
+ typeof LEGACY_PREPARATION_SPEC,
13
+ typeof PREPARATION_SPEC,
14
+ ] = [LEGACY_PREPARATION_SPEC, PREPARATION_SPEC] as const
15
+ type PreparationSpec = (typeof PREPARATION_SPECS)[number]
16
+ /** The legacy `sdk` marker's prepared value, which always meant spec 3. */
17
+ const PREPARED_SDK = '0.3'
18
+
19
+ /** Whether `version` is a preparation spec this package reads (a prepared `cook.json`). */
20
+ function isPreparationSpec(version: unknown): version is PreparationSpec {
21
+ return PREPARATION_SPECS.some(
22
+ (spec: PreparationSpec): boolean => spec === version,
23
+ )
24
+ }
25
+
26
+ /** The preparation spec a `shard.json` records: `preparation` from format 0.1.0.3, the legacy `sdk: '0.3'` marker as
27
+ * spec 3 before it, `undefined` for an unprepared file. */
28
+ function preparationSpecOf(
29
+ index: Readonly<{ sdk?: unknown; preparation?: unknown }>,
30
+ ): PreparationSpec | undefined {
31
+ if (isPreparationSpec(index.preparation)) {
32
+ return index.preparation
33
+ }
34
+ return index.sdk === PREPARED_SDK ? LEGACY_PREPARATION_SPEC : undefined
35
+ }
36
+
37
+ export {
38
+ LEGACY_PREPARATION_SPEC,
39
+ PREPARATION_SPEC,
40
+ PREPARATION_SPECS,
41
+ PREPARED_SDK,
42
+ type PreparationSpec,
43
+ isPreparationSpec,
44
+ preparationSpecOf,
45
+ }
@@ -18,7 +18,9 @@ const measured = { path, hash: v.pipe(v.string(), v.regex(/^[a-f0-9]{64}$/u)),
18
18
  bytes: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(Number.MAX_SAFE_INTEGER)) };
19
19
  const lineageSchemas = v.exactOptional(v.pipe(v.array(v.strictObject(measured)), v.maxLength(128)));
20
20
  // SHARD-COMPAT CP14: either envelope; 0.1.0.2 names its format in formatVersion and drops version and schemas.
21
- const inventory = v.object({ format: v.literal('wildshard.shardfile'), formatVersion: IndexFormatVersionSchema, sdk: v.picklist(['0.2', '0.3']),
21
+ const inventory = v.object({ format: v.literal('wildshard.shardfile'), formatVersion: IndexFormatVersionSchema,
22
+ // SHARD-COMPAT PR4: 0.1.0.3 drops `sdk` for `preparation`; the lineage reader needs neither.
23
+ sdk: v.exactOptional(v.picklist(['0.2', '0.3'])), preparation: v.exactOptional(v.unknown()),
22
24
  version: LegacyIndexVersionSchema, identity,
23
25
  sections: v.pipe(v.array(v.strictObject({ ...measured, name: v.pipe(v.string(), v.regex(/^[a-z][a-zA-Z0-9-]*\/[a-zA-Z][a-zA-Z0-9_-]*$/u)),
24
26
  schema: v.pipe(v.string(), v.maxLength(240)) })), v.maxLength(256)),
@@ -1,9 +1,11 @@
1
+ import { LEGACY_PREPARATION_SPEC, PREPARED_SDK } from './preparation-spec.ts'
2
+
1
3
  /** Canonical format revision, separate from the npm package and script ABI (J319).
2
4
  * The fourth component adds fields; the third changes their meaning. */
3
5
  const ZERO = 0
4
6
  const ONE = 1
5
7
 
6
- const SHARDFILE_VERSION = '0.1.0.2'
8
+ const SHARDFILE_VERSION = '0.1.0.3'
7
9
  /** Formats whose `shard.json` names its version in `version`, beside the frozen layout marker `formatVersion: 2`.
8
10
  * Legacy 0.1 is the byte-preserving spelling of 0.1.0.0. */
9
11
  const LEGACY_INDEX_VERSIONS: readonly ['0.1', '0.1.0.0', '0.1.0.1'] = [
@@ -13,7 +15,8 @@ const LEGACY_INDEX_VERSIONS: readonly ['0.1', '0.1.0.0', '0.1.0.1'] = [
13
15
  ] as const
14
16
  /** Formats whose `shard.json` names its version in `formatVersion` (SHARD-COMPAT CP14, FORMAT-PRUNE FP25): the
15
17
  * layout marker and `version` are gone, so a reader tells the two envelopes apart by the field's type. */
16
- const INDEX_FORMAT_VERSIONS: readonly [typeof SHARDFILE_VERSION] = [
18
+ const INDEX_FORMAT_VERSIONS: readonly ['0.1.0.2', typeof SHARDFILE_VERSION] = [
19
+ '0.1.0.2',
17
20
  SHARDFILE_VERSION,
18
21
  ] as const
19
22
  const SHARDFILE_VERSIONS: readonly [
@@ -27,6 +30,9 @@ type IndexFormatVersion = (typeof INDEX_FORMAT_VERSIONS)[number]
27
30
  const LEGACY_LAYOUT = 2
28
31
  /** The first format that writes its version in `formatVersion`. */
29
32
  const SHARDFILE_VERSION_INDEX_RENAME = '0.1.0.2'
33
+ /** The first format whose `shard.json` records the preparation spec version in `preparation` instead of the `sdk`
34
+ * marker (SHARD-COMPAT PR4, J487; FORMAT-PRUNE FP5 folded in). */
35
+ const SHARDFILE_VERSION_PREPARATION = '0.1.0.3'
30
36
  type ShardfileVersionParts = readonly [number, number, number, number]
31
37
 
32
38
  /** Read canonical bounded decimal components; the legacy two-part spelling has zero counters. */
@@ -111,6 +117,13 @@ function writesIndexFormatVersion(version: string): boolean {
111
117
  )
112
118
  }
113
119
 
120
+ /** Whether a format version records `preparation` in place of the `sdk` marker (0.1.0.3 on, PR4). */
121
+ function writesPreparationMarker(version: string): boolean {
122
+ return (
123
+ compareShardfileVersions(version, SHARDFILE_VERSION_PREPARATION) >= ZERO
124
+ )
125
+ }
126
+
114
127
  /** A copy of a JSON object without `keys`; anything else as it is. */
115
128
  function withoutKeys(value: unknown, keys: readonly string[]): unknown {
116
129
  if (typeof value !== 'object' || value === null || Array.isArray(value)) {
@@ -125,11 +138,16 @@ function withoutKeys(value: unknown, keys: readonly string[]): unknown {
125
138
 
126
139
  /** The rows `outputIndexEnvelope` returns: the writer's own rows, typed so it keeps reading them, with `version` and
127
140
  * `schemas` possibly gone and `formatVersion` possibly the version string. */
128
- type IndexEnvelope<Rows> = Omit<Rows, 'formatVersion' | 'schemas' | 'version'> &
141
+ type IndexEnvelope<Rows> = Omit<
142
+ Rows,
143
+ 'formatVersion' | 'schemas' | 'version' | 'sdk'
144
+ > &
129
145
  Readonly<{
130
146
  formatVersion: unknown
131
147
  version?: unknown
132
148
  schemas?: unknown
149
+ sdk?: unknown
150
+ preparation?: unknown
133
151
  }>
134
152
 
135
153
  /** SHARD-COMPAT CP14: writers build the legacy `shard.json` rows (`formatVersion: 2`, `version`, the always-empty lists)
@@ -149,6 +167,15 @@ function outputIndexEnvelope<Rows extends Readonly<Record<string, unknown>>>(
149
167
  formatVersion: version,
150
168
  runtime: withoutKeys(rest['runtime'], ['files', 'lines', 'trust']),
151
169
  }
170
+ // SHARD-COMPAT PR4: from 0.1.0.3 the `sdk` marker goes; a prepared writer's `'0.3'` becomes the spec version it
171
+ // meant, unless the writer already names one in `preparation`.
172
+ if (typeof version === 'string' && writesPreparationMarker(version)) {
173
+ const { sdk } = envelope
174
+ Reflect.deleteProperty(envelope, 'sdk')
175
+ if (sdk === PREPARED_SDK && envelope['preparation'] === undefined) {
176
+ envelope['preparation'] = LEGACY_PREPARATION_SPEC
177
+ }
178
+ }
152
179
  // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the writer's own rows; only envelope fields change
153
180
  return envelope as IndexEnvelope<Rows>
154
181
  }
@@ -165,6 +192,8 @@ export {
165
192
  type IndexFormatVersion,
166
193
  shardfileFormatOf,
167
194
  writesIndexFormatVersion,
195
+ writesPreparationMarker,
196
+ SHARDFILE_VERSION_PREPARATION,
168
197
  SHARDFILE_FLOOR,
169
198
  type ShardfileVersion,
170
199
  type ShardfileVersionParts,