@wildshard/shard-format 0.85.0 → 0.86.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.85.0",
3
+ "version": "0.86.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": {
@@ -9,18 +9,69 @@ import { layoutV2, type V2Layout } from './scriptWireV2.ts';
9
9
  const bindingName=v.pipe(v.string(),v.minLength(1),v.maxLength(512),v.regex(/^(?!__proto__$|prototype$|constructor$)/));
10
10
  const additionalNames=v.pipe(v.array(bindingName),v.maxLength(256));
11
11
  const names=v.pipe(v.array(bindingName),v.minLength(1),v.maxLength(256));
12
+ const queryNames=v.strictTuple([v.literal('groundHeight')]);
13
+ const queriesSchema=v.exactOptional(queryNames);
14
+ const MIN_BATCH_REQUESTS = 2;
15
+ const MAX_BATCH_REQUESTS = 64;
16
+ const MIN_BATCH_ROWS = 1;
17
+ const MAX_BATCH_ROWS = 8192;
18
+ const batchRequests=v.pipe(v.number(),v.integer(),v.minValue(MIN_BATCH_REQUESTS),v.maxValue(MAX_BATCH_REQUESTS));
19
+ const batchRowCount=v.pipe(v.number(),v.integer(),v.minValue(MIN_BATCH_ROWS),v.maxValue(MAX_BATCH_ROWS));
20
+ const batchRows=v.strictObject({requests:batchRequests,rows:batchRowCount});
21
+ const batchSchema=v.exactOptional(batchRows);
12
22
  export const ReturnProjectionV2Schema: v.GenericSchema<ReturnProjectionV2RowsInput, ReturnProjectionV2Rows> = v.strictObject({ artifact: v.strictObject({...ScriptArtifactV2Schema.entries,lane:v.literal('project')}),
13
- fields: names,signals: names,channels: names,additional:v.exactOptional(v.strictObject({fields:additionalNames,signals:additionalNames,channels:additionalNames})),outputCapacity:v.exactOptional(v.pipe(v.number(),v.integer(),v.minValue(1),v.maxValue(128))),bytes: v.pipe(v.string(),v.maxLength(2_000_000)),groups: v.exactOptional(GroupStepV2Schema) });
23
+ fields: names,signals: names,channels: names,additional:v.exactOptional(v.strictObject({fields:additionalNames,signals:additionalNames,channels:additionalNames})),outputCapacity:v.exactOptional(v.pipe(v.number(),v.integer(),v.minValue(1),v.maxValue(128))),queries:queriesSchema,batch:batchSchema,bytes: v.pipe(v.string(),v.maxLength(2_000_000)),groups: v.exactOptional(GroupStepV2Schema) });
14
24
 
15
25
  /** Nine detached field frames retain actor zero plus the original eight ordered
16
26
  * candidates. Missing fields remain absent and refuse only if actually read. */
