@staix/agent-mem 0.30.0 → 0.31.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.
@@ -1,8 +1,9 @@
1
1
  import { createHash } from "node:crypto";
2
2
 
3
3
  type Data = Record<string, unknown>;
4
- interface Entry { sampledAt: number; generation: number; epoch: number; data: Data; revision: string; cursor: number; until: number; bytes: number }
5
- export interface SnapshotValue { data: Data; until: number }
4
+ export const FRESH_SNAPSHOT_CURSOR_BASE = 2 ** 52;
5
+ interface Entry { sampledAt: number; generation: number; epoch: number; data: Data; revision: string; cursor: number; until: number; bytes: number; validate?: () => void }
6
+ export interface SnapshotValue { data: Data; until: number; validate?: () => void; validationBytes?: number }
6
7
  interface Reading extends SnapshotValue { sampledAt: number; generation: number; entry?: Entry }
7
8
 
8
9
  /** Provider-local screened snapshots. No disk cache, timer, watcher or retained store. */
@@ -14,7 +15,7 @@ export class SnapshotCache {
14
15
  private epoch = 0;
15
16
  private reuseUnavailable = false;
16
17
  // Cached digests occupy the lower 52 bits; fresh replies have distinct safe-integer cursors above them.
17
- private freshCursor = 2 ** 52;
18
+ private freshCursor = FRESH_SNAPSHOT_CURSOR_BASE;
18
19
  private bytes = 0;
19
20
  private closed = false;
20
21
  constructor(private readonly revision: () => string, private readonly now: () => number = Date.now) {}
@@ -36,6 +37,9 @@ export class SnapshotCache {
36
37
  const now = this.now();
37
38
  return now >= sampledAt && now < until;
38
39
  }
40
+ private valid(value: { validate?: () => void }): boolean {
41
+ try { value.validate?.(); return true; } catch { return false; }
42
+ }
39
43
  private readable(value?: SnapshotValue): void {
40
44
  if (this.closed) throw Error("console snapshots are closed");
41
45
  if (value?.data.error !== undefined) throw Error("console snapshot unavailable");
@@ -72,16 +76,21 @@ export class SnapshotCache {
72
76
  private dropEntry(key: string, entry: Entry | undefined): void {
73
77
  if (entry && this.entries.get(key) === entry) { this.entries.delete(key); this.bytes -= entry.bytes; }
74
78
  }
75
- async snapshot(key: string, after: number, read: () => Promise<SnapshotValue>): Promise<Data> {
79
+ async snapshot(key: string, after: number, read: () => Promise<SnapshotValue>, onMiss?: (reason: "uncacheable selection" | "revision change" | "expired" | "no cached result" | "cache capacity") => void): Promise<Data> {
76
80
  this.readable();
77
81
  const revision = this.sourceRevision();
78
82
  this.readable();
79
- if (revision === undefined) return this.fresh(read);
83
+ if (revision === undefined) { onMiss?.("uncacheable selection"); return this.fresh(read); }
80
84
  const cached = this.entries.get(key);
81
- if (cached && cached.revision === revision && this.timely(cached.sampledAt, cached.until)) {
85
+ const cachedValid = !cached || cached.revision !== revision || this.valid(cached);
86
+ if (cached && cached.revision === revision && this.timely(cached.sampledAt, cached.until) && cachedValid) {
82
87
  this.entries.delete(key); this.entries.set(key, cached);
83
88
  return this.reply(cached, after);
84
89
  }
90
+ if (cached) onMiss?.(cached.revision === revision && cachedValid ? "expired" : "revision change");
91
+ else onMiss?.("no cached result");
92
+ // A copied input can change without affecting the broader revision; never join an older flight in that case.
93
+ if (!cachedValid) this.clearReusable();
85
94
  // Revision is part of the flight key: a new write never joins an older read.
86
95
  const flightKey = JSON.stringify([this.generation, this.epoch, revision, key]);
87
96
  let flight = this.flights.get(flightKey);
@@ -95,14 +104,18 @@ export class SnapshotCache {
95
104
  this.readable(value);
96
105
  const data = structuredClone(value.data); delete data.changed;
97
106
  const reading: Reading = { data, until: value.until, sampledAt, generation };
98
- if (observed !== revision || epoch !== this.epoch || generation !== this.generation || !this.timely(sampledAt, value.until)) {
107
+ const proofValid = this.valid(value);
108
+ if (observed !== revision || epoch !== this.epoch || generation !== this.generation || !this.timely(sampledAt, value.until) || !proofValid) {
109
+ if (observed !== revision || epoch !== this.epoch || generation !== this.generation || !proofValid) onMiss?.("revision change");
99
110
  this.dropEntry(key, cached);
100
111
  if (this.flights.get(flightKey) === flight) this.flights.delete(flightKey);
101
112
  return reading;
102
113
  }
103
- const serialized = JSON.stringify(data), bytes = Buffer.byteLength(serialized);
114
+ const proofBytes = value.validationBytes ?? 0;
115
+ if (!Number.isSafeInteger(proofBytes) || proofBytes < 0) throw Error("console snapshot proof size unavailable");
116
+ const serialized = JSON.stringify(data), bytes = Buffer.byteLength(serialized) + proofBytes;
104
117
  const cursor = Number.parseInt(createHash("sha256").update(JSON.stringify([key, revision, value.until])).update(serialized).digest("hex").slice(0, 13), 16) + 1;
105
- const entry = { sampledAt, generation, epoch, data, revision, cursor, until: value.until, bytes };
118
+ const entry = { sampledAt, generation, epoch, data, revision, cursor, until: value.until, bytes, ...(value.validate ? { validate: value.validate } : {}) };
106
119
  // ponytail: 16 selections and 4 MiB per provider. Larger views are served once;
107
120
  // upgrade with paged panel results rather than retaining an unbounded corpus.
108
121
  if (bytes <= 4 * 1024 * 1024) {
@@ -111,7 +124,7 @@ export class SnapshotCache {
111
124
  const first = this.entries.keys().next().value!; this.bytes -= this.entries.get(first)!.bytes; this.entries.delete(first);
112
125
  }
113
126
  this.entries.set(key, entry); this.bytes += bytes;
114
- }
127
+ } else onMiss?.("cache capacity");
115
128
  reading.entry = entry;
116
129
  return reading;
117
130
  })();
@@ -127,7 +140,7 @@ export class SnapshotCache {
127
140
  const observed = this.sourceRevision();
128
141
  this.readable(reading);
129
142
  if (reading.generation !== this.generation) { this.dropEntry(key, entry); return this.readFresh(read, false); }
130
- if (observed !== entry.revision || entry.epoch !== this.epoch || !this.timely(entry.sampledAt, entry.until)) {
143
+ if (observed !== entry.revision || entry.epoch !== this.epoch || !this.timely(entry.sampledAt, entry.until) || !this.valid(entry)) {
131
144
  this.dropEntry(key, entry); return this.freshReply(reading.data);
132
145
  }
133
146
  return this.reply(entry, after);