@chrischall/gemini-mcp 1.6.2 → 1.7.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.
@@ -0,0 +1,138 @@
1
+ /**
2
+ * The durable half of the job registry (`src/jobs.ts`).
3
+ *
4
+ * ## Why this exists
5
+ *
6
+ * `JobRegistry` keeps jobs in a `Map`, which is exactly right for stdio: the
7
+ * process that starts a generation is the process that finishes it, and if it
8
+ * dies the user is sitting at a terminal watching it die.
9
+ *
10
+ * Hosted, that premise is false in a specific and non-obvious way. The
11
+ * connector is this same stdio server run as a Node child on an mcp-host Fly
12
+ * machine configured `auto_stop_machines = "stop"` with
13
+ * `min_machines_running = 0`. Fly stops that machine when nothing is in
14
+ * flight — mcp-host's own docs put it plainly: *"an open connection is exactly
15
+ * what stops the machine stopping"*. An `async: true` call is a request that
16
+ * answers in milliseconds, so it holds nothing open. The machine stops, the
17
+ * child is killed, and the generation dies **with the only record that it ever
18
+ * existed**. Polling `gemini_get_result` a couple of minutes later reports the
19
+ * id as unknown, which reads like an expiry bug and is actually a machine that
20
+ * went to sleep.
21
+ *
22
+ * This is also why a long `max_wait_ms` works where `async: true` does not: the
23
+ * wait keeps the HTTP request open, and the open request keeps the machine up.
24
+ *
25
+ * ## What durability can and cannot buy here
26
+ *
27
+ * It buys the RECORD, not the EXECUTION. There is no Workflows, no Queue, no
28
+ * Durable Object and no KV in reach — the child's environment is allowlist-only
29
+ * (mcp-host `spawn-env.ts`), and the only durable thing it can address is the
30
+ * blob store its media already goes to. Nothing here resumes a killed
31
+ * generation; re-running it would re-bill it, which is the cost this whole
32
+ * mechanism exists to avoid.
33
+ *
34
+ * So the contract is: a job never silently vanishes, and a job whose executor
35
+ * died ends in a **terminal** state that says so. The bytes a dead job managed
36
+ * to write are still in `gen/`, which is what `gemini_list_recent_media` is
37
+ * for.
38
+ *
39
+ * ## Liveness is a heartbeat, not a guess
40
+ *
41
+ * "Still running" and "the machine ate it" are indistinguishable from a single
42
+ * stored status, and a timeout long enough to be safe for a 12-scene set is far
43
+ * too long to be useful. So a running job re-stamps `updatedAt` on an interval
44
+ * ({@link HEARTBEAT_MS}), and a record whose stamp is older than
45
+ * {@link EXECUTOR_LOST_AFTER_MS} is read as `failed` — decided at READ time, so
46
+ * no boot scan is needed and a machine that never comes back still resolves its
47
+ * jobs the moment anybody asks.
48
+ *
49
+ * Every operation is best-effort and swallows its errors. A durable store that
50
+ * is down must degrade to the in-memory behaviour that came before it, never
51
+ * fail a generation the user is paying for.
52
+ */
53
+ import { createHash } from 'node:crypto';
54
+ /** How often a running job re-stamps its record so a reader can see it is alive. */
55
+ export const HEARTBEAT_MS = 60_000;
56
+ /**
57
+ * How stale a `running` record may get before its executor is presumed dead.
58
+ *
59
+ * Three missed heartbeats: long enough that a paused event loop mid-generation
60
+ * is not mistaken for a dead machine, short enough that a caller learns the
61
+ * truth in minutes rather than being told to keep polling forever.
62
+ */
63
+ export const EXECUTOR_LOST_AFTER_MS = 3 * HEARTBEAT_MS;
64
+ /** Where a job record lives, relative to the store root. */
65
+ const PREFIX = 'jobs';
66
+ /**
67
+ * An idempotency key is an arbitrary caller-supplied string: it can contain
68
+ * slashes, spaces, or `..`. Hashing it makes the key space closed by
69
+ * construction rather than by escaping, so no crafted key can address an object
70
+ * outside this tenant's prefix.
71
+ */
72
+ function keyHash(idempotencyKey) {
73
+ return createHash('sha256').update(idempotencyKey).digest('hex').slice(0, 32);
74
+ }
75
+ /** Job records over the mcp-host blob store. */
76
+ export function createBlobJobStore(bucket, opts) {
77
+ const now = opts.now ?? (() => Date.now());
78
+ const tenantOf = async () => (typeof opts.tenant === 'function' ? opts.tenant() : opts.tenant);
79
+ const recordKey = async (jobId) => `${PREFIX}/${await tenantOf()}/${encodeURIComponent(jobId)}.json`;
80
+ const pointerKey = async (k) => `${PREFIX}/${await tenantOf()}/keys/${keyHash(k)}.json`;
81
+ async function writeJson(key, value) {
82
+ try {
83
+ await bucket.put(key, JSON.stringify(value), { httpMetadata: { contentType: 'application/json' } });
84
+ }
85
+ catch {
86
+ // A durable write that fails leaves the in-memory registry exactly as
87
+ // capable as it was before this module existed. Failing the generation
88
+ // instead would trade a recoverable gap for a lost paid result.
89
+ }
90
+ }
91
+ async function readJson(key) {
92
+ try {
93
+ const obj = await bucket.get(key);
94
+ if (!obj)
95
+ return undefined;
96
+ return JSON.parse(new TextDecoder().decode(await obj.arrayBuffer()));
97
+ }
98
+ catch {
99
+ return undefined;
100
+ }
101
+ }
102
+ return {
103
+ async put(record) {
104
+ await writeJson(await recordKey(record.jobId), record);
105
+ },
106
+ async get(jobId) {
107
+ const record = await readJson(await recordKey(jobId));
108
+ if (!record || typeof record.status !== 'string')
109
+ return undefined;
110
+ if (record.status !== 'running')
111
+ return record;
112
+ if (now() - record.updatedAt <= EXECUTOR_LOST_AFTER_MS)
113
+ return record;
114
+ // Terminal, and honest about which kind of gone it is. Derived on read
115
+ // rather than persisted: the process that could have written the
116
+ // transition is by definition the one that is no longer there.
117
+ return {
118
+ ...record,
119
+ status: 'failed',
120
+ error: {
121
+ message: `Job "${jobId}" was lost: its executor stopped reporting ${Math.round((now() - record.updatedAt) / 1000)}s ` +
122
+ `ago, before the generation finished. This connector runs on a machine that stops when no request is ` +
123
+ `in flight, so background work started with \`async: true\` does not reliably survive.`,
124
+ hint: 'Re-run with `max_wait_ms` (e.g. 240000) instead of `async: true` — holding the request open keeps the ' +
125
+ 'executor alive. Any images the lost run did finish are still stored: list them with ' +
126
+ 'gemini_list_recent_media before paying for a regeneration.',
127
+ },
128
+ };
129
+ },
130
+ async putKey(idempotencyKey, jobId) {
131
+ await writeJson(await pointerKey(idempotencyKey), { jobId });
132
+ },
133
+ async jobIdForKey(idempotencyKey) {
134
+ const pointer = await readJson(await pointerKey(idempotencyKey));
135
+ return typeof pointer?.jobId === 'string' ? pointer.jobId : undefined;
136
+ },
137
+ };
138
+ }
package/dist/jobs.js CHANGED
@@ -1,8 +1,23 @@
1
1
  import { randomUUID, createHash } from 'node:crypto';