17
- export function returnProjectionV2Layout(fields: number,pages = 1,outputCapacity = 32,groups?:GroupStepRowsV2): V2Layout {
27
+ /** WASM-HOST H13: a projection that declares `queries` may ask its owner host for the ground height under a point. A
28
+ * height the frame does not answer is a miss (kind 11: the query kind 3 and x, z); the host answers every miss (kind 12
29
+ * replies, each keyed in kind 13 by its kind and seven inputs, then its height) and runs the request again, up to
30
+ * RETURN_PROJECTION_V2_ROUNDS times. Without `queries` the frame is unchanged. */
31
+ const QUERY_MISSES = 16;
32
+ const QUERY_REPLIES = 32;
33
+ const QUERY_KEY = 8;
34
+ const QUERY_ROUNDS = 8;
35
+ const RETURN_PROJECTION_V2_QUERIES: Readonly<{ misses: number; replies: number; key: number; rounds: number }> = Object.freeze({ misses: QUERY_MISSES,replies: QUERY_REPLIES,key: QUERY_KEY,rounds: QUERY_ROUNDS });
36
+ /** WASM-HOST H13: a projection that declares `batch` also answers up to `requests` independent requests in one call (one
37
+ * reset, one crossing; R1-17). Each request has three parameters (kind 4: its signal, its first field row, its row count)
38
+ * and its fields as sparse rows (kind 6: actor * fields + field + 1, present, value; `rows` in all), and tags each output
39
+ * (kind 10) with its request index. Misses and replies scale with `requests`. */
40
+ interface ReturnProjectionV2Batch { readonly requests: number; readonly rows: number }
41
+ /** Region kinds of the projection frame. */
42
+ const KIND = Object.freeze({ binding: 1,parameters: 4,state: 5,fields: 6,outputs: 10,misses: 11,replies: 12,replyData: 13,rng: 21,rngOut: 22 });
43
+ /** Nine field frames per request: the observations and eight candidates; three parameters per batch request. */
44
+ const FRAMES = 9;
45
+ const REQUEST_PARAMETERS = 3;
46
+ const ONE = 1;
47
+ const NONE = 0;
48
+ function validBatch(batch: ReturnProjectionV2Batch): boolean {
49
+ const { requests,rows } = batch;
50
+ return Number.isInteger(requests) && requests >= MIN_BATCH_REQUESTS && requests <= MAX_BATCH_REQUESTS
51
+ && Number.isInteger(rows) && rows >= MIN_BATCH_ROWS && rows <= MAX_BATCH_ROWS;
52
+ }
53
+ /** The banks a request (or a batch of them) and its ground-height queries need, as [kind, capacity]. */
54
+ function projectionBanks(fields: number,outputCapacity: number,asks: { readonly queries?: readonly ['groundHeight']; readonly batch?: ReturnProjectionV2Batch }): (readonly [number,number])[] {
55
+ const { batch,queries } = asks;
56
+ const requests = batch?.requests ?? ONE;
57
+ const banks: (readonly [number,number])[] = [[KIND.binding,ONE],[KIND.state,ONE],[KIND.rng,ONE],[KIND.rngOut,ONE],
58
+ [KIND.parameters,batch === undefined ? ONE : REQUEST_PARAMETERS*requests],[KIND.fields,Math.max(fields*FRAMES,batch?.rows ?? NONE)],[KIND.outputs,outputCapacity*requests]];
59
+ if (queries !== undefined) {
60
+ banks.push([KIND.misses,QUERY_MISSES*requests],[KIND.replies,QUERY_REPLIES*requests],[KIND.replyData,QUERY_REPLIES*requests*(QUERY_KEY+ONE)]);
61
+ }
62
+ return banks;
63
+ }
64
+ export function returnProjectionV2Layout(fields: number,pages = 1,outputCapacity = 32,groups?:GroupStepRowsV2,queries?:readonly ['groundHeight'],batch?:ReturnProjectionV2Batch): V2Layout {
18
65
  if (!Number.isInteger(fields) || fields < 1 || fields > 256) throw new Error('Invalid projection field count');
19
66
  if (!Number.isInteger(outputCapacity) || outputCapacity < 1 || outputCapacity > 128) throw new Error('Invalid projection output capacity');
20
67
  if(groups!==undefined&&groups.pages!==pages)throw new Error('Shared projection pages differ');
68
+ if (batch !== undefined && (groups !== undefined || !validBatch(batch))) {
69
+ throw new Error('Invalid projection batch');
70
+ }
21
71
  const capacities=new Map(groups===undefined?[]:groupStepLayoutV2(groups).regions.map(row=>[row.kind,row.capacity]));
22
- for(const [kind,capacity]of [[1,1],[4,1],[5,1],[6,fields*9],[10,outputCapacity],[21,1],[22,1]] as const)
23
- capacities.set(kind,Math.max(capacities.get(kind)??0,capacity));
72
+ for (const [kind,capacity] of projectionBanks(fields,outputCapacity,{ ...(queries === undefined ? {} : { queries }),...(batch === undefined ? {} : { batch }) })) {
73
+ capacities.set(kind,Math.max(capacities.get(kind) ?? NONE,capacity));
74
+ }
24
75
  return layoutV2(0,pages,capacities);
25
76
  }
26
77
  export function parseReturnProjectionV2(input: unknown): ReturnProjectionV2Rows {
@@ -32,9 +83,9 @@ export function parseReturnProjectionV2(input: unknown): ReturnProjectionV2Rows
32
83
  /** Check cross-field rules after ReturnProjectionV2Schema has admitted the
33
84
  * complete shape. Unknown inputs must use parseReturnProjectionV2 instead.
34
85
  * A containing admitted schema can reuse its rows without parsing them again. */
35
- export function checkReturnProjectionV2Rows(row: ReturnProjectionV2Rows): void {
86
+ function checkReturnProjectionV2Rows(row: ReturnProjectionV2Rows): void {
36
87
  if(row.groups!==undefined)parseGroupStepV2(row.groups);
37
- if (row.artifact.ioBytes !== returnProjectionV2Layout(row.fields.length,row.artifact.pages,row.outputCapacity,row.groups).bytes
88
+ if (row.artifact.ioBytes !== returnProjectionV2Layout(row.fields.length,row.artifact.pages,row.outputCapacity,row.groups,row.queries,row.batch).bytes
38
89
  || [row.fields,row.signals,row.channels].some(list => new Set(list).size !== list.length || list.some((name,i) => i > 0 && (list[i-1] ?? '') >= name))) throw new Error('Invalid projection declaration');
39
90
  for(const kind of ['fields','signals','channels'] as const){
40
91
  const extra=row.additional?.[kind]??[],declared=row[kind],base=declared.slice(0,declared.length-extra.length);
@@ -59,6 +110,11 @@ interface ReturnProjectionV2RowsInputFields {
59
110
  channels: (string)[];
60
111
  };
61
112
  outputCapacity?: number;
113
+ queries?: ['groundHeight'];
114
+ batch?: {
115
+ requests: number;
116
+ rows: number;
117
+ };
62
118
  bytes: string;
63
119
  groups?: v.InferInput<typeof GroupStepV2Schema>;
64
120
  }
@@ -78,7 +134,13 @@ interface ReturnProjectionV2RowsFields {
78
134
  channels: (string)[];
79
135
  };
80
136
  outputCapacity?: number;
137
+ queries?: ['groundHeight'];
138
+ batch?: {
139
+ requests: number;
140
+ rows: number;
141
+ };
81
142
  bytes: string;
82
143
  groups?: v.InferOutput<typeof GroupStepV2Schema>;
83
144
  }
84
145
  export type ReturnProjectionV2Rows = { [Key in keyof ReturnProjectionV2RowsFields]: ReturnProjectionV2RowsFields[Key] };
146
+ export { checkReturnProjectionV2Rows, RETURN_PROJECTION_V2_QUERIES, type ReturnProjectionV2Batch };
@@ -1,4 +1,6 @@
1
1
  import * as v from 'valibot';
2
+ import { ReturnProjectionV2Schema } from './returnProjectionV2.ts';
3
+ import type { ReturnProjectionV2Rows, ReturnProjectionV2RowsInput } from './returnProjectionV2.ts';
2
4
  import { AudioMixExpressionSchema, type AudioMixExpr } from './audioProfiles.ts';
3
5
 
4
6
  const id = v.pipe(v.string(), v.minLength(1), v.maxLength(128));
@@ -74,6 +76,8 @@ const named = v.pipe(v.array(v.tuple([name, source])), v.maxLength(32));
74
76
  export const WeatherShelterCirclesSchema: v.GenericSchema<WeatherReactionRows['shelter']['circles']> = v.pipe(v.array(v.tuple([finite, finite, v.pipe(finite, v.minValue(0))])), v.maxLength(256));
75
77
  export const WeatherReactionsSchema: v.StrictObjectSchema<WeatherReactionEntries, undefined> = v.strictObject({
76
78
  version: v.literal(1), hz: v.pipe(finite, v.minValue(1), v.maxValue(10)), script: v.unknown(), bytes: v.string(),
79
+ /** WASM-HOST H13: the program's ABI-2 candidate beside its ABI-1 witness; the engine's return program admits both. */
80
+ v2: v.exactOptional(ReturnProjectionV2Schema),
77
81
  actions: v.exactOptional(v.strictObject({ event: id, unless: v.exactOptional(name) })),
78
82
  active: v.pipe(v.array(id), v.maxLength(32)), flash: name,
79
83
  shelter: v.strictObject({ margin: v.pipe(finite, v.minValue(0)), circles: WeatherShelterCirclesSchema }),
@@ -290,6 +294,7 @@ interface WeatherReactionRowsInputFields {
290
294
  hz: number;
291
295
  script: unknown;
292
296
  bytes: string;
297
+ v2?: ReturnProjectionV2RowsInput;
293
298
  actions?: {
294
299
  event: string;
295
300
  unless?: string;
@@ -352,6 +357,7 @@ interface WeatherReactionRowsFields {
352
357
  hz: number;
353
358
  script: unknown;
354
359
  bytes: string;
360
+ v2?: ReturnProjectionV2Rows;
355
361
  actions?: {
356
362
  event: string;
357
363
  unless?: string;
@@ -412,9 +418,10 @@ export type WeatherReactionRows = { [Key in keyof WeatherReactionRowsFields]: We
412
418
 
413
419
  /** Keep composition metadata available to inline module admission. */
414
420
  type WeatherReactionEntries = {
415
- [Key in Exclude<keyof WeatherReactionRows, 'actions' | 'hud'>]:
421
+ [Key in Exclude<keyof WeatherReactionRows, 'actions' | 'hud' | 'v2'>]:
416
422
  v.GenericSchema<WeatherReactionRowsInput[Key], WeatherReactionRows[Key]>;
417
423
  } & {
424
+ v2: v.ExactOptionalSchema<v.GenericSchema<ReturnProjectionV2RowsInput, ReturnProjectionV2Rows>, undefined>;
418
425
  actions: v.ExactOptionalSchema<v.GenericSchema<NonNullable<WeatherReactionRowsInput['actions']>,
419
426
  NonNullable<WeatherReactionRows['actions']>>, undefined>;
420
427
  hud: v.ExactOptionalSchema<v.GenericSchema<NonNullable<WeatherReactionRowsInput['hud']>,