llm-large-files 0.8.1 → 0.9.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 (204) hide show
  1. package/CHANGELOG.md +631 -0
  2. package/README.md +9 -10
  3. package/dist/access.d.ts +86 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +131 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/ask.d.ts +45 -1
  8. package/dist/ask.d.ts.map +1 -1
  9. package/dist/ask.js +181 -20
  10. package/dist/ask.js.map +1 -1
  11. package/dist/cache.d.ts +12 -3
  12. package/dist/cache.d.ts.map +1 -1
  13. package/dist/cache.js +18 -5
  14. package/dist/cache.js.map +1 -1
  15. package/dist/encoding.js +5 -3
  16. package/dist/encoding.js.map +1 -1
  17. package/dist/error-info.d.ts +57 -0
  18. package/dist/error-info.d.ts.map +1 -0
  19. package/dist/error-info.js +217 -0
  20. package/dist/error-info.js.map +1 -0
  21. package/dist/errors.d.ts +72 -8
  22. package/dist/errors.d.ts.map +1 -1
  23. package/dist/errors.js +102 -15
  24. package/dist/errors.js.map +1 -1
  25. package/dist/events.d.ts +35 -9
  26. package/dist/events.d.ts.map +1 -1
  27. package/dist/events.js +42 -11
  28. package/dist/events.js.map +1 -1
  29. package/dist/evidence.d.ts +4 -2
  30. package/dist/evidence.d.ts.map +1 -1
  31. package/dist/evidence.js +14 -3
  32. package/dist/evidence.js.map +1 -1
  33. package/dist/extract.d.ts +32 -8
  34. package/dist/extract.d.ts.map +1 -1
  35. package/dist/extract.js +46 -35
  36. package/dist/extract.js.map +1 -1
  37. package/dist/gemini/adapter.d.ts +19 -3
  38. package/dist/gemini/adapter.d.ts.map +1 -1
  39. package/dist/gemini/adapter.js +84 -12
  40. package/dist/gemini/adapter.js.map +1 -1
  41. package/dist/gemini/chat.d.ts +18 -6
  42. package/dist/gemini/chat.d.ts.map +1 -1
  43. package/dist/gemini/chat.js +2 -0
  44. package/dist/gemini/chat.js.map +1 -1
  45. package/dist/gemini/index.d.ts +1 -1
  46. package/dist/gemini/index.d.ts.map +1 -1
  47. package/dist/index.d.ts +9 -6
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +4 -3
  50. package/dist/index.js.map +1 -1
  51. package/dist/ingest.d.ts +70 -0
  52. package/dist/ingest.d.ts.map +1 -0
  53. package/dist/ingest.js +868 -0
  54. package/dist/ingest.js.map +1 -0
  55. package/dist/library.d.ts +176 -34
  56. package/dist/library.d.ts.map +1 -1
  57. package/dist/library.js +332 -512
  58. package/dist/library.js.map +1 -1
  59. package/dist/limiter.d.ts +2 -2
  60. package/dist/limiter.js +2 -2
  61. package/dist/map-reduce.js +3 -3
  62. package/dist/map-reduce.js.map +1 -1
  63. package/dist/model.d.ts +43 -2
  64. package/dist/model.d.ts.map +1 -1
  65. package/dist/model.js +63 -3
  66. package/dist/model.js.map +1 -1
  67. package/dist/node/handlers.d.ts +234 -7
  68. package/dist/node/handlers.d.ts.map +1 -1
  69. package/dist/node/handlers.js +482 -121
  70. package/dist/node/handlers.js.map +1 -1
  71. package/dist/node/index.d.ts +3 -2
  72. package/dist/node/index.d.ts.map +1 -1
  73. package/dist/node/index.js +2 -1
  74. package/dist/node/index.js.map +1 -1
  75. package/dist/node/ingest-child.d.ts +7 -1
  76. package/dist/node/ingest-child.d.ts.map +1 -1
  77. package/dist/node/ingest-child.js +18 -3
  78. package/dist/node/ingest-child.js.map +1 -1
  79. package/dist/node/isolated.d.ts +3 -1
  80. package/dist/node/isolated.d.ts.map +1 -1
  81. package/dist/node/isolated.js +11 -4
  82. package/dist/node/isolated.js.map +1 -1
  83. package/dist/node/originals.d.ts +19 -0
  84. package/dist/node/originals.d.ts.map +1 -0
  85. package/dist/node/originals.js +82 -0
  86. package/dist/node/originals.js.map +1 -0
  87. package/dist/node/spool.d.ts +102 -0
  88. package/dist/node/spool.d.ts.map +1 -0
  89. package/dist/node/spool.js +365 -0
  90. package/dist/node/spool.js.map +1 -0
  91. package/dist/node/sse.d.ts +4 -3
  92. package/dist/node/sse.d.ts.map +1 -1
  93. package/dist/node/sse.js +7 -6
  94. package/dist/node/sse.js.map +1 -1
  95. package/dist/pdf-layout.d.ts.map +1 -1
  96. package/dist/pdf-layout.js +71 -12
  97. package/dist/pdf-layout.js.map +1 -1
  98. package/dist/ranged.d.ts +22 -11
  99. package/dist/ranged.d.ts.map +1 -1
  100. package/dist/ranged.js +50 -12
  101. package/dist/ranged.js.map +1 -1
  102. package/dist/retry.d.ts +3 -1
  103. package/dist/retry.d.ts.map +1 -1
  104. package/dist/retry.js +6 -2
  105. package/dist/retry.js.map +1 -1
  106. package/dist/scope.d.ts +25 -0
  107. package/dist/scope.d.ts.map +1 -0
  108. package/dist/scope.js +60 -0
  109. package/dist/scope.js.map +1 -0
  110. package/dist/source.d.ts +60 -0
  111. package/dist/source.d.ts.map +1 -0
  112. package/dist/source.js +170 -0
  113. package/dist/source.js.map +1 -0
  114. package/dist/stores.d.ts +120 -31
  115. package/dist/stores.d.ts.map +1 -1
  116. package/dist/stores.js +582 -139
  117. package/dist/stores.js.map +1 -1
  118. package/dist/testing/fake-fetch.d.ts +31 -0
  119. package/dist/testing/fake-fetch.d.ts.map +1 -0
  120. package/dist/testing/fake-fetch.js +66 -0
  121. package/dist/testing/fake-fetch.js.map +1 -0
  122. package/dist/testing/fake-model.d.ts +38 -0
  123. package/dist/testing/fake-model.d.ts.map +1 -0
  124. package/dist/testing/fake-model.js +62 -0
  125. package/dist/testing/fake-model.js.map +1 -0
  126. package/dist/testing/index.d.ts +7 -0
  127. package/dist/testing/index.d.ts.map +1 -0
  128. package/dist/testing/index.js +9 -0
  129. package/dist/testing/index.js.map +1 -0
  130. package/dist/testing/store-checks.d.ts +72 -0
  131. package/dist/testing/store-checks.d.ts.map +1 -0
  132. package/dist/testing/store-checks.js +619 -0
  133. package/dist/testing/store-checks.js.map +1 -0
  134. package/dist/tools.d.ts +25 -0
  135. package/dist/tools.d.ts.map +1 -1
  136. package/dist/tools.js +135 -13
  137. package/dist/tools.js.map +1 -1
  138. package/dist/types.d.ts +116 -13
  139. package/dist/types.d.ts.map +1 -1
  140. package/docs/README.md +17 -8
  141. package/docs/getting-started.md +18 -13
  142. package/docs/guides/anthropic.md +12 -8
  143. package/docs/guides/conversations.md +112 -47
  144. package/docs/guides/costs-and-limits.md +16 -7
  145. package/docs/guides/deployment.md +309 -87
  146. package/docs/guides/errors-and-reliability.md +161 -23
  147. package/docs/guides/own-adapter-and-loop.md +27 -10
  148. package/docs/guides/pdfs.md +123 -29
  149. package/docs/guides/storage.md +634 -143
  150. package/docs/guides/testing.md +78 -129
  151. package/docs/guides/vertex-and-gemini.md +10 -7
  152. package/docs/guides/web-app.md +322 -77
  153. package/docs/how-it-works.md +35 -8
  154. package/docs/measurements.md +63 -8
  155. package/docs/publishing.md +112 -36
  156. package/docs/reference/ask.md +154 -13
  157. package/docs/reference/errors-and-retries.md +166 -14
  158. package/docs/reference/events.md +53 -20
  159. package/docs/reference/gemini.md +70 -21
  160. package/docs/reference/library.md +709 -165
  161. package/docs/reference/node.md +633 -104
  162. package/docs/reference/testing.md +311 -0
  163. package/docs/reference/tools.md +15 -5
  164. package/docs/security.md +316 -70
  165. package/docs/troubleshooting.md +165 -58
  166. package/docs/upgrading-0.9.md +890 -0
  167. package/package.json +12 -3
  168. package/src/access.ts +144 -0
  169. package/src/ask.ts +230 -19
  170. package/src/cache.ts +20 -5
  171. package/src/encoding.ts +6 -4
  172. package/src/error-info.ts +255 -0
  173. package/src/errors.ts +129 -15
  174. package/src/events.ts +86 -21
  175. package/src/evidence.ts +14 -3
  176. package/src/extract.ts +50 -33
  177. package/src/gemini/adapter.ts +97 -11
  178. package/src/gemini/chat.ts +20 -6
  179. package/src/gemini/index.ts +1 -1
  180. package/src/index.ts +9 -6
  181. package/src/ingest.ts +927 -0
  182. package/src/library.ts +482 -523
  183. package/src/limiter.ts +2 -2
  184. package/src/map-reduce.ts +3 -3
  185. package/src/model.ts +69 -5
  186. package/src/node/handlers.ts +691 -117
  187. package/src/node/index.ts +13 -2
  188. package/src/node/ingest-child.ts +23 -5
  189. package/src/node/isolated.ts +11 -4
  190. package/src/node/originals.ts +78 -0
  191. package/src/node/spool.ts +365 -0
  192. package/src/node/sse.ts +7 -6
  193. package/src/pdf-layout.ts +69 -11
  194. package/src/ranged.ts +42 -14
  195. package/src/retry.ts +6 -2
  196. package/src/scope.ts +63 -0
  197. package/src/source.ts +178 -0
  198. package/src/stores.ts +635 -140
  199. package/src/testing/fake-fetch.ts +80 -0
  200. package/src/testing/fake-model.ts +78 -0
  201. package/src/testing/index.ts +11 -0
  202. package/src/testing/store-checks.ts +660 -0
  203. package/src/tools.ts +150 -14
  204. package/src/types.ts +106 -13
