browser-model-manager 0.0.0-stage → 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 (51) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -0
  3. package/PROVENANCE.json +16 -0
  4. package/README.md +607 -2
  5. package/THIRD_PARTY_NOTICES +4 -0
  6. package/dist/acquisition/grants.d.ts +31 -0
  7. package/dist/acquisition/grants.js +75 -0
  8. package/dist/acquisition/index.d.ts +4 -0
  9. package/dist/acquisition/index.js +6 -0
  10. package/dist/acquisition/output.d.ts +12 -0
  11. package/dist/acquisition/output.js +19 -0
  12. package/dist/acquisition/phases.d.ts +3 -0
  13. package/dist/acquisition/phases.js +64 -0
  14. package/dist/acquisition/progress.d.ts +29 -0
  15. package/dist/acquisition/progress.js +124 -0
  16. package/dist/acquisition/slots.d.ts +35 -0
  17. package/dist/acquisition/slots.js +296 -0
  18. package/dist/cache/index.d.ts +3 -0
  19. package/dist/cache/index.js +4 -0
  20. package/dist/cache/model-cache.d.ts +152 -0
  21. package/dist/cache/model-cache.js +1585 -0
  22. package/dist/cache/sha256.d.ts +11 -0
  23. package/dist/cache/sha256.js +97 -0
  24. package/dist/cache/shard-store.d.ts +89 -0
  25. package/dist/cache/shard-store.js +204 -0
  26. package/dist/contracts/budgets.d.ts +41 -0
  27. package/dist/contracts/budgets.js +48 -0
  28. package/dist/contracts/errors.d.ts +43 -0
  29. package/dist/contracts/errors.js +2 -0
  30. package/dist/contracts/grant.d.ts +47 -0
  31. package/dist/contracts/grant.js +44 -0
  32. package/dist/contracts/index.d.ts +6 -0
  33. package/dist/contracts/index.js +7 -0
  34. package/dist/contracts/leases.d.ts +20 -0
  35. package/dist/contracts/leases.js +14 -0
  36. package/dist/contracts/lifecycle.d.ts +88 -0
  37. package/dist/contracts/lifecycle.js +12 -0
  38. package/dist/contracts/manifest.d.ts +95 -0
  39. package/dist/contracts/manifest.js +174 -0
  40. package/dist/handles/cross-tab.d.ts +16 -0
  41. package/dist/handles/cross-tab.js +29 -0
  42. package/dist/handles/handle.d.ts +24 -0
  43. package/dist/handles/handle.js +93 -0
  44. package/dist/handles/index.d.ts +3 -0
  45. package/dist/handles/index.js +4 -0
  46. package/dist/handles/leases.d.ts +45 -0
  47. package/dist/handles/leases.js +103 -0
  48. package/dist/index.d.ts +5 -0
  49. package/dist/index.js +7 -0
  50. package/package.json +43 -4
  51. package/schema/model-manifest.v1.schema.json +215 -0
