Repository navigation
Expand file tree
/
Copy pathcache.ts
More file actions
183 lines (165 loc) · 4.56 KB
/
Copy pathcache.ts
File metadata and controls
183 lines (165 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { createHash } from "node:crypto";
import type {
CacheDisposition,
CachedVisionAnalysis,
CacheStats,
PersistedCacheRecord,
PersistedCachePut,
} from "./types.ts";
export interface CacheResolution {
value: CachedVisionAnalysis;
disposition: CacheDisposition;
}
type StoreHandler = (record: PersistedCachePut) => void;
/**
* Session-scoped LRU cache with in-flight request coalescing.
* A generation/epoch check prevents a request started before clear-cache or tree navigation
* from repopulating the new cache when it eventually finishes.
*/
export class VisionAnalysisCache {
private entries = new Map<string, CachedVisionAnalysis>();
private inFlight = new Map<string, Promise<CachedVisionAnalysis>>();
private epoch = 0;
private currentGeneration = 0;
private hitCount = 0;
private missCount = 0;
private maxEntries: number;
private storeHandler?: StoreHandler;
constructor(maxEntries: number) {
this.maxEntries = maxEntries;
}
setStoreHandler(handler: StoreHandler | undefined): void {
this.storeHandler = handler;
}
setMaxEntries(maxEntries: number): void {
this.maxEntries = Math.max(1, maxEntries);
this.evictIfNeeded();
}
get generation(): number {
return this.currentGeneration;
}
async resolve(
key: string,
options: { enabled: boolean; bypass?: boolean },
compute: () => Promise<CachedVisionAnalysis>,
): Promise<CacheResolution> {
if (options.bypass) {
return { value: await compute(), disposition: "bypassed" };
}
if (options.enabled) {
const cached = this.entries.get(key);
if (cached) {
this.entries.delete(key);
this.entries.set(key, cached);
this.hitCount += 1;
return { value: cached, disposition: "hit" };
}
}
const existingRequest = this.inFlight.get(key);
if (existingRequest) {
this.hitCount += 1;
return { value: await existingRequest, disposition: "in-flight" };
}
this.missCount += 1;
const requestEpoch = this.epoch;
const requestGeneration = this.currentGeneration;
const request = compute();
this.inFlight.set(key, request);
try {
const value = await request;
if (options.enabled && requestEpoch === this.epoch && requestGeneration === this.currentGeneration) {
this.entries.set(key, value);
this.evictIfNeeded();
try {
this.storeHandler?.({
version: 1,
op: "put",
generation: this.currentGeneration,
key,
value,
});
} catch {
// Cache persistence must never turn a successful vision response into a failed turn.
}
}
return { value, disposition: options.enabled ? "miss" : "disabled" };
} finally {
if (this.inFlight.get(key) === request) this.inFlight.delete(key);
}
}
/** Logically clears cache content. Existing session JSONL entries remain append-only. */
clear(): number {
this.epoch += 1;
this.currentGeneration += 1;
this.entries.clear();
this.inFlight.clear();
return this.currentGeneration;
}
/** Replace state by folding cache records from the active session branch. */
restore(records: PersistedCacheRecord[]): void {
this.epoch += 1;
this.entries.clear();
this.inFlight.clear();
this.hitCount = 0;
this.missCount = 0;
this.currentGeneration = 0;
for (const record of records) {
if (record.version !== 1) continue;
if (record.op === "clear") {
this.currentGeneration = record.generation;
this.entries.clear();
continue;
}
if (record.generation !== this.currentGeneration) continue;
this.entries.set(record.key, record.value);
this.evictIfNeeded();
}
}
snapshot(): CacheStats {
return {
entries: this.entries.size,
inFlight: this.inFlight.size,
hits: this.hitCount,
misses: this.missCount,
generation: this.currentGeneration,
};
}
private evictIfNeeded(): void {
while (this.entries.size > this.maxEntries) {
const oldest = this.entries.keys().next().value as string | undefined;
if (oldest === undefined) return;
this.entries.delete(oldest);
}
}
}
export function sha256(value: string | Uint8Array): string {
return createHash("sha256").update(value).digest("hex");
}
export function buildVisionCacheKey(parts: {
imageHash: string;
mimeType: string;
questionHash: string;
promptHash: string;
endpointHash: string;
model: string;
provider: string;
optionsHash: string;
credentialHash: string;
namespace: string;
}): string {
return sha256(
[
"pi-vision-handoff-cache-v1",
parts.imageHash,
parts.mimeType,
parts.questionHash,
parts.promptHash,
parts.endpointHash,
parts.model,
parts.provider,
parts.optionsHash,
parts.credentialHash,
parts.namespace,
].join("\0"),
);
}