@ainize/core 0.1.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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -0
  3. package/dist/ain-ledger.d.ts +191 -0
  4. package/dist/ain-ledger.d.ts.map +1 -0
  5. package/dist/ain-ledger.js +714 -0
  6. package/dist/ain-ledger.js.map +1 -0
  7. package/dist/browser.d.ts +15 -0
  8. package/dist/browser.d.ts.map +1 -0
  9. package/dist/browser.js +16 -0
  10. package/dist/browser.js.map +1 -0
  11. package/dist/bytes.d.ts +3 -0
  12. package/dist/bytes.d.ts.map +1 -0
  13. package/dist/bytes.js +14 -0
  14. package/dist/bytes.js.map +1 -0
  15. package/dist/canonical.d.ts +5 -0
  16. package/dist/canonical.d.ts.map +1 -0
  17. package/dist/canonical.js +70 -0
  18. package/dist/canonical.js.map +1 -0
  19. package/dist/catalog.d.ts +181 -0
  20. package/dist/catalog.d.ts.map +1 -0
  21. package/dist/catalog.js +496 -0
  22. package/dist/catalog.js.map +1 -0
  23. package/dist/config-schema.d.ts +222 -0
  24. package/dist/config-schema.d.ts.map +1 -0
  25. package/dist/config-schema.js +302 -0
  26. package/dist/config-schema.js.map +1 -0
  27. package/dist/config.d.ts +128 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +330 -0
  30. package/dist/config.js.map +1 -0
  31. package/dist/events.d.ts +14 -0
  32. package/dist/events.d.ts.map +1 -0
  33. package/dist/events.js +20 -0
  34. package/dist/events.js.map +1 -0
  35. package/dist/identity.d.ts +15 -0
  36. package/dist/identity.d.ts.map +1 -0
  37. package/dist/identity.js +59 -0
  38. package/dist/identity.js.map +1 -0
  39. package/dist/index.d.ts +17 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +17 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/ledger.d.ts +134 -0
  44. package/dist/ledger.d.ts.map +1 -0
  45. package/dist/ledger.js +2 -0
  46. package/dist/ledger.js.map +1 -0
  47. package/dist/lineage.d.ts +63 -0
  48. package/dist/lineage.d.ts.map +1 -0
  49. package/dist/lineage.js +156 -0
  50. package/dist/lineage.js.map +1 -0
  51. package/dist/local-ledger.d.ts +74 -0
  52. package/dist/local-ledger.d.ts.map +1 -0
  53. package/dist/local-ledger.js +229 -0
  54. package/dist/local-ledger.js.map +1 -0
  55. package/dist/npz.d.ts +107 -0
  56. package/dist/npz.d.ts.map +1 -0
  57. package/dist/npz.js +502 -0
  58. package/dist/npz.js.map +1 -0
  59. package/dist/ops.d.ts +65 -0
  60. package/dist/ops.d.ts.map +1 -0
  61. package/dist/ops.js +9 -0
  62. package/dist/ops.js.map +1 -0
  63. package/dist/teach-auth.d.ts +19 -0
  64. package/dist/teach-auth.d.ts.map +1 -0
  65. package/dist/teach-auth.js +33 -0
  66. package/dist/teach-auth.js.map +1 -0
  67. package/dist/types.d.ts +1096 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +98 -0
  70. package/dist/types.js.map +1 -0
  71. package/dist/x402.d.ts +35 -0
  72. package/dist/x402.d.ts.map +1 -0
  73. package/dist/x402.js +84 -0
  74. package/dist/x402.js.map +1 -0
  75. package/package.json +49 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ export * from './types.js';
