@wildshard/shard-format 0.80.0 → 0.81.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.80.0",
3
+ "version": "0.81.0",
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": {
@@ -29,6 +29,7 @@
29
29
  "packages/wire/src/net-command.ts",
30
30
  "src/cli/*.ts",
31
31
  "src/cli/*.mjs",
32
+ "src/scripts/.kit/wildshard-agent-requests.mjs",
32
33
  "!src/cli/*.test.ts",
33
34
  "corpus/exports/**",
34
35
  "src/format/meshopt-static.mjs",
@@ -1,3 +1,4 @@
1
+ import { markedFetch } from '../scripts/.kit/wildshard-agent-requests.mjs'
1
2
  import { loadCandidate } from './corpus-candidate.ts'
2
3
  import { corpusGate } from './corpus-gate.ts'
3
4
  import {
@@ -24,8 +25,13 @@ interface CorpusArgs {
24
25
  readonly only: string | undefined
25
26
  /** ZS3: store drift blocks too (corpus.yml's nightly once ZS11 finalizes the retired and proof shards). */
26
27
  readonly strictStore?: boolean
28
+ /** The network read under the Wildshard agent marker (QA 11-25 row 128); tests pass a stub. */
29
+ readonly fetch?: typeof fetch
27
30
  }
28
31
 
32
+ /** Every read the command makes, marked through the kit's shared helper: it reads production's store and release files. */
33
+ type MarkedArgs = Readonly<CorpusArgs> & Readonly<{ get: typeof fetch }>
34
+
29
35
  const EXIT_OK = 0
30
36
  const EXIT_INVALID = 3
31
37
  const NONE = 0
@@ -43,20 +49,21 @@ interface LiveSet {
43
49
  }
44
50
 
45
51
  /** ZS3 (J507): the API's list, engine's pins and every hash a signed release file or placement names. */
46
- async function liveSet(args: Readonly<CorpusArgs>): Promise<LiveSet> {
47
- const pins = await readPins(args.pins ?? LIVE_PINS)
52
+ async function liveSet(args: MarkedArgs): Promise<LiveSet> {
53
+ const pins = await readPins(args.pins ?? LIVE_PINS, args.get)
48
54
  const listed = await listKept({
49
55
  api: args.api,
50
56
  cache: args.cache,
51
57
  validator: '',
58
+ fetch: args.get,
52
59
  })
53
- const live = await liveHashes({ packs: PACKS, listed, pins })
60
+ const live = await liveHashes({ packs: PACKS, listed, pins }, args.get)
54
61
  return { pins, listed, live }
55
62
  }
56
63
 
57
64
  /** Every live original's row (ZS3, FE13). */
58
65
  async function corpusRows(
59
- args: Readonly<CorpusArgs>,
66
+ args: MarkedArgs,
60
67
  set: Readonly<LiveSet>,
61
68
  ): Promise<Readonly<{ rows: CorpusRow[]; validator: string }>> {
62
69
  const validator = await readVersion()
@@ -71,6 +78,7 @@ async function corpusRows(
71
78
  validator,
72
79
  pins: set.pins,
73
80
  live: set.live,
81
+ fetch: args.get,
74
82
  ...candidate,
75
83
  ...only,
76
84
  })
@@ -89,13 +97,13 @@ function gateBlocking(rows: readonly CorpusRow[]): number {
89
97
 
90
98
  /** ZS3: the API's listed originals beyond the live ones; a drifted one blocks. */
91
99
  async function storeDrift(
92
- args: Readonly<CorpusArgs>,
100
+ args: MarkedArgs,
93
101
  set: Readonly<LiveSet>,
94
102
  ): Promise<number> {
95
103
  const kept = classifyKept(set.listed, {
96
104
  live: set.live,
97
105
  pins: set.pins,
98
- pointed: await pointedHashes(args.api),
106
+ pointed: await pointedHashes(args.api, args.get),
99
107
  })
100
108
  process.stdout.write(`${keptLines(kept).join('\n')}\n`)
101
109
  return kept.drift.length
@@ -103,7 +111,8 @@ async function storeDrift(
103
111
 
104
112
  /** `shard-format corpus`: every live original, as stored (FE3, ZS3), gated by FE5's known failures and ZS3's store
105
113
  * drift; `--usage` writes FE13's record over the live originals only. */
106
- async function corpusCommand(args: Readonly<CorpusArgs>): Promise<number> {
114
+ async function corpusCommand(input: Readonly<CorpusArgs>): Promise<number> {
115
+ const args: MarkedArgs = { ...input, get: markedFetch(input.fetch ?? fetch) }
107
116
  const set = await liveSet(args)
108
117
  const { rows, validator } = await corpusRows(args, set)
109
118
  process.stdout.write(`${corpusReport(rows)}\n`)
@@ -17,6 +17,7 @@ import {
17
17
  CookPreparationSchema,
18
18
  CookSpecPreparationSchema,
19
19
  } from './cook-preparation.ts'
20
+ import { ShardVersionSchema } from './shard-version.ts'
20
21
 
21
22
  import type {
22
23
  CookPreparation,
@@ -44,6 +45,7 @@ interface CookMount {
44
45
  interface LegacyCookData {
45
46
  readonly version: typeof COOK_DATA_VERSION
46
47
  readonly mounts: readonly CookMount[]
48
+ readonly shardVersion?: string
47
49
  /** 'fence' packs what the boot reads before control; unset, the whole level. */
48
50
  readonly packs?: 'fence'
49
51
  /** The stable identities a recorded cold boot read before control. */
@@ -105,6 +107,7 @@ const identities: GenericSchema<unknown, string[]> = pipe(
105
107
  const LegacyCookDataSchema = strictObject({
106
108
  version: literal(COOK_DATA_VERSION),
107
109
  mounts: mountRows,
110
+ shardVersion: exactOptional(ShardVersionSchema),
108
111
  packs: exactOptional(literal('fence')),
109
112
  fence: exactOptional(identities),
110
113
  })
@@ -112,17 +115,21 @@ const LegacyCookDataSchema = strictObject({
112
115
  const PreparedCookDataSchema = strictObject({
113
116
  ...CookPreparationSchema.entries,
114
117
  mounts: mountRows,
118
+ shardVersion: exactOptional(ShardVersionSchema),
115
119
  })
116
120
  /** SHARD-COMPAT PR4: preparation spec 4, which names no engine. */
117
121
  const SpecCookDataSchema = strictObject({
118
122
  ...CookSpecPreparationSchema.entries,
119
123
  mounts: mountRows,
124
+ shardVersion: exactOptional(ShardVersionSchema),
120
125
  })
121
126
  interface PreparedCookData extends CookPreparation {
122
127
  readonly mounts: readonly CookMount[]
128
+ readonly shardVersion?: string
123
129
  }
124
130
  interface SpecCookData extends CookSpecPreparation {
125
131
  readonly mounts: readonly CookMount[]
132
+ readonly shardVersion?: string
126
133
  }
127
134
  type CookData = LegacyCookData | PreparedCookData | SpecCookData
128
135
  const CookDataSchema: GenericSchema<unknown, CookData> = variant('version', [
@@ -10,6 +10,7 @@ import type { AudioWaiver } from './audio-waiver.ts';
10
10
  import { isOutputIndex, outputBlobPath } from './outputAddress.ts';
11
11
  import { OUTPUT_SECTIONS, assembleOutputTransport, isPlatformDataSection, type OutputHostSectionName } from './outputFields.ts';
12
12
  import * as v from 'valibot';
13
+ import { ShardVersionSchema } from './shard-version.ts';
13
14
  import { assertFormatWindow } from './format-window.ts';
14
15
  import { assertCellBounds } from './cellBounds.ts';
15
16
  import { ShardfileFieldSchemas, parseShardfile, type Shardfile } from './shardSchema.ts';
@@ -45,6 +46,8 @@ const PreparationSpecNumberSchema = v.pipe(v.number(), v.integer(), v.minValue(L
45
46
  /** Format 2 inventories every emitted byte except its index and validation receipt. */
46
47
  export const OutputIndexSchema: v.GenericSchema<OutputIndexRowsInput, OutputIndexRows> & { readonly entries: { readonly [Key in keyof OutputIndexRows]-?: v.GenericSchema<OutputIndexRowsInput[Key], OutputIndexRows[Key]> } } = v.strictObject({
47
48
  format: v.literal('wildshard.shardfile'),
49
+ /** Author-written package version. Earlier stored shardfiles omit it. */
50
+ shardVersion: v.exactOptional(ShardVersionSchema),
48
51
  /** SHARD-COMPAT CP14 (FP25): the format version itself from 0.1.0.2; the legacy envelope carries the frozen layout marker
49
52
  * `2` here and its version in `version`. `checkIndexEnvelope` holds each envelope to its own fields. */
50
53
  formatVersion: IndexFormatVersionSchema, version: LegacyIndexVersionSchema,
@@ -242,6 +245,7 @@ export function assembleOutputShardfile(input: unknown, sections: ReadonlyMap<st
242
245
  /** Exact admitted data, independent of validator implementation metadata. */
243
246
  interface OutputIndexRowsInputFields {
244
247
  format: "wildshard.shardfile";
248
+ shardVersion?: string;
245
249
  formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
246
250
  sdk?: "0.2" | "0.3";
247
251
  /** A spec this package reads (3, 4), or a newer one kept unread (SP1b1a). */
@@ -342,6 +346,7 @@ interface OutputIndexRowsInputFields {
342
346
  export type OutputIndexRowsInput = { [Key in keyof OutputIndexRowsInputFields]: OutputIndexRowsInputFields[Key] };
343
347
  interface OutputIndexRowsFields {
344
348
  format: "wildshard.shardfile";
349
+ shardVersion?: string;
345
350
  formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
346
351
  sdk?: "0.2" | "0.3";
347
352
  /** A spec this package reads (3, 4), or a newer one kept unread (SP1b1a). */
@@ -0,0 +1,18 @@
1
+ import { maxLength, pipe, regex, string } from 'valibot'
2
+
3
+ import type { GenericSchema } from 'valibot'
4
+
5
+ // SemVer without build metadata, reserved for the catalogue's migrated copies.
6
+ const number = String.raw`(?:0|[1-9]\d*)`
7
+ const identifier = String.raw`(?:${number}|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)`
8
+ const shardVersion = new RegExp(
9
+ String.raw`^${number}\.${number}\.${number}(?:-${identifier}(?:\.${identifier})*)?$`,
10
+ 'u',
11
+ )
12
+ const MAX_VERSION_LENGTH = 128
13
+
14
+ export const ShardVersionSchema: GenericSchema<string> = pipe(
15
+ string(),
16
+ maxLength(MAX_VERSION_LENGTH),
17
+ regex(shardVersion, 'Expected a shardVersion without build metadata'),
18
+ )
@@ -0,0 +1,57 @@
1
+ // MT3: the operator protocol shared with engine's actorMarkers.json.
2
+ // oxlint-disable-next-line import/no-nodejs-modules -- These probes run in Node.
3
+ import { env } from 'node:process';
4
+
5
+ const AGENT_HEADER = 'x-wildshard-agent';
6
+ const TOKEN = 'WildshardAgent/1';
7
+ const LANE = /^[a-z0-9][a-z0-9._-]{0,63}$/iu;
8
+
9
+ function agentLane() {
10
+ /** @type {unknown} */
11
+ const environment = env;
12
+ const lane = typeof environment === 'object' && environment !== null && 'WS_LANE_NAME' in environment
13
+ ? environment.WS_LANE_NAME
14
+ : undefined;
15
+ return typeof lane === 'string' && LANE.test(lane) ? lane : '1';
16
+ }
17
+
18
+ /** @param {string} base */
19
+ function agentUserAgent(base) {
20
+ if (base.split(/\s+/u).includes(TOKEN)) {
21
+ return base;
22
+ }
23
+ const lane = agentLane();
24
+ const marker = lane === '1' ? TOKEN : `${TOKEN} (${lane})`;
25
+ return `${base.trim()} ${marker}`.trim();
26
+ }
27
+
28
+ /** @param {string} input */
29
+ function gameAddress(input) {
30
+ if (!URL.canParse(input)) {
31
+ return false;
32
+ }
33
+ const host = new URL(input).hostname;
34
+ return host === 'wildshard.io' || host.endsWith('.wildshard.io') || host === 'localhost' || host === '127.0.0.1' || host === '[::1]';
35
+ }
36
+
37
+ /** @param {typeof fetch} read @returns {typeof fetch} */
38
+ function markedFetch(read) {
39
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- Preserve the platform Fetch signature.
40
+ return async (input, options) => {
41
+ const request = input instanceof Request ? input : undefined;
42
+ const address = request?.url ?? (input instanceof URL ? input.href : input);
43
+ if (typeof address !== 'string' || !gameAddress(address)) {
44
+ return await read(input, options);
45
+ }
46
+ const headers = new Headers(options?.headers ?? request?.headers);
47
+ headers.set(AGENT_HEADER, agentLane());
48
+ headers.set('user-agent', agentUserAgent(headers.get('user-agent') ?? 'node'));
49
+ return await read(input, { ...options, headers });
50
+ };
51
+ }
52
+
53
+ /** @type {typeof fetch} */
54
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- Preserve the platform Fetch signature.
55
+ const agentFetch = async (input, options) => await markedFetch(globalThis.fetch)(input, options);
56
+
57
+ export { AGENT_HEADER, agentFetch, agentUserAgent, markedFetch };