@wildshard/shard-format 0.38.1 → 0.39.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.38.1",
3
+ "version": "0.39.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": {
@@ -108,6 +108,7 @@
108
108
  "./creatures": "./src/format/creatures.ts",
109
109
  "./creaturePerch": "./src/format/creature-perch.ts",
110
110
  "./creatureLayers": "./src/format/creature-layers.ts",
111
+ "./creatureRanks": "./src/format/creature-ranks.ts",
111
112
  "./creatureVolumes": "./src/format/creature-volumes.ts",
112
113
  "./creatureSizes": "./src/format/creature-sizes.ts",
113
114
  "./creatureSkeletons": "./src/format/creature-skeletons.ts",
@@ -10,13 +10,21 @@ import type { PicklistSchema } from 'valibot'
10
10
  * - `cpuMicrosPerFrame`: the shard's own runtime and page features, per 60 Hz frame on the reference phone.
11
11
  * - `memoryBytes`: resident memory its runtime and owned stages may hold beyond the admitted content.
12
12
  * - `bandwidthBytes`: compressed bytes a player downloads for the shard (J338: 50 MB is fine, 100 MB is the hard cap).
13
+ * - `gpu`: graph nodes, sampler slots, estimated instructions, and authored post passes.
13
14
  */
14
15
  interface BudgetTierLimits {
15
16
  readonly cpuMicrosPerFrame: number
16
17
  readonly memoryBytes: number
17
18
  readonly bandwidthBytes: number
19
+ readonly gpu: GraphTierLimit
18
20
  }
19
21
  type BudgetTier = 'small' | 'medium' | 'large'
22
+ interface GraphTierLimit {
23
+ readonly nodes: number
24
+ readonly samplers: number
25
+ readonly instructions: number
26
+ readonly passes: number
27
+ }
20
28
 
21
29
  const MEBIBYTE = 1_048_576
22
30
  const SMALL_CPU = 1000
@@ -34,16 +42,19 @@ const BUDGET_TIERS: Readonly<Record<BudgetTier, BudgetTierLimits>> = {
34
42
  cpuMicrosPerFrame: SMALL_CPU,
35
43
  memoryBytes: SMALL_MEMORY * MEBIBYTE,
36
44
  bandwidthBytes: SMALL_BANDWIDTH,
45
+ gpu: { nodes: 512, samplers: 4, instructions: 1024, passes: 2 },
37
46
  },
38
47
  medium: {
39
48
  cpuMicrosPerFrame: MEDIUM_CPU,
40
49
  memoryBytes: MEDIUM_MEMORY * MEBIBYTE,
41
50
  bandwidthBytes: MEDIUM_BANDWIDTH,
51
+ gpu: { nodes: 1280, samplers: 8, instructions: 3072, passes: 3 },
42
52
  },
43
53
  large: {
44
54
  cpuMicrosPerFrame: LARGE_CPU,
45
55
  memoryBytes: LARGE_MEMORY * MEBIBYTE,
46
56
  bandwidthBytes: LARGE_BANDWIDTH,
57
+ gpu: { nodes: 4096, samplers: 16, instructions: 8192, passes: 4 },
47
58
  },
48
59
  }
49
60
  /** The tier a shard picks; an unknown tier refuses. */
