app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

interop-manifest.ts (8284B)


      1 import assert from 'node:assert/strict';
      2 import { createHash } from 'node:crypto';
      3 import { checkFoodReplay } from './food-bidirectional.ts';
      4 import type { webTemplates } from './food-writer-cases.ts';
      5 
      6 export type InteropInputs = ReadonlyMap<string, Uint8Array>;
      7 const food = 'contracts/interop/food_availability/';
      8 const revision = '189c49b74b4bafc142b00b76b296477931139e72';
      9 export const interopInputPaths = [
     10   `${food}corpus.v1.json`,
     11   `${food}source_profile.v1.json`,
     12   `${food}provenance.v1.json`,
     13   `${food}oracle/src/main.rs`,
     14   `${food}oracle/Cargo.toml`,
     15   'radroots.lib.source-lock.v1.toml',
     16   'web/tests/conformance/food-writer-rust.v1.json',
     17   `${food}web_templates.v1.json`,
     18   `${food}numeric_boundaries.v1.json`,
     19   `${food}oracle/tests/native_manifest.rs`,
     20   `${food}oracle/tests/support/mod.rs`
     21 ] as const;
     22 const hash = (bytes: Uint8Array) =>
     23   createHash('sha256').update(bytes).digest('hex');
     24 interface Vector {
     25   id: string;
     26   kind: string;
     27   input: Record<string, unknown>;
     28   expected: Record<string, unknown>;
     29   signed_wires: Record<string, string>;
     30 }
     31 interface Corpus {
     32   contract_version: string;
     33   revision: string;
     34   source_sha256: string;
     35   vectors: Vector[];
     36 }
     37 interface Boundary {
     38   id: string;
     39   kind: 'timestamp' | 'identifier';
     40   value: string;
     41   expected: {
     42     native: Record<string, unknown>;
     43     typescript: { result: string; reason?: string };
     44   };
     45 }
     46 export function buildInteropManifest(
     47   inputs: InteropInputs,
     48   templates: typeof webTemplates
     49 ) {
     50   assert.deepEqual([...inputs.keys()].sort(), [...interopInputPaths].sort());
     51   const bytes = (path: string) => {
     52     const value = inputs.get(path);
     53     assert.ok(value && value.byteLength > 0 && value.byteLength <= 1024 * 1024);
     54     return value;
     55   };
     56   const text = (path: string) =>
     57     new TextDecoder('utf-8', { fatal: true }).decode(bytes(path));
     58   const descriptor = (path: string) => ({
     59     path,
     60     sha256: hash(bytes(path)),
     61     bytes: bytes(path).byteLength
     62   });
     63   const corpus = JSON.parse(text(`${food}corpus.v1.json`)) as Corpus;
     64   const profile = JSON.parse(text(`${food}source_profile.v1.json`)) as {
     65     contract_version: string;
     66     suite: string;
     67     vectors: { id: string; kind: string; input: unknown; expected: unknown }[];
     68   };
     69   const provenance = JSON.parse(text(`${food}provenance.v1.json`)) as {
     70     public_source: { repository: string; revision: string; sha256: string };
     71     corpus: { path: string; sha256: string; bytes: number };
     72   };
     73   assert.equal(corpus.revision, revision);
     74   assert.equal(provenance.public_source.revision, revision);
     75   assert.equal(
     76     provenance.public_source.repository,
     77     'https://github.com/radrootslabs/lib'
     78   );
     79   assert.equal(corpus.contract_version, '1.0.0');
     80   assert.equal(profile.contract_version, corpus.contract_version);
     81   assert.equal(profile.suite, 'food_availability_profile');
     82   assert.equal(
     83     corpus.source_sha256,
     84     hash(bytes(`${food}source_profile.v1.json`))
     85   );
     86   assert.equal(provenance.public_source.sha256, corpus.source_sha256);
     87   assert.deepEqual(provenance.corpus, {
     88     path: 'corpus.v1.json',
     89     sha256: hash(bytes(`${food}corpus.v1.json`)),
     90     bytes: bytes(`${food}corpus.v1.json`).byteLength
     91   });
     92   assert.equal(corpus.vectors.length, 40);
     93   assert.equal(profile.vectors.length, 40);
     94   assert.deepEqual(
     95     corpus.vectors.map(({ id, kind, input, expected }) => ({
     96       id,
     97       kind,
     98       input,
     99       expected
    100     })),
    101     profile.vectors
    102   );
    103   assert.deepEqual(
    104     [
    105       ...text('radroots.lib.source-lock.v1.toml').matchAll(
    106         /^revision = "([0-9a-f]{40})"$/gmu
    107       )
    108     ].map((match) => match[1]),
    109     [revision]
    110   );
    111   assert.ok(text(`${food}oracle/Cargo.toml`).includes(`rev = "${revision}"`));
    112   const replay = JSON.parse(
    113     text('web/tests/conformance/food-writer-rust.v1.json')
    114   ) as unknown;
    115   checkFoodReplay(
    116     replay,
    117     templates,
    118     [
    119       'corpus.v1.json',
    120       'source_profile.v1.json',
    121       'provenance.v1.json',
    122       'oracle/src/main.rs'
    123     ].map((name) => [name, bytes(food + name)] as const)
    124   );
    125   const templateBytes = new TextEncoder().encode(
    126     JSON.stringify(templates, null, 2) + '\n'
    127   );
    128   assert.deepEqual(bytes(`${food}web_templates.v1.json`), templateBytes);
    129   const boundaries = JSON.parse(text(`${food}numeric_boundaries.v1.json`)) as {
    130     schema_version: number;
    131     profile: string;
    132     contract_version: string;
    133     revision: string;
    134     vectors: Boundary[];
    135   };
    136   assert.equal(boundaries.schema_version, 1);
    137   assert.equal(boundaries.profile, profile.suite);
    138   assert.equal(boundaries.contract_version, profile.contract_version);
    139   assert.equal(boundaries.revision, revision);
    140   assert.equal(boundaries.vectors.length, 10);
    141   const fixture = (path: string, caseIndex: number, caseId: string) => ({
    142     ...descriptor(path),
    143     case_index: caseIndex,
    144     case_id: caseId
    145   });
    146   const publicCases = corpus.vectors.map((row, index) => {
    147     const reader = typeof row.signed_wires.event === 'string';
    148     return {
    149       id: row.id,
    150       protocol: {
    151         profile: profile.suite,
    152         contract_version: profile.contract_version
    153       },
    154       fixture: fixture(`${food}corpus.v1.json`, index, row.id),
    155       oracle_revision: revision,
    156       expected: row.expected,
    157       participation: {
    158         rust_oracle: 'REQUIRED',
    159         native_consumer: 'REQUIRED_NATIVE_TEST',
    160         typescript: reader ? 'food_reader' : 'UNSUPPORTED_CURRENT_ADAPTER',
    161         unsupported_reason: reader
    162           ? null
    163           : row.kind.includes('validate_revision')
    164             ? 'Current website reader is not a revision-transition adapter; original head-selection checkpoints retain this obligation.'
    165             : 'Public authored-details recipes include profile capabilities beyond the strict text-only prototype builder; its separate sixteen actual outputs are shared below.'
    166       }
    167     };
    168   });
    169   const writers = templates.map((row, index) => ({
    170     id: row.id,
    171     protocol: {
    172       profile: profile.suite,
    173       contract_version: profile.contract_version
    174     },
    175     fixture: fixture(`${food}web_templates.v1.json`, index, row.id),
    176     oracle_revision: revision,
    177     expected: { result: 'accepted', wire_parts: row.wire_parts },
    178     participation: {
    179       rust_oracle: 'REQUIRED',
    180       native_consumer: 'REQUIRED_NATIVE_TEST',
    181       typescript: 'food_writer',
    182       unsupported_reason: null
    183     }
    184   }));
    185   const boundaryCases = boundaries.vectors.map((row, index) => ({
    186     id: row.id,
    187     protocol: {
    188       profile: profile.suite,
    189       contract_version: profile.contract_version
    190     },
    191     fixture: fixture(`${food}numeric_boundaries.v1.json`, index, row.id),
    192     oracle_revision: revision,
    193     expected: row.expected,
    194     participation: {
    195       rust_oracle: 'REQUIRED',
    196       native_consumer: 'REQUIRED_NATIVE_TEST',
    197       typescript:
    198         row.expected.typescript.result === 'unsupported'
    199           ? 'UNSUPPORTED_JAVASCRIPT_U64'
    200           : 'food_boundary',
    201       unsupported_reason: row.expected.typescript.reason ?? null
    202     }
    203   }));
    204   const cases = [...publicCases, ...writers, ...boundaryCases];
    205   assert.equal(cases.length, 66);
    206   assert.equal(new Set(cases.map((row) => row.id)).size, cases.length);
    207   assert.equal(
    208     publicCases.filter((row) => row.participation.typescript === 'food_reader')
    209       .length,
    210     18
    211   );
    212   return {
    213     schema_version: 1,
    214     qualification: 'FIXTURE_ONLY_NOT_DEPLOYED_TERA',
    215     signing: 'NOT_RUN_NO_KEYS',
    216     oracle: { repository: provenance.public_source.repository, revision },
    217     inputs: interopInputPaths.map(descriptor),
    218     fixture_authority: {
    219       public_corpus:
    220         'Existing pinned public corpus/profile/provenance and producer remain immutable.',
    221       website_templates:
    222         'Derived from actual source-controlled Food builder recipes; regenerate only by explicit reviewed generate-interop-manifest.mjs --write. No real user data.',
    223       manifest:
    224         'Derived metadata and expectations, not a second editable protocol policy.'
    225     },
    226     pending_checks: [
    227       'Message_adapters_HCP071_HCP130',
    228       'deployed_Tera_qualification'
    229     ],
    230     cases
    231   };
    232 }
    233 export function validateInteropManifest(
    234   manifest: unknown,
    235   inputs: InteropInputs,
    236   templates: typeof webTemplates
    237 ) {
    238   assert.deepEqual(manifest, buildInteropManifest(inputs, templates));
    239 }