2
+ export * from './canonical.js';
3
+ export * from './identity.js';
4
+ export * from './npz.js';
5
+ export * from './x402.js';
6
+ export * from './ledger.js';
7
+ export * from './local-ledger.js';
8
+ export * from './ain-ledger.js';
9
+ export * from './catalog.js';
10
+ export * from './lineage.js';
11
+ export * from './config.js';
12
+ export * from './config-schema.js';
13
+ export * from './events.js';
14
+ export * from './bytes.js';
15
+ export * from './teach-auth.js';
16
+ export * from './ops.js';
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC"}
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Ledger abstraction (원장 130).
3
+ *
4
+ * Two backends implement it:
5
+ * - LocalLedger — signed, content-addressed record DAG in SQLite, replicated peer-to-peer by set
6
+ * reconciliation (zero-config; the reference prototype's ledger.jsonl imports into it).
7
+ * - AinLedger — AI Network blockchain via @ainblockchain/ain-js: anchors are `knowledge.explore()`
8
+ * entries (graph edges = lineage), attestations/settlements/branches/nodes live under
9
+ * /apps/knowledge/market/* guarded by write rules, payments are AIN transfers (x402).
10
+ *
11
+ * The node derives catalog state (status machine, quorum, lineage, supersede) from the records —
12
+ * "카탈로그 = 원장의 anchor 블록 집합" (청구항 19).
13
+ */
14
+ import type { Attestation, BranchInfo, Challenge, Dispute, LedgerRecord, PatchAnchor, PeerInfo, RecordKind, Settlement } from './types.js';
15
+ export interface SupersedeRecord {
16
+ old_patch_id: string;
17
+ new_patch_id: string;
18
+ overlap_rows: number;
19
+ reason: string;
20
+ /** When the supersede was written (ms). Older on-chain records lack it — readers fall back to the new patch's attest/anchor time. */
21
+ created_at?: number;
22
+ }
23
+ /**
24
+ * Author's takedown of their own knowledge (`retire` record). The anchor is immutable and stays on the record —
25
+ * this is the only way a publisher stops selling it: the catalogue drops it, the x402 gateway answers 410, and
26
+ * buyers who already paid keep their download rights. Only a record signed by the anchor's own author counts.
27
+ */
28
+ export interface RetireRecord {
29
+ patch_id: string;
30
+ /** Why it was taken down, in the publisher's words (shown to buyers; may be empty). */
31
+ reason: string;
32
+ created_at: number;
33
+ }
34
+ /**
35
+ * A price change, signed by the anchor's own author (`price` record, finding 278).
36
+ *
37
+ * The anchor is immutable, so before this the only way to re-price a listing was to publish a new one that
38
+ * superseded it — which restarts verification, splits the sales history and makes a discount indistinguishable
39
+ * from a new version. A seller who took the 0.1 default on a first listing, or who wants to run a launch price,
40
+ * or to make an obsolete knowledge free, had no path at all. The newest record by the anchor's author wins; every
41
+ * one of them stays on the ledger, so a claimed discount can be checked against the price it was discounted from.
42
+ */
43
+ export interface PriceRecord {
44
+ patch_id: string;
45
+ /** The new price in the anchor's currency, as a decimal string ('0' makes it free). */
46
+ price: string;
47
+ currency: string;
48
+ /** Why, in the seller's words (shown to buyers on the price history; may be empty). */
49
+ reason: string;
50
+ created_at: number;
51
+ }
52
+ export interface SubscriptionRecord {
53
+ node: string;
54
+ branch: string;
55
+ action: 'subscribe' | 'unsubscribe';
56
+ patch_ids: string[];
57
+ /** When the (un)subscribe was written (ms). Older on-chain records lack it — readers fall back to the branch's creation time. */
58
+ created_at?: number;
59
+ }
60
+ /**
61
+ * A royalty transfer the seller actually made (`payout` record, finding 314).
62
+ *
63
+ * The buyer pays the seller in full and the seller then sends one transfer per creator, with no memo and nothing
64
+ * tying it to the sale — 34 reported royalties, 29 the seller claimed and 8 visible in chain state could be
65
+ * reconciled by nobody. A payout record joins the two by settle hash, so an ancestor has evidence to dispute with
66
+ * and an honest seller has evidence to show. It is written by the SELLER after the transfer returns.
67
+ */
68
+ export interface PayoutRecord {
69
+ settle_hash: string;
70
+ patch_id: string;
71
+ /** Who was paid, and how much, in the settlement's currency. */
72
+ to: string;
73
+ amount: string;
74
+ currency: string;
75
+ /** The chain transfer: its hash, and the deterministic key it was written under (`/transfer/$from/$to/$key`). */
76
+ tx_hash: string;
77
+ transfer_key: string;
78
+ created_at: number;
79
+ }
80
+ export type RecordBody = PatchAnchor | Attestation | Settlement | Challenge | BranchInfo | PeerInfo | SupersedeRecord | SubscriptionRecord | RetireRecord | PriceRecord | PayoutRecord;
81
+ export interface LedgerInfo {
82
+ kind: 'local' | 'ain';
83
+ network: string;
84
+ head?: string;
85
+ height?: number;
86
+ valid?: boolean;
87
+ records: number;
88
+ app?: string;
89
+ provider?: string;
90
+ }
91
+ export interface Ledger {
92
+ readonly kind: 'local' | 'ain';
93
+ init(): Promise<void>;
94
+ close(): Promise<void>;
95
+ info(): Promise<LedgerInfo>;
96
+ /** Append a record authored by this node (signed) and return it. */
97
+ append<T extends RecordBody>(kind: RecordKind, body: T): Promise<LedgerRecord<T>>;
98
+ /** Ingest a record received from a peer (signature is verified). Returns true if new. */
99
+ ingest(record: LedgerRecord): Promise<boolean>;
100
+ /** All records, oldest first (optionally after a timestamp). */
101
+ list(opts?: {
102
+ since?: number;
103
+ kind?: RecordKind;
104
+ limit?: number;
105
+ }): Promise<LedgerRecord[]>;
106
+ get(hash: string): Promise<LedgerRecord | null>;
107
+ hashes(since?: number): Promise<string[]>;
108
+ /** Local-ledger replication: records received after a peer-local cursor. */
109
+ sync?(since?: number, limit?: number): Promise<{
110
+ records: LedgerRecord[];
111
+ cursor: number;
112
+ }>;
113
+ /** Convenience typed views. */
114
+ anchors(): Promise<LedgerRecord<PatchAnchor>[]>;
115
+ attestations(patchId?: string): Promise<LedgerRecord<Attestation>[]>;
116
+ settlements(patchId?: string): Promise<LedgerRecord<Settlement>[]>;
117
+ challenges(patchId?: string): Promise<LedgerRecord<Challenge>[]>;
118
+ /** Contested sales and the sellers' answers (item 347). Optional: a ledger written before the kind existed has none. */
119
+ disputes?(patchId?: string): Promise<LedgerRecord<Dispute>[]>;
120
+ branches(): Promise<LedgerRecord<BranchInfo>[]>;
121
+ nodes(): Promise<LedgerRecord<PeerInfo>[]>;
122
+ supersedes(): Promise<LedgerRecord<SupersedeRecord>[]>;
123
+ subscriptions(): Promise<LedgerRecord<SubscriptionRecord>[]>;
124
+ /** Integrity check (hash + signature of every record; chain linkage for imported linear chains). */
125
+ verify(): Promise<{
126
+ valid: boolean;
127
+ checked: number;
128
+ errors: string[];
129
+ }>;
130
+ }
131
+ export interface LedgerEvents {
132
+ onRecord?: (record: LedgerRecord) => void;
133
+ }
134
+ //# sourceMappingURL=ledger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../src/ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EACV,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EACzG,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,eAAe;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,qIAAqI;IACrI,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,uFAAuF;IACvF,MA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AAM,GAAG,OAAO,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IACrE,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACnE,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IACjE,wHAAwH;IACxH,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC9D,QAAQ,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAChD,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC3C,UAAU,IAAI,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;IACvD,aAAa,IAAI,OAAO,CAAC,YAAY,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;IAE7D,oGAAoG;IACpG,MAAM,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CAC1E;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;CAC3C"}
package/dist/ledger.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger.js","sourceRoot":"","sources":["../src/ledger.ts"],"names":[],"mappings":""}
@@ -0,0 +1,63 @@
1
+ import type { BenchmarkSample, DatasetAccess, DerivationKind, PatchAnchor } from './types.js';
2
+ export declare const DERIVATION_KINDS: readonly DerivationKind[];
3
+ /** sha256 over the canonical full sample list — the commit the ≤ 32 on-chain samples are a slice of (§5.1). */
4
+ export declare function answersHash(samples: BenchmarkSample[]): string;
5
+ /**
6
+ * The samples that go on the ledger for a teach anchor: the child's own first (in order), then one per parent
7
+ * (`source` = the parent id), never more than `cap`. Returns the capped list and the hash of the FULL list.
8
+ */
9
+ export declare function capBenchmarkSamples(own: BenchmarkSample[], perParent?: {
10
+ patch_id: string;
11
+ sample?: BenchmarkSample;
12
+ }[], cap?: number): {
13
+ samples: BenchmarkSample[];
14
+ full: BenchmarkSample[];
15
+ answers_hash: string;
16
+ };
17
+ export declare const accessRank: (a: DatasetAccess | undefined) => number;
18
+ export declare const accessOf: (a: {
19
+ dataset?: {
20
+ access?: DatasetAccess;
21
+ };
22
+ } | undefined | null) => DatasetAccess;
23
+ /**
24
+ * May a child with `child` licence / access build on a parent with `parent` licence / access (§6.4)?
25
+ * - CC0 / CC-BY / ODC-By parent → any child licence;
26
+ * - CC-BY-SA parent → the child must be CC-BY-SA with access at least the parent's;
27
+ * - Proprietary parent → allowed (delta-only blob: no inherited rows, no inherited on-chain samples).
28
+ */
29
+ export declare function licenseCompatible(parent: {
30
+ license?: string;
31
+ access?: DatasetAccess;
32
+ }, child: {
33
+ license: string;
34
+ access: DatasetAccess;
35
+ }): {
36
+ ok: true;
37
+ } | {
38
+ ok: false;
39
+ reason: string;
40
+ };
41
+ /** Rows a child may publish from a Proprietary parent: none (delta-only, §6.4). */
42
+ export declare const deltaOnlyParent: (license: string | undefined) => boolean;
43
+ /**
44
+ * The anchor invariant validated for anchors this node writes (§5.1; readers stay tolerant): every id in
45
+ * `base.stack`, `dataset.parents` and `derivation.bases` is in `parents[]`; kinds, access and licence are known;
46
+ * the on-chain sample list of a teach anchor respects the cap. Returns the problems, empty when fine.
47
+ */
48
+ export declare function lineageProblems(a: Pick<PatchAnchor, 'parents' | 'derivation' | 'base' | 'dataset' | 'benchmark' | 'origin' | 'id'>): string[];
49
+ /** Ids this anchor was trained on top of / built from (ordered stack first, then the other lineage ids), de-duplicated. */
50
+ export declare function lineageIds(a: Pick<PatchAnchor, 'parents' | 'derivation' | 'base' | 'dataset'>): string[];
51
+ /**
52
+ * bfloat16 rounding of float32 values (round-to-nearest-even on the dropped 16 bits) — the representation the table
53
+ * holds, so equality checks between a delta's `before` and a parent's `after` are exact (§5.4, §8).
54
+ */
55
+ export declare function bf16Bits(f32: number): number;
56
+ /**
57
+ * `pre_state_sha256` (§5.1): sha256 over the rows sorted by address, each contributing `addr` as int64 LE followed by
58
+ * the bf16 bits (uint16 LE) of every `before` value. `before` is float32 row-major, `dim` values per row.
59
+ */
60
+ export declare function preStateSha256(addrs: BigInt64Array, before: Float32Array, dim: number): string;
61
+ /** Merkle root over canonical row lines (leaf = sha256(line)); a single leaf is its own root, an empty set hashes ''. */
62
+ export declare function merkleRoot(leaves: string[]): string;
63
+ //# sourceMappingURL=lineage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.d.ts","sourceRoot":"","sources":["../src/lineage.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9F,eAAO,MAAM,gBAAgB,EAAE,SAAS,cAAc,EAA4D,CAAC;AAEnH,+GAA+G;AAC/G,wBAAgB,WAAW,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,MAAM,CAE9D;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,eAAe,EAAE,EAAE,SAAS,GAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,eAAe,CAAA;CAAE,EAAO,EAAE,GAAG,SAAyB,GAAG;IAAE,OAAO,EAAE,eAAe,EAAE,CAAC;IAAC,IAAI,EAAE,eAAe,EAAE,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAOzO;AAGD,eAAO,MAAM,UAAU,GAAI,GAAG,aA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,MAAM,CASnD"}
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Lineage helpers shared by the node, the CLI and the verifier (docs/lineage-teach-design.md §5, §6, §12.6):
3
+ * the on-chain sample cap, the answers hash, licence compatibility and the anchor invariant.
4
+ * Pure functions — no I/O.
5
+ */
6
+ import { canonicalJson, sha256Hex } from './canonical.js';
7
+ import { DATASET_ACCESS_LEVELS, DATASET_LICENSES, TEACH_SAMPLES_ON_CHAIN, isDatasetLicense } from './types.js';
8
+ export const DERIVATION_KINDS = ['extend', 'update', 'contradict', 'merge', 'transfer'];
9
+ /** sha256 over the canonical full sample list — the commit the ≤ 32 on-chain samples are a slice of (§5.1). */
10
+ export function answersHash(samples) {
11
+ return sha256Hex(canonicalJson(samples.map((s) => ({ prompt: s.prompt, expect: s.expect, ...(s.source ? { source: s.source } : {}) }))));
12
+ }
13
+ /**
14
+ * The samples that go on the ledger for a teach anchor: the child's own first (in order), then one per parent
15
+ * (`source` = the parent id), never more than `cap`. Returns the capped list and the hash of the FULL list.
16
+ */
17
+ export function capBenchmarkSamples(own, perParent = [], cap = TEACH_SAMPLES_ON_CHAIN) {
18
+ const seen = new Set();
19
+ const full = [];
20
+ const push = (s) => { const k = `${s.prompt}\u0000${s.expect}\u0000${s.source ?? ''}`; if (seen.has(k))
21
+ return; seen.add(k); full.push(s); };
22
+ for (const s of own)
23
+ push({ prompt: s.prompt, expect: s.expect });
24
+ for (const p of perParent)
25
+ if (p.sample)
26
+ push({ prompt: p.sample.prompt, expect: p.sample.expect, source: p.patch_id });
27
+ return { samples: full.slice(0, cap), full, answers_hash: answersHash(full) };
28
+ }
29
+ const ACCESS_RANK = { private: 0, derivative: 1, public: 2 };
30
+ export const accessRank = (a) => ACCESS_RANK[a ?? 'private'];
31
+ export const accessOf = (a) => a?.dataset?.access ?? 'private';
32
+ /**
33
+ * May a child with `child` licence / access build on a parent with `parent` licence / access (§6.4)?
34
+ * - CC0 / CC-BY / ODC-By parent → any child licence;
35
+ * - CC-BY-SA parent → the child must be CC-BY-SA with access at least the parent's;
36
+ * - Proprietary parent → allowed (delta-only blob: no inherited rows, no inherited on-chain samples).
37
+ */
38
+ export function licenseCompatible(parent, child) {
39
+ if (!isDatasetLicense(child.license))
40
+ return { ok: false, reason: `bad_license: "${child.license}" is not one of ${DATASET_LICENSES.join(', ')}` };
41
+ if (parent.license === 'CC-BY-SA-4.0') {
42
+ if (child.license !== 'CC-BY-SA-4.0')
43
+ return { ok: false, reason: 'license_incompatible: the base is CC-BY-SA-4.0, so this training set must be CC-BY-SA-4.0 too' };
44
+ if (accessRank(child.access) < accessRank(parent.access))
45
+ return { ok: false, reason: `license_incompatible: the base is CC-BY-SA-4.0 with "${parent.access}" access, so this training set must be at least as open` };
46
+ }
47
+ return { ok: true };
48
+ }
49
+ /** Rows a child may publish from a Proprietary parent: none (delta-only, §6.4). */
50
+ export const deltaOnlyParent = (license) => license === 'Proprietary';
51
+ /**
52
+ * The anchor invariant validated for anchors this node writes (§5.1; readers stay tolerant): every id in
53
+ * `base.stack`, `dataset.parents` and `derivation.bases` is in `parents[]`; kinds, access and licence are known;
54
+ * the on-chain sample list of a teach anchor respects the cap. Returns the problems, empty when fine.
55
+ */
56
+ export function lineageProblems(a) {
57
+ const out = [];
58
+ const parents = new Set(a.parents ?? []);
59
+ if (parents.has(a.id))
60
+ out.push('base_cycle: an anchor cannot list itself as a parent');
61
+ if (a.derivation) {
62
+ if (!DERIVATION_KINDS.includes(a.derivation.kind))
63
+ out.push(`derivation.kind must be one of ${DERIVATION_KINDS.join(', ')}`);
64
+ for (const b of a.derivation.bases ?? [])
65
+ if (!parents.has(b.patch_id))
66
+ out.push(`derivation.bases names ${b.patch_id}, which is not in parents`);
67
+ for (const k of ['added_rows', 'changed_rows', 'removed_rows'])
68
+ if (!Number.isInteger(a.derivation[k]) || a.derivation[k] < 0)
69
+ out.push(`derivation.${k} must be a non-negative integer`);
70
+ }
71
+ if (a.base) {
72
+ if (a.base.export !== 'delta' && a.base.export !== 'squash')
73
+ out.push('base.export must be "delta" or "squash"');
74
+ for (const b of a.base.stack ?? [])
75
+ if (!parents.has(b.patch_id))
76
+ out.push(`base.stack names ${b.patch_id}, which is not in parents`);
77
+ if (a.base.export === 'delta' && !(a.base.stack ?? []).length)
78
+ out.push('a delta needs a non-empty base.stack');
79
+ if (typeof a.base.pre_state_sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(a.base.pre_state_sha256))
80
+ out.push('base.pre_state_sha256 must be a sha256 hex');
81
+ }
82
+ if (a.dataset) {
83
+ if (a.dataset.access !== undefined && !DATASET_ACCESS_LEVELS.includes(a.dataset.access))
84
+ out.push(`dataset.access must be one of ${DATASET_ACCESS_LEVELS.join(', ')}`);
85
+ if (a.dataset.license !== undefined && !isDatasetLicense(a.dataset.license))
86
+ out.push(`bad_license: dataset.license must be one of ${DATASET_LICENSES.join(', ')}`);
87
+ for (const p of a.dataset.parents ?? [])
88
+ if (!parents.has(p.patch_id))
89
+ out.push(`dataset.parents names ${p.patch_id}, which is not in parents`);
90
+ }
91
+ if (a.origin === 'teach' && (a.benchmark?.samples?.length ?? 0) > TEACH_SAMPLES_ON_CHAIN)
92
+ out.push(`a taught anchor carries at most ${TEACH_SAMPLES_ON_CHAIN} benchmark samples on the ledger (answers_hash commits to the rest)`);
93
+ return out;
94
+ }
95
+ /** Ids this anchor was trained on top of / built from (ordered stack first, then the other lineage ids), de-duplicated. */
96
+ export function lineageIds(a) {
97
+ const out = [];
98
+ const add = (id) => { if (id && !out.includes(id))
99
+ out.push(id); };
100
+ for (const b of a.base?.stack ?? [])
101
+ add(b.patch_id);
102
+ for (const b of a.derivation?.bases ?? [])
103
+ add(b.patch_id);
104
+ for (const p of a.dataset?.parents ?? [])
105
+ add(p.patch_id);
106
+ return out;
107
+ }
108
+ /**
109
+ * bfloat16 rounding of float32 values (round-to-nearest-even on the dropped 16 bits) — the representation the table
110
+ * holds, so equality checks between a delta's `before` and a parent's `after` are exact (§5.4, §8).
111
+ */
112
+ export function bf16Bits(f32) {
113
+ const buf = new ArrayBuffer(4);
114
+ const dv = new DataView(buf);
115
+ dv.setFloat32(0, f32, true);
116
+ const bits = dv.getUint32(0, true);
117
+ if ((bits & 0x7f800000) === 0x7f800000)
118
+ return (bits >>> 16) & 0xffff; // NaN / Inf: truncate
119
+ const lower = bits & 0xffff;
120
+ const upper = bits >>> 16;
121
+ const round = lower > 0x8000 || (lower === 0x8000 && (upper & 1) === 1) ? 1 : 0;
122
+ return (upper + round) & 0xffff;
123
+ }
124
+ /**
125
+ * `pre_state_sha256` (§5.1): sha256 over the rows sorted by address, each contributing `addr` as int64 LE followed by
126
+ * the bf16 bits (uint16 LE) of every `before` value. `before` is float32 row-major, `dim` values per row.
127
+ */
128
+ export function preStateSha256(addrs, before, dim) {
129
+ const n = addrs.length;
130
+ const order = [...Array(n).keys()].sort((i, j) => (addrs[i] < addrs[j] ? -1 : addrs[i] > addrs[j] ? 1 : 0));
131
+ const buf = Buffer.alloc(n * (8 + 2 * dim));
132
+ let o = 0;
133
+ for (const i of order) {
134
+ buf.writeBigInt64LE(addrs[i], o);
135
+ o += 8;
136
+ for (let d = 0; d < dim; d++) {
137
+ buf.writeUInt16LE(bf16Bits(before[i * dim + d]), o);
138
+ o += 2;
139
+ }
140
+ }
141
+ return sha256Hex(buf);
142
+ }
143
+ /** Merkle root over canonical row lines (leaf = sha256(line)); a single leaf is its own root, an empty set hashes ''. */
144
+ export function merkleRoot(leaves) {
145
+ if (!leaves.length)
146
+ return sha256Hex('');
147
+ let level = leaves.map((l) => sha256Hex(l));
148
+ while (level.length > 1) {
149
+ const next = [];
150
+ for (let i = 0; i < level.length; i += 2)
151
+ next.push(i + 1 < level.length ? sha256Hex(level[i] + level[i + 1]) : level[i]);
152
+ level = next;
153
+ }
154
+ return level[0];
155
+ }
156
+ //# sourceMappingURL=lineage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.js","sourceRoot":"","sources":["../src/lineage.ts"],"names":[],"mappings":"AAAA;;;;GAI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AAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC;YAAE,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1H,KAAK,GAAG,IAAI,CAAC;IACf,CAAC;IACD,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC"}
@@ -0,0 +1,74 @@
1
+ import type { Ledger, LedgerEvents, LedgerInfo, RecordBody, SubscriptionRecord, SupersedeRecord } from './ledger.js';
2
+ import type { Attestation, BranchInfo, Challenge, Dispute, LedgerRecord, PatchAnchor, PeerInfo, RecordKind, Settlement } from './types.js';
3
+ import type { Identity } from './identity.js';
4
+ export declare function recordHash(kind: RecordKind, body: unknown, author: string, ts: number, parents: string[]): string;
5
+ /**
6
+ * A record this ledger will never accept, however often it is offered — a hash that does not match its body, a
7
+ * signature that is not its author's.
8
+ *
9
+ * The distinction matters to whoever is feeding records in: a refusal is final, so a sync may pass over it, while
10
+ * anything else (a busy database, a write that threw part-way) is this node's problem and the record must be
11
+ * offered again rather than skipped for ever.
12
+ */
13
+ export declare class RecordRefused extends Error {
14
+ readonly refused: true;
15
+ constructor(message: string);
16
+ }
17
+ /** Is this the ledger saying "never", rather than "not now"? */
18
+ export declare function isRecordRefusal(e: unknown): boolean;
19
+ export declare class LocalLedger implements Ledger {
20
+ private readonly path;
21
+ private readonly identity;
22
+ private readonly events;
23
+ private readonly network;
24
+ readonly kind: "local";
25
+ private db;
26
+ constructor(path: string, identity: Identity, events?: LedgerEvents, network?: string);
27
+ init(): Promise<void>;
28
+ close(): Promise<void>;
29
+ info(): Promise<LedgerInfo>;
30
+ private frontier;
31
+ append<T extends RecordBody>(kind: RecordKind, body: T): Promise<LedgerRecord<T>>;
32
+ ingest(record: LedgerRecord): Promise<boolean>;
33
+ static validate(record: LedgerRecord): boolean;
34
+ /** Legacy prototype block: {i,t,kind,body,prev,hash,sig(HMAC)} mapped into a record. */
35
+ private static validateLegacy;
36
+ private has;
37
+ private insert;
38
+ private rowToRecord;
39
+ list(opts?: {
40
+ since?: number;
41
+ kind?: RecordKind;
42
+ limit?: number;
43
+ }): Promise<LedgerRecord[]>;
44
+ get(hash: string): Promise<LedgerRecord | null>;
45
+ /** Records received after `since` (peer-local receive clock) plus the cursor to continue from. */
46
+ sync(since?: number, limit?: number): Promise<{
47
+ records: LedgerRecord[];
48
+ cursor: number;
49
+ }>;
50
+ hashes(since?: number): Promise<string[]>;
51
+ private byKind;
52
+ anchors(): Promise<LedgerRecord<PatchAnchor>[]>;
53
+ attestations(patchId?: string): Promise<LedgerRecord<Attestation>[]>;
54
+ settlements(patchId?: string): Promise<LedgerRecord<Settlement>[]>;
55
+ challenges(patchId?: string): Promise<LedgerRecord<Challenge>[]>;
56
+ disputes(patchId?: string): Promise<LedgerRecord<Dispute>[]>;
57
+ branches(): Promise<LedgerRecord<BranchInfo>[]>;
58
+ nodes(): Promise<LedgerRecord<PeerInfo>[]>;
59
+ supersedes(): Promise<LedgerRecord<SupersedeRecord>[]>;
60
+ subscriptions(): Promise<LedgerRecord<SubscriptionRecord>[]>;
61
+ verify(): Promise<{
62
+ valid: boolean;
63
+ checked: number;
64
+ errors: string[];
65
+ }>;
66
+ /**
67
+ * Import the reference prototype's ledger.jsonl (Python HMAC chain). Blocks become records whose
68
+ * `hash` is the original block hash; their Python-shaped bodies are normalised onto our schema by the
69
+ * caller (see node/seed.ts) — here we only preserve them verbatim so `verify()` can re-check the chain.
70
+ */
71
+ importPrototypeLedger(file: string): Promise<number>;
72
+ }
73
+ export type { SupersedeRecord, SubscriptionRecord };
74
+ //# sourceMappingURL=local-ledger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-ledger.d.ts","sourceRoot":"","sources":["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