llm-large-files 0.9.0 → 0.10.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.
Files changed (222) hide show
  1. package/CHANGELOG.md +446 -0
  2. package/README.md +13 -7
  3. package/dist/access.d.ts +45 -3
  4. package/dist/access.d.ts.map +1 -1
  5. package/dist/access.js +106 -3
  6. package/dist/access.js.map +1 -1
  7. package/dist/aggregate.d.ts +6 -0
  8. package/dist/aggregate.d.ts.map +1 -1
  9. package/dist/aggregate.js +86 -13
  10. package/dist/aggregate.js.map +1 -1
  11. package/dist/analyze.d.ts +4 -0
  12. package/dist/analyze.d.ts.map +1 -1
  13. package/dist/analyze.js +2 -2
  14. package/dist/analyze.js.map +1 -1
  15. package/dist/answer-info.d.ts +26 -0
  16. package/dist/answer-info.d.ts.map +1 -0
  17. package/dist/answer-info.js +32 -0
  18. package/dist/answer-info.js.map +1 -0
  19. package/dist/anthropic/adapter.d.ts +5 -1
  20. package/dist/anthropic/adapter.d.ts.map +1 -1
  21. package/dist/anthropic/adapter.js +35 -10
  22. package/dist/anthropic/adapter.js.map +1 -1
  23. package/dist/ask.d.ts +115 -14
  24. package/dist/ask.d.ts.map +1 -1
  25. package/dist/ask.js +572 -103
  26. package/dist/ask.js.map +1 -1
  27. package/dist/citations.d.ts +36 -0
  28. package/dist/citations.d.ts.map +1 -0
  29. package/dist/citations.js +291 -0
  30. package/dist/citations.js.map +1 -0
  31. package/dist/error-info.d.ts +13 -0
  32. package/dist/error-info.d.ts.map +1 -1
  33. package/dist/error-info.js +37 -1
  34. package/dist/error-info.js.map +1 -1
  35. package/dist/errors.d.ts +3 -1
  36. package/dist/errors.d.ts.map +1 -1
  37. package/dist/errors.js +3 -1
  38. package/dist/errors.js.map +1 -1
  39. package/dist/events.d.ts +25 -7
  40. package/dist/events.d.ts.map +1 -1
  41. package/dist/events.js.map +1 -1
  42. package/dist/evidence.d.ts.map +1 -1
  43. package/dist/evidence.js +21 -2
  44. package/dist/evidence.js.map +1 -1
  45. package/dist/extract.d.ts.map +1 -1
  46. package/dist/extract.js +6 -1
  47. package/dist/extract.js.map +1 -1
  48. package/dist/gemini/adapter.d.ts +35 -4
  49. package/dist/gemini/adapter.d.ts.map +1 -1
  50. package/dist/gemini/adapter.js +68 -29
  51. package/dist/gemini/adapter.js.map +1 -1
  52. package/dist/gemini/chat.d.ts +28 -55
  53. package/dist/gemini/chat.d.ts.map +1 -1
  54. package/dist/gemini/chat.js +20 -27
  55. package/dist/gemini/chat.js.map +1 -1
  56. package/dist/gemini/endpoints.d.ts +59 -11
  57. package/dist/gemini/endpoints.d.ts.map +1 -1
  58. package/dist/gemini/endpoints.js +33 -7
  59. package/dist/gemini/endpoints.js.map +1 -1
  60. package/dist/gemini/errors.d.ts +39 -5
  61. package/dist/gemini/errors.d.ts.map +1 -1
  62. package/dist/gemini/errors.js +70 -9
  63. package/dist/gemini/errors.js.map +1 -1
  64. package/dist/gemini/generate.d.ts +53 -2
  65. package/dist/gemini/generate.d.ts.map +1 -1
  66. package/dist/gemini/generate.js +100 -50
  67. package/dist/gemini/generate.js.map +1 -1
  68. package/dist/gemini/index.d.ts +3 -3
  69. package/dist/gemini/index.d.ts.map +1 -1
  70. package/dist/http.d.ts +41 -3
  71. package/dist/http.d.ts.map +1 -1
  72. package/dist/http.js +40 -2
  73. package/dist/http.js.map +1 -1
  74. package/dist/index.d.ts +6 -4
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +4 -2
  77. package/dist/index.js.map +1 -1
  78. package/dist/ingest.d.ts +9 -1
  79. package/dist/ingest.d.ts.map +1 -1
  80. package/dist/ingest.js +50 -25
  81. package/dist/ingest.js.map +1 -1
  82. package/dist/library.d.ts +129 -2
  83. package/dist/library.d.ts.map +1 -1
  84. package/dist/library.js +224 -149
  85. package/dist/library.js.map +1 -1
  86. package/dist/map-reduce.d.ts +176 -10
  87. package/dist/map-reduce.d.ts.map +1 -1
  88. package/dist/map-reduce.js +804 -58
  89. package/dist/map-reduce.js.map +1 -1
  90. package/dist/model.d.ts +36 -4
  91. package/dist/model.d.ts.map +1 -1
  92. package/dist/model.js +17 -1
  93. package/dist/model.js.map +1 -1
  94. package/dist/node/handlers.d.ts +29 -8
  95. package/dist/node/handlers.d.ts.map +1 -1
  96. package/dist/node/handlers.js +148 -126
  97. package/dist/node/handlers.js.map +1 -1
  98. package/dist/node/index.d.ts +1 -1
  99. package/dist/node/index.d.ts.map +1 -1
  100. package/dist/node/index.js.map +1 -1
  101. package/dist/node/ingest-child.js +2 -2
  102. package/dist/node/ingest-child.js.map +1 -1
  103. package/dist/node/spool.d.ts +9 -5
  104. package/dist/node/spool.d.ts.map +1 -1
  105. package/dist/node/spool.js +28 -15
  106. package/dist/node/spool.js.map +1 -1
  107. package/dist/node/sse.d.ts +6 -5
  108. package/dist/node/sse.d.ts.map +1 -1
  109. package/dist/node/sse.js +8 -7
  110. package/dist/node/sse.js.map +1 -1
  111. package/dist/openai/adapter.d.ts +5 -1
  112. package/dist/openai/adapter.d.ts.map +1 -1
  113. package/dist/openai/adapter.js +53 -12
  114. package/dist/openai/adapter.js.map +1 -1
  115. package/dist/output-limit.d.ts +2 -1
  116. package/dist/output-limit.d.ts.map +1 -1
  117. package/dist/output-limit.js +5 -2
  118. package/dist/output-limit.js.map +1 -1
  119. package/dist/pdf-outline.d.ts +71 -0
  120. package/dist/pdf-outline.d.ts.map +1 -0
  121. package/dist/pdf-outline.js +414 -0
  122. package/dist/pdf-outline.js.map +1 -0
  123. package/dist/plan.d.ts +137 -7
  124. package/dist/plan.d.ts.map +1 -1
  125. package/dist/plan.js +248 -26
  126. package/dist/plan.js.map +1 -1
  127. package/dist/prepare.d.ts +50 -0
  128. package/dist/prepare.d.ts.map +1 -0
  129. package/dist/prepare.js +207 -0
  130. package/dist/prepare.js.map +1 -0
  131. package/dist/ranged.d.ts +2 -1
  132. package/dist/ranged.d.ts.map +1 -1
  133. package/dist/ranged.js +16 -3
  134. package/dist/ranged.js.map +1 -1
  135. package/dist/search.d.ts +7 -0
  136. package/dist/search.d.ts.map +1 -1
  137. package/dist/search.js +72 -10
  138. package/dist/search.js.map +1 -1
  139. package/dist/stores.d.ts +6 -0
  140. package/dist/stores.d.ts.map +1 -1
  141. package/dist/stores.js +82 -11
  142. package/dist/stores.js.map +1 -1
  143. package/dist/tools.d.ts +22 -2
  144. package/dist/tools.d.ts.map +1 -1
  145. package/dist/tools.js +323 -76
  146. package/dist/tools.js.map +1 -1
  147. package/dist/types.d.ts +49 -1
  148. package/dist/types.d.ts.map +1 -1
  149. package/dist/whole-cost.d.ts +37 -0
  150. package/dist/whole-cost.d.ts.map +1 -0
  151. package/dist/whole-cost.js +46 -0
  152. package/dist/whole-cost.js.map +1 -0
  153. package/docs/README.md +4 -0
  154. package/docs/guides/anthropic.md +5 -2
  155. package/docs/guides/conversations.md +1 -1
  156. package/docs/guides/costs-and-limits.md +132 -36
  157. package/docs/guides/deployment.md +106 -13
  158. package/docs/guides/errors-and-reliability.md +157 -45
  159. package/docs/guides/explorer-model.md +4 -1
  160. package/docs/guides/openai-compatible.md +47 -7
  161. package/docs/guides/own-adapter-and-loop.md +33 -6
  162. package/docs/guides/pdfs.md +96 -7
  163. package/docs/guides/storage.md +46 -3
  164. package/docs/guides/testing.md +71 -0
  165. package/docs/guides/vertex-and-gemini.md +159 -50
  166. package/docs/guides/web-app.md +10 -6
  167. package/docs/how-it-works.md +120 -22
  168. package/docs/measurements.md +172 -5
  169. package/docs/publishing.md +76 -4
  170. package/docs/reference/anthropic.md +12 -7
  171. package/docs/reference/ask.md +262 -35
  172. package/docs/reference/errors-and-retries.md +30 -2
  173. package/docs/reference/events.md +27 -10
  174. package/docs/reference/gemini.md +184 -68
  175. package/docs/reference/library.md +190 -23
  176. package/docs/reference/node.md +51 -17
  177. package/docs/reference/openai.md +21 -10
  178. package/docs/reference/tools.md +271 -26
  179. package/docs/security.md +101 -12
  180. package/docs/troubleshooting.md +14 -12
  181. package/docs/upgrading-0.10.md +472 -0
  182. package/package.json +5 -1
  183. package/src/access.ts +127 -4
  184. package/src/aggregate.ts +92 -14
  185. package/src/analyze.ts +2 -2
  186. package/src/answer-info.ts +45 -0
  187. package/src/anthropic/adapter.ts +40 -11
  188. package/src/ask.ts +646 -106
  189. package/src/citations.ts +294 -0
  190. package/src/error-info.ts +36 -1
  191. package/src/errors.ts +3 -1
  192. package/src/events.ts +29 -6
  193. package/src/evidence.ts +21 -2
  194. package/src/extract.ts +6 -1
  195. package/src/gemini/adapter.ts +99 -32
  196. package/src/gemini/chat.ts +53 -82
  197. package/src/gemini/endpoints.ts +89 -16
  198. package/src/gemini/errors.ts +74 -11
  199. package/src/gemini/generate.ts +147 -57
  200. package/src/gemini/index.ts +3 -3
  201. package/src/http.ts +65 -4
  202. package/src/index.ts +6 -4
  203. package/src/ingest.ts +58 -27
  204. package/src/library.ts +336 -142
  205. package/src/map-reduce.ts +855 -67
  206. package/src/model.ts +44 -4
  207. package/src/node/handlers.ts +159 -118
  208. package/src/node/index.ts +1 -0
  209. package/src/node/ingest-child.ts +2 -2
  210. package/src/node/spool.ts +28 -14
  211. package/src/node/sse.ts +8 -7
  212. package/src/openai/adapter.ts +56 -13
  213. package/src/output-limit.ts +5 -2
  214. package/src/pdf-outline.ts +400 -0
  215. package/src/plan.ts +287 -36
  216. package/src/prepare.ts +253 -0
  217. package/src/ranged.ts +16 -3
  218. package/src/search.ts +69 -13
  219. package/src/stores.ts +76 -11
  220. package/src/tools.ts +327 -75
  221. package/src/types.ts +45 -1
  222. package/src/whole-cost.ts +64 -0