@@ -58,5 +69,26 @@ function budgetTierLimits(budgets: {
58
69
  return BUDGET_TIERS[budgets.tier ?? 'small']
59
70
  }
60
71
 
61
- export { BUDGET_TIERS, BudgetTierSchema, budgetTierLimits }
72
+ /** The compatibility tier for older runtime publications. */
73
+ const UNNAMED_TIER: BudgetTier = 'large'
74
+
75
+ /** Material programs and authored post stacks use their product's tier. */
76
+ function graphTierLimits(source: {
77
+ readonly budgets?: { readonly tier?: BudgetTier }
78
+ readonly runtime?: unknown
79
+ }): GraphTierLimit {
80
+ const fallback =
81
+ source.runtime === null || source.runtime === undefined
82
+ ? 'small'
83
+ : UNNAMED_TIER
84
+ return budgetTierLimits({ tier: source.budgets?.tier ?? fallback }).gpu
85
+ }
86
+
87
+ export {
88
+ BUDGET_TIERS,
89
+ BudgetTierSchema,
90
+ UNNAMED_TIER,
91
+ budgetTierLimits,
92
+ graphTierLimits,
93
+ }
62
94
  export type { BudgetTier, BudgetTierLimits }
@@ -252,7 +252,7 @@ export interface GraphBudget {
252
252
  }
253
253
  /** the default ceilings (a starter graph: the measure preset is ≈ 60 nodes, ≈ 170 instructions) */
254
254
  export const DEFAULT_GRAPH_BUDGET: GraphBudget = { nodes: 160, samplers: 4, instructions: 480 };
255
- /** Hard ceilings for a native declaration; loop trips remain charged to the estimated cost. */
255
+ /** Hard ceilings for an authored declaration; loop trips remain charged to the estimated cost. */
256
256
  export const MAX_GRAPH_BUDGET: GraphBudget = Object.freeze({ nodes: 4096, samplers: 16, instructions: 8192 });
257
257
 
258
258
  /** Raw graph JSON bounds, enforced before typing or compiler work; host-owned presets supply their own node budget. */
@@ -559,6 +559,8 @@ export interface GraphValidationOptions {
559
559
  /** the shard's declared public numeric state fields a param may bind to (any name when absent) */
560
560
  readonly stateFields?: readonly string[];
561
561
  readonly budget?: GraphBudget;
562
+ /** A product's ceiling also bounds explicit budgets and nested graphs. */
563
+ readonly budgetCeiling?: GraphBudget;
562
564
  /** Host authority to use an explicit declaration; content cannot grant this. */
563
565
  readonly declaredBudget?: boolean;
564
566
  /** the directional lights a `sun` sub-graph is built for (1 … SUN_CASCADES_MAX; SUN_CASCADES when absent) */
@@ -624,7 +626,9 @@ interface Scope {
624
626
  * every reason it is refused.
625
627
  */
626
628
  export function validateGraph(input: unknown, opts: GraphValidationOptions = {}): GraphValidation {
627
- const rawErrors = graphAdmissionErrors(input, opts.declaredBudget === true ? MAX_GRAPH_BUDGET.nodes : opts.budget?.nodes ?? DEFAULT_GRAPH_BUDGET.nodes);
629
+ const ceiling = opts.budgetCeiling;
630
+ const rawLimit = opts.declaredBudget === true ? MAX_GRAPH_BUDGET.nodes : opts.budget?.nodes ?? DEFAULT_GRAPH_BUDGET.nodes;
631
+ const rawErrors = graphAdmissionErrors(input, Math.min(rawLimit, ceiling?.nodes ?? rawLimit));
628
632
  if (rawErrors.length > 0) return { ok: false, errors: rawErrors };
629
633
  const errors: string[] = [];
630
634
  const fail = (msg: string): null => { errors.push(msg); return null; };
@@ -642,6 +646,13 @@ export function validateGraph(input: unknown, opts: GraphValidationOptions = {})
642
646
  if (opts.declaredBudget !== true) return { ok: false, errors: ['graph: declared budget requires native runtime authority'] };
643
647
  budget = { nodes: Number(declared.nodes), samplers: Number(declared['samplers']), instructions: Number(declared['instructions']) };
644
648
  }
649
+ if (ceiling !== undefined) {
650
+ budget = {
651
+ nodes: Math.min(budget.nodes, ceiling.nodes),
652
+ samplers: Math.min(budget.samplers, ceiling.samplers),
653
+ instructions: Math.min(budget.instructions, ceiling.instructions),
654
+ };
655
+ }
645
656
  if (input.version !== GRAPH_IR_VERSION) fail(`graph: version ${String(input.version)} (this engine reads ${GRAPH_IR_VERSION})`);
646
657
  const kind = input.kind, primitive = input.primitive;
647
658
  if (kind !== 'material' && kind !== 'post' && kind !== 'fog') fail(`graph: kind ${String(kind)} (material, post or fog)`);
@@ -1,3 +1,4 @@
1
+ import { graphTierLimits } from './budget-tiers.ts';
1
2
  import { hasDeclaredClientPage } from './runtimeBinds.ts';
2
3
  import * as v from 'valibot';
3
4
  import {
@@ -155,9 +156,10 @@ function numericFields(state: Shardfile['state'], privacy: 'public' | 'host'): s
155
156
  * programs are held to `PRESET_GRAPH_BUDGET` by the engine's own tests, never the author caps); a graph file must be a
156
157
  * JSON file of the library closure (`files` / `library` given: its bytes are validated by product admission, `graphFileRules`).
157
158
  */
158
- export function materialGraphRules(source: Pick<Shardfile, 'look' | 'state'> & Partial<Pick<Shardfile, 'files' | 'library' | 'runtime' | 'questHost'>>): string[] {
159
+ export function materialGraphRules(source: Pick<Shardfile, 'look' | 'state'> & Partial<Pick<Shardfile, 'files' | 'library' | 'runtime' | 'questHost' | 'budgets'>>): string[] {
159
160
  const errors: string[] = [];
160
161
  const { day: dayChannels, state: stateFields } = graphBindingSources(source);
162
+ const budget = graphTierLimits(source);
161
163
  const files = new Map((source.files ?? []).map((row) => [row.hash, row])), closure = new Set<string>(), pending = [...source.library ?? []];
162
164
  while (pending.length > 0) { const hash = pending.pop(); if (hash === undefined || closure.has(hash)) continue; closure.add(hash); pending.push(...files.get(hash)?.dependencies ?? []); }
163
165
  for (const [materialId, material] of Object.entries(source.look.materials)) {
@@ -167,7 +169,7 @@ export function materialGraphRules(source: Pick<Shardfile, 'look' | 'state'> & P
167
169
  continue;
168
170
  }
169
171
  if ('preset' in material) { const refusal = presetRefusal(material.preset); if (refusal !== null) errors.push(`material ${materialId}: ${refusal}`); continue; }
170
- const checked = validateGraph(material.graph, { dayKeys: [...dayChannels.keys()], stateFields, declaredBudget: source.runtime !== null && source.runtime !== undefined || hasDeclaredClientPage(source) });
172
+ const checked = validateGraph(material.graph, { dayKeys: [...dayChannels.keys()], stateFields, budget, budgetCeiling: budget, declaredBudget: true });
171
173
  if (!checked.ok) { errors.push(...checked.errors.map((error) => `material ${materialId}: ${error}`)); continue; }
172
174
  errors.push(...bindingTypeErrors(materialId, checked.graph, dayChannels));
173
175
  const fog = checked.graph.effectState?.fog;
@@ -184,15 +186,16 @@ export function materialGraphRules(source: Pick<Shardfile, 'look' | 'state'> & P
184
186
  * one `kind: "material"` graph that validates under the author caps against the shard's admitted bindings, and every
185
187
  * texture param names a KTX2 file its row declares as a direct dependency (so the texture is charged with the closure).
186
188
  */
187
- export function graphFileRules(source: Pick<Shardfile, 'look' | 'state' | 'files'> & Partial<Pick<Shardfile, 'runtime' | 'questHost'>>, bytes: (hash: string) => Uint8Array | undefined): string[] {
189
+ export function graphFileRules(source: Pick<Shardfile, 'look' | 'state' | 'files'> & Partial<Pick<Shardfile, 'runtime' | 'questHost' | 'budgets'>>, bytes: (hash: string) => Uint8Array | undefined): string[] {
188
190
  const errors: string[] = [];
189
191
  const { day: dayChannels, state: stateFields } = graphBindingSources(source);
192
+ const budget = graphTierLimits(source);
190
193
  const files = new Map(source.files.map((row) => [row.hash, row]));
191
194
  for (const [materialId, material] of Object.entries(source.look.materials)) {
192
195
  if (material.family !== 'graph' || !('file' in material)) continue;
193
196
  let ir: unknown;
194
197
  try { ir = graphFileJson(material.file, bytes); } catch (error) { errors.push(`material ${materialId}: ${error instanceof Error ? error.message : 'graph file'}`); continue; }
195
- const checked = validateGraph(ir, { dayKeys: [...dayChannels.keys()], stateFields, declaredBudget: source.runtime !== null && source.runtime !== undefined || hasDeclaredClientPage(source) });
198
+ const checked = validateGraph(ir, { dayKeys: [...dayChannels.keys()], stateFields, budget, budgetCeiling: budget, declaredBudget: true });
196
199
  if (!checked.ok) { errors.push(...checked.errors.map((error) => `material ${materialId}: ${error}`)); continue; }
197
200
  if (checked.graph.kind === 'post') { errors.push(`material ${materialId}: a graph file in a material slot holds a material or fog graph, not a post pass`); continue; }
198
201
  const fog = checked.graph.effectState?.fog, target = typeof fog === 'string' ? source.look.materials[fog] : undefined;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * A shard's own post stack (SHARD-PLATFORM SF59 steps 3–4, G158, budget v1 C4-R1-A5 / C7): inside its cell a shard's
3
3
  * look owns the whole frame, its post passes included, as data. `look.post` is an ordered list of at most
4
- * {@link POST_STACK_BUDGET}`.passes` authored post graphs (`kind: "post"`), each inline (`{ graph }`) or a graph file of the
4
+ * its tier’s allowed number of authored post graphs (`kind: "post"`), each inline (`{ graph }`) or a graph file of the
5
5
  * library closure (`{ file }`, validated with its bytes at product admission). Each pass is a full-screen program; its
6
6
  * instruction estimate is a per-pixel cost at the phone's 2× render scale, so the stack's cost is the sum of its passes'
7
7
  * estimates plus the normal pre-pass when any pass reads `sceneNormal` ({@link POST_NORMAL_PREPASS_COST}, paid once).
@@ -12,6 +12,7 @@
12
12
  import * as v from 'valibot';
13
13
  import { validateGraph, type GraphIr, type PostInputs } from './materialGraph.ts';
14
14
  import { graphBindingSources, graphFileJson } from './materials.ts';
15
+ import { graphTierLimits } from './budget-tiers.ts';
15
16
  import { isJsonData } from './json.ts';
16
17
  import type { Shardfile } from './shardSchema.ts';
17
18
 
@@ -48,7 +49,7 @@ export interface PostStackCost { readonly passes: number; readonly instructions:
48
49
  * Validate and cost a stack: each pass under the author caps against the shard's admitted bindings (a graph file read
49
50
  * through `bytes`; undefined bytes are refused), the sum against the stack budget. Returns the cost and every refusal.
50
51
  */
51
- export function postStackCost(source: Pick<Shardfile, 'look' | 'state'> & { readonly look: { readonly post?: readonly PostPass[] } }, bytes: (hash: string) => Uint8Array | undefined): { cost: PostStackCost; errors: string[] } {
52
+ export function postStackCost(source: Pick<Shardfile, 'look' | 'state'> & Partial<Pick<Shardfile, 'budgets' | 'runtime'>> & { readonly look: { readonly post?: readonly PostPass[] } }, bytes: (hash: string) => Uint8Array | undefined): { cost: PostStackCost; errors: string[] } {
52
53
  const errors: string[] = [], passes = source.look.post ?? [];
53
54
  const { day, state } = graphBindingSources(source);
54
55
  let instructions = 0;
@@ -63,7 +64,8 @@ export function postStackCost(source: Pick<Shardfile, 'look' | 'state'> & { read
63
64
  inputs.colour ||= checked.inputs.colour; inputs.depth ||= checked.inputs.depth; inputs.normal ||= checked.inputs.normal;
64
65
  });
65
66
  if (inputs.normal) instructions += POST_NORMAL_PREPASS_COST;
66
- if (passes.length > POST_STACK_BUDGET.passes) errors.push(`post: ${passes.length} passes (at most ${POST_STACK_BUDGET.passes})`);
67
+ const passLimit = Math.min(POST_STACK_BUDGET.passes, graphTierLimits(source).passes);
68
+ if (passes.length > passLimit) errors.push(`post: ${passes.length} passes (at most ${passLimit})`);
67
69
  if (instructions > POST_STACK_BUDGET.instructions) errors.push(`post: ≈ ${instructions} instructions per pixel at 2× (at most ${POST_STACK_BUDGET.instructions})`);
68
70
  return { cost: { passes: passes.length, instructions, inputs }, errors };
69
71
  }
@@ -73,7 +75,7 @@ export function postStackCost(source: Pick<Shardfile, 'look' | 'state'> & { read
73
75
  * file pass must be a JSON file of the library closure (`files` / `library` given; its bytes are costed by product
74
76
  * admission through `postStackCost`, so a stack holding files is summed there).
75
77
  */
76
- export function postStackRules(source: Pick<Shardfile, 'look' | 'state'> & Partial<Pick<Shardfile, 'files' | 'library'>> & { readonly look: { readonly post?: readonly PostPass[] } }): string[] {
78
+ export function postStackRules(source: Pick<Shardfile, 'look' | 'state'> & Partial<Pick<Shardfile, 'budgets' | 'runtime'>> & Partial<Pick<Shardfile, 'files' | 'library'>> & { readonly look: { readonly post?: readonly PostPass[] } }): string[] {
77
79
  const passes = source.look.post ?? [];
78
80
  if (passes.length === 0) return [];
79
81
  const errors: string[] = [];
@@ -1,12 +1,9 @@
1
- import { budgetTierLimits } from './budget-tiers.ts'
1
+ import { UNNAMED_TIER, budgetTierLimits } from './budget-tiers.ts'
2
2
  import { ratchetNote, ratchetSeverity } from './memory-ratchet.ts'
3
3
  import { runtimeAccountedBytes } from './runtimeCost.ts'
4
4
 
5
5
  import type { BudgetTier } from './budget-tiers.ts'
6
6
 
7
- /** The tier an unnamed runtime is held to until OF10c1b names every runtime shard's tier. */
8
- const UNNAMED_TIER: BudgetTier = 'large'
9
-
10
7
  /** What the check reads of a shard: its runtime's reviewed cost and its budgets. */
11
8
  type RuntimeTierSource = Readonly<{
12
9
  runtime: Readonly<{ cost?: unknown }> | null
@@ -55,9 +52,6 @@ function runtimeTierIssues(source: RuntimeTierSource): RuntimeTierIssue[] {
55
52
  ]
56
53
  }
57
54
 
58
- export {
59
- UNNAMED_TIER,
60
- type RuntimeTierIssue,
61
- type RuntimeTierSource,
62
- runtimeTierIssues,
63
- }
55
+ export { UNNAMED_TIER } from './budget-tiers.ts'
56
+
57
+ export { type RuntimeTierIssue, type RuntimeTierSource, runtimeTierIssues }