package/docs/security.md CHANGED
@@ -20,24 +20,41 @@ import { askGemini } from "llm-large-files/gemini";
20
20
  // your sign-in session, never from the request body. `bytes`, `documentIds` and `messages` (an
21
21
  // array of `ChatTurn` and `ToolTurn`, which both calls take) are yours.
22
22
  const owner = userId;
23
-
24
- const meta = await files.ingest({ name: "app.log", bytes, owner });
25
- const current = await files.getMeta(meta.id, { owner });
26
- const original = await files.getOriginal(meta.id, { owner });
27
- const result = await askWithFiles({ files, model, documentIds, messages, owner });
28
- // askGemini passes `owner` on to askWithFiles.
29
- const geminiResult = await askGemini({ files, endpoint, documentIds, messages, owner });
30
- const deleted = await files.delete(meta.id, { owner });
23
+ // New in 0.9.0, and optional: the chat the document belongs to. `chatId` comes from your session too.
24
+ const scope = [userId, chatId];
25
+
26
+ const meta = await files.ingest({ name: "app.log", bytes, owner, scope });
27
+ const current = await files.getMeta(meta.id, { owner, scope });
28
+ const original = await files.getOriginal(meta.id, { owner, scope });
29
+ const result = await askWithFiles({ files, model, documentIds, messages, owner, scope });
30
+ // askGemini passes `owner` and `scope` on to askWithFiles.
31
+ const geminiResult = await askGemini({ files, endpoint, documentIds, messages, owner, scope });
32
+ const deleted = await files.delete(meta.id, { owner, scope });
31
33
  ```
32
34
 
33
35
  A document that has an owner is used only by calls that pass that owner. A call with another
34
36
  owner, or with none, gets what it gets for a document that does not exist: `undefined`, `false`,
35
37
  `missing`, `DocumentsNotFoundError` or a 404 (the table is in
36
- [Storage](./guides/storage.md#how-ownership-works)).
38
+ [Storage](./guides/storage.md#how-ownership-works)). A document that has a scope (new in 0.9.0) is
39
+ used only by calls that pass the same scope, segment by segment, in the same way
40
+ ([Scopes](./guides/storage.md#scopes)). Every refusal reads as missing, never as forbidden.
37
41
 
38
42
  A caller cannot tell a document of another owner from one that never existed or has expired.
39
43
  `toDocumentSummary`, which the upload handlers answer with, leaves the owner out.
40
44
 
45
+ **One exception: `ingest` with an application's ID says that the ID is taken.** New in 0.9.0. An
46
+ ingest with `id` answers `IngestError` `"exists"` (409) when a document of that ID is there,
47
+ whoever's it is. Over a store that ignores the context, every store written for 0.8 among them, that
48
+ spans the whole store: a caller in one scope learns that an ID is used in another. It learns nothing
49
+ else: not the owner, the name or the text. Make such IDs hard to guess: random ones, such as
50
+ `` `up-${randomUUID().replaceAll("-", "")}` ``, or ones that start with the tenant's own prefix
51
+ ([Application IDs](./guides/storage.md#application-ids)).
52
+
53
+ **`ifExists: "replace"` never replaces another owner's document.** A document of another owner or
54
+ scope is refused with `"exists"`, even when its text seems to be gone, unless it has expired. A
55
+ store or a wrapper that loses the context of one read would make a document that is there look
56
+ gone: the library does not take that as leave to delete it.
57
+
41
58
  **An owner must be a string that is not empty.** `files.ingest` throws a `TypeError` for an owner
42
59
  of `""`, of spaces only, or `null`:
43
60
  `owner must be a string that is not empty or only spaces, such as the signed-in user's ID. To let anyone with the document's ID use it, leave owner out.`
@@ -47,14 +64,66 @@ others whose IDs it has. From JavaScript, an owner that is neither a string nor
47
64
  (the user object in place of its ID, `true`, `NaN`, an array) is refused with the same error. A
48
65
  number is accepted, as before. Only `ingest` and the upload handler refuse: calls that read with
49
66
  `owner: ""` work as before, so documents that 0.5.0 stored with that owner stay readable until
50
- they expire.
67
+ they expire. Under `requireOwner` (below), every call refuses such an owner, and those documents
68
+ can no longer be read.
51
69
 
52
70
  **A document ingested without an owner is open to every call that has its ID**, with an owner or
53
- without. Its ID then works like a password. An ID is `doc_` and 32 hex digits, a random UUID with
54
- 122 random bits: it cannot be guessed, but it can leak. The ID of a document the model reads
55
- through tools is in its card and in the system instruction your model provider receives (so a
56
- model can repeat it in an answer), and IDs end up in your URLs, in browser storage and in logs.
57
- Give every document an owner.
71
+ without. Its ID then works like a password. An ID the library makes is `doc_` and 32 hex digits, a
72
+ random UUID with 122 random bits: it cannot be guessed, but it can leak. An ID your application
73
+ gives (`IngestInput.id`, new in 0.9.0) may be guessed, if it counts up or follows a pattern, and
74
+ `ingest` tells whether it is taken (above): use random IDs, or IDs with a prefix per tenant. The ID
75
+ of a document the model reads through tools is in its card and in the system instruction your model
76
+ provider receives (so a model can repeat it in an answer), and IDs end up in your URLs, in browser
77
+ storage and in logs. So an ID must never hold personal data, and every document should have an
78
+ owner.
79
+
80
+ **Fail closed with `requireOwner` and `requireScope`.** New in 0.9.0, both off by default. With
81
+ `createLargeFiles({ ..., requireOwner: true })`, every call must name an owner: `ingest`,
82
+ `getMeta`, `getOriginal`, `delete`, `prepare`, `askWithFiles`, `askStream`, `askGemini`, and
83
+ `tools`, which then needs `access`. A call without one throws
84
+ `TypeError("owner is required: requireOwner is on")` before the store is asked anything, and a
85
+ document stored without an owner reads as missing. So a route that forgot the owner fails, instead
86
+ of reaching every document stored without one. `requireScope` does the same for scopes
87
+ (`"scope is required: requireScope is on"`); `files.sweep()` without a scope stays allowed. Turn on
88
+ both where every document belongs to a signed-in user and a chat. A document a store gives back
89
+ with the owner `null` (a MongoDB driver writes a field that was left out that way) counts as having
90
+ no owner: open without `requireOwner`, missing with it.
91
+
92
+ **A store that forgets the context fails closed, except under `unscoped: "root"`.** Every store
93
+ call carries the owner and the scope (new in 0.9.0). A wrapper or a subclass that passes on only the
94
+ ID (`super.getMeta(id)`) loses them. Over a store that keeps scopes apart, the documents then look
95
+ missing; over a `FileDocumentStore` with scoped folders, every call but `sweep` throws a
96
+ `DocumentScopeError`. With `scopes.unscoped: "root"`, which serves the documents of 0.8, nothing
97
+ throws, even with `requireScope` on: the text and the metadata go to the root folder, outside the
98
+ chat's folder, and removing the chat's folder does not erase them. Another chat still cannot read
99
+ them: the library checks `meta.scope`. And a `sweep` that drops the context sweeps every chat:
100
+ `files.sweep({ scope })` removes the expired documents of all of them.
101
+
102
+ What the library checks is the metadata, against the caller: the owner and `meta.scope`. It asks
103
+ `getMeta` before every use of a document. When `get` returns a document whose metadata is not the
104
+ one `getMeta` described (another ID, owner, scope or version: a wrapper that keeps what `get` gave
105
+ by ID, or a document replaced on another server in between), the document is missing for that call.
106
+ So is a text of another length or number of lines than its metadata says, read whole or by range
107
+ (new in 0.9.0): a store that writes the text and the metadata apart can give one document's
108
+ metadata with another's text, when two servers ingest one ID at the same moment. A text of the same
109
+ length and lines is not caught: give each upload an ID of its own.
110
+ Bytes read by range and the original file carry no metadata: after they are read, the library asks
111
+ `getMeta` again, and they are missing unless the store still describes the same document. That
112
+ catches a document replaced while it was read. It cannot catch a store that answers `readTextRange`,
113
+ `getIndex` or `getOriginal` with another document's bytes while its `getMeta` is right, such as a
114
+ wrapper that keeps those answers by ID alone: such a store serves one scope's text to another. Pass
115
+ the context on, and key any cache of those reads by it too, as
116
+ [Writing a store of your own](./guides/storage.md#writing-a-store-of-your-own) shows.
117
+
118
+ **One ID, one ingest at a time.** In one library, while an ingest stores an application's ID,
119
+ another ingest of that ID is refused with `"exists"`, so two ingests cannot both find the ID free
120
+ and mix one user's file with another's metadata. Across servers, the store is
121
+ asked just before the writes too, but two servers can still write one ID at the same moment: the
122
+ library takes them for one upload sent twice. Give each upload an ID of its own. When the writes,
123
+ or the clean-up after a failed write, do not end in time, the ID stays refused in that library until they have ended. The clean-up after
124
+ a failed write leaves a document whose metadata it finds under the ID. The parts of one that another
125
+ server is still writing at that moment may be removed: one more reason to give each upload an ID of
126
+ its own.
58
127
 
59
128
  What the owner should be (a user, or a user and a chat session), and how to keep a list of a
60
129
  session's documents, is in [Storage](./guides/storage.md). The owner stops one user from reaching
@@ -77,7 +146,10 @@ before its body is read: `{ "error": "Upload failed (empty owner)." }` when `get
77
146
  when it returns something that is neither a string nor a finite number, such as the user object in
