@limo-labs/deity 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +519 -0
- package/dist/src/component.d.ts +19 -0
- package/dist/src/component.d.ts.map +1 -0
- package/dist/src/component.js +2 -0
- package/dist/src/component.js.map +1 -0
- package/dist/src/context/context-scope.d.ts +24 -0
- package/dist/src/context/context-scope.d.ts.map +1 -0
- package/dist/src/context/context-scope.js +19 -0
- package/dist/src/context/context-scope.js.map +1 -0
- package/dist/src/context/memory-view.d.ts +62 -0
- package/dist/src/context/memory-view.d.ts.map +1 -0
- package/dist/src/context/memory-view.js +104 -0
- package/dist/src/context/memory-view.js.map +1 -0
- package/dist/src/context/scoped-context.d.ts +51 -0
- package/dist/src/context/scoped-context.d.ts.map +1 -0
- package/dist/src/context/scoped-context.js +74 -0
- package/dist/src/context/scoped-context.js.map +1 -0
- package/dist/src/context/scoped-execution-context.d.ts +55 -0
- package/dist/src/context/scoped-execution-context.d.ts.map +1 -0
- package/dist/src/context/scoped-execution-context.js +78 -0
- package/dist/src/context/scoped-execution-context.js.map +1 -0
- package/dist/src/conversation/conversation-manager.d.ts +272 -0
- package/dist/src/conversation/conversation-manager.d.ts.map +1 -0
- package/dist/src/conversation/conversation-manager.js +11 -0
- package/dist/src/conversation/conversation-manager.js.map +1 -0
- package/dist/src/conversation/conversation-pruner.d.ts +190 -0
- package/dist/src/conversation/conversation-pruner.d.ts.map +1 -0
- package/dist/src/conversation/conversation-pruner.js +274 -0
- package/dist/src/conversation/conversation-pruner.js.map +1 -0
- package/dist/src/conversation/conversation-tree.d.ts +185 -0
- package/dist/src/conversation/conversation-tree.d.ts.map +1 -0
- package/dist/src/conversation/conversation-tree.js +288 -0
- package/dist/src/conversation/conversation-tree.js.map +1 -0
- package/dist/src/conversation/file-conversation-store.d.ts +93 -0
- package/dist/src/conversation/file-conversation-store.d.ts.map +1 -0
- package/dist/src/conversation/file-conversation-store.js +284 -0
- package/dist/src/conversation/file-conversation-store.js.map +1 -0
- package/dist/src/conversation/in-memory-conversation-store.d.ts +36 -0
- package/dist/src/conversation/in-memory-conversation-store.d.ts.map +1 -0
- package/dist/src/conversation/in-memory-conversation-store.js +146 -0
- package/dist/src/conversation/in-memory-conversation-store.js.map +1 -0
- package/dist/src/copilot-adapter.d.ts +33 -0
- package/dist/src/copilot-adapter.d.ts.map +1 -0
- package/dist/src/copilot-adapter.js +119 -0
- package/dist/src/copilot-adapter.js.map +1 -0
- package/dist/src/file-trace-enhanced.d.ts +123 -0
- package/dist/src/file-trace-enhanced.d.ts.map +1 -0
- package/dist/src/file-trace-enhanced.js +177 -0
- package/dist/src/file-trace-enhanced.js.map +1 -0
- package/dist/src/file-trace.d.ts +24 -0
- package/dist/src/file-trace.d.ts.map +1 -0
- package/dist/src/file-trace.js +60 -0
- package/dist/src/file-trace.js.map +1 -0
- package/dist/src/index.d.ts +63 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +40 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/json-store.d.ts +27 -0
- package/dist/src/json-store.d.ts.map +1 -0
- package/dist/src/json-store.js +93 -0
- package/dist/src/json-store.js.map +1 -0
- package/dist/src/llm.d.ts +35 -0
- package/dist/src/llm.d.ts.map +1 -0
- package/dist/src/llm.js +2 -0
- package/dist/src/llm.js.map +1 -0
- package/dist/src/memory/cold-storage.d.ts +60 -0
- package/dist/src/memory/cold-storage.d.ts.map +1 -0
- package/dist/src/memory/cold-storage.js +132 -0
- package/dist/src/memory/cold-storage.js.map +1 -0
- package/dist/src/memory/compression.d.ts +161 -0
- package/dist/src/memory/compression.d.ts.map +1 -0
- package/dist/src/memory/compression.js +193 -0
- package/dist/src/memory/compression.js.map +1 -0
- package/dist/src/memory/hot-memory.d.ts +69 -0
- package/dist/src/memory/hot-memory.d.ts.map +1 -0
- package/dist/src/memory/hot-memory.js +116 -0
- package/dist/src/memory/hot-memory.js.map +1 -0
- package/dist/src/memory/memory-budget.d.ts +162 -0
- package/dist/src/memory/memory-budget.d.ts.map +1 -0
- package/dist/src/memory/memory-budget.js +241 -0
- package/dist/src/memory/memory-budget.js.map +1 -0
- package/dist/src/memory/memory-config.d.ts +419 -0
- package/dist/src/memory/memory-config.d.ts.map +1 -0
- package/dist/src/memory/memory-config.js +297 -0
- package/dist/src/memory/memory-config.js.map +1 -0
- package/dist/src/memory/prefetcher.d.ts +137 -0
- package/dist/src/memory/prefetcher.d.ts.map +1 -0
- package/dist/src/memory/prefetcher.js +186 -0
- package/dist/src/memory/prefetcher.js.map +1 -0
- package/dist/src/memory/tiered-memory.d.ts +116 -0
- package/dist/src/memory/tiered-memory.d.ts.map +1 -0
- package/dist/src/memory/tiered-memory.js +215 -0
- package/dist/src/memory/tiered-memory.js.map +1 -0
- package/dist/src/memory/warm-storage.d.ts +74 -0
- package/dist/src/memory/warm-storage.d.ts.map +1 -0
- package/dist/src/memory/warm-storage.js +207 -0
- package/dist/src/memory/warm-storage.js.map +1 -0
- package/dist/src/openai-adapter.d.ts +20 -0
- package/dist/src/openai-adapter.d.ts.map +1 -0
- package/dist/src/openai-adapter.js +73 -0
- package/dist/src/openai-adapter.js.map +1 -0
- package/dist/src/parser.d.ts +27 -0
- package/dist/src/parser.d.ts.map +1 -0
- package/dist/src/parser.js +76 -0
- package/dist/src/parser.js.map +1 -0
- package/dist/src/runtime.d.ts +172 -0
- package/dist/src/runtime.d.ts.map +1 -0
- package/dist/src/runtime.js +436 -0
- package/dist/src/runtime.js.map +1 -0
- package/dist/src/schema-utils.d.ts +7 -0
- package/dist/src/schema-utils.d.ts.map +1 -0
- package/dist/src/schema-utils.js +71 -0
- package/dist/src/schema-utils.js.map +1 -0
- package/dist/src/stage.d.ts +139 -0
- package/dist/src/stage.d.ts.map +1 -0
- package/dist/src/stage.js +2 -0
- package/dist/src/stage.js.map +1 -0
- package/dist/src/store.d.ts +51 -0
- package/dist/src/store.d.ts.map +1 -0
- package/dist/src/store.js +2 -0
- package/dist/src/store.js.map +1 -0
- package/dist/src/tool.d.ts +12 -0
- package/dist/src/tool.d.ts.map +1 -0
- package/dist/src/tool.js +2 -0
- package/dist/src/tool.js.map +1 -0
- package/dist/src/trace.d.ts +60 -0
- package/dist/src/trace.d.ts.map +1 -0
- package/dist/src/trace.js +2 -0
- package/dist/src/trace.js.map +1 -0
- package/dist/src/validator.d.ts +17 -0
- package/dist/src/validator.d.ts.map +1 -0
- package/dist/src/validator.js +21 -0
- package/dist/src/validator.js.map +1 -0
- package/dist/src/workflow.d.ts +192 -0
- package/dist/src/workflow.d.ts.map +1 -0
- package/dist/src/workflow.js +50 -0
- package/dist/src/workflow.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory budget system for preventing resource exhaustion.
|
|
3
|
+
*
|
|
4
|
+
* Limits memory usage per workflow to prevent unbounded growth.
|
|
5
|
+
* Supports blocking or eviction strategies when budget is exceeded.
|
|
6
|
+
*
|
|
7
|
+
* @module memory/memory-budget
|
|
8
|
+
* @since 2.0.0
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Memory budget configuration.
|
|
12
|
+
*/
|
|
13
|
+
export interface MemoryBudgetConfig {
|
|
14
|
+
/** Maximum memory in bytes */
|
|
15
|
+
maxMemory: number;
|
|
16
|
+
/**
|
|
17
|
+
* Strategy when budget is exceeded:
|
|
18
|
+
* - 'block': Block allocation until memory is freed
|
|
19
|
+
* - 'evict': Evict lowest priority items to make space
|
|
20
|
+
*/
|
|
21
|
+
strategy: "block" | "evict";
|
|
22
|
+
/**
|
|
23
|
+
* Warning threshold (0-1).
|
|
24
|
+
* Emit warning when usage exceeds this fraction of maxMemory.
|
|
25
|
+
* @default 0.8
|
|
26
|
+
*/
|
|
27
|
+
warningThreshold?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Memory usage statistics.
|
|
31
|
+
*/
|
|
32
|
+
export interface MemoryUsage {
|
|
33
|
+
/** Currently used memory in bytes */
|
|
34
|
+
used: number;
|
|
35
|
+
/** Maximum allowed memory in bytes */
|
|
36
|
+
total: number;
|
|
37
|
+
/** Usage percentage (0-100) */
|
|
38
|
+
percentage: number;
|
|
39
|
+
/** Number of allocated items */
|
|
40
|
+
itemCount: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Memory allocation record.
|
|
44
|
+
*/
|
|
45
|
+
interface AllocationRecord {
|
|
46
|
+
key: string;
|
|
47
|
+
size: number;
|
|
48
|
+
priority: number;
|
|
49
|
+
allocatedAt: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Memory budget manager.
|
|
53
|
+
*
|
|
54
|
+
* Tracks memory allocations and enforces budget limits per workflow.
|
|
55
|
+
* Prevents resource exhaustion through blocking or eviction strategies.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const budget = new MemoryBudget({
|
|
60
|
+
* maxMemory: 1024 * 1024 * 100, // 100MB
|
|
61
|
+
* strategy: 'evict',
|
|
62
|
+
* warningThreshold: 0.8
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // Allocate memory
|
|
66
|
+
* await budget.allocate('key1', 1024, 5); // 1KB, priority 5
|
|
67
|
+
*
|
|
68
|
+
* // Check usage
|
|
69
|
+
* const usage = budget.getUsage();
|
|
70
|
+
* console.log(`Memory: ${usage.percentage}%`);
|
|
71
|
+
*
|
|
72
|
+
* // Release memory
|
|
73
|
+
* budget.release('key1');
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @since 2.0.0
|
|
77
|
+
*/
|
|
78
|
+
export declare class MemoryBudget {
|
|
79
|
+
private config;
|
|
80
|
+
private allocations;
|
|
81
|
+
private usedMemory;
|
|
82
|
+
private warningEmitted;
|
|
83
|
+
constructor(config: MemoryBudgetConfig);
|
|
84
|
+
/**
|
|
85
|
+
* Allocate memory for a key.
|
|
86
|
+
*
|
|
87
|
+
* @param key - Unique identifier for this allocation
|
|
88
|
+
* @param size - Size in bytes
|
|
89
|
+
* @param priority - Priority (higher = more important, less likely to evict)
|
|
90
|
+
* @throws {Error} If key already allocated or budget exceeded (when strategy is 'block')
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* await budget.allocate('stage:Step1', 2048, 10);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
allocate(key: string, size: number, priority?: number): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Release allocated memory.
|
|
100
|
+
*
|
|
101
|
+
* @param key - Key to release
|
|
102
|
+
* @returns True if memory was released, false if key not found
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* budget.release('stage:Step1');
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
release(key: string): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Update the size of an existing allocation.
|
|
112
|
+
*
|
|
113
|
+
* @param key - Key to update
|
|
114
|
+
* @param newSize - New size in bytes
|
|
115
|
+
* @throws {Error} If key not found or budget exceeded
|
|
116
|
+
*/
|
|
117
|
+
resize(key: string, newSize: number): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Get current memory usage.
|
|
120
|
+
*
|
|
121
|
+
* @returns Memory usage statistics
|
|
122
|
+
*/
|
|
123
|
+
getUsage(): MemoryUsage;
|
|
124
|
+
/**
|
|
125
|
+
* Get available memory.
|
|
126
|
+
*
|
|
127
|
+
* @returns Available bytes
|
|
128
|
+
*/
|
|
129
|
+
getAvailable(): number;
|
|
130
|
+
/**
|
|
131
|
+
* Check if an allocation would fit within budget.
|
|
132
|
+
*
|
|
133
|
+
* @param size - Size to check
|
|
134
|
+
* @returns True if allocation would fit
|
|
135
|
+
*/
|
|
136
|
+
canAllocate(size: number): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Get all current allocations.
|
|
139
|
+
*
|
|
140
|
+
* @returns Array of allocation records
|
|
141
|
+
*/
|
|
142
|
+
getAllocations(): AllocationRecord[];
|
|
143
|
+
/**
|
|
144
|
+
* Clear all allocations.
|
|
145
|
+
*/
|
|
146
|
+
clear(): void;
|
|
147
|
+
/**
|
|
148
|
+
* Evict items to free up required space.
|
|
149
|
+
*
|
|
150
|
+
* Evicts lowest priority items first, then oldest items.
|
|
151
|
+
*
|
|
152
|
+
* @param bytesNeeded - Minimum bytes to free
|
|
153
|
+
* @throws {Error} If cannot free enough space
|
|
154
|
+
*/
|
|
155
|
+
private evictToFree;
|
|
156
|
+
/**
|
|
157
|
+
* Check if warning threshold is exceeded and emit warning.
|
|
158
|
+
*/
|
|
159
|
+
private checkWarningThreshold;
|
|
160
|
+
}
|
|
161
|
+
export {};
|
|
162
|
+
//# sourceMappingURL=memory-budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-budget.d.ts","sourceRoot":"","sources":["../../../src/memory/memory-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,8BAA8B;IAC9B,SAAS,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC;IAE5B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAC;IAEb,sCAAsC;IACtC,KAAK,EAAE,MAAM,CAAC;IAEd,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IAEnB,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,UAAU,gBAAgB;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAA+B;IAC7C,OAAO,CAAC,WAAW,CAA4C;IAC/D,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,cAAc,CAAkB;gBAE5B,MAAM,EAAE,kBAAkB;IAkBtC;;;;;;;;;;;;OAYG;IACG,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAsC9E;;;;;;;;;;OAUG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAkB7B;;;;;;OAMG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBzD;;;;OAIG;IACH,QAAQ,IAAI,WAAW;IASvB;;;;OAIG;IACH,YAAY,IAAI,MAAM;IAItB;;;;;OAKG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIlC;;;;OAIG;IACH,cAAc,IAAI,gBAAgB,EAAE;IAIpC;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;;;;;;OAOG;YACW,WAAW;IAmCzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;CAW9B"}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory budget system for preventing resource exhaustion.
|
|
3
|
+
*
|
|
4
|
+
* Limits memory usage per workflow to prevent unbounded growth.
|
|
5
|
+
* Supports blocking or eviction strategies when budget is exceeded.
|
|
6
|
+
*
|
|
7
|
+
* @module memory/memory-budget
|
|
8
|
+
* @since 2.0.0
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Memory budget manager.
|
|
12
|
+
*
|
|
13
|
+
* Tracks memory allocations and enforces budget limits per workflow.
|
|
14
|
+
* Prevents resource exhaustion through blocking or eviction strategies.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const budget = new MemoryBudget({
|
|
19
|
+
* maxMemory: 1024 * 1024 * 100, // 100MB
|
|
20
|
+
* strategy: 'evict',
|
|
21
|
+
* warningThreshold: 0.8
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Allocate memory
|
|
25
|
+
* await budget.allocate('key1', 1024, 5); // 1KB, priority 5
|
|
26
|
+
*
|
|
27
|
+
* // Check usage
|
|
28
|
+
* const usage = budget.getUsage();
|
|
29
|
+
* console.log(`Memory: ${usage.percentage}%`);
|
|
30
|
+
*
|
|
31
|
+
* // Release memory
|
|
32
|
+
* budget.release('key1');
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @since 2.0.0
|
|
36
|
+
*/
|
|
37
|
+
export class MemoryBudget {
|
|
38
|
+
config;
|
|
39
|
+
allocations = new Map();
|
|
40
|
+
usedMemory = 0;
|
|
41
|
+
warningEmitted = false;
|
|
42
|
+
constructor(config) {
|
|
43
|
+
this.config = {
|
|
44
|
+
...config,
|
|
45
|
+
warningThreshold: config.warningThreshold ?? 0.8,
|
|
46
|
+
};
|
|
47
|
+
if (this.config.maxMemory <= 0) {
|
|
48
|
+
throw new Error("maxMemory must be positive");
|
|
49
|
+
}
|
|
50
|
+
if (this.config.warningThreshold < 0 ||
|
|
51
|
+
this.config.warningThreshold > 1) {
|
|
52
|
+
throw new Error("warningThreshold must be between 0 and 1");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Allocate memory for a key.
|
|
57
|
+
*
|
|
58
|
+
* @param key - Unique identifier for this allocation
|
|
59
|
+
* @param size - Size in bytes
|
|
60
|
+
* @param priority - Priority (higher = more important, less likely to evict)
|
|
61
|
+
* @throws {Error} If key already allocated or budget exceeded (when strategy is 'block')
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* await budget.allocate('stage:Step1', 2048, 10);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
async allocate(key, size, priority = 5) {
|
|
69
|
+
if (this.allocations.has(key)) {
|
|
70
|
+
throw new Error(`Memory already allocated for key: ${key}`);
|
|
71
|
+
}
|
|
72
|
+
if (size < 0) {
|
|
73
|
+
throw new Error("Size must be non-negative");
|
|
74
|
+
}
|
|
75
|
+
const newUsed = this.usedMemory + size;
|
|
76
|
+
// Check if allocation would exceed budget
|
|
77
|
+
if (newUsed > this.config.maxMemory) {
|
|
78
|
+
if (this.config.strategy === "block") {
|
|
79
|
+
throw new Error(`Memory budget exceeded: ${newUsed} > ${this.config.maxMemory} ` +
|
|
80
|
+
`(would need ${newUsed - this.config.maxMemory} more bytes)`);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// Eviction strategy: free up space
|
|
84
|
+
const needed = newUsed - this.config.maxMemory;
|
|
85
|
+
await this.evictToFree(needed);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Allocate
|
|
89
|
+
this.allocations.set(key, {
|
|
90
|
+
key,
|
|
91
|
+
size,
|
|
92
|
+
priority,
|
|
93
|
+
allocatedAt: Date.now(),
|
|
94
|
+
});
|
|
95
|
+
this.usedMemory += size;
|
|
96
|
+
// Check warning threshold
|
|
97
|
+
this.checkWarningThreshold();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Release allocated memory.
|
|
101
|
+
*
|
|
102
|
+
* @param key - Key to release
|
|
103
|
+
* @returns True if memory was released, false if key not found
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* budget.release('stage:Step1');
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
release(key) {
|
|
111
|
+
const record = this.allocations.get(key);
|
|
112
|
+
if (!record) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
this.allocations.delete(key);
|
|
116
|
+
this.usedMemory -= record.size;
|
|
117
|
+
// Reset warning flag if usage drops below threshold
|
|
118
|
+
const usage = this.usedMemory / this.config.maxMemory;
|
|
119
|
+
if (usage < this.config.warningThreshold) {
|
|
120
|
+
this.warningEmitted = false;
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Update the size of an existing allocation.
|
|
126
|
+
*
|
|
127
|
+
* @param key - Key to update
|
|
128
|
+
* @param newSize - New size in bytes
|
|
129
|
+
* @throws {Error} If key not found or budget exceeded
|
|
130
|
+
*/
|
|
131
|
+
async resize(key, newSize) {
|
|
132
|
+
const record = this.allocations.get(key);
|
|
133
|
+
if (!record) {
|
|
134
|
+
throw new Error(`No allocation found for key: ${key}`);
|
|
135
|
+
}
|
|
136
|
+
const sizeDelta = newSize - record.size;
|
|
137
|
+
const newUsed = this.usedMemory + sizeDelta;
|
|
138
|
+
if (newUsed > this.config.maxMemory) {
|
|
139
|
+
if (this.config.strategy === "block") {
|
|
140
|
+
throw new Error(`Memory budget exceeded: resize would require ${sizeDelta} more bytes`);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
await this.evictToFree(sizeDelta);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
record.size = newSize;
|
|
147
|
+
this.usedMemory += sizeDelta;
|
|
148
|
+
this.checkWarningThreshold();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Get current memory usage.
|
|
152
|
+
*
|
|
153
|
+
* @returns Memory usage statistics
|
|
154
|
+
*/
|
|
155
|
+
getUsage() {
|
|
156
|
+
return {
|
|
157
|
+
used: this.usedMemory,
|
|
158
|
+
total: this.config.maxMemory,
|
|
159
|
+
percentage: (this.usedMemory / this.config.maxMemory) * 100,
|
|
160
|
+
itemCount: this.allocations.size,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get available memory.
|
|
165
|
+
*
|
|
166
|
+
* @returns Available bytes
|
|
167
|
+
*/
|
|
168
|
+
getAvailable() {
|
|
169
|
+
return Math.max(0, this.config.maxMemory - this.usedMemory);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Check if an allocation would fit within budget.
|
|
173
|
+
*
|
|
174
|
+
* @param size - Size to check
|
|
175
|
+
* @returns True if allocation would fit
|
|
176
|
+
*/
|
|
177
|
+
canAllocate(size) {
|
|
178
|
+
return this.usedMemory + size <= this.config.maxMemory;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get all current allocations.
|
|
182
|
+
*
|
|
183
|
+
* @returns Array of allocation records
|
|
184
|
+
*/
|
|
185
|
+
getAllocations() {
|
|
186
|
+
return Array.from(this.allocations.values());
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Clear all allocations.
|
|
190
|
+
*/
|
|
191
|
+
clear() {
|
|
192
|
+
this.allocations.clear();
|
|
193
|
+
this.usedMemory = 0;
|
|
194
|
+
this.warningEmitted = false;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Evict items to free up required space.
|
|
198
|
+
*
|
|
199
|
+
* Evicts lowest priority items first, then oldest items.
|
|
200
|
+
*
|
|
201
|
+
* @param bytesNeeded - Minimum bytes to free
|
|
202
|
+
* @throws {Error} If cannot free enough space
|
|
203
|
+
*/
|
|
204
|
+
async evictToFree(bytesNeeded) {
|
|
205
|
+
// Sort by priority (ascending) then by age (oldest first)
|
|
206
|
+
const candidates = Array.from(this.allocations.values()).sort((a, b) => {
|
|
207
|
+
if (a.priority !== b.priority) {
|
|
208
|
+
return a.priority - b.priority; // Lower priority first
|
|
209
|
+
}
|
|
210
|
+
return a.allocatedAt - b.allocatedAt; // Older first
|
|
211
|
+
});
|
|
212
|
+
let freedBytes = 0;
|
|
213
|
+
const evicted = [];
|
|
214
|
+
for (const record of candidates) {
|
|
215
|
+
this.allocations.delete(record.key);
|
|
216
|
+
this.usedMemory -= record.size;
|
|
217
|
+
freedBytes += record.size;
|
|
218
|
+
evicted.push(record.key);
|
|
219
|
+
if (freedBytes >= bytesNeeded) {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (freedBytes < bytesNeeded) {
|
|
224
|
+
throw new Error(`Cannot free enough memory: needed ${bytesNeeded}, freed ${freedBytes}. ` +
|
|
225
|
+
`Evicted ${evicted.length} items: ${evicted.join(", ")}`);
|
|
226
|
+
}
|
|
227
|
+
console.warn(`Memory budget: evicted ${evicted.length} items to free ${freedBytes} bytes`);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Check if warning threshold is exceeded and emit warning.
|
|
231
|
+
*/
|
|
232
|
+
checkWarningThreshold() {
|
|
233
|
+
const usage = this.usedMemory / this.config.maxMemory;
|
|
234
|
+
if (usage >= this.config.warningThreshold && !this.warningEmitted) {
|
|
235
|
+
console.warn(`Memory budget warning: ${(usage * 100).toFixed(1)}% used ` +
|
|
236
|
+
`(${this.usedMemory} / ${this.config.maxMemory} bytes)`);
|
|
237
|
+
this.warningEmitted = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=memory-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-budget.js","sourceRoot":"","sources":["../../../src/memory/memory-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAmDH;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,YAAY;IACf,MAAM,CAA+B;IACrC,WAAW,GAAkC,IAAI,GAAG,EAAE,CAAC;IACvD,UAAU,GAAW,CAAC,CAAC;IACvB,cAAc,GAAY,KAAK,CAAC;IAExC,YAAY,MAA0B;QACpC,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,GAAG;SACjD,CAAC;QAEF,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS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