@qmediat.io/gemini-code-context-mcp 1.0.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/CHANGELOG.md +79 -0
- package/LICENSE +21 -0
- package/README.md +159 -0
- package/dist/auth/credentials-store.d.ts +47 -0
- package/dist/auth/credentials-store.d.ts.map +1 -0
- package/dist/auth/credentials-store.js +215 -0
- package/dist/auth/credentials-store.js.map +1 -0
- package/dist/auth/fingerprint.d.ts +8 -0
- package/dist/auth/fingerprint.d.ts.map +1 -0
- package/dist/auth/fingerprint.js +12 -0
- package/dist/auth/fingerprint.js.map +1 -0
- package/dist/auth/init-command.d.ts +9 -0
- package/dist/auth/init-command.d.ts.map +1 -0
- package/dist/auth/init-command.js +279 -0
- package/dist/auth/init-command.js.map +1 -0
- package/dist/auth/profile-loader.d.ts +28 -0
- package/dist/auth/profile-loader.d.ts.map +1 -0
- package/dist/auth/profile-loader.js +107 -0
- package/dist/auth/profile-loader.js.map +1 -0
- package/dist/cache/cache-manager.d.ts +78 -0
- package/dist/cache/cache-manager.d.ts.map +1 -0
- package/dist/cache/cache-manager.js +364 -0
- package/dist/cache/cache-manager.js.map +1 -0
- package/dist/cache/files-uploader.d.ts +42 -0
- package/dist/cache/files-uploader.d.ts.map +1 -0
- package/dist/cache/files-uploader.js +176 -0
- package/dist/cache/files-uploader.js.map +1 -0
- package/dist/cache/ttl-watcher.d.ts +21 -0
- package/dist/cache/ttl-watcher.d.ts.map +1 -0
- package/dist/cache/ttl-watcher.js +75 -0
- package/dist/cache/ttl-watcher.js.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +34 -0
- package/dist/config.js.map +1 -0
- package/dist/gemini/client.d.ts +7 -0
- package/dist/gemini/client.d.ts.map +1 -0
- package/dist/gemini/client.js +19 -0
- package/dist/gemini/client.js.map +1 -0
- package/dist/gemini/model-registry.d.ts +25 -0
- package/dist/gemini/model-registry.d.ts.map +1 -0
- package/dist/gemini/model-registry.js +70 -0
- package/dist/gemini/model-registry.js.map +1 -0
- package/dist/gemini/models.d.ts +23 -0
- package/dist/gemini/models.d.ts.map +1 -0
- package/dist/gemini/models.js +98 -0
- package/dist/gemini/models.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/indexer/globs.d.ts +25 -0
- package/dist/indexer/globs.d.ts.map +1 -0
- package/dist/indexer/globs.js +210 -0
- package/dist/indexer/globs.js.map +1 -0
- package/dist/indexer/hasher.d.ts +31 -0
- package/dist/indexer/hasher.d.ts.map +1 -0
- package/dist/indexer/hasher.js +61 -0
- package/dist/indexer/hasher.js.map +1 -0
- package/dist/indexer/workspace-scanner.d.ts +27 -0
- package/dist/indexer/workspace-scanner.d.ts.map +1 -0
- package/dist/indexer/workspace-scanner.js +77 -0
- package/dist/indexer/workspace-scanner.js.map +1 -0
- package/dist/manifest/db.d.ts +38 -0
- package/dist/manifest/db.d.ts.map +1 -0
- package/dist/manifest/db.js +223 -0
- package/dist/manifest/db.js.map +1 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +117 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/ask.tool.d.ts +34 -0
- package/dist/tools/ask.tool.d.ts.map +1 -0
- package/dist/tools/ask.tool.js +216 -0
- package/dist/tools/ask.tool.js.map +1 -0
- package/dist/tools/clear.tool.d.ts +15 -0
- package/dist/tools/clear.tool.d.ts.map +1 -0
- package/dist/tools/clear.tool.js +31 -0
- package/dist/tools/clear.tool.js.map +1 -0
- package/dist/tools/code.tool.d.ts +42 -0
- package/dist/tools/code.tool.d.ts.map +1 -0
- package/dist/tools/code.tool.js +327 -0
- package/dist/tools/code.tool.js.map +1 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +14 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +37 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +16 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/reindex.tool.d.ts +21 -0
- package/dist/tools/reindex.tool.d.ts.map +1 -0
- package/dist/tools/reindex.tool.js +62 -0
- package/dist/tools/reindex.tool.js.map +1 -0
- package/dist/tools/status.tool.d.ts +15 -0
- package/dist/tools/status.tool.d.ts.map +1 -0
- package/dist/tools/status.tool.js +105 -0
- package/dist/tools/status.tool.js.map +1 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/cost-estimator.d.ts +32 -0
- package/dist/utils/cost-estimator.d.ts.map +1 -0
- package/dist/utils/cost-estimator.js +83 -0
- package/dist/utils/cost-estimator.js.map +1 -0
- package/dist/utils/logger.d.ts +11 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +21 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/paths.d.ts +20 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +41 -0
- package/dist/utils/paths.js.map +1 -0
- package/dist/utils/progress.d.ts +16 -0
- package/dist/utils/progress.d.ts.map +1 -0
- package/dist/utils/progress.js +47 -0
- package/dist/utils/progress.js.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build / reuse / invalidate Gemini Context Caches keyed by workspace state.
|
|
3
|
+
*
|
|
4
|
+
* Cache key = `(workspaceRoot, filesHash, model, systemPromptHash)`. When any
|
|
5
|
+
* component changes, we rebuild. When the same key is requested concurrently
|
|
6
|
+
* (two ask() calls in parallel), we coalesce via a per-workspace in-process
|
|
7
|
+
* mutex so only one cache is created.
|
|
8
|
+
*
|
|
9
|
+
* Inline fallback path: for small workspaces or `noCache:true`, we skip the
|
|
10
|
+
* Files API upload entirely and embed file text inline. This avoids unnecessary
|
|
11
|
+
* network latency for workspaces that wouldn't benefit from caching anyway
|
|
12
|
+
* (Gemini rejects caches below 1024 tokens, and small workspaces don't amortise
|
|
13
|
+
* the upload cost over enough queries).
|
|
14
|
+
*
|
|
15
|
+
* Content shape: we emit ONE `{role:'user', parts: [...]}` Content with a
|
|
16
|
+
* `--- FILE: <relpath> ---` text marker before each file's data. This (a) gives
|
|
17
|
+
* the model enough context to resolve imports and architecture, and (b) avoids
|
|
18
|
+
* the consecutive-user-role-violation risk on stricter Gemini models.
|
|
19
|
+
*/
|
|
20
|
+
import { readFile } from 'node:fs/promises';
|
|
21
|
+
import { logger } from '../utils/logger.js';
|
|
22
|
+
import { uploadWorkspaceFiles } from './files-uploader.js';
|
|
23
|
+
function ttlString(seconds) {
|
|
24
|
+
return `${Math.max(60, Math.floor(seconds))}s`;
|
|
25
|
+
}
|
|
26
|
+
function isUsableExistingCache(ws, scan, model, systemPromptHash, now) {
|
|
27
|
+
if (!ws)
|
|
28
|
+
return false;
|
|
29
|
+
if (ws.filesHash !== scan.filesHash)
|
|
30
|
+
return false;
|
|
31
|
+
if (ws.model !== model.resolved)
|
|
32
|
+
return false;
|
|
33
|
+
if (ws.systemPromptHash !== systemPromptHash)
|
|
34
|
+
return false;
|
|
35
|
+
if (!ws.cacheId)
|
|
36
|
+
return false;
|
|
37
|
+
if (ws.cacheExpiresAt !== null && ws.cacheExpiresAt < now)
|
|
38
|
+
return false;
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Build a single consolidated user-role Content from uploaded files, with
|
|
43
|
+
* `--- FILE: <relpath> ---` markers so the model can resolve paths.
|
|
44
|
+
*
|
|
45
|
+
* Consolidating into ONE Content avoids Gemini's user/model alternation warnings
|
|
46
|
+
* (repeated `role:'user'` entries are tolerated today on flash/pro but the spec
|
|
47
|
+
* discourages them).
|
|
48
|
+
*/
|
|
49
|
+
function buildContentFromUploaded(files) {
|
|
50
|
+
const parts = [];
|
|
51
|
+
for (const f of files) {
|
|
52
|
+
if (typeof f.fileId !== 'string' || f.fileId.length === 0)
|
|
53
|
+
continue;
|
|
54
|
+
parts.push({ text: `\n\n--- FILE: ${f.relpath} ---\n` });
|
|
55
|
+
parts.push({
|
|
56
|
+
fileData: {
|
|
57
|
+
fileUri: f.fileId,
|
|
58
|
+
mimeType: 'text/plain',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (parts.length === 0)
|
|
63
|
+
return [];
|
|
64
|
+
return [{ role: 'user', parts }];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Build inline text Content directly from disk — used on the no-cache path.
|
|
68
|
+
* Skips the Files API entirely: we read each file (async, parallel per worker),
|
|
69
|
+
* prefix with a path marker, and embed as a single `{text}` part. Saves one
|
|
70
|
+
* network round-trip per file and avoids Gemini's 48 h auto-delete housekeeping
|
|
71
|
+
* on small workspaces.
|
|
72
|
+
*
|
|
73
|
+
* Hard aggregate cap (`MAX_INLINE_TOTAL_BYTES`) prevents a single pathological
|
|
74
|
+
* file from blocking the event loop or exhausting V8's string length cap. Files
|
|
75
|
+
* that would push the total over the cap are replaced with a visible
|
|
76
|
+
* `[SKIPPED: inline size cap]` marker so the model knows they were omitted.
|
|
77
|
+
*/
|
|
78
|
+
const MAX_INLINE_TOTAL_BYTES = 20 * 1024 * 1024; // 20 MB aggregate inline content
|
|
79
|
+
async function buildInlineContentFromDisk(scan) {
|
|
80
|
+
const parts = [];
|
|
81
|
+
let total = 0;
|
|
82
|
+
for (const f of scan.files) {
|
|
83
|
+
if (total + f.size > MAX_INLINE_TOTAL_BYTES) {
|
|
84
|
+
parts.push({
|
|
85
|
+
text: `\n\n--- FILE: ${f.relpath} [SKIPPED: inline size cap reached] ---\n`,
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
let content;
|
|
90
|
+
try {
|
|
91
|
+
content = await readFile(f.absolutePath, 'utf8');
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
logger.warn(`failed to read ${f.relpath}: ${String(err)}`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
parts.push({ text: `\n\n--- FILE: ${f.relpath} ---\n${content}\n` });
|
|
98
|
+
total += f.size;
|
|
99
|
+
}
|
|
100
|
+
if (parts.length === 0)
|
|
101
|
+
return [];
|
|
102
|
+
return [{ role: 'user', parts }];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* In-process mutex. Concurrent `prepareContext` calls for the SAME
|
|
106
|
+
* `(workspaceRoot, filesHash, model, systemPromptHash, allowCaching, cacheMinTokens)`
|
|
107
|
+
* fingerprint coalesce onto a single in-flight promise, avoiding duplicate
|
|
108
|
+
* cache creation (which would leak orphaned Gemini caches at $$$/M-token-hour).
|
|
109
|
+
*
|
|
110
|
+
* Keying on the full fingerprint is critical: `ask` and `code` tools use
|
|
111
|
+
* different system instructions, so their `systemPromptHash` differs. Coalescing
|
|
112
|
+
* on workspaceRoot alone would serve one tool's PreparedContext to the other —
|
|
113
|
+
* wrong cache / system-instruction mismatch. This bug was flagged by GPT + Copilot.
|
|
114
|
+
*
|
|
115
|
+
* Scope: single Node process. Cross-process races (two MCP servers sharing the
|
|
116
|
+
* same manifest DB) are not covered — documented in docs/KNOWN-DEFICITS.md.
|
|
117
|
+
*/
|
|
118
|
+
const inFlight = new Map();
|
|
119
|
+
function inFlightKey(opts) {
|
|
120
|
+
return [
|
|
121
|
+
opts.scan.workspaceRoot,
|
|
122
|
+
opts.scan.filesHash,
|
|
123
|
+
opts.model.resolved,
|
|
124
|
+
opts.systemPromptHash,
|
|
125
|
+
String(opts.allowCaching),
|
|
126
|
+
String(opts.cacheMinTokens ?? ''),
|
|
127
|
+
].join('\u241E'); // record-separator sentinel unlikely to appear in any field
|
|
128
|
+
}
|
|
129
|
+
export async function prepareContext(opts) {
|
|
130
|
+
const key = inFlightKey(opts);
|
|
131
|
+
const pending = inFlight.get(key);
|
|
132
|
+
if (pending)
|
|
133
|
+
return pending;
|
|
134
|
+
const promise = (async () => {
|
|
135
|
+
try {
|
|
136
|
+
return await doPrepareContext(opts);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
inFlight.delete(key);
|
|
140
|
+
}
|
|
141
|
+
})();
|
|
142
|
+
inFlight.set(key, promise);
|
|
143
|
+
return promise;
|
|
144
|
+
}
|
|
145
|
+
async function doPrepareContext(opts) {
|
|
146
|
+
const { client, manifest, scan, model, systemPromptHash, ttlSeconds, emitter, allowCaching } = opts;
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const existing = manifest.getWorkspace(scan.workspaceRoot);
|
|
149
|
+
// 1) Ensure the workspace row exists before any file rows reference it.
|
|
150
|
+
// The `files` table has a FK to `workspaces(workspace_root)`, so first-time
|
|
151
|
+
// uploads on a new workspace would otherwise fail with FOREIGN KEY constraint.
|
|
152
|
+
if (!existing) {
|
|
153
|
+
manifest.upsertWorkspace({
|
|
154
|
+
workspaceRoot: scan.workspaceRoot,
|
|
155
|
+
filesHash: scan.filesHash,
|
|
156
|
+
model: model.resolved,
|
|
157
|
+
systemPromptHash,
|
|
158
|
+
cacheId: null,
|
|
159
|
+
cacheExpiresAt: null,
|
|
160
|
+
fileIds: [],
|
|
161
|
+
createdAt: now,
|
|
162
|
+
updatedAt: now,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
// 2) If caching is disabled upfront, skip the Files API entirely and embed
|
|
166
|
+
// file text inline. No upload round-trip, no 48 h housekeeping.
|
|
167
|
+
if (!allowCaching) {
|
|
168
|
+
logger.debug('caching disabled — embedding files inline from disk.');
|
|
169
|
+
const inlineContents = await buildInlineContentFromDisk(scan);
|
|
170
|
+
manifest.upsertWorkspace({
|
|
171
|
+
workspaceRoot: scan.workspaceRoot,
|
|
172
|
+
filesHash: scan.filesHash,
|
|
173
|
+
model: model.resolved,
|
|
174
|
+
systemPromptHash,
|
|
175
|
+
cacheId: null,
|
|
176
|
+
cacheExpiresAt: null,
|
|
177
|
+
fileIds: [],
|
|
178
|
+
createdAt: existing?.createdAt ?? now,
|
|
179
|
+
updatedAt: now,
|
|
180
|
+
});
|
|
181
|
+
return {
|
|
182
|
+
cacheId: null,
|
|
183
|
+
cacheExpiresAt: null,
|
|
184
|
+
inlineContents,
|
|
185
|
+
uploaded: { files: [], uploadedCount: 0, reusedCount: 0, failedCount: 0, failures: [] },
|
|
186
|
+
rebuilt: false,
|
|
187
|
+
reused: false,
|
|
188
|
+
inlineOnly: true,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// 3) Reuse existing cache if fingerprints match. This runs BEFORE the
|
|
192
|
+
// token-floor check because reuse costs nothing — even a workspace that
|
|
193
|
+
// shrank below the floor should keep its valid cache until TTL expiry
|
|
194
|
+
// rather than being forced inline (which would orphan the cache on
|
|
195
|
+
// Google's side). Reordering from the previous build->reuse order was
|
|
196
|
+
// flagged by GPT review.
|
|
197
|
+
if (isUsableExistingCache(existing, scan, model, systemPromptHash, now)) {
|
|
198
|
+
logger.debug(`cache hit: ${existing?.cacheId}`);
|
|
199
|
+
return {
|
|
200
|
+
cacheId: existing?.cacheId ?? null,
|
|
201
|
+
cacheExpiresAt: existing?.cacheExpiresAt ?? null,
|
|
202
|
+
inlineContents: [],
|
|
203
|
+
uploaded: { files: [], uploadedCount: 0, reusedCount: 0, failedCount: 0, failures: [] },
|
|
204
|
+
rebuilt: false,
|
|
205
|
+
reused: true,
|
|
206
|
+
inlineOnly: false,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
// 4) Token-floor check — Gemini rejects `caches.create` below 1024 tokens.
|
|
210
|
+
// Skip the Files API entirely — there's no cache-build benefit for small
|
|
211
|
+
// workspaces and we save the upload overhead. Only applies when we're
|
|
212
|
+
// about to BUILD a new cache (reuse above already returned).
|
|
213
|
+
const minTokens = opts.cacheMinTokens ?? 1024;
|
|
214
|
+
const estimatedTokens = scan.files.reduce((sum, f) => sum + Math.ceil(f.size / 4), 0);
|
|
215
|
+
if (estimatedTokens < minTokens) {
|
|
216
|
+
logger.debug(`workspace too small for context cache (~${estimatedTokens} tokens < ${minTokens}); embedding inline.`);
|
|
217
|
+
const inlineContents = await buildInlineContentFromDisk(scan);
|
|
218
|
+
manifest.upsertWorkspace({
|
|
219
|
+
workspaceRoot: scan.workspaceRoot,
|
|
220
|
+
filesHash: scan.filesHash,
|
|
221
|
+
model: model.resolved,
|
|
222
|
+
systemPromptHash,
|
|
223
|
+
cacheId: null,
|
|
224
|
+
cacheExpiresAt: null,
|
|
225
|
+
fileIds: [],
|
|
226
|
+
createdAt: existing?.createdAt ?? now,
|
|
227
|
+
updatedAt: now,
|
|
228
|
+
});
|
|
229
|
+
return {
|
|
230
|
+
cacheId: null,
|
|
231
|
+
cacheExpiresAt: null,
|
|
232
|
+
inlineContents,
|
|
233
|
+
uploaded: { files: [], uploadedCount: 0, reusedCount: 0, failedCount: 0, failures: [] },
|
|
234
|
+
rebuilt: false,
|
|
235
|
+
reused: false,
|
|
236
|
+
inlineOnly: true,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// 5) Upload files (parallel pool inside uploadWorkspaceFiles).
|
|
240
|
+
emitter.emit(`indexing ${scan.files.length} files…`, 0, scan.files.length);
|
|
241
|
+
const uploaded = await uploadWorkspaceFiles({
|
|
242
|
+
client,
|
|
243
|
+
manifest,
|
|
244
|
+
workspaceRoot: scan.workspaceRoot,
|
|
245
|
+
files: scan.files,
|
|
246
|
+
emitter,
|
|
247
|
+
...(opts.uploadConcurrency !== undefined ? { concurrency: opts.uploadConcurrency } : {}),
|
|
248
|
+
});
|
|
249
|
+
// If enough uploads failed that the context is clearly lossy, bail now so the
|
|
250
|
+
// caller surfaces an error instead of silently returning a partial answer.
|
|
251
|
+
// Threshold: tiny workspaces (≤5 files) fail-fast on ANY upload failure; larger
|
|
252
|
+
// workspaces tolerate ≤5 % or <3 failures. The tiny-workspace carve-out
|
|
253
|
+
// prevents a 2-file repo with both uploads failing from falling through to a
|
|
254
|
+
// confusing Gemini 400 on empty contents.
|
|
255
|
+
const failureThreshold = scan.files.length <= 5 ? 1 : Math.max(3, Math.ceil(scan.files.length * 0.05));
|
|
256
|
+
if (uploaded.failedCount >= failureThreshold) {
|
|
257
|
+
const preview = uploaded.failures
|
|
258
|
+
.slice(0, 3)
|
|
259
|
+
.map((f) => `${f.relpath}: ${f.error}`)
|
|
260
|
+
.join(' | ');
|
|
261
|
+
throw new Error(`Upload failed for ${uploaded.failedCount}/${scan.files.length} files (threshold ${failureThreshold}). First errors: ${preview}`);
|
|
262
|
+
}
|
|
263
|
+
// 6) Delete any stale cache BEFORE attempting to build the new one.
|
|
264
|
+
// If the new build fails, the catch block can null-out cacheId in the
|
|
265
|
+
// manifest without leaking a reference on Google's side.
|
|
266
|
+
if (existing?.cacheId) {
|
|
267
|
+
try {
|
|
268
|
+
await client.caches.delete({ name: existing.cacheId });
|
|
269
|
+
logger.debug(`deleted stale cache ${existing.cacheId} before rebuild`);
|
|
270
|
+
}
|
|
271
|
+
catch (err) {
|
|
272
|
+
logger.debug(`pre-rebuild cache delete (${existing.cacheId}) failed: ${String(err)}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// 7) Build a new cache.
|
|
276
|
+
emitter.emit('building context cache…');
|
|
277
|
+
const fileContents = buildContentFromUploaded(uploaded.files);
|
|
278
|
+
try {
|
|
279
|
+
const baseConfig = {
|
|
280
|
+
contents: fileContents,
|
|
281
|
+
ttl: ttlString(ttlSeconds),
|
|
282
|
+
displayName: `gcctx-${scan.workspaceRoot.slice(-40)}`,
|
|
283
|
+
};
|
|
284
|
+
const cacheConfig = opts.systemInstruction !== undefined
|
|
285
|
+
? { ...baseConfig, systemInstruction: opts.systemInstruction }
|
|
286
|
+
: baseConfig;
|
|
287
|
+
const created = await client.caches.create({
|
|
288
|
+
model: model.resolved,
|
|
289
|
+
config: cacheConfig,
|
|
290
|
+
});
|
|
291
|
+
// Re-read the clock AFTER create returns — long uploads would otherwise
|
|
292
|
+
// make our tracked `cacheExpiresAt` artificially earlier than Google's.
|
|
293
|
+
const afterCreateMs = Date.now();
|
|
294
|
+
const cacheId = created.name ?? null;
|
|
295
|
+
const cacheExpiresAt = afterCreateMs + ttlSeconds * 1000;
|
|
296
|
+
manifest.upsertWorkspace({
|
|
297
|
+
workspaceRoot: scan.workspaceRoot,
|
|
298
|
+
filesHash: scan.filesHash,
|
|
299
|
+
model: model.resolved,
|
|
300
|
+
systemPromptHash,
|
|
301
|
+
cacheId,
|
|
302
|
+
cacheExpiresAt,
|
|
303
|
+
fileIds: uploaded.files.map((f) => f.fileId).filter((v) => v !== null),
|
|
304
|
+
createdAt: existing?.createdAt ?? now,
|
|
305
|
+
updatedAt: afterCreateMs,
|
|
306
|
+
});
|
|
307
|
+
return {
|
|
308
|
+
cacheId,
|
|
309
|
+
cacheExpiresAt,
|
|
310
|
+
inlineContents: [],
|
|
311
|
+
uploaded,
|
|
312
|
+
rebuilt: true,
|
|
313
|
+
reused: false,
|
|
314
|
+
inlineOnly: false,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
logger.warn(`cache build failed — falling back to inline parts: ${String(err)}`);
|
|
319
|
+
manifest.upsertWorkspace({
|
|
320
|
+
workspaceRoot: scan.workspaceRoot,
|
|
321
|
+
filesHash: scan.filesHash,
|
|
322
|
+
model: model.resolved,
|
|
323
|
+
systemPromptHash,
|
|
324
|
+
cacheId: null,
|
|
325
|
+
cacheExpiresAt: null,
|
|
326
|
+
fileIds: uploaded.files.map((f) => f.fileId).filter((v) => v !== null),
|
|
327
|
+
createdAt: existing?.createdAt ?? now,
|
|
328
|
+
updatedAt: now,
|
|
329
|
+
});
|
|
330
|
+
return {
|
|
331
|
+
cacheId: null,
|
|
332
|
+
cacheExpiresAt: null,
|
|
333
|
+
inlineContents: fileContents,
|
|
334
|
+
uploaded,
|
|
335
|
+
rebuilt: false,
|
|
336
|
+
reused: false,
|
|
337
|
+
inlineOnly: false,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
export async function invalidateWorkspaceCache(args) {
|
|
342
|
+
const ws = args.manifest.getWorkspace(args.workspaceRoot);
|
|
343
|
+
if (ws?.cacheId) {
|
|
344
|
+
try {
|
|
345
|
+
await args.client.caches.delete({ name: ws.cacheId });
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
logger.debug(`cache delete (${ws.cacheId}) failed: ${String(err)}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
args.manifest.deleteWorkspace(args.workspaceRoot);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Heuristic check for "cached content is stale / not found" errors from the
|
|
355
|
+
* Gemini API, used by ask/code to decide whether a retry with a rebuilt cache
|
|
356
|
+
* is worth attempting. Matches observed error strings; conservative by design
|
|
357
|
+
* — returns false for any error we don't recognise as cache-specific.
|
|
358
|
+
*/
|
|
359
|
+
export function isStaleCacheError(err) {
|
|
360
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
361
|
+
return (/cachedContent|cached[_ ]?content/i.test(msg) &&
|
|
362
|
+
(/not[_ ]?found|not-found|does not exist|expired|404/i.test(msg) || /NOT_FOUND/.test(msg)));
|
|
363
|
+
}
|
|
364
|
+
//# sourceMappingURL=cache-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-manager.js","sourceRoot":"","sources":["../../src/cache/cache-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAK5C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAqB,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AA2C9E,SAAS,SAAS,CAAC,OAAe;IAChC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,EA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,UAAU,iBAAiB,CAAC,GAAY;IAC5C,MAAM,GAAG,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7D,OAAO,CACL,mCAAmC,CAAC,IAAI,CAAC,GAAG,CAAC;QAC7C,CAAC,qDAAqD,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAC3F,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upload scanned files to the Gemini Files API with hash-based dedup.
|
|
3
|
+
*
|
|
4
|
+
* For each `ScannedFile` we look up an existing fresh `file_id` in the manifest
|
|
5
|
+
* keyed by content hash — if found AND the file has enough lifetime left to
|
|
6
|
+
* outlast a fresh Context Cache, we reuse it. Otherwise we upload.
|
|
7
|
+
*
|
|
8
|
+
* Files API auto-deletes uploads after 48h (Google-managed). We track
|
|
9
|
+
* `expires_at = uploadedAt + 47h`. When a reuse candidate's remaining life
|
|
10
|
+
* drops below `CACHE_BUILD_SAFETY_MARGIN_MS`, we re-upload so the subsequent
|
|
11
|
+
* Context Cache doesn't end up pointing at a file Google is about to delete.
|
|
12
|
+
*
|
|
13
|
+
* Uploads run in a bounded concurrent pool so a 500-file workspace doesn't
|
|
14
|
+
* block on serial HTTP round-trips.
|
|
15
|
+
*/
|
|
16
|
+
import type { GoogleGenAI } from '@google/genai';
|
|
17
|
+
import type { ScannedFile } from '../indexer/workspace-scanner.js';
|
|
18
|
+
import type { ManifestDb } from '../manifest/db.js';
|
|
19
|
+
import type { FileRow } from '../types.js';
|
|
20
|
+
import type { ProgressEmitter } from '../utils/progress.js';
|
|
21
|
+
export interface UploadFailure {
|
|
22
|
+
relpath: string;
|
|
23
|
+
error: string;
|
|
24
|
+
}
|
|
25
|
+
export interface UploadResult {
|
|
26
|
+
/** Rows matching what we now have in the manifest. */
|
|
27
|
+
files: FileRow[];
|
|
28
|
+
uploadedCount: number;
|
|
29
|
+
reusedCount: number;
|
|
30
|
+
/** Files that failed to upload. Caller decides how to surface this to the user. */
|
|
31
|
+
failedCount: number;
|
|
32
|
+
failures: UploadFailure[];
|
|
33
|
+
}
|
|
34
|
+
export declare function uploadWorkspaceFiles(args: {
|
|
35
|
+
client: GoogleGenAI;
|
|
36
|
+
manifest: ManifestDb;
|
|
37
|
+
workspaceRoot: string;
|
|
38
|
+
files: ScannedFile[];
|
|
39
|
+
emitter: ProgressEmitter;
|
|
40
|
+
concurrency?: number;
|
|
41
|
+
}): Promise<UploadResult>;
|
|
42
|
+
//# sourceMappingURL=files-uploader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files-uploader.d.ts","sourceRoot":"","sources":["../../src/cache/files-uploader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AA2B5D,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,sDAAsD;IACtD,KAAK,EAAE,OAAO,EAAE,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,mFAAmF;IACnF,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,aAAa,EAAE,CAAC;CAC3B;AAmCD,wBAAsB,oBAAoB,CAAC,IAAI,EAAE;IAC/C,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,UAAU,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,OAAO,EAAE,eAAe,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,YAAY,CAAC,CAuHxB"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upload scanned files to the Gemini Files API with hash-based dedup.
|
|
3
|
+
*
|
|
4
|
+
* For each `ScannedFile` we look up an existing fresh `file_id` in the manifest
|
|
5
|
+
* keyed by content hash — if found AND the file has enough lifetime left to
|
|
6
|
+
* outlast a fresh Context Cache, we reuse it. Otherwise we upload.
|
|
7
|
+
*
|
|
8
|
+
* Files API auto-deletes uploads after 48h (Google-managed). We track
|
|
9
|
+
* `expires_at = uploadedAt + 47h`. When a reuse candidate's remaining life
|
|
10
|
+
* drops below `CACHE_BUILD_SAFETY_MARGIN_MS`, we re-upload so the subsequent
|
|
11
|
+
* Context Cache doesn't end up pointing at a file Google is about to delete.
|
|
12
|
+
*
|
|
13
|
+
* Uploads run in a bounded concurrent pool so a 500-file workspace doesn't
|
|
14
|
+
* block on serial HTTP round-trips.
|
|
15
|
+
*/
|
|
16
|
+
import { basename } from 'node:path';
|
|
17
|
+
import { logger } from '../utils/logger.js';
|
|
18
|
+
const FILES_API_TTL_MS = 47 * 3600 * 1000; // 1 h safety margin before 48 h auto-delete
|
|
19
|
+
/**
|
|
20
|
+
* Files are unsafe to reuse when Google is about to delete them. A cache build
|
|
21
|
+
* takes 30-90 s and the cache itself lives for `cacheTtlSeconds` (default 1 h),
|
|
22
|
+
* so if the underlying file has less than this margin left, re-upload.
|
|
23
|
+
*/
|
|
24
|
+
const CACHE_BUILD_SAFETY_MARGIN_MS = 2 * 3600 * 1000; // 2 h
|
|
25
|
+
const DEFAULT_UPLOAD_CONCURRENCY = 10;
|
|
26
|
+
/**
|
|
27
|
+
* All source files upload as `text/plain`. Gemini accepts plain UTF-8 for any
|
|
28
|
+
* source format, and crucially: `fileData.fileUri` references must carry the
|
|
29
|
+
* SAME mime type declared at upload — otherwise `caches.create` returns 400
|
|
30
|
+
* "Request contains an invalid argument" (verified empirically).
|
|
31
|
+
*
|
|
32
|
+
* Per-extension MIME tuning (json → application/json, md → text/markdown) is
|
|
33
|
+
* tracked in `docs/ACCEPTED-RISKS.md`; it requires threading the per-file
|
|
34
|
+
* mime through `FileRow` + `buildContentFromUploaded` so upload and reference
|
|
35
|
+
* agree. Deferred pending evidence that the quality delta is worth the
|
|
36
|
+
* complexity.
|
|
37
|
+
*/
|
|
38
|
+
function uploadMimeType(_relpath) {
|
|
39
|
+
return 'text/plain';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Run `task` over `items` with up to `concurrency` in flight. Order of
|
|
43
|
+
* results matches order of items. Never throws — every failure is captured
|
|
44
|
+
* as a rejected `PromiseSettledResult` in the returned array.
|
|
45
|
+
*/
|
|
46
|
+
async function runPool(items, concurrency, task) {
|
|
47
|
+
const results = new Array(items.length);
|
|
48
|
+
let next = 0;
|
|
49
|
+
const workers = Array.from({ length: Math.max(1, Math.min(concurrency, items.length || 1)) }, async () => {
|
|
50
|
+
while (next < items.length) {
|
|
51
|
+
const i = next;
|
|
52
|
+
next += 1;
|
|
53
|
+
const item = items[i];
|
|
54
|
+
if (item === undefined)
|
|
55
|
+
continue;
|
|
56
|
+
try {
|
|
57
|
+
const value = await task(item, i);
|
|
58
|
+
results[i] = { status: 'fulfilled', value };
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
results[i] = { status: 'rejected', reason: err };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
await Promise.all(workers);
|
|
66
|
+
return results;
|
|
67
|
+
}
|
|
68
|
+
export async function uploadWorkspaceFiles(args) {
|
|
69
|
+
const { client, manifest, workspaceRoot, files, emitter } = args;
|
|
70
|
+
const concurrency = args.concurrency ?? DEFAULT_UPLOAD_CONCURRENCY;
|
|
71
|
+
const now = Date.now();
|
|
72
|
+
const out = new Array(files.length);
|
|
73
|
+
let uploadedCount = 0;
|
|
74
|
+
let reusedCount = 0;
|
|
75
|
+
let completed = 0;
|
|
76
|
+
const failures = [];
|
|
77
|
+
// First pass: decide reuse vs upload without hitting the network.
|
|
78
|
+
const plan = files.map((file) => {
|
|
79
|
+
const existing = manifest.findFileRowByHash(workspaceRoot, file.contentHash, now);
|
|
80
|
+
const canReuse = existing?.fileId != null &&
|
|
81
|
+
(existing.expiresAt == null || existing.expiresAt > now + CACHE_BUILD_SAFETY_MARGIN_MS);
|
|
82
|
+
return { file, existing: canReuse ? existing : null };
|
|
83
|
+
});
|
|
84
|
+
// In-batch dedup: two ScannedFiles with identical contentHash shouldn't both
|
|
85
|
+
// upload. We share the first upload's Promise and resolve both to the same
|
|
86
|
+
// fileId. Keyed on contentHash; scoped to this batch (not persisted).
|
|
87
|
+
const inBatchUploads = new Map();
|
|
88
|
+
await runPool(plan, concurrency, async (entry, i) => {
|
|
89
|
+
// Reuse path: preserve original uploadedAt / expiresAt so the Google-side
|
|
90
|
+
// 48 h clock continues from the original upload, not from our reuse moment.
|
|
91
|
+
if (entry.existing?.fileId) {
|
|
92
|
+
reusedCount += 1;
|
|
93
|
+
const row = {
|
|
94
|
+
workspaceRoot,
|
|
95
|
+
relpath: entry.file.relpath,
|
|
96
|
+
contentHash: entry.file.contentHash,
|
|
97
|
+
fileId: entry.existing.fileId,
|
|
98
|
+
uploadedAt: entry.existing.uploadedAt,
|
|
99
|
+
expiresAt: entry.existing.expiresAt,
|
|
100
|
+
};
|
|
101
|
+
manifest.upsertFile(row);
|
|
102
|
+
out[i] = row;
|
|
103
|
+
completed += 1;
|
|
104
|
+
emitter.emit(`indexed ${completed}/${files.length}: ${entry.file.relpath}`, completed, files.length);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Same-batch dedup: if another worker is already uploading this content hash,
|
|
108
|
+
// wait for their result instead of duplicating the upload.
|
|
109
|
+
const existingUpload = inBatchUploads.get(entry.file.contentHash);
|
|
110
|
+
let fileId;
|
|
111
|
+
let isDuplicate = false;
|
|
112
|
+
if (existingUpload) {
|
|
113
|
+
fileId = await existingUpload;
|
|
114
|
+
isDuplicate = true;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const uploadPromise = (async () => {
|
|
118
|
+
const uploaded = await client.files.upload({
|
|
119
|
+
file: entry.file.absolutePath,
|
|
120
|
+
config: {
|
|
121
|
+
mimeType: uploadMimeType(entry.file.relpath),
|
|
122
|
+
displayName: basename(entry.file.relpath),
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
// Prefer `.uri` — Gemini rejects `files/abc123` (the admin-API name)
|
|
126
|
+
// when used as `fileData.fileUri`. Verified via integration test.
|
|
127
|
+
const id = uploaded.uri ?? uploaded.name ?? null;
|
|
128
|
+
if (!id) {
|
|
129
|
+
throw new Error(`Files API returned no identifier for ${entry.file.relpath}`);
|
|
130
|
+
}
|
|
131
|
+
return id;
|
|
132
|
+
})();
|
|
133
|
+
inBatchUploads.set(entry.file.contentHash, uploadPromise);
|
|
134
|
+
fileId = await uploadPromise;
|
|
135
|
+
uploadedCount += 1;
|
|
136
|
+
}
|
|
137
|
+
const row = {
|
|
138
|
+
workspaceRoot,
|
|
139
|
+
relpath: entry.file.relpath,
|
|
140
|
+
contentHash: entry.file.contentHash,
|
|
141
|
+
fileId,
|
|
142
|
+
uploadedAt: now,
|
|
143
|
+
expiresAt: now + FILES_API_TTL_MS,
|
|
144
|
+
};
|
|
145
|
+
manifest.upsertFile(row);
|
|
146
|
+
out[i] = row;
|
|
147
|
+
if (isDuplicate)
|
|
148
|
+
reusedCount += 1;
|
|
149
|
+
completed += 1;
|
|
150
|
+
emitter.emit(`indexed ${completed}/${files.length}: ${entry.file.relpath}`, completed, files.length);
|
|
151
|
+
}).then((settled) => {
|
|
152
|
+
// Collect failures after the pool finishes — don't lose the mapping to files.
|
|
153
|
+
for (let i = 0; i < settled.length; i += 1) {
|
|
154
|
+
const r = settled[i];
|
|
155
|
+
const file = files[i];
|
|
156
|
+
if (r?.status === 'rejected' && file) {
|
|
157
|
+
const message = r.reason instanceof Error ? r.reason.message : String(r.reason);
|
|
158
|
+
logger.warn(`upload failed for ${file.relpath}: ${message}`);
|
|
159
|
+
failures.push({ relpath: file.relpath, error: message });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
// Pack only the successful rows into the final contiguous array for callers.
|
|
164
|
+
const files_out = [];
|
|
165
|
+
for (const row of out)
|
|
166
|
+
if (row)
|
|
167
|
+
files_out.push(row);
|
|
168
|
+
return {
|
|
169
|
+
files: files_out,
|
|
170
|
+
uploadedCount,
|
|
171
|
+
reusedCount,
|
|
172
|
+
failedCount: failures.length,
|
|
173
|
+
failures,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=files-uploader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files-uploader.js","sourceRoot":"","sources":["../../src/cache/files-uploader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAKrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,4CAA4C;AACvF;;;;GAIG;AACH,MAAM,4BAA4B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,MAAM;AAC5D,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAEtC;;;;;;;;;;;GAWG;AACH,SAAS,cAAc,CAAC,QAAgB;IACtC,OAAO,YAAY,CAAC;AACtB,CAAC;AAiBD;;;;GAIG;AACH,KAAK,UAAU,OAAO,CACpB,KAAU,EACV,WAAmB,EACn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,CAAC,CAAC,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;gBAChF,MAAM,CAAC,IAAI,CAAC,qBAAqB,IAAI,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC,CAAC;gBAC7D,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,6EAA6E;IAC7E,MAAM,SAAS,GAAc,EAAE,CAAC;IAChC,KAAK,MAAM,GAAG,IAAI,GAAG;QAAE,IAAI,GAAG;YAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEpD,OAAO;QACL,KAAK,EAAE,SAAS;QAChB,aAAa;QACb,WAAW;QACX,WAAW,EAAE,QAAQ,CAAC,MAAM;QAC5B,QAAQ;KACT,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background TTL refresher for "hot" workspaces.
|
|
3
|
+
*
|
|
4
|
+
* Gemini Context Caches default to 1h TTL and can be extended via `caches.update`
|
|
5
|
+
* with a new `ttl` value. We only refresh caches that were used in the last
|
|
6
|
+
* 10 minutes — cold workspaces are allowed to expire.
|
|
7
|
+
*/
|
|
8
|
+
import type { GoogleGenAI } from '@google/genai';
|
|
9
|
+
import type { ManifestDb } from '../manifest/db.js';
|
|
10
|
+
export declare class TtlWatcher {
|
|
11
|
+
private readonly client;
|
|
12
|
+
private readonly manifest;
|
|
13
|
+
private readonly hot;
|
|
14
|
+
private timer;
|
|
15
|
+
constructor(client: GoogleGenAI, manifest: ManifestDb);
|
|
16
|
+
start(): void;
|
|
17
|
+
stop(): void;
|
|
18
|
+
markHot(workspaceRoot: string, cacheId: string, ttlSeconds: number): void;
|
|
19
|
+
private tick;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=ttl-watcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ttl-watcher.d.ts","sourceRoot":"","sources":["../../src/cache/ttl-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAcpD,qBAAa,UAAU;IAKnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAL3B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA+B;IACnD,OAAO,CAAC,KAAK,CAA+B;gBAGzB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,UAAU;IAGvC,KAAK,IAAI,IAAI;IASb,IAAI,IAAI,IAAI;IAQZ,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;YAS3D,IAAI;CA4BnB"}
|