@@ -0,0 +1,103 @@
1
+ // Reference-counted asset leases (fm-ffn7, contracts/leases.ts). A loaded handle leases the verified local assets it
2
+ // needs; manager eviction and deletion skip any asset with a count above zero. When the platform takes a leased asset
3
+ // anyway (browser eviction, corruption, another tab's deletion, device or worker loss), invalidate() tells every
4
+ // holder once, with the contract's mapped error and settlement rule. Leases constrain the manager, not the browser.
5
+ import { LEASE_INVALIDATION, } from '../contracts/leases.js';
6
+ export function invalidationError(sha256, cause) {
7
+ const code = LEASE_INVALIDATION[cause].error;
8
+ const message = `asset ${sha256.slice(0, 12)} is no longer available locally (${cause}); nothing is refetched inside an operation`;
9
+ if (code === 'evicted' || code === 'corrupt')
10
+ return { code, sha256, message };
11
+ return { code: 'invalidated', cause, message };
12
+ }
13
+ export const lockName = (sha256) => `facemorph-model-asset:${sha256}`;
14
+ /** With `locks`, every leased asset also holds a shared Web Lock until its last local lease is released, so another
15
+ * tab's eviction (which needs the lock exclusively) keeps it too. */
16
+ export function createLeaseRegistry({ locks } = {}) {
17
+ const holders = new Map();
18
+ const held = new Map();
19
+ const hold = (sha256) => {
20
+ if (!locks || held.has(sha256))
21
+ return;
22
+ let free = () => { };
23
+ const released = new Promise((r) => (free = r));
24
+ held.set(sha256, free);
25
+ void locks.request(lockName(sha256), { mode: 'shared' }, () => released);
26
+ };
27
+ const unhold = (sha256) => {
28
+ held.get(sha256)?.();
29
+ held.delete(sha256);
30
+ };
31
+ const count = (sha256) => [...(holders.get(sha256)?.values() ?? [])].reduce((a, b) => a + b, 0);
32
+ return {
33
+ lease(assets, holder) {
34
+ return assets.map(({ sha256, bytes }) => {
35
+ const byHolder = holders.get(sha256) ?? new Map();
36
+ holders.set(sha256, byHolder);
37
+ hold(sha256);
38
+ byHolder.set(holder, (byHolder.get(holder) ?? 0) + 1);
39
+ let released = false;
40
+ return {
41
+ sha256,
42
+ bytes,
43
+ get refCount() {
44
+ return count(sha256);
45
+ },
46
+ release() {
47
+ if (released)
48
+ return;
49
+ released = true;
50
+ const n = (byHolder.get(holder) ?? 1) - 1;
51
+ if (n > 0)
52
+ byHolder.set(holder, n);
53
+ else
54
+ byHolder.delete(holder);
55
+ if (byHolder.size === 0 && holders.get(sha256) === byHolder) {
56
+ holders.delete(sha256);
57
+ unhold(sha256);
58
+ }
59
+ },
60
+ };
61
+ });
62
+ },
63
+ refCount: count,
64
+ leased: () => [...holders.keys()].sort(),
65
+ invalidate(sha256, cause) {
66
+ const byHolder = holders.get(sha256);
67
+ if (!byHolder)
68
+ return 0;
69
+ const error = invalidationError(sha256, cause);
70
+ const told = [...byHolder.keys()];
71
+ for (const h of told)
72
+ h.invalidated(cause, error);
73
+ return told.length;
74
+ },
75
+ };
76
+ }
77
+ /** Manager-controlled eviction: removes the requested assets from every store except leased ones, in this tab (local
78
+ * count) and, with `locks`, in any tab (an asset whose lock cannot be taken exclusively right now is kept). */
79
+ export async function evictUnleased(sha256s, stores, registry, locks) {
80
+ const removed = [];
81
+ const kept = [];
82
+ for (const sha of sha256s) {
83
+ if (registry.refCount(sha) > 0) {
84
+ kept.push(sha);
85
+ continue;
86
+ }
87
+ const remove = async () => {
88
+ for (const store of stores)
89
+ await store.remove(sha);
90
+ removed.push(sha);
91
+ };
92
+ if (!locks)
93
+ await remove();
94
+ else
95
+ await locks.request(lockName(sha), { mode: 'exclusive', ifAvailable: true }, async (lock) => {
96
+ if (lock)
97
+ await remove();
98
+ else
99
+ kept.push(sha);
100
+ });
101
+ }
102
+ return { removed, kept };
103
+ }
@@ -0,0 +1,5 @@
1
+ export declare const PACKAGE: 'browser-model-manager';
2
+ export * from './contracts/index.ts';
3
+ export * from './cache/index.ts';
4
+ export * from './handles/index.ts';
5
+ export * from './acquisition/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // browser-model-manager: the manager contracts (fm-hwh) and the model cache, shard store and
2
+ // SHA-256 moved from the candidate (fm-eulc), and the acquisition primitives (fm-fr9s).
3
+ export const PACKAGE = 'browser-model-manager';
4
+ export * from './contracts/index.js';
5
+ export * from './cache/index.js';
6
+ export * from './handles/index.js';
7
+ export * from './acquisition/index.js';
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "browser-model-manager",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Consented acquisition, verification, storage and eviction of browser model assets. Contracts so far (fm-hwh): lifecycle, leases, grants, budgets and the model manifest schema.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/facemorph-me/facemorph.me.git",
10
+ "directory": "packages/browser-model-manager"
11
+ },
12
+ "homepage": "https://github.com/facemorph-me/facemorph.me/tree/main/packages/browser-model-manager#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/facemorph-me/facemorph.me/issues"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "source": "./src/index.ts",
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "schema",
26
+ "LICENSE",
27
+ "THIRD_PARTY_NOTICES",
28
+ "PROVENANCE.json",
29
+ "CHANGELOG.md"
30
+ ],
31
+ "dependencies": {
32
+ "stylegan-js": "^0.1.0"
33
+ },
34
+ "engines": {
35
+ "node": ">=24"
36
+ },
37
+ "scripts": {
38
+ "typecheck": "tsc -p tsconfig.json --incremental && tsc -p tsconfig.test.json --incremental",
39
+ "build": "tsc -p tsconfig.build.json && node ../../scripts/packages/write-provenance.mjs",
40
+ "test": "node test/run.mjs test/*.test.ts"
41
+ },
42
+ "devDependencies": {
43
+ "onnxruntime-web": "^1.30.0"
44
+ }
45
+ }
@@ -0,0 +1,215 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/facemorph-me/facemorph.me/blob/main/packages/browser-model-manager/schema/model-manifest.v1.schema.json",
4
+ "title": "FaceMorph model manifest, major 1",
5
+ "description": "Producer-facing schema owned by browser-model-manager. cloudflare-model-host and model-conv validate the manifests they emit against it. An asset is either hosted (an https url) or unpublished (a relative file path, as model-conv writes it before hosting adds URLs). Fields beyond those listed are allowed within major 1; a newer major is rejected.",
6
+ "type": "object",
7
+ "required": [
8
+ "schemaVersion",
9
+ "bundleVersion",
10
+ "modelSourceSha256",
11
+ "noiseSha256",
12
+ "mapping",
13
+ "synthesis",
14
+ "noise"
15
+ ],
16
+ "properties": {
17
+ "schemaVersion": {
18
+ "const": 1
19
+ },
20
+ "bundleVersion": {
21
+ "type": "string",
22
+ "minLength": 1
23
+ },
24
+ "releaseQualified": {
25
+ "type": "boolean"
26
+ },
27
+ "modelSourceSha256": {
28
+ "$ref": "#/$defs/sha256"
29
+ },
30
+ "noiseSha256": {
31
+ "$ref": "#/$defs/sha256"
32
+ },
33
+ "mapping": {
34
+ "$ref": "#/$defs/asset"
35
+ },
36
+ "synthesis": {
37
+ "$ref": "#/$defs/asset"
38
+ },
39
+ "average": {
40
+ "$ref": "#/$defs/asset"
41
+ },
42
+ "sampleIndices": {
43
+ "$ref": "#/$defs/asset"
44
+ },
45
+ "noise": {
46
+ "type": "array",
47
+ "minItems": 1,
48
+ "items": {
49
+ "type": "object",
50
+ "allOf": [
51
+ {
52
+ "$ref": "#/$defs/asset"
53
+ }
54
+ ],
55
+ "required": ["name", "shape"],
56
+ "properties": {
57
+ "name": {
58
+ "type": "string",
59
+ "minLength": 1
60
+ },
61
+ "shape": {
62
+ "type": "array",
63
+ "items": {
64
+ "type": "integer",
65
+ "minimum": 1
66
+ }
67
+ }
68
+ }
69
+ }
70
+ },
71
+ "encoder": {
72
+ "$ref": "#/$defs/asset"
73
+ },
74
+ "encoderStream": {
75
+ "type": "object",
76
+ "allOf": [
77
+ {
78
+ "$ref": "#/$defs/asset"
79
+ }
80
+ ],
81
+ "required": ["sourceEncoderSha256", "preprocessingSha256", "phoneAdmitted", "maxWasmBytes"],
82
+ "properties": {
83
+ "sourceEncoderSha256": {
84
+ "$ref": "#/$defs/sha256"
85
+ },
86
+ "preprocessingSha256": {
87
+ "$ref": "#/$defs/sha256"
88
+ },
89
+ "phoneAdmitted": {
90
+ "type": "boolean"
91
+ },
92
+ "maxWasmBytes": {
93
+ "type": "integer",
94
+ "minimum": 1,
95
+ "maximum": 268435456
96
+ },
97
+ "releaseQualified": {
98
+ "type": "boolean"
99
+ }
100
+ }
101
+ },
102
+ "landmarks": {
103
+ "$ref": "#/$defs/asset"
104
+ },
105
+ "alignmentSha256": {
106
+ "$ref": "#/$defs/sha256"
107
+ },
108
+ "kernel": {
109
+ "type": "object",
110
+ "required": ["candidateId", "sourceHash", "sha256"],
111
+ "properties": {
112
+ "candidateId": {
113
+ "type": "string",
114
+ "minLength": 1
115
+ },
116
+ "sourceHash": {
117
+ "$ref": "#/$defs/sha256"
118
+ },
119
+ "sha256": {
120
+ "$ref": "#/$defs/sha256"
121
+ },
122
+ "file": {
123
+ "type": "string"
124
+ },
125
+ "url": {
126
+ "$ref": "#/$defs/httpsUrl"
127
+ }
128
+ }
129
+ }
130
+ },
131
+ "$defs": {
132
+ "sha256": {
133
+ "type": "string",
134
+ "pattern": "^[0-9a-f]{64}$"
135
+ },
136
+ "httpsUrl": {
137
+ "type": "string",
138
+ "pattern": "^https://[^/@\\s]+/\\S*$"
139
+ },
140
+ "asset": {
141
+ "type": "object",
142
+ "required": ["sha256", "size"],
143
+ "properties": {
144
+ "url": {
145
+ "$ref": "#/$defs/httpsUrl"
146
+ },
147
+ "sha256": {
148
+ "$ref": "#/$defs/sha256"
149
+ },
150
+ "size": {
151
+ "type": "integer",
152
+ "minimum": 0
153
+ },
154
+ "chunks": {
155
+ "type": "array",
156
+ "minItems": 1,
157
+ "items": {
158
+ "type": "object",
159
+ "required": ["sha256", "size"],
160
+ "properties": {
161
+ "url": {
162
+ "$ref": "#/$defs/httpsUrl"
163
+ },
164
+ "sha256": {
165
+ "$ref": "#/$defs/sha256"
166
+ },
167
+ "size": {
168
+ "type": "integer",
169
+ "minimum": 1
170
+ },
171
+ "file": {
172
+ "$ref": "#/$defs/relativeFile"
173
+ }
174
+ },
175
+ "oneOf": [
176
+ {
177
+ "required": ["url"],
178
+ "not": {
179
+ "required": ["file"]
180
+ }
181
+ },
182
+ {
183
+ "required": ["file"],
184
+ "not": {
185
+ "required": ["url"]
186
+ }
187
+ }
188
+ ]
189
+ }
190
+ },
191
+ "file": {
192
+ "$ref": "#/$defs/relativeFile"
193
+ }
194
+ },
195
+ "oneOf": [
196
+ {
197
+ "required": ["url"],
198
+ "not": {
199
+ "required": ["file"]
200
+ }
201
+ },
202
+ {
203
+ "required": ["file"],
204
+ "not": {
205
+ "required": ["url"]
206
+ }
207
+ }
208
+ ]
209
+ },
210
+ "relativeFile": {
211
+ "type": "string",
212
+ "pattern": "^(?![/\\\\])(?!.*(^|/)\\.\\.(/|$))(?![a-zA-Z][a-zA-Z0-9+.-]*:)[^\\s\\\\]+$"
213
+ }
214
+ }
215
+ }