78
147
  place of its ID. Nothing is logged. Where every upload needs a signed-in user, throw a 401 as
79
148
  below. Return `undefined` only where anonymous uploads are meant (`user?.id || undefined`). GET and
80
- DELETE pass such an owner on as before.
149
+ DELETE pass such an owner on as before, except under `requireOwner`, where the library refuses it
150
+ on every call: they answer `""` or spaces only with a 500 then, "The file could not be looked up
151
+ (empty owner)." and "The file could not be deleted (empty owner).", without calling it, and `null`
152
+ with a 401, as `undefined` (new in 0.9.0).
81
153
 
82
154
  ```ts
83
155
  import type { IncomingMessage } from "node:http";
@@ -96,6 +168,14 @@ export const documentHandlers = createDocumentHandlers(files, {
96
168
  });
97
169
  ```
98
170
 
171
+ New in 0.9.0: with `requireOwner: true` in `createLargeFiles`, the library refuses every call
172
+ without an owner, and the handlers follow. `createDocumentHandlers` throws a `TypeError` without
173
+ `getOwner`, and answers an owner of `undefined` with a `401` `{ "error": "Sign in first." }`, on
174
+ every route, before the body is read. A mistake that would store an upload open to anyone fails
175
+ at once instead. `requireScope` works the same way with `getScope`, which gives the scope of a
176
+ request (such as `[userId, chatId]`): a scope that is not valid, or none, is a `500` before the
177
+ body is read.
178
+
99
179
  The routes, statuses and the rest of the upload flow are in [Web app](./guides/web-app.md) and the