2
2
  import { textResult, McpToolError } from '@chrischall/mcp-utils';
3
3
  import { downloadFilename } from './media-name.js';
4
+ import { HEARTBEAT_MS } from './job-store.js';
4
5
  /** Recently-completed jobs are reusable by key for this long, then evicted. */
5
6
  const JOB_TTL_MS = 600_000; // 10 min
7
+ /**
8
+ * How long a DURABLE record stays reusable by idempotency key.
9
+ *
10
+ * Longer than {@link JOB_TTL_MS} on purpose: the in-memory window is bounded by
11
+ * a process that no longer exists, and the case this covers — a retry after the
12
+ * hosted machine went to sleep — can easily land further out than ten minutes.
13
+ * Still bounded, because a stored record survives for the blob store's ~30-day
14
+ * retention and silently replaying a month-old generation is a wrong answer
15
+ * that looks like a cache hit.
16
+ *
17
+ * 24h is also the longest a signed media link can live here, so a replay inside
18
+ * this window is one whose URLs can still be re-minted from their keys.
19
+ */
20
+ const DURABLE_JOB_REUSE_MS = 24 * 60 * 60 * 1000;
6
21
  /** Hard cap on retained entries (oldest-settled evicted first). */
7
22
  const JOB_MAX = 64;
8
23
  function isExpired(entry, now) {
@@ -131,13 +146,38 @@ function annotateReused(result, jobId) {
131
146
  };
132
147
  }
133
148
  /** Immediate handle for an async call — the caller polls `gemini_get_result`. */
