@xdarkicex/openclaw-memory-libravdb 1.4.58 → 1.4.60

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.
@@ -186,6 +186,15 @@ function truncateContentToTokenBudget(content, tokenBudget) {
186
186
  // Keep the tail so recent tool output / latest answer content is preserved.
187
187
  return normalized.slice(normalized.length - maxChars);
188
188
  }
189
+ function truncateSystemPromptAdditionToTokenBudget(value, tokenBudget) {
190
+ if (tokenBudget <= 0)
191
+ return "";
192
+ const maxChars = Math.max(1, tokenBudget * APPROX_CHARS_PER_TOKEN);
193
+ if (value.length <= maxChars)
194
+ return value;
195
+ // System additions are head-structured: preserve XML/preamble/instructions.
196
+ return value.slice(0, maxChars);
197
+ }
189
198
  function trimMessagesToBudget(messages, tokenBudget) {
190
199
  if (tokenBudget <= 0 || messages.length === 0) {
191
200
  return [];
@@ -205,7 +214,10 @@ function trimMessagesToBudget(messages, tokenBudget) {
205
214
  return kept.reverse();
206
215
  }
207
216
  const last = messages[messages.length - 1];
208
- const contentBudget = Math.max(1, tokenBudget - 8);
217
+ const contentBudget = tokenBudget - 8;
218
+ if (contentBudget <= 0) {
219
+ return [];
220
+ }
209
221
  const truncated = truncateContentToTokenBudget(last.content, contentBudget);
210
222
  if (!truncated) {
211
223
  return [];
@@ -219,16 +231,29 @@ function enforceTokenBudgetInvariant(result, tokenBudget) {
219
231
  const hardBudget = Math.max(1, Math.floor(tokenBudget));
220
232
  const effectiveBudget = resolveEffectiveAssembleBudget(hardBudget);
221
233
  const estimated = typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0;
234
+ const systemPromptTokens = approximateTokenCount(result.systemPromptAddition);
222
235
  const approxFromMessages = approximateMessagesTokens(result.messages);
223
- if (estimated <= effectiveBudget && approxFromMessages <= effectiveBudget) {
236
+ const approxTotal = systemPromptTokens + approxFromMessages;
237
+ if (estimated <= effectiveBudget && approxTotal <= effectiveBudget) {
224
238
  return result;
225
239
  }
226
- const trimmedMessages = trimMessagesToBudget(result.messages, effectiveBudget);
240
+ if (systemPromptTokens >= effectiveBudget) {
241
+ const trimmedSystemPromptAddition = truncateSystemPromptAdditionToTokenBudget(result.systemPromptAddition, effectiveBudget);
242
+ const trimmedSystemPromptTokens = approximateTokenCount(trimmedSystemPromptAddition);
243
+ return {
244
+ ...result,
245
+ systemPromptAddition: trimmedSystemPromptAddition,
246
+ messages: [],
247
+ estimatedTokens: Math.min(effectiveBudget, trimmedSystemPromptTokens),
248
+ };
249
+ }
250
+ const messageBudget = Math.max(0, effectiveBudget - systemPromptTokens);
251
+ const trimmedMessages = trimMessagesToBudget(result.messages, messageBudget);
227
252
  const trimmedEstimate = approximateMessagesTokens(trimmedMessages);
228
253
  return {
229
254
  ...result,
230
255
  messages: trimmedMessages,
231
- estimatedTokens: Math.min(effectiveBudget, trimmedEstimate),
256
+ estimatedTokens: Math.min(effectiveBudget, systemPromptTokens + trimmedEstimate),
232
257
  };
233
258
  }
234
259
  function buildBudgetFallbackContext(messages, tokenBudget) {
package/dist/index.js CHANGED
@@ -33806,6 +33806,12 @@ function truncateContentToTokenBudget(content, tokenBudget) {
33806
33806
  if (normalized.length <= maxChars) return normalized;
33807
33807
  return normalized.slice(normalized.length - maxChars);
33808
33808
  }
33809
+ function truncateSystemPromptAdditionToTokenBudget(value, tokenBudget) {
33810
+ if (tokenBudget <= 0) return "";
33811
+ const maxChars = Math.max(1, tokenBudget * APPROX_CHARS_PER_TOKEN);
33812
+ if (value.length <= maxChars) return value;
33813
+ return value.slice(0, maxChars);
33814
+ }
33809
33815
  function trimMessagesToBudget(messages, tokenBudget) {
33810
33816
  if (tokenBudget <= 0 || messages.length === 0) {
33811
33817
  return [];
@@ -33825,7 +33831,10 @@ function trimMessagesToBudget(messages, tokenBudget) {
33825
33831
  return kept.reverse();
33826
33832
  }
33827
33833
  const last = messages[messages.length - 1];
33828
- const contentBudget = Math.max(1, tokenBudget - 8);
33834
+ const contentBudget = tokenBudget - 8;
33835
+ if (contentBudget <= 0) {
33836
+ return [];
33837
+ }
33829
33838
  const truncated = truncateContentToTokenBudget(last.content, contentBudget);
33830
33839
  if (!truncated) {
33831
33840
  return [];
@@ -33839,16 +33848,32 @@ function enforceTokenBudgetInvariant(result, tokenBudget) {
33839
33848
  const hardBudget = Math.max(1, Math.floor(tokenBudget));
33840
33849
  const effectiveBudget = resolveEffectiveAssembleBudget(hardBudget);
33841
33850
  const estimated = typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0;
33851
+ const systemPromptTokens = approximateTokenCount(result.systemPromptAddition);
33842
33852
  const approxFromMessages = approximateMessagesTokens(result.messages);
33843
- if (estimated <= effectiveBudget && approxFromMessages <= effectiveBudget) {
33853
+ const approxTotal = systemPromptTokens + approxFromMessages;
33854
+ if (estimated <= effectiveBudget && approxTotal <= effectiveBudget) {
33844
33855
  return result;
33845
33856
  }
33846
- const trimmedMessages = trimMessagesToBudget(result.messages, effectiveBudget);
33857
+ if (systemPromptTokens >= effectiveBudget) {
33858
+ const trimmedSystemPromptAddition = truncateSystemPromptAdditionToTokenBudget(
33859
+ result.systemPromptAddition,
33860
+ effectiveBudget
33861
+ );
33862
+ const trimmedSystemPromptTokens = approximateTokenCount(trimmedSystemPromptAddition);
33863
+ return {
33864
+ ...result,
33865
+ systemPromptAddition: trimmedSystemPromptAddition,
33866
+ messages: [],
33867
+ estimatedTokens: Math.min(effectiveBudget, trimmedSystemPromptTokens)
33868
+ };
33869
+ }
33870
+ const messageBudget = Math.max(0, effectiveBudget - systemPromptTokens);
33871
+ const trimmedMessages = trimMessagesToBudget(result.messages, messageBudget);
33847
33872
  const trimmedEstimate = approximateMessagesTokens(trimmedMessages);
33848
33873
  return {
33849
33874
  ...result,
33850
33875
  messages: trimmedMessages,
33851
- estimatedTokens: Math.min(effectiveBudget, trimmedEstimate)
33876
+ estimatedTokens: Math.min(effectiveBudget, systemPromptTokens + trimmedEstimate)
33852
33877
  };
33853
33878
  }
33854
33879
  function buildBudgetFallbackContext(messages, tokenBudget) {
@@ -39370,6 +39395,7 @@ import path4 from "node:path";
39370
39395
  var STARTUP_CONNECT_MAX_RETRIES = 5;
39371
39396
  var STARTUP_CONNECT_BASE_DELAY_MS = 100;
39372
39397
  var STARTUP_CONNECT_MAX_TOTAL_WAIT_MS = 2e3;
39398
+ var CONNECTION_STABILITY_WINDOW_MS = 15e3;
39373
39399
  var SupervisorSocket = class {
39374
39400
  onData = /* @__PURE__ */ new Set();
39375
39401
  onClose = /* @__PURE__ */ new Set();
@@ -39478,12 +39504,13 @@ var SidecarSupervisor = class {
39478
39504
  degraded = false;
39479
39505
  shuttingDown = false;
39480
39506
  reconnectScheduled = false;
39507
+ stabilityTimer = null;
39481
39508
  socket;
39482
39509
  async start() {
39483
39510
  const endpoint = await this.runtime.resolveEndpoint(this.cfg);
39484
39511
  const socket = await this.connectEndpointWithRetry(endpoint);
39485
- this.retries = 0;
39486
39512
  this.reconnectScheduled = false;
39513
+ this.scheduleStabilityReset();
39487
39514
  if (this.socket instanceof SupervisorSocket) {
39488
39515
  this.socket.bind(socket);
39489
39516
  } else {
@@ -39496,8 +39523,29 @@ var SidecarSupervisor = class {
39496
39523
  }
39497
39524
  async shutdown() {
39498
39525
  this.shuttingDown = true;
39526
+ this.clearStabilityTimer();
39499
39527
  this.socket.destroy();
39500
39528
  }
39529
+ scheduleStabilityReset() {
39530
+ this.clearStabilityTimer();
39531
+ const windowMs = this.runtime.stabilityWindowMs ?? CONNECTION_STABILITY_WINDOW_MS;
39532
+ if (windowMs <= 0) {
39533
+ this.retries = 0;
39534
+ return;
39535
+ }
39536
+ this.stabilityTimer = setTimeout(() => {
39537
+ this.stabilityTimer = null;
39538
+ this.retries = 0;
39539
+ this.logger.info?.("[libravdb] sidecar connection stable; retry counter reset");
39540
+ }, windowMs);
39541
+ this.stabilityTimer.unref?.();
39542
+ }
39543
+ clearStabilityTimer() {
39544
+ if (this.stabilityTimer) {
39545
+ clearTimeout(this.stabilityTimer);
39546
+ this.stabilityTimer = null;
39547
+ }
39548
+ }
39501
39549
  async connectEndpointWithRetry(endpoint) {
39502
39550
  if (isTcpEndpoint(endpoint)) {
39503
39551
  this.logger.info?.(`[libravdb] using TCP endpoint ${endpoint}`);
@@ -39549,6 +39597,7 @@ var SidecarSupervisor = class {
39549
39597
  if (this.reconnectScheduled) {
39550
39598
  return;
39551
39599
  }
39600
+ this.clearStabilityTimer();
39552
39601
  const maxRetries = this.cfg.maxRetries ?? 3;
39553
39602
  if (this.retries >= maxRetries) {
39554
39603
  this.logger.error("[libravdb] sidecar retries exhausted; degraded mode");
package/dist/sidecar.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface SidecarRuntime {
6
6
  resolveEndpoint(cfg: PluginConfig): string | Promise<string>;
7
7
  createSocket(endpoint: string): SidecarSocket;
8
8
  scheduleRestart(delayMs: number, restart: () => void): void;
9
+ stabilityWindowMs?: number;
9
10
  }
10
11
  declare class PlaceholderSocket implements SidecarSocket {
11
12
  private readonly onData;
package/dist/sidecar.js CHANGED
@@ -5,6 +5,7 @@ import path from "node:path";
5
5
  const STARTUP_CONNECT_MAX_RETRIES = 5;
6
6
  const STARTUP_CONNECT_BASE_DELAY_MS = 100;
7
7
  const STARTUP_CONNECT_MAX_TOTAL_WAIT_MS = 2000;
8
+ const CONNECTION_STABILITY_WINDOW_MS = 15_000;
8
9
  class PlaceholderSocket {
9
10
  onData = new Set();
10
11
  onClose = new Set();
@@ -197,6 +198,7 @@ class SidecarSupervisor {
197
198
  degraded = false;
198
199
  shuttingDown = false;
199
200
  reconnectScheduled = false;
201
+ stabilityTimer = null;
200
202
  socket;
201
203
  constructor(cfg, logger, runtime) {
202
204
  this.cfg = cfg;
@@ -207,8 +209,8 @@ class SidecarSupervisor {
207
209
  async start() {
208
210
  const endpoint = await this.runtime.resolveEndpoint(this.cfg);
209
211
  const socket = await this.connectEndpointWithRetry(endpoint);
210
- this.retries = 0;
211
212
  this.reconnectScheduled = false;
213
+ this.scheduleStabilityReset();
212
214
  if (this.socket instanceof SupervisorSocket) {
213
215
  this.socket.bind(socket);
214
216
  }
@@ -222,8 +224,29 @@ class SidecarSupervisor {
222
224
  }
223
225
  async shutdown() {
224
226
  this.shuttingDown = true;
227
+ this.clearStabilityTimer();
225
228
  this.socket.destroy();
226
229
  }
230
+ scheduleStabilityReset() {
231
+ this.clearStabilityTimer();
232
+ const windowMs = this.runtime.stabilityWindowMs ?? CONNECTION_STABILITY_WINDOW_MS;
233
+ if (windowMs <= 0) {
234
+ this.retries = 0;
235
+ return;
236
+ }
237
+ this.stabilityTimer = setTimeout(() => {
238
+ this.stabilityTimer = null;
239
+ this.retries = 0;
240
+ this.logger.info?.("[libravdb] sidecar connection stable; retry counter reset");
241
+ }, windowMs);
242
+ this.stabilityTimer.unref?.();
243
+ }
244
+ clearStabilityTimer() {
245
+ if (this.stabilityTimer) {
246
+ clearTimeout(this.stabilityTimer);
247
+ this.stabilityTimer = null;
248
+ }
249
+ }
227
250
  async connectEndpointWithRetry(endpoint) {
228
251
  if (isTcpEndpoint(endpoint)) {
229
252
  this.logger.info?.(`[libravdb] using TCP endpoint ${endpoint}`);
@@ -275,6 +298,7 @@ class SidecarSupervisor {
275
298
  if (this.reconnectScheduled) {
276
299
  return;
277
300
  }
301
+ this.clearStabilityTimer();
278
302
  const maxRetries = this.cfg.maxRetries ?? 3;
279
303
  if (this.retries >= maxRetries) {
280
304
  this.logger.error("[libravdb] sidecar retries exhausted; degraded mode");
@@ -2,7 +2,7 @@
2
2
  "id": "libravdb-memory",
3
3
  "name": "LibraVDB Memory",
4
4
  "description": "Persistent vector memory with three-tier hybrid scoring",
5
- "version": "1.4.58",
5
+ "version": "1.4.60",
6
6
  "kind": [
7
7
  "memory",
8
8
  "context-engine"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xdarkicex/openclaw-memory-libravdb",
3
- "version": "1.4.58",
3
+ "version": "1.4.60",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",