@seanhogg/builderforce-webcontainers 2026.10.12 → 2026.10.14
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/README.md +22 -0
- package/dist/adaptive/controller.d.ts +39 -0
- package/dist/adaptive/controller.d.ts.map +1 -0
- package/dist/adaptive/controller.js +108 -0
- package/dist/adaptive/controller.js.map +1 -0
- package/dist/adaptive/costModel.d.ts +37 -0
- package/dist/adaptive/costModel.d.ts.map +1 -0
- package/dist/adaptive/costModel.js +102 -0
- package/dist/adaptive/costModel.js.map +1 -0
- package/dist/adaptive/planner.d.ts +22 -0
- package/dist/adaptive/planner.d.ts.map +1 -0
- package/dist/adaptive/planner.js +114 -0
- package/dist/adaptive/planner.js.map +1 -0
- package/dist/adaptive/types.d.ts +138 -0
- package/dist/adaptive/types.d.ts.map +1 -0
- package/dist/adaptive/types.js +12 -0
- package/dist/adaptive/types.js.map +1 -0
- package/dist/assets.js +2 -2
- package/dist/check/cachedFetch.js.map +1 -1
- package/dist/check/index.d.ts +6 -1
- package/dist/check/index.d.ts.map +1 -1
- package/dist/check/index.js +6 -1
- package/dist/check/index.js.map +1 -1
- package/dist/componentCompilers.d.ts.map +1 -1
- package/dist/componentCompilers.js.map +1 -1
- package/dist/esbuildWasm.js.map +1 -1
- package/dist/host.d.ts +11 -5
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +19 -8
- package/dist/host.js.map +1 -1
- package/dist/inference/backends/mock.d.ts +24 -0
- package/dist/inference/backends/mock.d.ts.map +1 -0
- package/dist/inference/backends/mock.js +55 -0
- package/dist/inference/backends/mock.js.map +1 -0
- package/dist/inference/backends/onnx.d.ts +33 -0
- package/dist/inference/backends/onnx.d.ts.map +1 -0
- package/dist/inference/backends/onnx.js +60 -0
- package/dist/inference/backends/onnx.js.map +1 -0
- package/dist/inference/backends/webllm.d.ts +27 -0
- package/dist/inference/backends/webllm.d.ts.map +1 -0
- package/dist/inference/backends/webllm.js +52 -0
- package/dist/inference/backends/webllm.js.map +1 -0
- package/dist/inference/backends/wllama.d.ts +29 -0
- package/dist/inference/backends/wllama.d.ts.map +1 -0
- package/dist/inference/backends/wllama.js +51 -0
- package/dist/inference/backends/wllama.js.map +1 -0
- package/dist/inference/capabilities.d.ts +20 -0
- package/dist/inference/capabilities.d.ts.map +1 -0
- package/dist/inference/capabilities.js +33 -0
- package/dist/inference/capabilities.js.map +1 -0
- package/dist/inference/client.d.ts +45 -0
- package/dist/inference/client.d.ts.map +1 -0
- package/dist/inference/client.js +201 -0
- package/dist/inference/client.js.map +1 -0
- package/dist/inference/modelCache.d.ts +71 -0
- package/dist/inference/modelCache.d.ts.map +1 -0
- package/dist/inference/modelCache.js +91 -0
- package/dist/inference/modelCache.js.map +1 -0
- package/dist/inference/protocol.d.ts +94 -0
- package/dist/inference/protocol.d.ts.map +1 -0
- package/dist/inference/protocol.js +19 -0
- package/dist/inference/protocol.js.map +1 -0
- package/dist/inference/tokenStream.d.ts +26 -0
- package/dist/inference/tokenStream.d.ts.map +1 -0
- package/dist/inference/tokenStream.js +79 -0
- package/dist/inference/tokenStream.js.map +1 -0
- package/dist/inference/types.d.ts +108 -0
- package/dist/inference/types.d.ts.map +1 -0
- package/dist/inference/types.js +8 -0
- package/dist/inference/types.js.map +1 -0
- package/dist/inference/worker.bundle.js +5 -0
- package/dist/inference/worker.d.ts +35 -0
- package/dist/inference/worker.d.ts.map +1 -0
- package/dist/inference/worker.js +157 -0
- package/dist/inference/worker.js.map +1 -0
- package/dist/inference/workerEntry.d.ts +15 -0
- package/dist/inference/workerEntry.d.ts.map +1 -0
- package/dist/inference/workerEntry.js +17 -0
- package/dist/inference/workerEntry.js.map +1 -0
- package/dist/kvcache/advanced.d.ts +39 -0
- package/dist/kvcache/advanced.d.ts.map +1 -0
- package/dist/kvcache/advanced.js +59 -0
- package/dist/kvcache/advanced.js.map +1 -0
- package/dist/kvcache/compatibility.d.ts +14 -0
- package/dist/kvcache/compatibility.d.ts.map +1 -0
- package/dist/kvcache/compatibility.js +44 -0
- package/dist/kvcache/compatibility.js.map +1 -0
- package/dist/kvcache/lru.d.ts +45 -0
- package/dist/kvcache/lru.d.ts.map +1 -0
- package/dist/kvcache/lru.js +91 -0
- package/dist/kvcache/lru.js.map +1 -0
- package/dist/kvcache/paged.d.ts +86 -0
- package/dist/kvcache/paged.d.ts.map +1 -0
- package/dist/kvcache/paged.js +238 -0
- package/dist/kvcache/paged.js.map +1 -0
- package/dist/kvcache/quant.d.ts +30 -0
- package/dist/kvcache/quant.d.ts.map +1 -0
- package/dist/kvcache/quant.js +73 -0
- package/dist/kvcache/quant.js.map +1 -0
- package/dist/kvcache/radix.d.ts +28 -0
- package/dist/kvcache/radix.d.ts.map +1 -0
- package/dist/kvcache/radix.js +58 -0
- package/dist/kvcache/radix.js.map +1 -0
- package/dist/kvcache/runtime.d.ts +11 -0
- package/dist/kvcache/runtime.d.ts.map +1 -0
- package/dist/kvcache/runtime.js +204 -0
- package/dist/kvcache/runtime.js.map +1 -0
- package/dist/kvcache/types.d.ts +151 -0
- package/dist/kvcache/types.d.ts.map +1 -0
- package/dist/kvcache/types.js +10 -0
- package/dist/kvcache/types.js.map +1 -0
- package/dist/learning/fingerprint.d.ts +17 -0
- package/dist/learning/fingerprint.d.ts.map +1 -0
- package/dist/learning/fingerprint.js +46 -0
- package/dist/learning/fingerprint.js.map +1 -0
- package/dist/learning/index.d.ts +5 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +3 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/learning/learner.d.ts +21 -0
- package/dist/learning/learner.d.ts.map +1 -0
- package/dist/learning/learner.js +272 -0
- package/dist/learning/learner.js.map +1 -0
- package/dist/learning/ledger.d.ts +26 -0
- package/dist/learning/ledger.d.ts.map +1 -0
- package/dist/learning/ledger.js +40 -0
- package/dist/learning/ledger.js.map +1 -0
- package/dist/learning/selection.d.ts +44 -0
- package/dist/learning/selection.d.ts.map +1 -0
- package/dist/learning/selection.js +111 -0
- package/dist/learning/selection.js.map +1 -0
- package/dist/learning/stats.d.ts +19 -0
- package/dist/learning/stats.d.ts.map +1 -0
- package/dist/learning/stats.js +31 -0
- package/dist/learning/stats.js.map +1 -0
- package/dist/learning/types.d.ts +133 -0
- package/dist/learning/types.d.ts.map +1 -0
- package/dist/learning/types.js +8 -0
- package/dist/learning/types.js.map +1 -0
- package/dist/memory/envProbe.d.ts +16 -0
- package/dist/memory/envProbe.d.ts.map +1 -0
- package/dist/memory/envProbe.js +28 -0
- package/dist/memory/envProbe.js.map +1 -0
- package/dist/memory/estimates.d.ts +46 -0
- package/dist/memory/estimates.d.ts.map +1 -0
- package/dist/memory/estimates.js +72 -0
- package/dist/memory/estimates.js.map +1 -0
- package/dist/memory/eviction.d.ts +42 -0
- package/dist/memory/eviction.d.ts.map +1 -0
- package/dist/memory/eviction.js +143 -0
- package/dist/memory/eviction.js.map +1 -0
- package/dist/memory/manager.d.ts +34 -0
- package/dist/memory/manager.d.ts.map +1 -0
- package/dist/memory/manager.js +346 -0
- package/dist/memory/manager.js.map +1 -0
- package/dist/memory/managerTypes.d.ts +49 -0
- package/dist/memory/managerTypes.d.ts.map +1 -0
- package/dist/memory/managerTypes.js +7 -0
- package/dist/memory/managerTypes.js.map +1 -0
- package/dist/memory/pressure.d.ts +56 -0
- package/dist/memory/pressure.d.ts.map +1 -0
- package/dist/memory/pressure.js +80 -0
- package/dist/memory/pressure.js.map +1 -0
- package/dist/memory/spillTier.d.ts +47 -0
- package/dist/memory/spillTier.d.ts.map +1 -0
- package/dist/memory/spillTier.js +128 -0
- package/dist/memory/spillTier.js.map +1 -0
- package/dist/memory/storageSink.d.ts +69 -0
- package/dist/memory/storageSink.d.ts.map +1 -0
- package/dist/memory/storageSink.js +221 -0
- package/dist/memory/storageSink.js.map +1 -0
- package/dist/memory/types.d.ts +202 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +15 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/memory/victims.d.ts +32 -0
- package/dist/memory/victims.d.ts.map +1 -0
- package/dist/memory/victims.js +74 -0
- package/dist/memory/victims.js.map +1 -0
- package/dist/memory/workspace.d.ts +34 -0
- package/dist/memory/workspace.d.ts.map +1 -0
- package/dist/memory/workspace.js +55 -0
- package/dist/memory/workspace.js.map +1 -0
- package/dist/memoryIntegration/index.d.ts +8 -0
- package/dist/memoryIntegration/index.d.ts.map +1 -0
- package/dist/memoryIntegration/index.js +4 -0
- package/dist/memoryIntegration/index.js.map +1 -0
- package/dist/memoryIntegration/knowledgeMemory.d.ts +43 -0
- package/dist/memoryIntegration/knowledgeMemory.d.ts.map +1 -0
- package/dist/memoryIntegration/knowledgeMemory.js +285 -0
- package/dist/memoryIntegration/knowledgeMemory.js.map +1 -0
- package/dist/memoryIntegration/persistence.d.ts +15 -0
- package/dist/memoryIntegration/persistence.d.ts.map +1 -0
- package/dist/memoryIntegration/persistence.js +43 -0
- package/dist/memoryIntegration/persistence.js.map +1 -0
- package/dist/memoryIntegration/text.d.ts +57 -0
- package/dist/memoryIntegration/text.d.ts.map +1 -0
- package/dist/memoryIntegration/text.js +179 -0
- package/dist/memoryIntegration/text.js.map +1 -0
- package/dist/memoryIntegration/types.d.ts +131 -0
- package/dist/memoryIntegration/types.d.ts.map +1 -0
- package/dist/memoryIntegration/types.js +11 -0
- package/dist/memoryIntegration/types.js.map +1 -0
- package/dist/ml.d.ts +52 -0
- package/dist/ml.d.ts.map +1 -0
- package/dist/ml.js +33 -0
- package/dist/ml.js.map +1 -0
- package/dist/node/childRelay.d.ts +10 -0
- package/dist/node/childRelay.d.ts.map +1 -1
- package/dist/node/childRelay.js +12 -2
- package/dist/node/childRelay.js.map +1 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/portResponder.d.ts +2 -0
- package/dist/node/portResponder.d.ts.map +1 -1
- package/dist/node/portResponder.js +3 -3
- package/dist/node/portResponder.js.map +1 -1
- package/dist/node/processHost.d.ts +4 -1
- package/dist/node/processHost.d.ts.map +1 -1
- package/dist/node/processHost.js +19 -16
- package/dist/node/processHost.js.map +1 -1
- package/dist/node/protocol.d.ts +1 -0
- package/dist/node/protocol.d.ts.map +1 -1
- package/dist/node/responseWire.d.ts.map +1 -1
- package/dist/node/worker.js +127 -120
- package/dist/node/workerProcess.js +1 -1
- package/dist/node/workerProcess.js.map +1 -1
- package/dist/prefetch/admission.d.ts +41 -0
- package/dist/prefetch/admission.d.ts.map +1 -0
- package/dist/prefetch/admission.js +49 -0
- package/dist/prefetch/admission.js.map +1 -0
- package/dist/prefetch/loadRegistry.d.ts +56 -0
- package/dist/prefetch/loadRegistry.d.ts.map +1 -0
- package/dist/prefetch/loadRegistry.js +108 -0
- package/dist/prefetch/loadRegistry.js.map +1 -0
- package/dist/prefetch/predictor.d.ts +40 -0
- package/dist/prefetch/predictor.d.ts.map +1 -0
- package/dist/prefetch/predictor.js +178 -0
- package/dist/prefetch/predictor.js.map +1 -0
- package/dist/prefetch/prefetcher.d.ts +37 -0
- package/dist/prefetch/prefetcher.d.ts.map +1 -0
- package/dist/prefetch/prefetcher.js +339 -0
- package/dist/prefetch/prefetcher.js.map +1 -0
- package/dist/prefetch/residentStore.d.ts +49 -0
- package/dist/prefetch/residentStore.d.ts.map +1 -0
- package/dist/prefetch/residentStore.js +87 -0
- package/dist/prefetch/residentStore.js.map +1 -0
- package/dist/prefetch/selfTuner.d.ts +40 -0
- package/dist/prefetch/selfTuner.d.ts.map +1 -0
- package/dist/prefetch/selfTuner.js +78 -0
- package/dist/prefetch/selfTuner.js.map +1 -0
- package/dist/prefetch/stats.d.ts +52 -0
- package/dist/prefetch/stats.d.ts.map +1 -0
- package/dist/prefetch/stats.js +72 -0
- package/dist/prefetch/stats.js.map +1 -0
- package/dist/prefetch/streaming.d.ts +132 -0
- package/dist/prefetch/streaming.d.ts.map +1 -0
- package/dist/prefetch/streaming.js +141 -0
- package/dist/prefetch/streaming.js.map +1 -0
- package/dist/prefetch/targetIndex.d.ts +18 -0
- package/dist/prefetch/targetIndex.d.ts.map +1 -0
- package/dist/prefetch/targetIndex.js +32 -0
- package/dist/prefetch/targetIndex.js.map +1 -0
- package/dist/prefetch/types.d.ts +283 -0
- package/dist/prefetch/types.d.ts.map +1 -0
- package/dist/prefetch/types.js +19 -0
- package/dist/prefetch/types.js.map +1 -0
- package/dist/previewBridge.d.ts +6 -0
- package/dist/previewBridge.d.ts.map +1 -1
- package/dist/previewBridge.js +25 -5
- package/dist/previewBridge.js.map +1 -1
- package/dist/previewMessages.d.ts +4 -3
- package/dist/previewMessages.d.ts.map +1 -1
- package/dist/previewMessages.js +12 -6
- package/dist/previewMessages.js.map +1 -1
- package/dist/process-worker.js +127 -120
- package/dist/relay.html +1 -1
- package/dist/shared/clock.d.ts +7 -0
- package/dist/shared/clock.d.ts.map +1 -0
- package/dist/shared/clock.js +10 -0
- package/dist/shared/clock.js.map +1 -0
- package/dist/shared/hardwareProbe.d.ts +26 -0
- package/dist/shared/hardwareProbe.d.ts.map +1 -0
- package/dist/shared/hardwareProbe.js +62 -0
- package/dist/shared/hardwareProbe.js.map +1 -0
- package/dist/shared/idb.d.ts +41 -0
- package/dist/shared/idb.d.ts.map +1 -0
- package/dist/shared/idb.js +184 -0
- package/dist/shared/idb.js.map +1 -0
- package/dist/shared/tokens.d.ts +7 -0
- package/dist/shared/tokens.d.ts.map +1 -0
- package/dist/shared/tokens.js +9 -0
- package/dist/shared/tokens.js.map +1 -0
- package/dist/snapshotStore.d.ts +2 -1
- package/dist/snapshotStore.d.ts.map +1 -1
- package/dist/snapshotStore.js +27 -12
- package/dist/snapshotStore.js.map +1 -1
- package/dist/styleCompilers.d.ts.map +1 -1
- package/dist/styleCompilers.js.map +1 -1
- package/dist/transport.js.map +1 -1
- package/package.json +11 -5
- package/src/adaptive/controller.ts +151 -0
- package/src/adaptive/costModel.ts +131 -0
- package/src/adaptive/planner.ts +127 -0
- package/src/adaptive/types.ts +137 -0
- package/src/check/index.ts +6 -1
- package/src/host.ts +33 -11
- package/src/inference/backends/mock.ts +77 -0
- package/src/inference/backends/onnx.ts +91 -0
- package/src/inference/backends/webllm.ts +79 -0
- package/src/inference/backends/wllama.ts +79 -0
- package/src/inference/capabilities.ts +46 -0
- package/src/inference/client.ts +276 -0
- package/src/inference/modelCache.ts +145 -0
- package/src/inference/protocol.ts +119 -0
- package/src/inference/tokenStream.ts +92 -0
- package/src/inference/types.ts +115 -0
- package/src/inference/worker.ts +191 -0
- package/src/inference/workerEntry.ts +18 -0
- package/src/kvcache/advanced.ts +86 -0
- package/src/kvcache/compatibility.ts +51 -0
- package/src/kvcache/lru.ts +100 -0
- package/src/kvcache/paged.ts +270 -0
- package/src/kvcache/quant.ts +88 -0
- package/src/kvcache/radix.ts +68 -0
- package/src/kvcache/runtime.ts +239 -0
- package/src/kvcache/types.ts +167 -0
- package/src/learning/fingerprint.ts +50 -0
- package/src/learning/index.ts +15 -0
- package/src/learning/learner.ts +304 -0
- package/src/learning/ledger.ts +54 -0
- package/src/learning/selection.ts +137 -0
- package/src/learning/stats.ts +39 -0
- package/src/learning/types.ts +142 -0
- package/src/memory/envProbe.ts +30 -0
- package/src/memory/estimates.ts +98 -0
- package/src/memory/eviction.ts +145 -0
- package/src/memory/manager.ts +380 -0
- package/src/memory/managerTypes.ts +61 -0
- package/src/memory/pressure.ts +120 -0
- package/src/memory/spillTier.ts +159 -0
- package/src/memory/storageSink.ts +289 -0
- package/src/memory/types.ts +217 -0
- package/src/memory/victims.ts +80 -0
- package/src/memory/workspace.ts +74 -0
- package/src/memoryIntegration/index.ts +7 -0
- package/src/memoryIntegration/knowledgeMemory.ts +339 -0
- package/src/memoryIntegration/persistence.ts +54 -0
- package/src/memoryIntegration/text.ts +185 -0
- package/src/memoryIntegration/types.ts +145 -0
- package/src/ml.ts +180 -0
- package/src/node/childRelay.ts +16 -3
- package/src/node/portResponder.ts +5 -3
- package/src/node/processHost.ts +16 -15
- package/src/node/protocol.ts +1 -1
- package/src/node/workerProcess.ts +1 -1
- package/src/prefetch/admission.ts +74 -0
- package/src/prefetch/loadRegistry.ts +156 -0
- package/src/prefetch/predictor.ts +199 -0
- package/src/prefetch/prefetcher.ts +369 -0
- package/src/prefetch/residentStore.ts +121 -0
- package/src/prefetch/selfTuner.ts +110 -0
- package/src/prefetch/stats.ts +120 -0
- package/src/prefetch/streaming.ts +231 -0
- package/src/prefetch/targetIndex.ts +40 -0
- package/src/prefetch/types.ts +307 -0
- package/src/previewBridge.ts +25 -5
- package/src/previewMessages.ts +11 -6
- package/src/shared/clock.ts +9 -0
- package/src/shared/hardwareProbe.ts +68 -0
- package/src/shared/idb.ts +203 -0
- package/src/shared/tokens.ts +8 -0
- package/src/snapshotStore.ts +25 -10
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 7 — Online Performance Learning: shared types.
|
|
3
|
+
*
|
|
4
|
+
* Learning emits plain-data suggestions. It never applies them to another
|
|
5
|
+
* module, and it never changes model correctness or memory semantics.
|
|
6
|
+
*/
|
|
7
|
+
/** A strategy the learner is allowed to recommend. Unregistered ids are refused. */
|
|
8
|
+
export interface SafeStrategy {
|
|
9
|
+
id: string;
|
|
10
|
+
/**
|
|
11
|
+
* Must be `true`. The learner refuses a strategy that has not been marked
|
|
12
|
+
* prevalidated — it will not invent or trial an unknown execution path.
|
|
13
|
+
*/
|
|
14
|
+
validated: true;
|
|
15
|
+
/** Plain-data config this strategy implies. Copied into recommendations; never applied here. */
|
|
16
|
+
deltas?: ReadonlyArray<ConfigDeltaField>;
|
|
17
|
+
/** Heavier on memory or thermals. Skipped while pressure/thermal is hot. */
|
|
18
|
+
pressureSensitive?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface ConfigDeltaField {
|
|
21
|
+
field: string;
|
|
22
|
+
value: string | number | boolean;
|
|
23
|
+
}
|
|
24
|
+
/** Bucketed workload identity. Same buckets ⇒ same signature. */
|
|
25
|
+
export interface WorkloadFingerprintInput {
|
|
26
|
+
paramsB?: number;
|
|
27
|
+
quantization?: string;
|
|
28
|
+
promptTokens?: number;
|
|
29
|
+
decodeTokens?: number;
|
|
30
|
+
}
|
|
31
|
+
/** One measured run. `correct: false` is ignored so a bad run cannot teach a strategy. */
|
|
32
|
+
export interface OutcomeObservation {
|
|
33
|
+
signature: string;
|
|
34
|
+
strategy: string;
|
|
35
|
+
/** Wall-clock cost. Lower is better. */
|
|
36
|
+
costMs: number;
|
|
37
|
+
overheadMs?: number;
|
|
38
|
+
benefitMs?: number;
|
|
39
|
+
/**
|
|
40
|
+
* When `false`, the observation is dropped. Omitted means the caller asserts
|
|
41
|
+
* the run preserved correctness (the only kind of run learning may use).
|
|
42
|
+
*/
|
|
43
|
+
correct?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export interface Recommendation {
|
|
46
|
+
signature: string;
|
|
47
|
+
/** Strategy to use. Equals the incumbent when learning declines to switch. */
|
|
48
|
+
strategy: string;
|
|
49
|
+
/** Previous strategy, when this recommendation would change it. */
|
|
50
|
+
from: string;
|
|
51
|
+
/** Human-readable why. Always present. */
|
|
52
|
+
reason: string;
|
|
53
|
+
/** Suggestions only. Applying them is the caller's job. */
|
|
54
|
+
deltas: ConfigDeltaField[];
|
|
55
|
+
/** Time spent producing this recommendation. */
|
|
56
|
+
overheadMs: number;
|
|
57
|
+
/** True when the guard reverted an underperforming strategy. */
|
|
58
|
+
autoReverted: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface HistoryEntry {
|
|
61
|
+
signature: string;
|
|
62
|
+
from: string;
|
|
63
|
+
to: string;
|
|
64
|
+
reason: string;
|
|
65
|
+
deltas: ConfigDeltaField[];
|
|
66
|
+
overheadMs: number;
|
|
67
|
+
at: number;
|
|
68
|
+
auto: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface LearningStats {
|
|
71
|
+
decisions: number;
|
|
72
|
+
observations: number;
|
|
73
|
+
ignored: number;
|
|
74
|
+
lastOverheadMs: number;
|
|
75
|
+
totalOverheadMs: number;
|
|
76
|
+
disabled: boolean;
|
|
77
|
+
/** Mean recorded overhead per decision. */
|
|
78
|
+
meanOverheadMs: number;
|
|
79
|
+
}
|
|
80
|
+
export interface LearnerConfig {
|
|
81
|
+
/** When true, recommend() is a deterministic pass-through and apply() is a no-op. */
|
|
82
|
+
disabled?: boolean;
|
|
83
|
+
/** Exploration rate for epsilon-greedy. Ignored when disabled. Default 0.1. */
|
|
84
|
+
epsilon?: number;
|
|
85
|
+
/** Samples required before a strategy can win on exploit. Default 3. */
|
|
86
|
+
minSamples?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Auto-revert when the applied strategy's mean cost exceeds the incumbent's
|
|
89
|
+
* by this factor. Default 1.15 (15% worse).
|
|
90
|
+
*/
|
|
91
|
+
underperformRatio?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Optional. When true (and only after the table has observations), selection
|
|
94
|
+
* uses UCB1 on inverse cost instead of epsilon-greedy. Default false.
|
|
95
|
+
*/
|
|
96
|
+
contextualBandit?: boolean;
|
|
97
|
+
/** Injectable clock so tests can pin overhead accounting. Default `performance.now`. */
|
|
98
|
+
now?: () => number;
|
|
99
|
+
/** Injectable RNG in `[0, 1)`. Default `Math.random`. Never called when disabled. */
|
|
100
|
+
rng?: () => number;
|
|
101
|
+
/** Resource pressure. When true, pressure-sensitive strategies are not chosen. */
|
|
102
|
+
pressure?: () => boolean;
|
|
103
|
+
/** Thermal band. `'hot'` is treated as pressure. */
|
|
104
|
+
thermal?: () => 'ok' | 'warm' | 'hot';
|
|
105
|
+
clock?: () => number;
|
|
106
|
+
/**
|
|
107
|
+
* Applied changes kept in `history()` (and reachable by `rollback()`),
|
|
108
|
+
* oldest dropped first. Default 256.
|
|
109
|
+
*/
|
|
110
|
+
historyLimit?: number;
|
|
111
|
+
}
|
|
112
|
+
export interface ObserveResult {
|
|
113
|
+
accepted: boolean;
|
|
114
|
+
reason: string;
|
|
115
|
+
}
|
|
116
|
+
export interface Learner {
|
|
117
|
+
register(strategy: SafeStrategy): void;
|
|
118
|
+
observe(observation: OutcomeObservation): ObserveResult;
|
|
119
|
+
recommend(signature: string, incumbent: string): Recommendation;
|
|
120
|
+
/** Record a recommendation as applied. Does not mutate any other module. */
|
|
121
|
+
apply(recommendation: Recommendation): HistoryEntry | null;
|
|
122
|
+
/** Undo the last applied change. Returns the inverse entry, or null if none. */
|
|
123
|
+
rollback(): HistoryEntry | null;
|
|
124
|
+
history(): readonly HistoryEntry[];
|
|
125
|
+
/** Strategy currently recorded as applied for this signature, if any. */
|
|
126
|
+
active(signature: string): string | undefined;
|
|
127
|
+
overheadMs(): number;
|
|
128
|
+
stats(): LearningStats;
|
|
129
|
+
disable(): void;
|
|
130
|
+
enable(): void;
|
|
131
|
+
readonly disabled: boolean;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/learning/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,oFAAoF;AACpF,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX;;;OAGG;IACH,SAAS,EAAE,IAAI,CAAC;IAChB,gGAAgG;IAChG,MAAM,CAAC,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;IACzC,4EAA4E;IAC5E,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CAClC;AAED,iEAAiE;AACjE,MAAM,WAAW,wBAAwB;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,0FAA0F;AAC1F,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,MAAM,CAAC;IA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,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 7 — Online Performance Learning: shared types.
|
|
3
|
+
*
|
|
4
|
+
* Learning emits plain-data suggestions. It never applies them to another
|
|
5
|
+
* module, and it never changes model correctness or memory semantics.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/learning/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — environment facts the residency manager reports.
|
|
3
|
+
*
|
|
4
|
+
* Facts, never gates: every probe resolves a conservative default when the
|
|
5
|
+
* environment has no answer, and none of them throws.
|
|
6
|
+
*/
|
|
7
|
+
/** RAM hint used when the environment reports none. */
|
|
8
|
+
export declare const DEFAULT_RAM_BYTES: number;
|
|
9
|
+
/** Probe a RAM hint. Fact, not a gate. 4 GiB when the environment has none. */
|
|
10
|
+
export declare function probeRamBytes(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Probe a browser-storage quota hint. Resolves 0 when the Storage API is
|
|
13
|
+
* absent (Node tests, locked-down iframes). Never throws.
|
|
14
|
+
*/
|
|
15
|
+
export declare function probeStorageQuotaBytes(): Promise<number>;
|
|
16
|
+
//# sourceMappingURL=envProbe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envProbe.d.ts","sourceRoot":"","sources":["../../src/memory/envProbe.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,uDAAuD;AACvD,eAAO,MAAM,iBAAiB,QAAyB,CAAC;AAExD,+EAA+E;AAC/E,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED;;;GAGG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,MAAM,CAAC,CAQ9D"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — environment facts the residency manager reports.
|
|
3
|
+
*
|
|
4
|
+
* Facts, never gates: every probe resolves a conservative default when the
|
|
5
|
+
* environment has no answer, and none of them throws.
|
|
6
|
+
*/
|
|
7
|
+
import { probeDeviceMemoryBytes } from '../shared/hardwareProbe.js';
|
|
8
|
+
/** RAM hint used when the environment reports none. */
|
|
9
|
+
export const DEFAULT_RAM_BYTES = 4 * 1024 * 1024 * 1024;
|
|
10
|
+
/** Probe a RAM hint. Fact, not a gate. 4 GiB when the environment has none. */
|
|
11
|
+
export function probeRamBytes() {
|
|
12
|
+
return probeDeviceMemoryBytes() ?? DEFAULT_RAM_BYTES;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Probe a browser-storage quota hint. Resolves 0 when the Storage API is
|
|
16
|
+
* absent (Node tests, locked-down iframes). Never throws.
|
|
17
|
+
*/
|
|
18
|
+
export async function probeStorageQuotaBytes() {
|
|
19
|
+
const nav = globalThis.navigator;
|
|
20
|
+
try {
|
|
21
|
+
const estimate = await nav?.storage?.estimate?.();
|
|
22
|
+
return typeof estimate?.quota === 'number' && estimate.quota > 0 ? estimate.quota : 0;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=envProbe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envProbe.js","sourceRoot":"","sources":["../../src/memory/envProbe.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAEpE,uDAAuD;AACvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAExD,+EAA+E;AAC/E,MAAM,UAAU,aAAa;IAC3B,OAAO,sBAAsB,EAAE,IAAI,iBAAiB,CAAC;AACvD,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB;IAC1C,MAAM,GAAG,GAAI,UAA6F,CAAC,SAAS,CAAC;IACrH,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,CAAC;QAClD,OAAO,OAAO,QAAQ,EAAE,KAAK,KAAK,QAAQ,IAAI,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACxF,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — reuse-probability and reload-cost estimation.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions. The manager calls these when the caller does not supply an
|
|
5
|
+
* estimate, and cost-aware eviction weighs `reuseProbability * reloadCostMs`
|
|
6
|
+
* against footprint. Nothing here touches inference or decides when to run.
|
|
7
|
+
*/
|
|
8
|
+
import type { ResidencyTier } from './types.js';
|
|
9
|
+
/** Bytes per parameter by quantization bucket. Mirrors the Phase 2 controller. */
|
|
10
|
+
export declare function bytesPerParam(quantization: string): number;
|
|
11
|
+
/**
|
|
12
|
+
* Estimated weight footprint for a model, in bytes.
|
|
13
|
+
* `paramsB` is billions of parameters. Headroom (default 20%) covers KV and
|
|
14
|
+
* activations so a residency admission does not under-count.
|
|
15
|
+
*/
|
|
16
|
+
export declare function estimateWeightBytes(paramsB: number, quantization: string, headroom?: number): number;
|
|
17
|
+
export interface ReloadCostInput {
|
|
18
|
+
/** Footprint in bytes. */
|
|
19
|
+
bytes: number;
|
|
20
|
+
/** Where the bytes would be reloaded from. */
|
|
21
|
+
from: ResidencyTier | 'network';
|
|
22
|
+
/** Optional measured throughput in bytes/ms. Overrides the tier default. */
|
|
23
|
+
bytesPerMs?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Estimated milliseconds to reload `bytes` from `from`.
|
|
27
|
+
* Always >= 0. A zero-byte entry reloads in 0.
|
|
28
|
+
*/
|
|
29
|
+
export declare function estimateReloadCostMs(input: ReloadCostInput): number;
|
|
30
|
+
export interface ReuseObservation {
|
|
31
|
+
/** Hits recorded for this id. */
|
|
32
|
+
hits: number;
|
|
33
|
+
/** Misses (or admissions that were not hits) recorded for this id. */
|
|
34
|
+
misses: number;
|
|
35
|
+
/** Ms since the last touch. Undefined if never touched. */
|
|
36
|
+
idleMs?: number;
|
|
37
|
+
/** Half-life of recency, ms. Default 60s. */
|
|
38
|
+
halfLifeMs?: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Estimated probability this id is reused before the next pressure event.
|
|
42
|
+
* Combines a Laplace-smoothed hit frequency with an exponential recency decay
|
|
43
|
+
* so a hot-but-idle entry cools off. Result is in [0, 1].
|
|
44
|
+
*/
|
|
45
|
+
export declare function estimateReuseProbability(observation: ReuseObservation): number;
|
|
46
|
+
//# sourceMappingURL=estimates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimates.d.ts","sourceRoot":"","sources":["../../src/memory/estimates.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,kFAAkF;AAClF,wBAAgB,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAa1D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,SAAM,GAAG,MAAM,CAGjG;AAED,MAAM,WAAW,eAAe;IAC9B,0BAA0B;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,8CAA8C;IAC9C,IAAI,EAAE,aAAa,GAAG,SAAS,CAAC;IAChC,4EAA4E;IAC5E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAcD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAInE;AAED,MAAM,WAAW,gBAAgB;IAC/B,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,gBAAgB,GAAG,MAAM,CAS9E"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — reuse-probability and reload-cost estimation.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions. The manager calls these when the caller does not supply an
|
|
5
|
+
* estimate, and cost-aware eviction weighs `reuseProbability * reloadCostMs`
|
|
6
|
+
* against footprint. Nothing here touches inference or decides when to run.
|
|
7
|
+
*/
|
|
8
|
+
/** Bytes per parameter by quantization bucket. Mirrors the Phase 2 controller. */
|
|
9
|
+
export function bytesPerParam(quantization) {
|
|
10
|
+
switch (quantization) {
|
|
11
|
+
case 'q4':
|
|
12
|
+
return 0.5;
|
|
13
|
+
case 'q5':
|
|
14
|
+
return 0.625;
|
|
15
|
+
case 'q8':
|
|
16
|
+
return 1;
|
|
17
|
+
case 'fp16':
|
|
18
|
+
return 2;
|
|
19
|
+
default:
|
|
20
|
+
return 0.5;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Estimated weight footprint for a model, in bytes.
|
|
25
|
+
* `paramsB` is billions of parameters. Headroom (default 20%) covers KV and
|
|
26
|
+
* activations so a residency admission does not under-count.
|
|
27
|
+
*/
|
|
28
|
+
export function estimateWeightBytes(paramsB, quantization, headroom = 1.2) {
|
|
29
|
+
const params = Number.isFinite(paramsB) && paramsB > 0 ? paramsB : 0;
|
|
30
|
+
return Math.round(params * 1e9 * bytesPerParam(quantization) * Math.max(1, headroom));
|
|
31
|
+
}
|
|
32
|
+
/** Default reload throughput by source, bytes per ms. Conservative CPU-only figures. */
|
|
33
|
+
const DEFAULT_THROUGHPUT = {
|
|
34
|
+
// A RAM hit is not a reload; callers should not ask. Kept non-zero for safety.
|
|
35
|
+
ram: 1_000_000,
|
|
36
|
+
// VRAM copy over the optional GPU path. Hint only.
|
|
37
|
+
vram: 200_000,
|
|
38
|
+
// IndexedDB read on a typical laptop-class disk.
|
|
39
|
+
storage: 50_000,
|
|
40
|
+
// Re-download. Slow, and the reason cost-aware keeps hot weights.
|
|
41
|
+
network: 2_000,
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Estimated milliseconds to reload `bytes` from `from`.
|
|
45
|
+
* Always >= 0. A zero-byte entry reloads in 0.
|
|
46
|
+
*/
|
|
47
|
+
export function estimateReloadCostMs(input) {
|
|
48
|
+
const bytes = Number.isFinite(input.bytes) && input.bytes > 0 ? input.bytes : 0;
|
|
49
|
+
const throughput = input.bytesPerMs && input.bytesPerMs > 0 ? input.bytesPerMs : DEFAULT_THROUGHPUT[input.from];
|
|
50
|
+
return bytes / throughput;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Estimated probability this id is reused before the next pressure event.
|
|
54
|
+
* Combines a Laplace-smoothed hit frequency with an exponential recency decay
|
|
55
|
+
* so a hot-but-idle entry cools off. Result is in [0, 1].
|
|
56
|
+
*/
|
|
57
|
+
export function estimateReuseProbability(observation) {
|
|
58
|
+
const hits = Math.max(0, observation.hits);
|
|
59
|
+
const misses = Math.max(0, observation.misses);
|
|
60
|
+
// Laplace smoothing: an unseen id is 0.5, not 0 or 1.
|
|
61
|
+
const frequency = (hits + 1) / (hits + misses + 2);
|
|
62
|
+
const halfLife = observation.halfLifeMs && observation.halfLifeMs > 0 ? observation.halfLifeMs : 60_000;
|
|
63
|
+
const idle = observation.idleMs && observation.idleMs > 0 ? observation.idleMs : 0;
|
|
64
|
+
const recency = Math.pow(0.5, idle / halfLife);
|
|
65
|
+
return clamp01(frequency * recency);
|
|
66
|
+
}
|
|
67
|
+
function clamp01(value) {
|
|
68
|
+
if (Number.isNaN(value))
|
|
69
|
+
return 0;
|
|
70
|
+
return Math.min(1, Math.max(0, value));
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=estimates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimates.js","sourceRoot":"","sources":["../../src/memory/estimates.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,kFAAkF;AAClF,MAAM,UAAU,aAAa,CAAC,YAAoB;IAChD,QAAQ,YAAY,EAAE,CAAC;QACrB,KAAK,IAAI;YACP,OAAO,GAAG,CAAC;QACb,KAAK,IAAI;YACP,OAAO,KAAK,CAAC;QACf,KAAK,IAAI;YACP,OAAO,CAAC,CAAC;QACX,KAAK,MAAM;YACT,OAAO,CAAC,CAAC;QACX;YACE,OAAO,GAAG,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAe,EAAE,YAAoB,EAAE,QAAQ,GAAG,GAAG;IACvF,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,GAAG,GAAG,aAAa,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;AACxF,CAAC;AAWD,wFAAwF;AACxF,MAAM,kBAAkB,GAA4C;IAClE,+EAA+E;IAC/E,GAAG,EAAE,SAAS;IACd,mDAAmD;IACnD,IAAI,EAAE,OAAO;IACb,iDAAiD;IACjD,OAAO,EAAE,MAAM;IACf,kEAAkE;IAClE,OAAO,EAAE,KA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AC,SAAS,GAAG,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,SAAS,OAAO,CAAC,KAAa;IAC5B,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAClC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — pluggable eviction strategies.
|
|
3
|
+
*
|
|
4
|
+
* Each strategy ranks evictable residents from most-evictable to least.
|
|
5
|
+
* The manager walks that order until the byte budget (and quotas) fit.
|
|
6
|
+
* Strategies never mutate the manager's map, so they are swappable in tests.
|
|
7
|
+
*
|
|
8
|
+
* Cost-aware is the default: it evicts the entry with the worst
|
|
9
|
+
* (reuseProbability * reloadCostMs) / footprint score first — a large, cold,
|
|
10
|
+
* cheap-to-reload entry leaves before a small, hot, expensive one.
|
|
11
|
+
*/
|
|
12
|
+
import type { EvictionStrategy, TrackedEntry } from './types.js';
|
|
13
|
+
/** Least-recently-used. Evicts the entry with the oldest `lastUsedAt` first. */
|
|
14
|
+
export declare function lruStrategy(): EvictionStrategy;
|
|
15
|
+
/** Least-frequently-used. Evicts the entry with the fewest hits first. */
|
|
16
|
+
export declare function lfuStrategy(): EvictionStrategy;
|
|
17
|
+
/**
|
|
18
|
+
* Value of keeping an entry, per byte. Higher means "keep".
|
|
19
|
+
* `reuseProbability * reloadCostMs` is the expected reload penalty of evicting
|
|
20
|
+
* it; dividing by footprint prefers dropping large cold entries.
|
|
21
|
+
* A zero-byte entry is treated as infinitely cheap to keep (never preferred).
|
|
22
|
+
*/
|
|
23
|
+
export declare function retentionValue(entry: TrackedEntry): number;
|
|
24
|
+
/** Cost-aware. Evicts the lowest retention-value-per-byte entry first. */
|
|
25
|
+
export declare function costAwareStrategy(): EvictionStrategy;
|
|
26
|
+
/**
|
|
27
|
+
* Adaptive Replacement Cache (Megiddo & Modha).
|
|
28
|
+
*
|
|
29
|
+
* Maintains four logical lists over the manager's tracked entries plus two
|
|
30
|
+
* ghost lists of recently evicted ids:
|
|
31
|
+
* T1 recent residents, T2 frequent residents,
|
|
32
|
+
* B1 ghost of evicted-once, B2 ghost of evicted-frequent.
|
|
33
|
+
* A hit on B1 grows the T1 target (recency is winning); a hit on B2 shrinks it
|
|
34
|
+
* (frequency is winning). Ranking evicts from the list that is over its target.
|
|
35
|
+
*
|
|
36
|
+
* Ghost ids are bounded by `ghostCapacity` (default 1024) so a sustained
|
|
37
|
+
* workload cannot grow strategy state without bound.
|
|
38
|
+
*/
|
|
39
|
+
export declare function arcStrategy(ghostCapacity?: number): EvictionStrategy;
|
|
40
|
+
/** Resolve a strategy by name. Unknown names fall back to cost-aware. */
|
|
41
|
+
export declare function strategyByName(name: EvictionStrategy['name']): EvictionStrategy;
|
|
42
|
+
//# sourceMappingURL=eviction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eviction.d.ts","sourceRoot":"","sources":["../../src/memory/eviction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAYjE,gFAAgF;AAChF,wBAAgB,WAAW,IAAI,gBAAgB,CAO9C;AAED,0EAA0E;AAC1E,wBAAgB,WAAW,IAAI,gBAAgB,CAO9C;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAI1D;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,IAAI,gBAAgB,CAOpD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,aAAa,SAAO,GAAG,gBAAgB,CA4ClE;AAED,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,gBAAgB,CAa/E"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — pluggable eviction strategies.
|
|
3
|
+
*
|
|
4
|
+
* Each strategy ranks evictable residents from most-evictable to least.
|
|
5
|
+
* The manager walks that order until the byte budget (and quotas) fit.
|
|
6
|
+
* Strategies never mutate the manager's map, so they are swappable in tests.
|
|
7
|
+
*
|
|
8
|
+
* Cost-aware is the default: it evicts the entry with the worst
|
|
9
|
+
* (reuseProbability * reloadCostMs) / footprint score first — a large, cold,
|
|
10
|
+
* cheap-to-reload entry leaves before a small, hot, expensive one.
|
|
11
|
+
*/
|
|
12
|
+
function byNumber(pick, direction) {
|
|
13
|
+
const sign = direction === 'asc' ? 1 : -1;
|
|
14
|
+
return (a, b) => {
|
|
15
|
+
const delta = pick(a) - pick(b);
|
|
16
|
+
if (delta !== 0)
|
|
17
|
+
return sign * delta;
|
|
18
|
+
// Stable tie-break: older admission leaves first.
|
|
19
|
+
return a.admittedAt - b.admittedAt;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Least-recently-used. Evicts the entry with the oldest `lastUsedAt` first. */
|
|
23
|
+
export function lruStrategy() {
|
|
24
|
+
return {
|
|
25
|
+
name: 'lru',
|
|
26
|
+
rank(candidates) {
|
|
27
|
+
return [...candidates].sort(byNumber((e) => e.lastUsedAt, 'asc'));
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Least-frequently-used. Evicts the entry with the fewest hits first. */
|
|
32
|
+
export function lfuStrategy() {
|
|
33
|
+
return {
|
|
34
|
+
name: 'lfu',
|
|
35
|
+
rank(candidates) {
|
|
36
|
+
return [...candidates].sort(byNumber((e) => e.hits, 'asc'));
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Value of keeping an entry, per byte. Higher means "keep".
|
|
42
|
+
* `reuseProbability * reloadCostMs` is the expected reload penalty of evicting
|
|
43
|
+
* it; dividing by footprint prefers dropping large cold entries.
|
|
44
|
+
* A zero-byte entry is treated as infinitely cheap to keep (never preferred).
|
|
45
|
+
*/
|
|
46
|
+
export function retentionValue(entry) {
|
|
47
|
+
const footprint = entry.bytes > 0 ? entry.bytes : 1;
|
|
48
|
+
const expectedReload = clamp01(entry.reuseProbability) * Math.max(0, entry.reloadCostMs);
|
|
49
|
+
return expectedReload / footprint;
|
|
50
|
+
}
|
|
51
|
+
/** Cost-aware. Evicts the lowest retention-value-per-byte entry first. */
|
|
52
|
+
export function costAwareStrategy() {
|
|
53
|
+
return {
|
|
54
|
+
name: 'cost-aware',
|
|
55
|
+
rank(candidates) {
|
|
56
|
+
return [...candidates].sort(byNumber((e) => retentionValue(e), 'asc'));
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Adaptive Replacement Cache (Megiddo & Modha).
|
|
62
|
+
*
|
|
63
|
+
* Maintains four logical lists over the manager's tracked entries plus two
|
|
64
|
+
* ghost lists of recently evicted ids:
|
|
65
|
+
* T1 recent residents, T2 frequent residents,
|
|
66
|
+
* B1 ghost of evicted-once, B2 ghost of evicted-frequent.
|
|
67
|
+
* A hit on B1 grows the T1 target (recency is winning); a hit on B2 shrinks it
|
|
68
|
+
* (frequency is winning). Ranking evicts from the list that is over its target.
|
|
69
|
+
*
|
|
70
|
+
* Ghost ids are bounded by `ghostCapacity` (default 1024) so a sustained
|
|
71
|
+
* workload cannot grow strategy state without bound.
|
|
72
|
+
*/
|
|
73
|
+
export function arcStrategy(ghostCapacity = 1024) {
|
|
74
|
+
const b1 = [];
|
|
75
|
+
const b2 = [];
|
|
76
|
+
let p = 0;
|
|
77
|
+
const remember = (list, id) => {
|
|
78
|
+
const existing = list.indexOf(id);
|
|
79
|
+
if (existing >= 0)
|
|
80
|
+
list.splice(existing, 1);
|
|
81
|
+
list.push(id);
|
|
82
|
+
while (list.length > ghostCapacity)
|
|
83
|
+
list.shift();
|
|
84
|
+
};
|
|
85
|
+
const forget = (list, id) => {
|
|
86
|
+
const existing = list.indexOf(id);
|
|
87
|
+
if (existing < 0)
|
|
88
|
+
return false;
|
|
89
|
+
list.splice(existing, 1);
|
|
90
|
+
return true;
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
name: 'arc',
|
|
94
|
+
observe(event) {
|
|
95
|
+
const id = event.entry.id;
|
|
96
|
+
if (event.kind === 'miss') {
|
|
97
|
+
// A miss that was recently evicted adapts the target.
|
|
98
|
+
if (forget(b1, id))
|
|
99
|
+
p = Math.min(ghostCapacity, p + Math.max(1, b2.length / Math.max(1, b1.length + 1)));
|
|
100
|
+
else if (forget(b2, id))
|
|
101
|
+
p = Math.max(0, p - Math.max(1, b1.length / Math.max(1, b2.length + 1)));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (event.kind === 'evict') {
|
|
105
|
+
if (event.entry.arcList === 't2' || event.entry.hits > 1)
|
|
106
|
+
remember(b2, id);
|
|
107
|
+
else
|
|
108
|
+
remember(b1, id);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
rank(candidates) {
|
|
112
|
+
const t1 = candidates.filter((e) => (e.arcList ?? (e.hits <= 1 ? 't1' : 't2')) === 't1');
|
|
113
|
+
const t2 = candidates.filter((e) => (e.arcList ?? (e.hits <= 1 ? 't1' : 't2')) === 't2');
|
|
114
|
+
// Evict from whichever list is over its target. p is the T1 target.
|
|
115
|
+
const evictT1First = t1.length >= p || t2.length === 0;
|
|
116
|
+
const primary = evictT1First ? t1 : t2;
|
|
117
|
+
const secondary = evictT1First ? t2 : t1;
|
|
118
|
+
const order = byNumber((e) => e.lastUsedAt, 'asc');
|
|
119
|
+
return [...primary].sort(order).concat([...secondary].sort(order));
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** Resolve a strategy by name. Unknown names fall back to cost-aware. */
|
|
124
|
+
export function strategyByName(name) {
|
|
125
|
+
switch (name) {
|
|
126
|
+
case 'lru':
|
|
127
|
+
return lruStrategy();
|
|
128
|
+
case 'lfu':
|
|
129
|
+
return lfuStrategy();
|
|
130
|
+
case 'arc':
|
|
131
|
+
return arcStrategy();
|
|
132
|
+
case 'cost-aware':
|
|
133
|
+
return costAwareStrategy();
|
|
134
|
+
default:
|
|
135
|
+
return costAwareStrategy();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function clamp01(value) {
|
|
139
|
+
if (Number.isNaN(value))
|
|
140
|
+
return 0;
|
|
141
|
+
return Math.min(1, Math.max(0, value));
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=eviction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eviction.js","sourceRoot":"","sources":["../../src/memory/eviction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,SAAS,QAAQ,CAAC,IAAqC,EAAE,SAAyB;IAChF,MAAM,IAAI,GAAG,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QAChC,IAAI,KAAK,KAAK,CAAC;YAAE,OAAO,IAAI,GAAG,KAAK,CAAC;QACrC,kDAAkD;QAClD,OAAO,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC;IACrC,CAAC,CAAC;AACJ,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,WAAW;IACzB,OAAO;QACL,IAAI,EAAE,KAAK;QACX,IAAI,CAAC,UAAU;YACb,OAAO,CAAC,GAAG,UAAU,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;QACpE,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AAG,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QACrE,CAAC;KACF,CAAC;AACJ,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,cAAc,CAAC,IAA8B;IAC3D,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,KAAK;YACR,OAAO,WAAW,EAAE,CAAC;QACvB,KAAK,KAAK;YACR,OAAO,WAAW,EAAE,CAAC;QACvB,KAAK,KAAK;YACR,OAAO,WAAW,EAAE,CAAC;QACvB,KAAK,YAAY;YACf,OAAO,iBAAiB,EAAE,CAAC;QAC7B;YACE,OAAO,iBAAiB,EAAE,CAAC;IAC/B,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CAAC,KAAa;IAC5B,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAClC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3 — residency manager.
|
|
3
|
+
*
|
|
4
|
+
* Admits model weights and per-layer components into a hard byte budget,
|
|
5
|
+
* evicts with an injected strategy when the budget (or a quota) would be
|
|
6
|
+
* exceeded, spills cold entries to a browser-storage sink, and grows or
|
|
7
|
+
* shrinks a workspace under pressure.
|
|
8
|
+
*
|
|
9
|
+
* Decoupled from execution: nothing here calls an inference backend or decides
|
|
10
|
+
* when to run. The controller reads `pressure()` / `stats()` if it wants a
|
|
11
|
+
* residency hint; this module never calls the controller.
|
|
12
|
+
*
|
|
13
|
+
* Invariants:
|
|
14
|
+
* - `stats().usedBytes <= config.byteBudget` after every public call.
|
|
15
|
+
* - Eviction is transactional: entries are dropped only once the admission
|
|
16
|
+
* is known to fit. A rejected admission leaves every resident untouched.
|
|
17
|
+
* - Eviction drops whole entries. It never mutates another entry's payload,
|
|
18
|
+
* so an in-use entry stays consistent.
|
|
19
|
+
* - Pinned entries are never evicted. An admission that cannot fit even
|
|
20
|
+
* after evicting every unpinned resident is spilled (when a sink and a
|
|
21
|
+
* storage budget allow) or rejected — it is never allowed to exceed the
|
|
22
|
+
* RAM budget.
|
|
23
|
+
* - Spilled bytes (including concurrent spills), strategy ghost state, and
|
|
24
|
+
* the event log are all bounded.
|
|
25
|
+
*
|
|
26
|
+
* The concerns live in their own modules: victims.ts (victim selection +
|
|
27
|
+
* quota accounting), spillTier.ts (the storage tier), workspace.ts (the
|
|
28
|
+
* dynamic workspace), envProbe.ts (RAM / storage facts).
|
|
29
|
+
*/
|
|
30
|
+
import type { MemoryManager, MemoryManagerOptions } from './managerTypes.js';
|
|
31
|
+
export { probeRamBytes, probeStorageQuotaBytes } from './envProbe.js';
|
|
32
|
+
export type { AdmitResult, MemoryManager, MemoryManagerOptions } from './managerTypes.js';
|
|
33
|
+
export declare function createMemoryManager(options: MemoryManagerOptions): MemoryManager;
|
|
34
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/memory/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAWH,OAAO,KAAK,EAAe,aAAa,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAM1F,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACtE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAM1F,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,oBAAoB,GAAG,aAAa,CAmUhF"}
|