package/src/ingest.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // `files.ingest`: reads a file, prepares its text, and stores it. Moved out of library.ts in 0.9.0:
2
2
  // the library hands it what it needs in an `IngestContext`.
3
3
  import { randomUUID } from "node:crypto";
4
- import { checkMetaVersion, checkOwner, heldBy, metaOf, OWNER_REQUIRED, SCOPE_REQUIRED, storeCall, storeContext, type AccessRules } from "./access.js";
4
+ import { checkMetaVersion, checkOwner, checkStuck, formatDuration, heldBy, metaOf, OWNER_REQUIRED, SCOPE_REQUIRED, storeCall, storeContext, watchStuck, type AccessRules } from "./access.js";
5
5
  import { buildLogProfileInSlices, buildOutlineInSlices, looksLikeLog } from "./analyze.js";
6
6
  import { DocumentScopeError, DocumentStoreError, IngestError, isStoreWriteFailure, noteStoreWriteFailure, toIngestError } from "./errors.js";
7
7
  import { EventEmitter, eventErrorOf, newRunId, type EventError, type IngestEvent, type IngestStage } from "./events.js";
@@ -9,6 +9,7 @@ import { detectFormat, extractorFor, extractText, isZip, namedFormat, sniffParts
9
9
  import type { IngestFromSource, IngestInput } from "./library.js";
10
10
  import type { Limiter } from "./limiter.js";
11
11
  import { TimeSlicer } from "./pause.js";
12
+ import { headingsOf, pdfOutline, type PdfHeading } from "./pdf-outline.js";
12
13
  import { formatBytes } from "./plan.js";
13
14
  import { measureQuality, type TextQuality } from "./quality.js";
14
15
  import { checkScope } from "./scope.js";
@@ -33,8 +34,16 @@ export type IngestContext = {
33
34
  readonly extractors: Partial<Record<DocumentFormat, Extractor>> | undefined;
34
35
  /** The ingests doing their work, and those waiting their turn (`maxConcurrentIngests`). */
35
36
  readonly ingests: Limiter;
36
- /** How long work an ingest waits on, or gave up on, may keep its turn (see `IngestSlot`). */
37
+ /**
38
+ * How long work an ingest waits on, or gave up on, may keep its turn (see `IngestSlot`). Its
39
+ * store calls are given this long too, and count as stuck past it (`watchStuck`).
40
+ */
37
41
  readonly slotCapMs: number;
42
+ /**
43
+ * While this many calls of the process are stuck, the ingest's store calls fail at once, without
44
+ * calling the store (`maxStuckStoreCalls`). New in 0.10.0.
45
+ */
46
+ readonly maxStuck: number;
38
47
  /**
39
48
  * Whether the uploaded file of a document of this format is kept: by its ref (`IngestInput.original`,
40
49
  * read by `LargeFilesOptions.originals`), or else as a copy in the store (`putOriginal`).
@@ -96,11 +105,14 @@ const exists = (name: string) => new IngestError("exists", `${name}: a document
96
105
  * call would say so of a document that is there. A document written by a newer version of the
97
106
  * package is never replaced (`DocumentStoreError` "NEWER_VERSION"). Returns true when there is a
98
107
  * document to delete before the new one is written: the one being replaced, or one expired.
108
+ *
109
+ * Since 0.10.0 the look-up is refused at once while too many calls of the process are stuck
110
+ * ("STORE_STUCK"), and counts as stuck once it has run `slotCapMs`, the time the ingest gives it.
99
111
  */
100
112
  async function claim(context: IngestContext, target: Target & { id: string }, name: string): Promise<boolean> {
101
- const { store, now } = context;
113
+ const { store, now, maxStuck, slotCapMs } = context;
102
114
  const { id } = target;
103
- const found = metaOf(await storeCall("read", () => store.getMeta(id, target.context)), id);
115
+ const found = metaOf(await watchStuck(storeCall("read", () => store.getMeta(id, target.context), { maxStuck }), slotCapMs), id);
104
116
  if (!found) return false;
105
117
  if (Date.parse(found.expiresAt) <= now().getTime()) return true;
106
118
  if (target.ifExists === "fail") throw exists(name);
@@ -116,18 +128,23 @@ async function claim(context: IngestContext, target: Target & { id: string }, na
116
128
  * be there: one the ingest was to replace (its delete failed), or one another server stored
117
129
  * meanwhile. It is left. "taken" when the ingest could not have replaced it: it has not expired, and
118
130
  * it is of another owner or scope. "left" for any other.
131
+ *
132
+ * Since 0.10.0 its calls count as stuck once they have run `slotCapMs`. They are not refused while
133
+ * too many calls are stuck: they follow writes of the ingest that began under the limit and have
134
+ * just ended, and a document left behind would be kept until it expires (for good, under
135
+ * `ttlMs: Infinity`) though its caller left or was told it failed.
119
136
  */
120
- async function removeOwn(context: Pick<IngestContext, "store" | "now">, target: Target, meta: DocumentMeta): Promise<"removed" | "left" | "taken"> {
121
- const { store, now } = context;
137
+ async function removeOwn(context: IngestContext, target: Target, meta: DocumentMeta): Promise<"removed" | "left" | "taken"> {
138
+ const { store, now, slotCapMs } = context;
122
139
  if (target.id !== undefined) {
123
- const found = metaOf(await storeCall("read", () => store.getMeta(meta.id, target.context)), meta.id);
140
+ const found = metaOf(await watchStuck(storeCall("read", () => store.getMeta(meta.id, target.context)), slotCapMs), meta.id);
124
141
  if (found && !(found.createdAt === meta.createdAt && heldBy(found, meta.owner, meta.scope))) {
125
142
  // As `claim` sees it: a date that cannot be read has not expired.
126
143
  const expired = Date.parse(found.expiresAt) <= now().getTime();
127
144
  return !expired && !heldBy(found, target.owner, target.scope) ? "taken" : "left";
128
145
  }
129
146
  }
130
- await store.delete(meta.id, target.context);
147
+ await watchStuck(Promise.resolve(store.delete(meta.id, target.context)), slotCapMs);
131
148
  return "removed";
132
149
  }
133
150
 
@@ -370,24 +387,19 @@ class IngestSlot {
370
387
  }
371
388
  }
372
389
 
373
- /** A time limit in the words of a message: "500 ms", "45 s", "2 min". */
374
- function formatDuration(ms: number): string {
375
- if (ms < 1_000) return `${Math.max(1, Math.round(ms))} ms`;
376
- if (ms < 120_000) return `${Number((ms / 1_000).toFixed(1))} s`;
377
- return `${Number((ms / 60_000).toFixed(1))} min`;
378
- }
379
-
380
390
  /**
381
391
  * The signal one ingest runs under: aborted when the caller's is, or when `timeoutMs` has passed
382
- * (with an error that says the file took too long). `dispose` stops the timer.
392
+ * (with an error that says the file took too long). `dispose` stops the timer. `timedOut` says
393
+ * whether an error is the one of the time limit.
383
394
  */
384
- function ingestSignal(name: string, caller: AbortSignal | undefined, timeoutMs: number): { signal: AbortSignal; dispose(): void } {
395
+ function ingestSignal(name: string, caller: AbortSignal | undefined, timeoutMs: number): { signal: AbortSignal; dispose(): void; timedOut(error: unknown): boolean } {
385
396
  const controller = new AbortController();
386
397
  const onAbort = () => controller.abort(caller!.reason);
387
398
  if (caller?.aborted) controller.abort(caller.reason);
388
399
  else caller?.addEventListener("abort", onAbort, { once: true });
400
+ let late: Error | undefined;
389
401
  const timer = timeoutMs > 0 && timeoutMs <= LONGEST_TIMER_MS
390
- ? setTimeout(() => controller.abort(new Error(`${name} took too long to read (more than ${formatDuration(timeoutMs)}). Split it, or save it as plain text, and attach that.`)), timeoutMs)
402
+ ? setTimeout(() => controller.abort(late = new Error(`${name} took too long to read (more than ${formatDuration(timeoutMs)}). Split it, or save it as plain text, and attach that.`)), timeoutMs)
391
403
  : undefined;
392
404
  return {
393
405
  signal: controller.signal,
@@ -395,6 +407,7 @@ function ingestSignal(name: string, caller: AbortSignal | undefined, timeoutMs:
395
407
  clearTimeout(timer);
396
408
  caller?.removeEventListener("abort", onAbort);
397
409
  },
410
+ timedOut: (error) => late !== undefined && error === late,
398
411
  };
399
412
  }
400
413
 
@@ -569,7 +582,7 @@ async function ingestFile(
569
582
  const mayCopy = !byRef && (keeps(format, false) || (format === "text" && keeps("log", false)));
570
583
  // A file at a path that its extractor reads itself (an isolated reader) is not read here at all.
571
584
  const extractorReads = path !== undefined && extractorFor(format, extractors).readsPath === true && !mayCopy;
572
- const { signal, dispose } = ingestSignal(name, input.signal, ingestTimeoutMs);
585
+ const { signal, dispose, timedOut } = ingestSignal(name, input.signal, ingestTimeoutMs);
573
586
  // Each long step works in slices, with pauses for other requests: one large upload no longer
574
587
  // holds the whole server while it is read.
575
588
  const slicer = new TimeSlicer(signal);
@@ -580,18 +593,27 @@ async function ingestFile(
580
593
  /** The bytes to keep in the store (`putOriginal`), until they are written. */
581
594
  let copy: Uint8Array | undefined;
582
595
  /**
583
- * A read of the file at `path`, or the signal's reason once it aborts: a volume that does not
584
- * answer fails the ingest in its time, as an extractor does. The ingest keeps its turn until the
585
- * read ends (`IngestSlot`).
596
+ * A read of the file at `path`, or the signal's reason once it aborts. A volume that does not
597
+ * answer within the ingest's time is the server's trouble, not the file's: a system error
598
+ * ETIMEDOUT (503, to try again), not the `IngestError` "timeout" of a file that takes too long to
599
+ * read (422, "split it"), since 0.10.0. The ingest keeps its turn until the read ends (`IngestSlot`).
586
600
  */
587
601
  const readWithin = async <T>(read: Promise<T>): Promise<T> => {
588
602
  work.abandoned = read;
589
- const got = await untilAborted(read, signal);
603
+ let got: T;
604
+ try {
605
+ got = await untilAborted(read, signal);
606
+ } catch (error) {
607
+ if (timedOut(error)) throw Object.assign(new Error(`The file to ingest did not answer within ${formatDuration(ingestTimeoutMs)}.`), { code: "ETIMEDOUT", syscall: "read" });
608
+ throw error;
609
+ }
590
610
  work.abandoned = undefined;
591
611
  return got;
592
612
  };
593
613
  let pages: number | undefined;
594
614
  let reading: ReturnType<typeof readingOf>;
615
+ /** A PDF's headings from the size of its type, as its extractor found them (0.10.0). */
616
+ let headings: PdfHeading[] | undefined;
595
617
  let text: string;
596
618
  let wrapped: number;
597
619
  let lines: string[];
@@ -653,10 +675,10 @@ async function ingestFile(
653
675
  // A character is at least one byte: a text this long is too large whatever it holds.
654
676
  if (extracted.text.length > maxTextBytes) throw tooMuchText(name, maxTextBytes);
655
677
  await slicer.pauseNow();
656
- return { pages: extracted.pages, reading: readingOf(extracted), file: fileOf(extracted), ...await prepareText(extracted.text, slicer, progress && ((done, total) => progress.report("prepare", done, total, "characters"))) };
678
+ return { pages: extracted.pages, reading: readingOf(extracted), file: fileOf(extracted), headings: format === "pdf" ? headingsOf(extracted) : undefined, ...await prepareText(extracted.text, slicer, progress && ((done, total) => progress.report("prepare", done, total, "characters"))) };
657
679
  };
658
680
  const done = await read();
659
- ({ pages, reading, text, wrapped, lines } = done);
681
+ ({ pages, reading, text, wrapped, lines, headings } = done);
660
682
  progress?.end();
661
683
  // Bytes given are hashed once the extractor is done with them, a slice at a time: the extractor
662
684
  // is called at once at the ingest's turn, as before 0.9.0, so the ingests of one turn reach it
@@ -681,6 +703,8 @@ async function ingestFile(
681
703
  measured = await measureText(text, slicer);
682
704
  if (measured.utf8 > maxTextBytes) throw tooMuchText(name, maxTextBytes);
683
705
  outline = await buildOutlineInSlices(lines, format, slicer);
706
+ // A PDF whose text gives fewer than 3 headings gets those of its type sizes, when 3 or more of them are found (0.10.0).
707
+ if (format === "pdf") outline = await pdfOutline(outline, lines, headings, slicer);
684
708
  logProfile = format === "log" ? await buildLogProfileInSlices(lines, slicer) : undefined;
685
709
  quality = await measureQuality(lines, format, slicer);
686
710
  // The line index, so that the text can be read by range: a few bytes a line, built from the
@@ -754,8 +778,14 @@ async function ingestFile(
754
778
  const clear = target.id !== undefined
755
779
  && await settleWithin(claim(context, { ...target, id: target.id }, name), slotCapMs, () => storeTimeout(name, slotCapMs, "read"));
756
780
  // The files first, the document last: a document is there once its metadata is. Every call
757
- // carries the owner and scope of the document.
781
+ // carries the owner and scope of the document. Since 0.10.0 the writes are refused at once while
782
+ // too many calls of the process are stuck (`writing` stays false: nothing is written, and nothing
783
+ // is to remove), and count as one stuck call once they have run `slotCapMs`, the time they are
784
+ // given: they are made one after the other, and hold one thread of the pool at a time.
785
+ let writing = false;
758
786
  const storing = (async () => {
787
+ checkStuck("write", context.maxStuck);
788
+ writing = true;
759
789
  if (clear) {
760
790
  await store.delete(meta.id, target.context);
761
791
  forget(meta.id);
@@ -772,6 +802,7 @@ async function ingestFile(
772
802
  await store.put({ meta, text }, target.context);
773
803
  wrote(true);
774
804
  })();
805
+ watchStuck(storing, slotCapMs);
775
806
  try {
776
807
  // A store that does not answer fails the ingest after `slotCapMs`: it held the ingest's turn,
777
808
  // and the upload handler's room for the file, for as long as it did not answer.
@@ -786,7 +817,7 @@ async function ingestFile(
786
817
  const remove = async () => {
787
818
  taken = await removeOwn(context, target, meta) === "taken";
788
819
  };
789
- const removal = storing.then(remove, remove).catch(() => undefined).then(() => {
820
+ const removal = (writing ? storing.then(remove, remove) : Promise.resolve()).catch(() => undefined).then(() => {
790
821
  ended = true;
791
822
  });
792
823
  await settleWithin(removal, slotCapMs, () => new Error("")).catch(() => undefined);