134
- function jobHandle(jobId, status) {
149
+ function jobHandle(jobId, status, durable = false) {
135
150
  return textResult({
136
151
  job_id: jobId,
137
152
  status,
138
- hint: `Generation running in the background. Poll gemini_get_result with job_id "${jobId}" until status is "done" (results are per-process and expire ~10 min after completion).`,
153
+ hint: durable
154
+ ? `Generation running in the background. Poll gemini_get_result with job_id "${jobId}" until status is "done". The job record is stored durably, so a poll still works if this connector restarts — but background work is NOT restarted, so a long generation is safer with max_wait_ms than with async.`
155
+ : `Generation running in the background. Poll gemini_get_result with job_id "${jobId}" until status is "done" (results are per-process and expire ~10 min after completion).`,
139
156
  });
140
157
  }
158
+ /**
159
+ * Whether a result is worth persisting to the durable store.
160
+ *
161
+ * A hosted result is a small JSON manifest of `r2_key`s, which is exactly what
162
+ * a restart needs — the keys outlive every signed URL and `refreshMedia`
163
+ * re-signs from them. An `inline: true` result is raw base64 image bytes: too
164
+ * big to keep, and pointless to keep, since nothing about it survives usefully.
165
+ * Recording the omission is what lets a poll say *why* rather than reporting a
166
+ * success with nothing in it.
167
+ */
168
+ const MAX_PERSISTED_RESULT_BYTES = 64 * 1024;
169
+ function persistableResult(result) {
170
+ if ((result.content ?? []).some((block) => block.type !== 'text'))
171
+ return { resultOmitted: 'inline' };
172
+ try {
173
+ if (JSON.stringify(result).length > MAX_PERSISTED_RESULT_BYTES)
174
+ return { resultOmitted: 'too_large' };
175
+ }
176
+ catch {
177
+ return { resultOmitted: 'too_large' };
178
+ }
179
+ return { result };
180
+ }
141
181
  /**
142
182
  * Await `promise` for at most `waitMs`; `undefined` means the budget expired
143
183
  * with the work still pending (rejections propagate as usual). The timer never
@@ -163,14 +203,123 @@ export class JobRegistry {
163
203
  * never expire.
164
204
  */
165
205
  resigner;
206
+ /**
207
+ * Durable backing for this registry, when the deployment has somewhere to put
208
+ * it. Absent on a local stdio install, where the process that starts a
209
+ * generation is the process that finishes it and a `Map` is the whole truth.
210
+ *
211
+ * Present on the hosted connector, where it is load-bearing: see the header of
212
+ * `src/job-store.ts` for why an in-memory-only registry cannot work on a
213
+ * machine that stops whenever no request is in flight.
214
+ */
215
+ store;
216
+ /**
217
+ * When set, `async: true` is served as a bounded in-band wait instead of an
218
+ * immediate hand-off.
219
+ *
220
+ * Set by the hosted client, because there the two are not the trade-off they
221
+ * look like. Returning immediately releases the request, and the released
222
+ * request is the thing keeping the executor alive — so `async: true` is
223
+ * precisely the way to get a generation killed. Waiting holds the machine up
224
+ * until the work is done. If the budget still expires the caller gets the same
225
+ * job handle it asked for, now backed by a durable record.
226
+ */
227
+ asyncWaitFallbackMs;
228
+ /** Injectable clock (tests). */
229
+ now = () => Date.now();
166
230
  jobs = new Map();
167
231
  byKey = new Map(); // idempotencyKey -> jobId
168
232
  runningByFingerprint = new Map(); // fingerprint -> jobId (only while running)
233
+ /** Durable bookkeeping still in flight — awaited by `drain()` in tests. */
234
+ pending = new Set();
235
+ heartbeatTimer;
169
236
  /** Drop every job. Used by `SessionState.reset()` (tests) — not by handlers. */
170
237
  reset() {
171
238
  this.jobs.clear();
172
239
  this.byKey.clear();
173
240
  this.runningByFingerprint.clear();
241
+ this.stopHeartbeat();
242
+ }
243
+ /** Await outstanding durable writes. Tests only — handlers never need it. */
244
+ async drain() {
245
+ // Yield a macrotask first so a settle handler that has already fired gets
246
+ // its write queued before we look. Deliberately flushes only what is
247
+ // ISSUABLE: a job still running has no terminal write to wait for, and
248
+ // blocking on one would hang any test that drains before finishing a job.
249
+ for (let round = 0; round < 50; round++) {
250
+ await new Promise((resolve) => setTimeout(resolve, 0));
251
+ if (!this.pending.size)
252
+ return;
253
+ await Promise.allSettled([...this.pending]);
254
+ }
255
+ }
256
+ /**
257
+ * Re-stamp every running job's durable record.
258
+ *
259
+ * This is what separates "still working" from "the machine went away": a
260
+ * reader compares the stamp against `EXECUTOR_LOST_AFTER_MS`, so a 12-scene
261
+ * set that legitimately runs for ten minutes stays `running` while a job whose
262
+ * child was killed goes terminal within three missed beats.
263
+ */
264
+ async beat() {
265
+ if (!this.store)
266
+ return;
267
+ await Promise.allSettled([...this.jobs.values()]
268
+ .filter((e) => e.status === 'running')
269
+ // The status is re-read INSIDE the queued task, not sampled here: a job
270
+ // that finishes while this beat waits its turn must not have `running`
271
+ // written back over its terminal record.
272
+ .map((e) => this.writeRecord(e, { onlyWhileRunning: true })));
273
+ }
274
+ /**
275
+ * Queue one durable write for `entry`, after any write already in flight for
276
+ * it. `onlyWhileRunning` drops the write if the job settled in the meantime —
277
+ * which is what makes a late heartbeat a no-op rather than a resurrection.
278
+ */
279
+ writeRecord(entry, opts = {}) {
280
+ const next = (entry.writeChain ?? Promise.resolve()).then(async () => {
281
+ if (opts.onlyWhileRunning && entry.status !== 'running')
282
+ return;
283
+ await this.store.put(this.recordFor(entry, this.now()));
284
+ }, () => undefined);
285
+ entry.writeChain = next;
286
+ return next;
287
+ }
288
+ recordFor(entry, updatedAt) {
289
+ return {
290
+ jobId: entry.jobId,
291
+ toolName: entry.toolName,
292
+ fingerprint: entry.fingerprint,
293
+ ...(entry.idempotencyKey !== undefined ? { idempotencyKey: entry.idempotencyKey } : {}),
294
+ status: entry.status,
295
+ createdAt: entry.createdAt,
296
+ updatedAt,
297
+ ...(entry.error ? { error: entry.error } : {}),
298
+ ...(entry.status === 'done' && entry.result ? persistableResult(entry.result) : {}),
299
+ };
300
+ }
301
+ /** Run `p` as durable bookkeeping: tracked for `drain()`, never thrown from. */
302
+ track(p) {
303
+ const wrapped = p.catch(() => undefined).finally(() => this.pending.delete(wrapped));
304
+ this.pending.add(wrapped);
305
+ }
306
+ startHeartbeat() {
307
+ if (this.heartbeatTimer || !this.store)
308
+ return;
309
+ this.heartbeatTimer = setInterval(() => {
310
+ if (![...this.jobs.values()].some((e) => e.status === 'running')) {
311
+ this.stopHeartbeat();
312
+ return;
313
+ }
314
+ this.track(this.beat());
315
+ }, HEARTBEAT_MS);
316
+ // Never hold the process open for bookkeeping.
317
+ this.heartbeatTimer.unref?.();
318
+ }
319
+ stopHeartbeat() {
320
+ if (this.heartbeatTimer)
321
+ clearInterval(this.heartbeatTimer);
322
+ this.heartbeatTimer = undefined;
174
323
  }
175
324
  /** Drop expired entries, then enforce JOB_MAX by evicting oldest-settled first. */
176
325
  evict(now) {
@@ -213,8 +362,13 @@ export class JobRegistry {
213
362
  * different registry and therefore a genuinely separate generation.
214
363
  */
215
364
  async dispatch(opts, work) {
216
- const { toolName, fingerprint, idempotencyKey, async, waitMs } = opts;
217
- const now = Date.now();
365
+ const { toolName, fingerprint, idempotencyKey } = opts;
366
+ // Hosted, an immediate hand-off is what gets the executor reclaimed, so
367
+ // `async` is honoured by HOLDING the request rather than releasing it.
368
+ const degrade = opts.async === true && this.asyncWaitFallbackMs !== undefined;
369
+ const async = degrade ? false : opts.async;
370
+ const waitMs = degrade ? (opts.waitMs ?? this.asyncWaitFallbackMs) : opts.waitMs;
371
+ const now = this.now();
218
372
  let hit;
219
373
  if (idempotencyKey !== undefined) {
220
374
  const id = this.byKey.get(idempotencyKey);
@@ -228,6 +382,14 @@ export class JobRegistry {
228
382
  if (entry && entry.status === 'running')
229
383
  hit = entry;
230
384
  }
385
+ // Nothing in memory, but the key may name a job an earlier child ran. Only
386
+ // an explicit key unlocks this — the same rule the in-memory path uses, so
387
+ // a deliberate same-prompt variation is never silently deduplicated.
388
+ if (!hit && idempotencyKey !== undefined && this.store) {
389
+ const durable = await this.durableByKey(idempotencyKey, fingerprint);
390
+ if (durable)
391
+ return durable;
392
+ }
231
393
  if (hit) {
232
394
  if (async)
233
395
  return jobHandle(hit.jobId, hit.status);
@@ -249,25 +411,69 @@ export class JobRegistry {
249
411
  if (idempotencyKey !== undefined)
250
412
  this.byKey.set(idempotencyKey, jobId);
251
413
  this.runningByFingerprint.set(fingerprint, jobId);
252
- promise.then((res) => { entry.status = 'done'; entry.result = res; entry.settledAt = Date.now(); }, (err) => {
414
+ // Write the "running" record BEFORE the work can finish. A record that only
415
+ // appeared on completion would be missing for exactly the window this whole
416
+ // mechanism exists to cover.
417
+ if (this.store) {
418
+ this.track(this.writeRecord(entry));
419
+ if (idempotencyKey !== undefined)
420
+ this.track(this.store.putKey(idempotencyKey, jobId));
421
+ this.startHeartbeat();
422
+ }
423
+ const settle = promise.then((res) => { entry.status = 'done'; entry.result = res; entry.settledAt = this.now(); }, (err) => {
253
424
  entry.status = 'failed';
254
425
  entry.error = { message: err instanceof Error ? err.message : String(err), hint: err?.hint };
255
- entry.settledAt = Date.now();
426
+ entry.settledAt = this.now();
256
427
  }).finally(() => {
257
428
  if (this.runningByFingerprint.get(fingerprint) === jobId)
258
429
  this.runningByFingerprint.delete(fingerprint);
259
430
  });
431
+ if (this.store)
432
+ void settle.then(() => this.track(this.writeRecord(entry)));
260
433
  if (async)
261
- return jobHandle(jobId, 'running');
434
+ return jobHandle(jobId, 'running', this.store !== undefined);
262
435
  if (waitMs !== undefined) {
263
436
  const settled = await awaitWithBudget(promise, waitMs);
264
437
  // Budget expired with the work still running: hand back the job id — the
265
438
  // work continues and the caller polls, exactly as with `async: true`,
266
439
  // except a fast result would have been returned in-band.
267
- return settled ?? jobHandle(jobId, 'running');
440
+ return settled ?? jobHandle(jobId, 'running', this.store !== undefined);
268
441
  }
269
442
  return promise;
270
443
  }
444
+ /**
445
+ * Resolve an idempotency key against the durable store — the path a retry
446
+ * takes when the child that ran the original job is gone.
447
+ *
448
+ * `undefined` means "nothing durable to reuse", which includes a job that
449
+ * FAILED: a retry after an error must really retry. A job still recorded as
450
+ * running belongs to an executor this process cannot await, so the caller gets
451
+ * the handle back rather than a promise that would never settle.
452
+ */
453
+ async durableByKey(idempotencyKey, fingerprint) {
454
+ const id = await this.store.jobIdForKey(idempotencyKey);
455
+ if (!id)
456
+ return undefined;
457
+ const record = await this.store.get(id);
458
+ if (!record)
459
+ return undefined;
460
+ // An idempotency key is a habit as often as it is a promise — "1", "test",
461
+ // "retry". In memory that was survivable because the entry expired in ten
462
+ // minutes; a durable record lives for the store's retention, so replaying
463
+ // on the key ALONE would hand back a different prompt's image, weeks later,
464
+ // and label it a cache hit. The fingerprint is what makes this idempotency
465
+ // rather than a name lookup: same key + different request is a genuinely
466
+ // new generation.
467
+ if (record.fingerprint !== fingerprint)
468
+ return undefined;
469
+ if (this.now() - record.updatedAt > DURABLE_JOB_REUSE_MS)
470
+ return undefined;
471
+ if (record.status === 'running')
472
+ return jobHandle(id, 'running', true);
473
+ if (record.status !== 'done' || !record.result)
474
+ return undefined;
475
+ return annotateReused(await refreshMedia(record.result, this.resigner), id);
476
+ }
271
477
  /**
272
478
  * Retrieve an async job by id (backs the `gemini_get_result` tool):
273
479
  * `running` → a status handle; `done` → the recorded result payload;
@@ -277,15 +483,22 @@ export class JobRegistry {
277
483
  * the unknown-id path — the poll tool cannot be used to read another user's
278
484
  * result even if they know the uuid.
279
485
  */
280
- getResult(jobId) {
486
+ async getResult(jobId) {
281
487
  const entry = this.jobs.get(jobId);
282
488
  if (!entry) {
283
- throw new McpToolError(`No job "${jobId}" — unknown or expired.`, {
284
- hint: 'Jobs live with your session and are evicted ~10 min after completion (or on server restart). If the generation likely finished, check the output dir / <image>.json sidecar instead of polling.',
489
+ const durable = this.store ? await this.store.get(jobId) : undefined;
490
+ if (durable)
491
+ return this.fromRecord(durable);
492
+ throw new McpToolError(this.store
493
+ ? `No job "${jobId}" — no record of it exists. Either the id is wrong, or the job is older than the ${JOB_RECORD_RETENTION_LABEL} job records are kept.`
494
+ : `No job "${jobId}" — unknown or expired.`, {
495
+ hint: this.store
496
+ ? 'Job records are stored durably here, so a restart alone does not lose one — an id with no record was never started under this account. Images from a completed run are listed by gemini_list_recent_media.'
497
+ : 'Jobs live with your session and are evicted ~10 min after completion (or on server restart). If the generation likely finished, check the output dir / <image>.json sidecar instead of polling.',
285
498
  });
286
499
  }
287
500
  if (entry.status === 'running')
288
- return jobHandle(jobId, 'running');
501
+ return jobHandle(jobId, 'running', this.store !== undefined);
289
502
  if (entry.status === 'failed') {
290
503
  throw new McpToolError(entry.error?.message ?? 'Job failed.', entry.error?.hint ? { hint: entry.error.hint } : undefined);
291
504
  }
@@ -296,4 +509,36 @@ export class JobRegistry {
296
509
  throw new McpToolError(`Job "${jobId}" completed without a recorded result.`);
297
510
  return entry.result;
298
511
  }
512
+ /**
513
+ * Render a durable record as a poll answer — the path taken when the child
514
+ * that ran the job is gone.
515
+ *
516
+ * The three outcomes are kept distinct on purpose. Collapsing "your job
517
+ * failed", "your job was killed" and "no such job" into one message is what
518
+ * made the original bug read as an expiry problem when it was a machine
519
+ * lifecycle problem.
520
+ */
521
+ async fromRecord(record) {
522
+ if (record.status === 'running')
523
+ return jobHandle(record.jobId, 'running', true);
524
+ if (record.status === 'failed') {
525
+ throw new McpToolError(record.error?.message ?? 'Job failed.', record.error?.hint ? { hint: record.error.hint } : undefined);
526
+ }
527
+ if (record.resultOmitted) {
528
+ // Both reasons mean "the generation happened, the payload is not here",
529
+ // but only one of them is the caller's to change.
530
+ throw new McpToolError(record.resultOmitted === 'inline'
531
+ ? `Job "${record.jobId}" finished, but its result was returned inline (base64 image data) and inline results are not stored, so there is nothing to replay. Re-run without \`inline: true\` so the media is stored and returned as URLs, which a poll can replay.`
532
+ : `Job "${record.jobId}" finished, but its result was too large to store (over ${Math.round(MAX_PERSISTED_RESULT_BYTES / 1024)} KB), so there is nothing to replay.`, {
533
+ hint: 'The generation itself completed — list what it produced with gemini_list_recent_media rather than paying for it again.',
534
+ });
535
+ }
536
+ if (!record.result)
537
+ throw new McpToolError(`Job "${record.jobId}" completed without a recorded result.`);
538
+ // Re-sign before replaying: the record outlives the signed URLs inside it,
539
+ // and handing back a dead link is a failure that looks like a success.
540
+ return refreshMedia(record.result, this.resigner);
541
+ }
299
542
  }
543
+ /** How long the host keeps a stored object, quoted in the unknown-job message. */
544
+ const JOB_RECORD_RETENTION_LABEL = '30 days';
package/dist/session.js CHANGED
@@ -5,16 +5,17 @@ import { JobRegistry } from './jobs.js';
5
5
  * All of this used to be module-level (`jobs`/`byKey`/`runningByFingerprint` in
6
6
  * jobs.ts, `lastInteractionId`/`writtenOutputs` in tools/interact.ts). That is
7
7
  * correct for stdio, where a process serves exactly one user — and a
8
- * cross-tenant leak on the hosted connector, where Cloudflare shares ONE Worker
9
- * isolate across many Durable Object instances. Module state there is shared by
10
- * every authenticated claude.ai session in the isolate: the per-session
8
+ * cross-tenant leak on the hosted connector, where ONE child process serves
9
+ * every session of this registration at once (mcp-host runs the stdio server as
10
+ * a supervised child). Module state there is shared by
11
+ * every authenticated claude.ai session on that child: the per-session
11
12
  * `GeminiClient` isolated the API key but not the memory around it, so a
12
13
  * colliding `idempotency_key` replayed another user's result, an identical
13
14
  * prompt attached to another user's in-flight (billable) job, and
14
15
  * `continue_last: true` resumed another user's interaction under your key.
15
16
  *
16
17
  * So session memory hangs off the `GeminiClient` — the one object the connector
17
- * already builds per authenticated session (`buildClient` in src/worker.ts) and
18
+ * already builds per authenticated session and
18
19
  * threads into every registrar. Handlers reach it as `client.session`.
19
20
  *
20
21
  * The rule this encodes: **`src/` must hold no module-level mutable state.**
@@ -45,6 +45,30 @@ export async function tenantIdFor(secret) {
45
45
  // `gen`, not `media`: the objects are served at `/media/<key>`, and a `media`
46
46
  // prefix would make every URL read `/media/media/…`.
47
47
  const KEY_PREFIX_DEFAULT = 'gen';
48
+ /**
49
+ * How many listing pages one `listRecent` call will walk. A listing walk is
50
+ * unbounded work and this runs on a tool call, so it is capped — and the cap
51
+ * is REPORTED (see `listRecentPage`) rather than silently truncating.
52
+ */
53
+ const MAX_LIST_PAGES = 10;
54
+ /**
55
+ * Split a stored key back into the day it was written and the name it was asked
56
+ * for — the inverse of the key format `persist` builds.
57
+ *
58
+ * `undefined` for anything that is not a dated object key, so a stray object
59
+ * under the prefix is skipped rather than turned into a listing failure.
60
+ */
61
+ function parseMediaKey(key, prefix) {
62
+ const rest = key.slice(prefix.length);
63
+ const match = /^(\d{4}-\d{2}-\d{2})\/(.+)$/.exec(rest);
64
+ if (!match)
65
+ return undefined;
66
+ // Drop the random id `persist` prepends and the extension, leaving the
67
+ // caller's own slug — which is what makes a listing recognisable to a human
68
+ // looking for "the summer camp ones".
69
+ const name = match[2].replace(/^[0-9a-f]{4,}-/i, '').replace(/\.[A-Za-z0-9]+$/, '');
70
+ return { day: match[1], name: name || match[2] };
71
+ }
48
72
  /** Object-key-safe name: no slashes, no dot-segments, never empty. */
49
73
  export function safeKeySegment(base) {
50
74
  const cleaned = base
@@ -103,6 +127,52 @@ export function createR2Sink(bucket, opts) {
103
127
  mimeType: object.httpMetadata?.contentType ?? 'application/octet-stream',
104
128
  };
105
129
  },
130
+ async listRecent(opts = {}) {
131
+ return (await this.listRecentPage(opts)).media;
132
+ },
133
+ async listRecentPage({ limit = 20, sinceDay } = {}) {
134
+ if (!bucket.list)
135
+ return { media: [], truncated: false, scannedPages: 0 };
136
+ // Scoped at the STORE, not filtered afterwards: asking only for our own
137
+ // prefix means another account's keys are never in hand to leak by a
138
+ // filtering mistake.
139
+ const prefix = await ownPrefix();
140
+ const found = [];
141
+ let cursor;
142
+ let truncated = false;
143
+ let scannedPages = 0;
144
+ // Bounded: a listing walk is unbounded work, and this runs on a tool call.
145
+ for (let page = 0; page < MAX_LIST_PAGES; page++) {
146
+ scannedPages = page + 1;
147
+ const listed = await bucket.list({ prefix, cursor, limit: 1000 });
148
+ for (const obj of listed.objects) {
149
+ const parsed = parseMediaKey(obj.key, prefix);
150
+ if (!parsed)
151
+ continue; // not a dated object key — skip, never throw
152
+ if (sinceDay && parsed.day < sinceDay)
153
+ continue;
154
+ found.push({ ...parsed, key: obj.key, ...(obj.size !== undefined ? { sizeBytes: obj.size } : {}) });
155
+ }
156
+ if (!listed.truncated || !listed.cursor)
157
+ break;
158
+ cursor = listed.cursor;
159
+ // Ran out of budget with more to read: the answer is a subset, and
160
+ // must say so rather than passing for the whole picture.
161
+ if (page === MAX_LIST_PAGES - 1)
162
+ truncated = true;
163
+ }
164
+ // Keys sort lexicographically by day, and the id after it is random, so
165
+ // ordering is by day — which is the honest grain: the store records no
166
+ // per-object timestamp we could do better with.
167
+ found.sort((a, b) => (a.day < b.day ? 1 : a.day > b.day ? -1 : a.key < b.key ? 1 : -1));
168
+ const page = found.slice(0, limit);
169
+ const expiresAtMs = now().getTime() + ttlMs;
170
+ const media = await Promise.all(page.map(async (entry) => {
171
+ const link = await describe(entry.key, expiresAtMs);
172
+ return { ...entry, ...(link.unavailable ? {} : { url: link.ref, expiresAt: link.expiresAt }) };
173
+ }));
174
+ return { media, truncated: truncated || found.length > limit, scannedPages };
175
+ },
106
176
  async resign(key) {
107
177
  if (!(await owned(key)))
108
178
  return undefined;
@@ -228,6 +228,55 @@ export function registerFileTools(server, client) {
228
228
  hint: 'In a Claude chat client, download this URL into your sandbox and present it as an output file — inline image blocks and markdown embeds do not render for the end user.',
229
229
  });
230
230
  });
231
+ // The recovery path for media whose RESULT was lost rather than never
232
+ // produced — a background job killed with its machine still wrote its
233
+ // images, but every reference to them went with the response. Without this
234
+ // the only way to see them again is to pay for the generation twice.
235
+ server.registerTool('gemini_list_recent_media', {
236
+ description: 'List images/video/audio this connector recently generated for your account, newest first, with fresh openable URLs. ' +
237
+ 'Use it when a result was lost — a generation whose response never arrived, or an async job whose id came back unknown — ' +
238
+ 'to find what already exists BEFORE re-running (and re-paying for) it. Keywords: lost result, orphaned job, ' +
239
+ 'missing image, what did I generate, recover, recent media.',
240
+ annotations: { readOnlyHint: true, openWorldHint: false },
241
+ inputSchema: {
242
+ limit: z.number().int().positive().max(100).optional().describe('How many to return (default 20, newest first)'),
243
+ since_day: z
244
+ .string()
245
+ .regex(/^\d{4}-\d{2}-\d{2}$/)
246
+ .optional()
247
+ .describe('Only media stored on or after this UTC day, e.g. "2026-08-18"'),
248
+ },
249
+ }, async (args) => {
250
+ const sink = client.mediaSink;
251
+ if (!sink?.listRecent) {
252
+ throw new McpToolError('This server writes generated files to disk — list the output directory instead.', {
253
+ hint: 'gemini_list_recent_media applies to the hosted connector only.',
254
+ });
255
+ }
256
+ const limit = args.limit ?? 20;
257
+ const page = sink.listRecentPage
258
+ ? await sink.listRecentPage({ limit, sinceDay: args.since_day })
259
+ : { media: await sink.listRecent({ limit, sinceDay: args.since_day }), truncated: false };
260
+ const found = page.media;
261
+ return textResult({
262
+ count: found.length,
263
+ // Never let a bounded walk pass for the whole picture: concluding an
264
+ // image is gone is what makes someone pay to generate it twice.
265
+ ...(page.truncated ? { truncated: true } : {}),
266
+ media: found.map((m) => ({
267
+ r2_key: m.key,
268
+ day: m.day,
269
+ name: m.name,
270
+ ...(m.sizeBytes !== undefined ? { size_bytes: m.sizeBytes } : {}),
271
+ ...(m.url ? { url: m.url, curl_hint: `curl -sS -o ${downloadFilename(m.key)} "${m.url}"` } : {}),
272
+ ...(m.expiresAt ? { expires_at: m.expiresAt } : {}),
273
+ })),
274
+ hint: found.length
275
+ ? 'Ordered by the day each object was stored (the store keeps no finer timestamp), newest first. Re-sign any expired link with gemini_sign_media.' +
276
+ (page.truncated ? ' More media exists than is listed here — narrow with since_day, or raise limit, before concluding something is missing.' : '')
277
+ : 'Nothing stored in that window. Media is swept on a retention schedule, so an older generation may already be gone.',
278
+ });
279
+ });
231
280
  }
232
281
  server.registerTool('gemini_delete_file', {
233
282
  description: 'Delete an uploaded file, image or photo (by file_uri) from the Gemini Files API before its ~48h expiry. Any tool call still referencing it ' +