@wildshard/shard-format 0.79.1 → 0.79.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.79.1",
3
+ "version": "0.79.2",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -17,6 +17,7 @@ import {
17
17
  union,
18
18
  } from 'valibot'
19
19
 
20
+ import { sentSdkOf } from './incoming-sent.ts'
20
21
  import {
21
22
  failureOf,
22
23
  fenceValid,
@@ -246,11 +247,16 @@ function requireCookPreparation(
246
247
  return
247
248
  }
248
249
  // SHARD-COMPAT PR5 (J487): every failing prepared part is named, not the first.
250
+ // FP60: a published client hashed the index as sent, `requires.sdk` included (`incoming-sent.ts`).
251
+ const sdk = sentSdkOf(index)
252
+ const sent = { ...index, requires: { ...index.requires, sdk } }
253
+ const fresh = (sdk === undefined ? [index] : [index, sent]).some(
254
+ (row: Frozen<OutputIndex>): boolean =>
255
+ cook.inputHash ===
256
+ digest(cookPreparationBytes(row, cook.mounts, cook.renderer)),
257
+ )
249
258
  const failures = [
250
- cook.inputHash ===
251
- digest(cookPreparationBytes(index, cook.mounts, cook.renderer))
252
- ? undefined
253
- : `stale cook preparation: run ${fix}`,
259
+ fresh ? undefined : `stale cook preparation: run ${fix}`,
254
260
  failureOf((): void => {
255
261
  fenceValid(index, { fence: cook.fence ?? [], mounts: cook.mounts }, fix)
256
262
  }),
@@ -0,0 +1,36 @@
1
+ /** FORMAT-PRUNE FP60: what an incoming original's index carried before `incoming.ts` mended it. A published client
2
+ * declared its hashes (the source hash, the cook preparation's input hash) over the index as it sent it, `requires.sdk`
3
+ * included, so each check also accepts the index as sent. A leaf: `outputIndex.ts` reaches the preparation check, and
4
+ * this imports nothing. */
5
+
6
+ /** Each parsed incoming index whose `requires.sdk` was mended away, with the value the client sent. */
7
+ const SENT_SDK = new WeakMap<object, unknown>()
8
+
9
+ /** Remember the `requires.sdk` the client sent with `index`. */
10
+ function rememberSentSdk(index: object, sdk: unknown): void {
11
+ SENT_SDK.set(index, sdk)
12
+ }
13
+
14
+ /** The `requires.sdk` the client sent with `index`, when it was mended away. */
15
+ function sentSdkOf(index: object): unknown {
16
+ return SENT_SDK.get(index)
17
+ }
18
+
19
+ /** `value` with the `requires.sdk` its client sent. */
20
+ function withSentSdk(
21
+ value: Readonly<{ requires?: object }>,
22
+ sdk: unknown,
23
+ ): object {
24
+ return { ...value, requires: { ...value.requires, sdk } }
25
+ }
26
+
27
+ /** `value` as checked, then, when its client sent a `requires.sdk` that was mended away, as the client hashed it. */
28
+ function sentForms(
29
+ value: Readonly<{ requires?: object }>,
30
+ index: object,
31
+ ): readonly object[] {
32
+ const sdk = SENT_SDK.get(index)
33
+ return sdk === undefined ? [value] : [value, withSentSdk(value, sdk)]
34
+ }
35
+
36
+ export { rememberSentSdk, sentForms, sentSdkOf }
@@ -0,0 +1,55 @@
1
+ /** FORMAT-PRUNE FP60: an incoming original, mended of each deleted field before its strict parse. A published client
2
+ * still sends what its own format required: `@wildshard/upload` 0.4.22 (the oldest supported) checks with format 0.63.0
3
+ * and 0.4.25 with 0.66.0, both needing `requires.sdk`, and format 0.79.1's strict index refused every such upload
4
+ * ("Invalid key: Expected never but received sdk"). The stored bytes keep the field; the chain mends each copy it
5
+ * writes (`format-migration-deleted.ts`). Both validators read an original's index and sections through here, and check
6
+ * its declared source hash here: the client hashed `requires` as it sent it, `sdk` included. */
7
+ import { mendDeletedFields } from './format-migration-deleted.ts'
8
+ import { rememberSentSdk, sentForms } from './incoming-sent.ts'
9
+ import { parseOutputIndex, validateOutputSection } from './outputIndex.ts'
10
+ import { writtenSourceBytes } from './save-revision.ts'
11
+
12
+ import type { OutputIndex } from './outputIndex.ts'
13
+
14
+ function isRecord(value: unknown): value is Record<string, unknown> {
15
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
16
+ }
17
+
18
+ /** `shard.json` as uploaded: its deleted fields mended in place (`requires.sdk`), then parsed strictly. */
19
+ function parseIncomingIndex(index: unknown): OutputIndex {
20
+ const requires = isRecord(index) ? index['requires'] : undefined
21
+ const sdk = isRecord(requires) ? requires['sdk'] : undefined
22
+ if (isRecord(index)) {
23
+ mendDeletedFields({ index, sections: new Map(), mounts: new Map() })
24
+ }
25
+ const parsed = parseOutputIndex(index)
26
+ if (sdk !== undefined) {
27
+ rememberSentSdk(parsed, sdk)
28
+ }
29
+ return parsed
30
+ }
31
+
32
+ /** A section as uploaded: its deleted fields mended in place (the old grid pointer becomes `shardfile`), then
33
+ * validated strictly. */
34
+ function validateIncomingSection(name: string, value: unknown): void {
35
+ const sections = new Map([[name, value]])
36
+ mendDeletedFields({ index: {}, sections, mounts: new Map() })
37
+ validateOutputSection(name, value)
38
+ }
39
+
40
+ /** Whether `index`'s declared source hash covers `shard` as admitted, or as its client hashed it: with the
41
+ * `requires.sdk` it sent (`parseIncomingIndex`). */
42
+ function declaredSourceMatches(
43
+ shard: Readonly<{ requires?: object }>,
44
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot's output type, only read
45
+ index: OutputIndex,
46
+ hash: (bytes: Readonly<Uint8Array>) => string,
47
+ ): boolean {
48
+ return sentForms(shard, index).some(
49
+ (source: object): boolean =>
50
+ hash(writtenSourceBytes(source, index.formatVersion)) ===
51
+ index.build.sourceHash,
52
+ )
53
+ }
54
+
55
+ export { declaredSourceMatches, parseIncomingIndex, validateIncomingSection }
@@ -8,10 +8,10 @@ import {
8
8
  assetAdmission,
9
9
  preflightDeclaredCosts,
10
10
  } from './assetAdmission.ts'
11
+ import { declaredSourceMatches } from './incoming.ts'
11
12
  import { issueError, withIssueCode } from './issue-codes.ts'
12
13
  import { assembleOutputShardfile } from './outputIndex.ts'
13
14
  import { admittedVerdict, recordVerdict } from './policy-verdict.ts'
14
- import { writtenSourceBytes } from './save-revision.ts'
15
15
  import { offlineMemoryAdmission } from './shardfile-assets.ts'
16
16
  import {
17
17
  assertShardfileTargets,
@@ -142,10 +142,7 @@ function admitTail(
142
142
  }>,
143
143
  ): void {
144
144
  checkWorldTargets(shard, product.payloads.get('world/targets'))
145
- if (
146
- product.hash(writtenSourceBytes(shard, product.index.formatVersion)) !==
147
- product.index.build.sourceHash
148
- ) {
145
+ if (!declaredSourceMatches(shard, product.index, product.hash)) {
149
146
  throw issueError(
150
147
  'source-hash',
151
148
  'Output sections differ from the declared source hash',
@@ -15,9 +15,9 @@
15
15
 
16
16
  import { SHARDFILE_ADMISSION_LIMITS as limits } from './admissionLimits.ts'
17
17
  import { assertSectionSides } from './feature-sides.ts'
18
+ import { parseIncomingIndex, validateIncomingSection } from './incoming.ts'
18
19
  import { issueError, withIssueCode } from './issue-codes.ts'
19
20
  import { unheldMount } from './manifest-deprecations.ts'
20
- import { parseOutputIndex, validateOutputSection } from './outputIndex.ts'
21
21
  import { admittedVerdict } from './policy-verdict.ts'
22
22
  import { assertScriptSides } from './script-sides.ts'
23
23
  import { scriptAdmissionRows } from './scriptContract.ts'
@@ -212,7 +212,7 @@ function readIndex(replay: Readonly<Replay>): OutputIndex {
212
212
  hash: shardJson.sha,
213
213
  bytes: shardJson.size,
214
214
  }
215
- return parseOutputIndex(readCanonicalJson(listedBytes(replay, listed)))
215
+ return parseIncomingIndex(readCanonicalJson(listedBytes(replay, listed)))
216
216
  }
217
217
 
218
218
  /** Each section: listed, hashed, canonical and valid; with the script side warnings (`assertScriptSides`). */
@@ -226,7 +226,7 @@ function readSections(
226
226
  withIssueCode('section-invalid', (): void => {
227
227
  const value = readCanonicalJson(listedBytes(replay, entry))
228
228
  assertSectionSides(entry.name, value)
229
- validateOutputSection(entry.name, value)
229
+ validateIncomingSection(entry.name, value)
230
230
  payloads.set(entry.name, value)
231
231
  })
232
232
  }
@@ -1,8 +1,8 @@
1
1
  import { cookMountPath, readCookData } from './cook-data.ts';
2
2
  import { checkDiscoveryMount } from './manifest-deprecations.ts';
3
3
  import { requireCookPreparation } from './cook-preparation.ts';
4
- import { parseOutputIndex, validateOutputSection, assembleOutputShardfile, type OutputFile, type OutputIndex } from './outputIndex.ts';
5
- import { asWrittenIdentity } from './save-revision.ts';
4
+ import { declaredSourceMatches, parseIncomingIndex, validateIncomingSection } from './incoming.ts';
5
+ import { assembleOutputShardfile, type OutputFile, type OutputIndex } from './outputIndex.ts';
6
6
  import { decisionModuleContracts, scriptAdmissionRows, type ScriptAdmissionRows, type ScriptContract } from './scriptContract.ts';
7
7
  import { registeredJsonContract, validateRegisteredJson } from './registeredJson.ts';
8
8
  import { validatePlatformFile } from './platformFileFamilies.ts';
@@ -183,7 +183,7 @@ type ReadListed = (entry: Readonly<{ path: string; hash: string; bytes: number }
183
183
  function readSections(index: OutputIndex, read: ReadListed): Map<string, unknown> {
184
184
  const payloads = new Map<string, unknown>();
185
185
  for (const entry of index.sections) {
186
- coded('section-invalid', (): void => { const value = readCanonicalJson(read(entry)); assertSectionSides(entry.name, value); validateOutputSection(entry.name, value); payloads.set(entry.name, value); });
186
+ coded('section-invalid', (): void => { const value = readCanonicalJson(read(entry)); assertSectionSides(entry.name, value); validateIncomingSection(entry.name, value); payloads.set(entry.name, value); });
187
187
  }
188
188
  return payloads;
189
189
  }
@@ -234,7 +234,7 @@ function readCook(index: OutputIndex, read: ReadListed, ports: Pick<ShardfileVal
234
234
  * check that refused (SA41, `issue-codes.ts`). */
235
235
  export function validateShardfileFolder(files: ShardfileFiles, ports: ShardfileValidationPorts): ValidatedShardfile {
236
236
  const verify = ports.verify ?? ((bytes: Uint8Array, expected: string): boolean => ports.hash(bytes) === expected);
237
- const index = coded('index-invalid', (): OutputIndex => parseOutputIndex(readCanonicalJson(files.read('shard.json', limits.sourceBytes))));
237
+ const index = coded('index-invalid', (): OutputIndex => parseIncomingIndex(readCanonicalJson(files.read('shard.json', limits.sourceBytes))));
238
238
  const listed = new Set(['shard.json', 'validation.json']), { assets, keep } = assetStore(files.read, ports.readThrough === true);
239
239
  const read: ReadListed = (entry): Uint8Array => coded('file-mismatch', (): Uint8Array => {
240
240
  listed.add(entry.path);
@@ -260,6 +260,6 @@ export function validateShardfileFolder(files: ShardfileFiles, ports: ShardfileV
260
260
  for (const path of files.paths()) if (!listed.has(path)) throw issueError('unlisted-file', `Unlisted output file: ${path}`);
261
261
  const shard = admitShardfileFolder(coded('shard-schema', (): unknown => assembleOutputShardfile(index, payloads)), assets, ports.hash, 'warn', scripts, { sections: payloads, mountedImages, index, judged: ports.judged ?? 'upload', armed: ports.armed });
262
262
  checkWorldTargets(shard, payloads.get('world/targets'));
263
- if (ports.hash(validationSourceBytes(asWrittenIdentity(shard, index.formatVersion))) !== index.build.sourceHash) throw issueError('source-hash', 'Output sections differ from the declared source hash');
263
+ if (!declaredSourceMatches(shard, index, ports.hash)) throw issueError('source-hash', 'Output sections differ from the declared source hash');
264
264
  return { index, shard, assets, sections: payloads, scripts, ...deprecationReport({ index, sections: payloads, cook }, warnings), policy: admittedVerdict(shard), audio: shardfileRecordings({ cook, read: files.read, shard, assets }) };
265
265
  }