100
180
  [node reference](./reference/node.md).
101
181
 
@@ -109,19 +189,26 @@ may use. `view_pages` has its own list: the PDFs whose original is kept (in a st
109
189
  that only its text was kept, any other is an unknown `document_id`.
110
190
 
111
191
  `askWithFiles` and `askGemini` make one tool set per question, from the plan that `prepare` made
112
- with the question's owner. You have nothing to do.
113
-
114
- **`files.tools()` checks owners through the metadata it is given.** It has no owner option. On
115
- every call that reads a document's text or the pages of its PDF, it checks that the document is
116
- still in the store and has not expired, and compares the owner stored with the document with the
117
- `owner` of the `DocumentMeta` you passed for that ID. When they differ, the call answers
118
- `doc_… is no longer available on the server. Ask the user to attach the file again.`, as for a
119
- document that is gone, and `view_pages` shows no page. So metadata built from an ID a client sent,
120
- with no owner or another one, reads nothing of a document that has an owner. The check compares
121
- strings: metadata that carries the stored owner reads the document, wherever it came from, and a
122
- document stored without an owner is open to any metadata. (Before 0.6.0, `tools()` checked no
123
- owner.) In a tool loop of your own, give it only metadata that `prepare(ids, base, { owner })` or
124
- `getMeta(id, { owner })` returned, never objects built from IDs a client sent:
192
+ with the question's owner and scope, and pass them to the tools as `access`. You have nothing to do.
193
+
194
+ **`files.tools(documents, { access })` checks the owner and scope the store holds.** New in 0.9.0.
195
+ On every call that reads a document's text or the pages of its PDF, it asks the store for the
196
+ document with the owner and scope of `access`, checks that it is still there and has not expired,
197
+ and compares its stored owner and scope with `access`, as `getMeta(id, access)` does. When they
198
+ differ, the call answers `doc_… is no longer available on the server. Ask the user to attach the
199
+ file again.`, as for a document that is gone, and `view_pages` shows no page. The metadata handed
200
+ in decides nothing: metadata read straight from the store for another user (such as
201
+ `await store.getMeta(id)`) reads nothing with `access: { owner: "bob" }`. Under `requireOwner` or
202
+ `requireScope`, `tools` throws without `access`.
203
+
204
+ **Without `access`, `files.tools()` checks owners through the metadata it is given**, as before
205
+ 0.9.0. It compares the owner and scope stored with the document with those of the `DocumentMeta`
206
+ you passed for that ID. So metadata built from an ID a client sent, with no owner or another one,
207
+ reads nothing of a document that has an owner. But the check compares strings: metadata that
208
+ carries the stored owner reads the document, wherever it came from, and a document stored without
209
+ an owner is open to any metadata. (Before 0.6.0, `tools()` checked no owner.) In a tool loop of
210
+ your own, pass `access`, and give `tools` only metadata that `prepare(ids, base, { owner, scope })`
211
+ or `getMeta(id, { owner, scope })` returned, never objects built from IDs a client sent:
125
212
 
126
213
  ```ts
127
214
  import { DocumentsNotFoundError } from "llm-large-files";
@@ -129,9 +216,9 @@ import { DocumentsNotFoundError } from "llm-large-files";
129
216
  // Your own loop: `base` is the size of the rest of your request. The tools below have no
130
217
  // `generate`: `wholeDocumentAnalysis: false` keeps the instruction from naming
131
218
  // `analyze_whole_document`, and the cards from offering a whole-file read.
132
- const prepared = await files.prepare(documentIds, base, { owner, wholeDocumentAnalysis: false });
219
+ const prepared = await files.prepare(documentIds, base, { owner, scope, wholeDocumentAnalysis: false });
133
220
  if (prepared.missing.length > 0) throw new DocumentsNotFoundError(prepared.missing);
134
- const tools = files.tools(prepared.plan.searchable);
221
+ const tools = files.tools(prepared.plan.searchable, { access: { owner, scope } });
135
222
  ```
136
223
 
137
224
  Writing your own loop is covered in [Your own adapter and loop](./guides/own-adapter-and-loop.md),
@@ -401,6 +488,12 @@ U+FFFD. `meta.replacedCharacters` counts the U+FFFD, and the model is told of th
401
488
  (one hour by default). `FileDocumentStore`'s `sweep` throws nothing: a directory it cannot read
402
489
  gives 0, and a document it cannot remove is left for the next sweep. Watch the count it returns,
403
490
  and the volume, to know that expired files really go.
491
+ - **A delete is done once the metadata is gone.** Since 0.9.0, `FileDocumentStore.delete` returns
492
+ true once it removed the metadata, and a text, index or original it could not remove then waits
493
+ for the sweep (before, it threw). Where the files must go for good (a user's erasure request),
494
+ remove the folder of the scope, or keep the sweep running. The sweep removes such leftovers in the
495
+ store's own folders (`scopes`), and in a folder it shares with your files only under the IDs the
496
+ library makes.
404
497
  - **A delete takes effect on every server that shares the store, from the next tool call on.**
405
498
  Every use of a document first asks the store whether it is still there. A copy kept in memory
406
499
  (within `cacheBytes`, 256 MB by default) is used only after that, and dropped when the document
@@ -410,25 +503,49 @@ U+FFFD. `meta.replacedCharacters` counts the U+FFFD, and the model is told of th
410
503
  later questions. Neither reaches the answers, steps and evidence you stored, your logs, or what
411
504
  your model provider keeps. To forget a document, also drop the tool rounds that name it from the
412
505
  stored conversation ([Conversations](./guides/conversations.md)).
413
- - **`FileDocumentStore` writes plain files** in one directory: `<id>.txt`, the text; `<id>.json`,
414
- the metadata (owner, file name, MIME type, sizes, dates, outline headings and, for a log, its
415
- message templates); `<id>.idx`, a line index; `<id>.bin`, the original with `keepOriginals`; and
416
- `.tmp` files while it writes. Nothing is encrypted, and a delete removes the files without
506
+ - **`FileDocumentStore` writes plain files** in one directory, or with `scopes` in a folder per
507
+ scope (`<directory>/<userId>/<chatId>/.llm-large-files/`): `<id>.txt`, the text; `<id>.json`,
508
+ the metadata (owner, scope, file name, MIME type, sizes, dates, outline headings and, for a log,
509
+ its message templates); `<id>.idx`, a line index; `<id>.bin`, the original with `keepOriginals`;
510
+ and `.tmp` files while it writes. Nothing is encrypted, and a delete removes the files without
417
511
  overwriting them. Use an encrypted volume, or write a `DocumentStore` over storage with encryption
418
512
  at rest ([Storage](./guides/storage.md)).
419
- - **No path traversal.** Only IDs that match `DOCUMENT_ID_PATTERN` (`doc_` and 32 lowercase hex
420
- digits) reach the file system. File names never become paths.
513
+ - **File modes.** Since 0.9.0, `FileDocumentStore` writes its files `0600` and makes its folders
514
+ `0700` (`fileMode`, `directoryMode`), minus the umask: only the user the server runs as can read
515
+ them. Before, they were `0666` and `0777` minus the umask, which gave files every user of the
516
+ machine could read (`0664` under the usual umask `0002`). On a volume shared by servers that run
517
+ as different users of one group (a Kubernetes `fsGroup`), set `fileMode: 0o640` and
518
+ `directoryMode: 0o750`, never more. Files written before 0.9.0 keep their modes: change them once
519
+ with `chmod` to narrow them.
520
+ - **No path traversal.** File names are document IDs, and only IDs of the shape
521
+ `APP_DOCUMENT_ID_PATTERN` (lowercase letters, digits, `_` and `-`, and no name Windows keeps for
522
+ a device) reach the file system. With `scopes`, each segment of a scope names a folder, and must
523
+ pass `isSafeScopeSegment` (the same shape, no `.` or `..`), or the call is refused. A call checks
524
+ that the folder of its scope is where its path says inside the directory, so a link there, to
525
+ another chat or out of the volume, is refused, and `sweep` follows no link. Writes and deletes
526
+ check every time; a read takes a check made less than a second ago, so a link put in place of a
527
+ chat's folder is seen by reads within a second. `createScopeFolders` makes a scope's folders one
528
+ level at a time, and nothing where a link points. A read does not follow a link at a file name
529
+ either (`O_NOFOLLOW`), except on Windows, which has no such flag: there, only links in the
530
+ folders are refused. A store with `scopes` never makes the folder of a chat again once you
531
+ removed it, unless `createScopeFolders` is on
532
+ ([Storage](./guides/storage.md#files-in-a-folder-per-user-or-per-chat)).
533
+ - **Your files beside the store's.** In the store's directory without `scopes`, or with
534
+ `scopes.folder` `""`, the names `<id>.json`, `.txt`, `.idx` and `.bin`, and their temporary
535
+ files, belong to the store for every ID it is given: an ingest writes over your file of such a
536
+ name. Keep your own files in a subfolder, or under other names.
421
537
  - `MemoryDocumentStore` keeps everything in the memory of one process, until it restarts. It is for
422
538
  tests and single-process demos. It copies the metadata it is given and the metadata it hands out,
423
539
  so deleting `owner` from what `ingest` or `getMeta` returned, before you send it to a browser,
424
- changes nothing stored. (In 0.5.0 it opened the document to every owner.) The original it returns
425
- is the stored bytes themselves: treat them as read-only.
540
+ changes nothing stored. (In 0.5.0 it opened the document to every owner.) Since 0.9.0, the
541
+ original it returns is a copy too.
426
542
 
427
543
  ## Original files
428
544
 
429
545
  With `keepOriginals` (for example `["pdf"]`), the uploaded file itself is stored next to its text
430
- (`<id>.bin` in `FileDocumentStore`). That is off by default, and a store without `putOriginal`
431
- keeps none. When you turn it on:
546
+ (`<id>.bin` in `FileDocumentStore`), or, since 0.9.0, read back from where your application keeps it
547
+ (`IngestInput.original`, [below](#originals-your-application-keeps)). That is off by default, and a
548
+ store without `putOriginal` keeps no copy. When you turn it on:
432
549
 
433
550
  - **More is kept than before.** The text of a PDF leaves out its pictures, signatures, stamps and
434
551
  hidden layers; the file has them all, and its metadata (author, software, dates). Treat the store
@@ -449,6 +566,40 @@ keeps none. When you turn it on:
449
566
  or copying is opened with an empty password, and the cut is not encrypted: the restriction does
450
567
  not travel with it. A PDF that needs a password to open is not shown.
451
568
 
569
+ - **Its bytes are checked when they are read back.** Since 0.9.0 every document has `sha256`, the
570
+ SHA-256 of the upload: a copy the store gives back is used only when it has that size and
571
+ SHA-256, so a store that answers with another document's file of the same size is caught. The
572
+ metadata is asked for again once the bytes are read, so a document deleted and stored again
573
+ under its ID by another server meanwhile is not mixed up with it.
574
+
575
+ ### Originals your application keeps
576
+
577
+ New in 0.9.0. With `IngestInput.original: { ref }` and `LargeFilesOptions.originals`, the store keeps
578
+ no copy: the library records the ref with the file's size and SHA-256, and reads the file back
579
+ through your reader.
580
+
581
+ - **The ref never reaches the model,** nor a summary. It is in the metadata
582
+ (`meta.original.ref`), which you should not send to a browser: send `toDocumentSummary(meta)`.
583
+ An event holds it only with `eventResults: true`, in the full plan of `run.finish` and
584
+ `run.error`: keep such events on your server
585
+ ([events](./reference/events.md#documents-eventresults-and-eventresult)).
586
+ - **Only the ref the store holds is read,** never one from metadata handed to `files.tools`: a
587
+ forged `original.ref` in a meta made by hand reads nothing it names.
588
+ - **A changed file is not shown.** One of another size or SHA-256 than at ingest gives `undefined`
589
+ from `getOriginal`, and `view_pages` tells the model it changed and to attach it again: its pages
590
+ would not be those of the text the model has.
591
+ - **`fileOriginals(root)` keeps refs inside `root`.** The ref is resolved under `root`, and must
592
+ stay inside it (`root` and a path separator before it): an absolute path elsewhere, or `..` that
593
+ climbs out, is a `TypeError`. A link at any level below `root` is not followed: the file's real
594
+ path must be the path the ref names, or it reads as missing. The file is opened without following
595
+ a link at its name, except on Windows. A folder swapped for a link between that check and the
596
+ read is not seen: keep the folders of the uploads writable only by your server. It reads at most
597
+ one byte past the size recorded, and only reads.
598
+ - **It is still a file of the user.** `delete` and `sweep` never touch it: remove the upload with
599
+ its chat, and with the user's other data when the account is closed.
600
+ - **The upload handler can write it.** `prepareUpload` gives the path and the ref, and the handler
601
+ saves the body there as it arrives, made new: [Files the upload handler writes](#files-the-upload-handler-writes).
602
+
452
603
  How `view_pages` works, and what to weigh before you keep originals, is in [PDFs](./guides/pdfs.md).
453
604
 
454
605
  ## Reading PDFs for pages and pictures
@@ -583,11 +734,20 @@ server that memory is the server's heap. Choose the isolation with care:
583
734
  `express.raw()`) has already read the body, it buffered it under its own `limit`: set that one
584
735
  too. `files.ingest` checks the bytes you give it, which are already in memory.
585
736
  - **Memory.** The handlers buffer the whole upload, up to `maxUploadBytes`, before reading its
586
- text, and an ingest then holds several copies of the text at once (several times the size of the
587
- text), in your server's heap. Cap concurrent uploads in front (a reverse proxy, a rate limiter),
588
- or read PDF and Office files with `createIsolatedExtractors`, whose `concurrency` (2) and
589
- `maxQueue` (8) bound how many are read and wait, or ingest in a process of its own:
590
- [Deployment](./guides/deployment.md) has measurements and recipes.
737
+ text, unless it is written to a file (`uploadSpool`, or the `path` of `prepareUpload`, new in
738
+ 0.9.0: [below](#files-the-upload-handler-writes)), and an ingest then holds several copies of the
739
+ text at once (several times the size of the text), in your server's heap. Since 0.8.2 the
740
+ handlers hold uploads of at most `maxPendingUploadBytes` at once in memory (twice
741
+ `maxUploadBytes`), and answer the others 503 before reading their body; each library lets `maxConcurrentIngests` (2) ingests work at once, and queues the
742
+ others. A route of your own must bound the bodies it reads itself. The room of an upload is
743
+ taken from its `Content-Length` before its body arrives, so a few clients that send headers and
744
+ then trickle their bodies can hold all of it: `uploadIdleTimeoutMs` (30 s) frees the room of a
745
+ body that stops, and `server.requestTimeout` that of one that trickles. A reverse proxy that reads
746
+ whole bodies before passing them on, and a limit on the connections and uploads of each client,
747
+ keep that room for the others ([Deployment](./guides/deployment.md#uploads-and-ingest)). Read PDF and Office files with
748
+ `createIsolatedExtractors`, whose `concurrency` (2) and `maxQueue` (8) bound how many are read and
749
+ wait, or ingest in a process of its own: [Deployment](./guides/deployment.md) has measurements
750
+ and recipes.
591
751
  - **Text.** A small file can hold far more text than its size: a PDF of 3 KB whose page tree listed
592
752
  one dense page 150 times drew 107 MB of text in 0.5.0. `maxTextBytes` (100 MB by default, or
593
753
  `maxUploadBytes` when that is larger) bounds the text kept from one file, in UTF-8 bytes. The PDF
@@ -653,12 +813,15 @@ server that memory is the server's heap. Choose the isolation with care:
653
813
  on their documents (`viewPages: false`), or keep no PDF originals
654
814
  ([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)).
655
815
  - **Stalls.** Ingest pauses for other requests about every 12 ms of its work: between the pages of
656
- a PDF, as a DOCX or PPTX is unpacked, as a text file that is not UTF-8 is decoded, and in every
657
- step after extraction, a line of megabytes included. In a measurement on one machine, the longest
658
- stall of the event loop was 31 to 34 ms for a 99 MB log and 0.16 s for a 2,000-page PDF, against
659
- 1.9 to 2.0 s and 4.4 to 4.5 s with the ingest of 0.5.0
816
+ a PDF, as a DOCX or PPTX is unpacked, as a text file that is not UTF-8 is decoded, and in the
817
+ steps after extraction up to the store, a line of megabytes included. In a measurement of 0.6.0
818
+ on one machine, the longest stall of the event loop was 31 to 34 ms for a 99 MB log and 0.16 s for
819
+ a 2,000-page PDF, against 1.9 to 2.0 s and 4.4 to 4.5 s with the ingest of 0.5.0; 0.9.0 measured
820
+ 0.23 to 0.36 s for a 95 MB log on a slower, busy machine
660
821
  ([Measurements](./measurements.md#the-server)). Some steps still hold the server, and neither the
661
822
  time limit nor a signal cuts them short:
823
+ - A text file in UTF-8, as it is decoded when its reading starts, and, over a
824
+ `FileDocumentStore`, as its text is written: about 2 to 4 ms for each MB.
662
825
  - A PDF page, for as long as pdf.js takes to read it: up to about 1.5 s for a page that passes
663
826
  the check in a small file, longer in a large file, and without limit when the page check does
664
827
  not run (without `@cantoo/pdf-lib`, on a file it cannot read or that declares more than 150,000
@@ -743,7 +906,8 @@ server that memory is the server's heap. Choose the isolation with care:
743
906
  count or log them apart, by user. `"out_of_memory"` and `"timeout"` from the child processes of
744
907
  `createIsolatedExtractors` can be such files too.
745
908
  - **Scanned PDFs** (no text at all) are refused, unless PDF originals are kept (`keepOriginals`
746
- includes `"pdf"` and the store has `putOriginal`) and the file has at most `scannedPdfPages` pages
909
+ includes `"pdf"` and the store has `putOriginal`, or the ingest gives `original` and the library
910
+ has `originals`) and the file has at most `scannedPdfPages` pages
747
911
  (30 by default): their pages can then be shown.
748
912
  - **File names** are reduced at ingest to one printable line: the last path segment. Control
749
913
  characters, line and paragraph separators and text-direction controls become a space, and the
@@ -753,9 +917,29 @@ server that memory is the server's heap. Choose the isolation with care:
753
917
  characters, and is never cut in the middle of a character (`file` when nothing is left). An
754
918
  emoji of several characters, such as a flag, can lose its end at the cut. A name that an
755
919
  earlier version cut in the middle of a character is sent with U+FFFD in place of the half.
756
- - **The MIME type is the client's word.** The handlers take it from an `X-File-Type` header, or
757
- from `Content-Type` unless that is `application/octet-stream`. The format is picked from the file
758
- name first, then from it. It is stored, and returned in summaries: do not trust it.
920
+ - **The MIME type is the client's word.** The handlers take it from an `X-File-Type` header, which
921
+ they URI-decode, or from `Content-Type` unless that is `application/octet-stream`. Since 0.9.0
922
+ `Content-Type` is taken as it came, not URI-decoded: before, `%0D%0A` in it became a line break
923
+ (S11). The format is picked from the file name first, then from it. It is stored, and returned in
924
+ summaries: do not trust it. Since 0.9.0 `ingest` keeps only its type and subtype, in lower case,
925
+ without parameters, for every caller, your own routes included: a value that is not a
926
+ type/subtype of RFC 9110 token characters of at most 127 characters, or that holds anything but
927
+ printable ASCII anywhere (a line break, U+202E or another text-direction control, a tag
928
+ character), is stored as `application/octet-stream`. Before, it was only trimmed, so a line
929
+ break, a direction override or 8 KB of it reached the metadata, the summaries, `ingest.finish`
930
+ and your logs. It is never sent to the model.
931
+ - **A path is read as it is given.** `ingest({ source: { path } })` (new in 0.9.0) reads the file
932
+ at that path, which can be any file the process may read: never build it from what a client sends.
933
+ A link at the path is refused, not followed, and so is a folder, a pipe or a device. The file is
934
+ opened without following a link at its name (not on Windows), and read once, at most
935
+ `maxUploadBytes + 1` bytes, so a file that grew after it was looked at is refused. A relative
936
+ path is made absolute once, when `ingest` is called: a change of the working folder before the
937
+ ingest's turn, or the working folder of an isolated reader's child process, does not change which
938
+ file is read. It is not normalized as text: a `..` after a link, and a `/` at the end, are the
939
+ system's to resolve, as they are in an absolute path. So `fs.promises.realpath` (or
940
+ `fs.realpathSync.native`) of the path, relative or absolute, names the file the library reads.
941
+ `path.resolve`, `fs.realpath` and `fs.realpathSync` remove them as text first, and can name
942
+ another file: do not check a path with them. The library never writes, moves or deletes it.
759
943
  - **Text is cleaned.** Terminal escape sequences are removed, and so are control characters other
760
944
  than tab and line breaks, C1 (U+0080-U+009F) included, and the other characters listed in
761
945
  [Characters that show nothing](#characters-that-show-nothing). An `ESC ]` without its end on the
@@ -784,6 +968,51 @@ server that memory is the server's heap. Choose the isolation with care:
784
968
  Formats, optional dependencies and the messages of refused files are in [PDFs](./guides/pdfs.md);
785
969
  the upload routes in [Web app](./guides/web-app.md).
786
970
 
971
+ ### Files the upload handler writes
972
+
973
+ New in 0.9.0. `createDocumentHandlers` writes no file unless it is asked to: by the `path` that
974
+ `prepareUpload` returns, or by `uploadSpool`.
975
+
976
+ - **Where.** The body of an upload goes to the absolute path `prepareUpload` returns, in your app's
977
+ folder, or to `<directory>/llf-upload-<random UUID>` of the spool (`os.tmpdir()` by default).
978
+ Nothing else is written by the handler. Build the path from IDs your server chose and checked (the
979
+ user, the chat, the upload), never from the file's name or another value the client sent.
980
+ - **Modes.** A file is made with mode `0o600`, minus the umask of the process: only the server's
981
+ user can read it. `uploadFileMode` changes it for the files of `prepareUpload` (`0o640` lets a
982
+ group read them); the spool's files are always `0o600`. The handler makes no folder: the folders,
983
+ and their modes, are yours.
984
+ - **Never through a link, never over a file.** Each file is opened with `"wx"` (`O_CREAT` with
985
+ `O_EXCL`): when anything is already at its name, a file or a link (even one that points nowhere),
986
+ the open fails and nothing is written. That upload is answered `409`, and what was there stays as
987
+ it is: it is never written through, truncated or removed. So the handler never follows a link at
988
+ the file's name. A link in a folder on the way is followed, except where `fileOriginals` would
989
+ not read through it: when the path of `prepareUpload` ends with the ref of its `original`
990
+ (`<root>/<ref>`), a link in a folder below that root is refused before anything is written
991
+ (`500` "Upload failed (ELOOP)."). Keep the folders of the uploads, and the spool, writable only
992
+ by your server.
993
+ - **Removed when they are not needed.** A file of the spool is removed when its ingest ends,
994
+ whatever the end. A file of `prepareUpload` is removed when its upload fails (too large, stopped,
995
+ a client that left, a full disk, a disk that does not answer, a file refused, an ID taken,
996
+ `onUploaded` throwing), and is yours once its document is stored: the library never removes it,
997
+ even when the document is deleted or expires.
998
+ A removal that fails (a file another process holds open, on Windows) is tried three times, then
999
+ left. A server that stops in the middle of an upload leaves its file. Empty the spool when the
1000
+ server starts: give each server process a spool folder of its own (an `emptyDir` or
1001
+ `os.hostname()` gives one a pod: [Deployment](./guides/deployment.md#uploads-and-ingest) says how
1002
+ to give one to each process), and let each empty only its own. Never empty a folder that several
1003
+ servers share: it holds the uploads the others are still ingesting. The partial file of `prepareUpload` stays until a job of yours removes it
1004
+ ([Stopping a server](./guides/deployment.md#stopping-a-server)).
1005
+ - **Bounded.** A body written to a file has the same limits as one in memory: `maxUploadBytes` and
1006
+ `uploadIdleTimeoutMs`, which also bounds each call to the disk (a volume that stops answering is
1007
+ a `500` "Upload failed (ETIMEDOUT).", and gives the upload's room back). `maxSpooledUploadBytes`
1008
+ (ten times `maxUploadBytes`) bounds the spool's files at once. A full disk (`ENOSPC`, `EDQUOT`) is
1009
+ answered `503`, and the file removed. The files of `prepareUpload` are bounded by your disk only.
1010
+ - **Not sent.** The path of a file and its ref are never in an answer, an event, a summary, or a
1011
+ request to a model. A system error that names a path, such as Node's `ENOENT` for a folder that is
1012
+ not there, is answered with its code only: `"Upload failed (ENOENT)."`.
1013
+ - **Not on a memory-backed volume.** A spool on a tmpfs (a Kubernetes `emptyDir` with
1014
+ `medium: Memory`) counts against the pod's memory: [Deployment](./guides/deployment.md#uploads-and-ingest).
1015
+
787
1016
  ## Regex search
788
1017
 
789
1018
  The model writes the regex, and the model reads untrusted file text, so a file can ask for a slow
@@ -892,19 +1121,31 @@ Waits, retries and deadlines are explained in
892
1121
  ## Storage errors
893
1122
 
894
1123
  A store's own errors can name paths and hosts. Reads reach users and models only as
895
- `DocumentStoreError`, whose message names neither, with the original error as its `cause`. Writes
896
- are passed on as they came, and the upload handler answers a write error that has neither a
897
- `status` (or `statusCode`) from 400 to 599 nor a Node system error's `code` and `syscall` with 500
898
- "Upload failed.", without the error's own message. A write error with such a status, such as an
899
- SDK's error with `statusCode`, is answered with that status and its own message, which can name a
900
- host. Two paths answer a store's write error 422 with its own message, so a database driver's host
901
- can reach the browser through them: an `ingest` of your own that throws a new error in place of the
902
- store's, as `ingestInProcess` in [Deployment](./guides/deployment.md#uploads-and-ingest) does, and a
903
- route of your own, as in [Web app](./guides/web-app.md#filesingest-and-its-errors). Give your
904
- store's write errors a status and a message of your own, and log the cause yourself, since the
905
- handlers log nothing: see [Storage](./guides/storage.md#errors) and the
906
- [node reference](./reference/node.md#uploadrequest-response). What to show a user is in
907
- [Errors and reliability](./guides/errors-and-reliability.md).
1124
+ `DocumentStoreError`, whose message names neither, with the original error as its `cause`. Since
1125
+ 0.9.0 so do writes: a store that fails in `ingest`, `files.delete` or `files.sweep` is a
1126
+ `DocumentStoreError` of operation `"write"`, `"delete"` or `"sweep"`, with status 503, and the
1127
+ upload handler answers it 503 with that message. (Before, writes were passed on as they came, and a
1128
+ write error with a `status` of its own was answered with its own message, which could name a host.)
1129
+ The handler also adds `Retry-After: 5`.
1130
+
1131
+ S12, closed in 0.9.0: the upload handler answered any `Error` without a status `422` with its
1132
+ message, as a file that could not be read. A `LargeFiles` of your own whose `ingest` threw a
1133
+ database driver's error (`MongoServerSelectionError: connect ECONNREFUSED mongo-prod.internal…:27017`)
1134
+ sent that host to the browser. Now only an `IngestError` is a `422` with its message, and an
1135
+ `IngestError` of code `"unreadable"` is told in fixed words: its message is a parser's, or that of
1136
+ an extractor of your own, which may name a host (an OCR service's). Any other error without a
1137
+ status is a `500` `{ "error": "Upload failed." }`, a system error adds only its code, and a model's
1138
+ error is told by its kind, never in the provider's words. The rule is
1139
+ `uploadAnswerOf` from `llm-large-files/node`: answer with it in a route of your own around
1140
+ `files.ingest` ([Web app](./guides/web-app.md#filesingest-and-its-errors)). An `ingest` of your own
1141
+ that refuses a file throws `new IngestError(code, message)`, as `ingestInProcess` in
1142
+ [Deployment](./guides/deployment.md#uploads-and-ingest) does. An error with a `status` of its own
1143
+ still keeps its message: give such errors words for users. Pass the `DocumentStoreError` on as it
1144
+ is, and log its `cause` yourself, since the handlers log nothing: see
1145
+ [Storage](./guides/storage.md#errors) and the
1146
+ [node reference](./reference/node.md#how-errors-are-answered). What to show a user, in your own
1147
+ words and language, is `errorInfoOf`'s job: see
1148
+ [Errors and reliability](./guides/errors-and-reliability.md#your-users-words-errorinfoof).
908
1149
 
909
1150
  ## What the library logs
910
1151
 
@@ -952,13 +1193,18 @@ What you log is your choice, and some of what the library hands you holds user d
952
1193
  browser ([Prompt injection](#prompt-injection)).
953
1194
  - Set `ttlMs` to your data policy, run `sweep` on a schedule, and make your deletion rules cover
954
1195
  stored `toolTurns` ([Storage and retention](#storage-and-retention)).
955
- - Keep the store on encrypted storage.
1196
+ - Keep the store on encrypted storage. With `FileDocumentStore`, keep the default file modes, or
1197
+ `0o640` and `0o750` for one group ([Storage and retention](#storage-and-retention)).
956
1198
  - Turn on `keepOriginals` only if you want `view_pages`, and knowing what it keeps
957
1199
  ([Original files](#original-files)).
958
1200
  - Set `limits` and a deadline, and log `usage` for failed questions too
959
1201
  ([What a question can cost](#what-a-question-can-cost)).
960
- - Cap concurrent uploads, and set the limit of any body parser in front of the handlers
961
- ([Uploads](#uploads)).
1202
+ - Keep the bound on uploads held at once (`maxPendingUploadBytes` of the handlers, or your own in a
1203
+ route of your own), limit uploads per user, and set the limit of any body parser in front of the
1204
+ handlers ([Uploads](#uploads)).
1205
+ - With `uploadSpool` or the `path` of `prepareUpload`, keep those folders writable only by your
1206
+ server, on a disk rather than a tmpfs, and build each path from IDs your server chose
1207
+ ([Files the upload handler writes](#files-the-upload-handler-writes)).
962
1208
  - Keep the limits on one file (`maxTextBytes`, `maxPdfPages`, `ingestTimeoutMs`) as low as your
963
1209
  users allow ([Uploads](#uploads)).
964
1210
  - For untrusted uploads, parse them in a process of their own with a memory limit, not in a worker