@crewhaus/tool-wiki 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ CrewHaus Factory
2
+ Copyright 2026 Max Meier
3
+
4
+ This product includes software developed by Max Meier
5
+ (operating as StudioMax — https://studiomax.io) and contributors.
6
+
7
+ The CrewHaus name and logo are trademarks of Max Meier. See
8
+ https://github.com/crewhaus/factory/blob/main/TRADEMARK.md for the
9
+ use-of-marks policy.
package/README.md CHANGED
@@ -34,16 +34,24 @@ for (const tool of bundle.all) defaultCatalog.register(tool);
34
34
  | `wiki_stats` | readOnly | Corpus health |
35
35
  | `log_knowledge_gap` | destructive (sideEffect audit-and-allow, **no** justification) | Record what the expert could not answer |
36
36
 
37
- All tools are `scope: "internal"` — local files, no network.
37
+ The tools read and write local files and are `scope: "internal"`, with one
38
+ exception: when `memory.wiki.embedder` names a provider outside the process
39
+ (anything but `mock/…`), `wiki_recall`, `wiki_semantic_search` and
40
+ `wiki_related` send the query and article text to it, so they are built
41
+ `scope: "external"` with `ioCapability: "network"` (still `readOnly`). The
42
+ egress classifier then sees the query, and `compile --strict` counts them.
38
43
 
39
44
  ## Pillar 3 — the `memory` TrustOrigin
40
45
 
41
- Article bodies returned by the read tools are classified via
42
- `boundary-classifier` at the new **`"memory"` origin** (default policy:
43
- block tier, like `"skill"`) before reaching the model — an article written
44
- in an earlier session may have absorbed attacker text, and recall re-injects
45
- it across a session boundary. Malicious verdicts return the redaction notice
46
- instead of the body; non-blocked bodies are `tagContent`-ed into
46
+ Everything the read tools render from an article (body, title, tags and
47
+ the sources line) is classified via `boundary-classifier` at the
48
+ **`"memory"` origin** (default policy: block tier, like `"skill"`) before
49
+ reaching the model: an article written in an earlier session may have
50
+ absorbed attacker text, and recall re-injects it across a session boundary.
51
+ The unit is one article (`wiki_get`, `wiki_recall`) or one row (`wiki_search`,
52
+ `wiki_semantic_search`, `wiki_list`, `wiki_related`). A malicious verdict
53
+ replaces that article or row with its slug, version and the redaction notice,
54
+ and the rest render normally; non-blocked units are `tagContent`-ed into
47
55
  `RunContext.dataLineage` under `"memory"` (the skills-registry two-site
48
56
  pattern) so the egress fabric can attribute a later exfiltration to the
49
57
  memory boundary.
package/dist/index.js CHANGED
@@ -28,19 +28,42 @@
28
28
  * justification; logging a gap is the
29
29
  * honest low-friction path, §7.4)
30
30
  *
31
- * All tools are `scope: "internal"` — local files, no network.
31
+ * The tools read and write local files and are `scope: "internal"`, with
32
+ * one exception. When the store has an embedder that leaves the process
33
+ * (`memory.wiki.embedder` naming anything but `mock/…`), `wiki_recall`,
34
+ * `wiki_semantic_search` and `wiki_related` POST the query and article text
35
+ * to that provider, so they are built `scope: "external"` with
36
+ * `ioCapability: "network"`: the query goes through the egress classifier
37
+ * and `compile --strict` counts them. They stay `readOnly`, and the
38
+ * destination is the operator's configured embedder, never one the model
39
+ * picks. `wiki_search`, `wiki_get`, `wiki_list` and the writers never call
40
+ * the embedder.
32
41
  *
33
42
  * ## Pillar 3 — the `memory` TrustOrigin (two-site pattern)
34
43
  *
35
- * Article bodies returned by the read tools (`wiki_recall`, `wiki_get`)
36
- * are classified at the new `"memory"` TrustOrigin BEFORE they reach the
37
- * model: a wiki article written in an earlier session may have absorbed
38
- * attacker text (a poisoned page STUDYed into an article), and recall
39
- * re-injects it across a session boundary. On a malicious verdict the body
40
- * is replaced by the redaction notice; on a non-blocked verdict the body
41
- * is `tagContent`-ed into `RunContext.dataLineage` under origin `"memory"`
42
- * (the skills-registry two-site pattern) so the sink-side egress fabric
43
- * can attribute a later exfiltration to the memory boundary.
44
+ * Everything the read tools render from an article is classified at the
45
+ * `"memory"` TrustOrigin BEFORE it reaches the model: a wiki article written
46
+ * in an earlier session may have absorbed attacker text (a poisoned page
47
+ * STUDYed into an article), and recall re-injects it across a session
48
+ * boundary. That is the body, and also the title, the tags and the sources
49
+ * line, which are free text too (0.7.1; only the slug is validated).
50
+ *
51
+ * The unit is one article: `wiki_get` and `wiki_recall` classify each
52
+ * article's header and body together, and the list-style tools (`wiki_search`,
53
+ * `wiki_semantic_search`, `wiki_list`, `wiki_related`) classify each row. On
54
+ * a malicious verdict that article or row is replaced by its slug, version
55
+ * and the redaction notice (the slug stays, so a REFLECT pass can still find
56
+ * and fix it), and the others render normally. Per-article classification
57
+ * also keeps each unit inside the classifier's window, which a whole large
58
+ * result would not be. List rows are checked by the local layers one by one,
59
+ * but go to a model-backed classifier (Layer 3) in chunks of rows, so a long
60
+ * list costs a few model calls rather than one per row; see `classifyRows`.
61
+ * Neighbouring rows are also classified side by side, so an injection split
62
+ * across two rows redacts both of them.
63
+ * A non-blocked unit is `tagContent`-ed into
64
+ * `RunContext.dataLineage` under origin `"memory"` (the skills-registry
65
+ * two-site pattern) so the sink-side egress fabric can attribute a later
66
+ * exfiltration to the memory boundary.
44
67
  *
45
68
  * ## Write-path governance (design §3.3)
46
69
  *
@@ -65,7 +88,7 @@
65
88
  import { buildRedactionNotice, classifyBoundary } from "@crewhaus/boundary-classifier";
66
89
  import { formatAgentIdentity, tagContent } from "@crewhaus/run-context";
67
90
  import { buildTool } from "@crewhaus/tool-builder";
68
- import { WikiVersionConflictError, createWikiStore, } from "@crewhaus/wiki-store";
91
+ import { WikiVersionConflictError, createWikiStore, isWikiSlug, } from "@crewhaus/wiki-store";
69
92
  import { z } from "zod";
70
93
  /**
71
94
  * The thredz-mcp wiki tool names, pinned (server.ts TOOLS, v0.2.0). The
@@ -219,25 +242,218 @@ function resolveRunContext(ctx) {
219
242
  return bridge?.runContext;
220
243
  }
221
244
  /**
222
- * Pillar 3 source side, applied to ONE article body: classify at origin
223
- * `"memory"`, return the redaction notice on a malicious verdict, tag the
224
- * lineage otherwise. The caller splices the result into its rendering.
245
+ * Pillar 3 source side, applied to ONE rendered unit (an article with its
246
+ * header, or one list row): classify at origin `"memory"`, return the
247
+ * redaction notice on a malicious verdict, tag the lineage otherwise.
225
248
  */
226
- async function classifyBody(body, rc) {
227
- const boundary = await classifyBoundary(body, { origin: "memory" });
249
+ async function classifyMemory(text, rc) {
250
+ const boundary = await classifyBoundary(text, { origin: "memory" });
228
251
  if (boundary.action === "redact") {
229
- return boundary.redacted ?? buildRedactionNotice(boundary.verdict.hits);
252
+ return {
253
+ safe: false,
254
+ notice: boundary.redacted ?? buildRedactionNotice(boundary.verdict.hits),
255
+ };
230
256
  }
231
257
  if (rc !== undefined) {
232
- tagContent(rc, body, "memory");
258
+ tagContent(rc, text, "memory");
233
259
  }
234
- return body;
260
+ return { safe: true, text };
235
261
  }
236
262
  function refLine(ref) {
237
263
  const tagSuffix = ref.tags.length > 0 ? ` [${ref.tags.join(", ")}]` : "";
238
264
  const verifiedSuffix = ref.verified ? " ✓verified" : "";
239
265
  return `${ref.slug} (v${ref.version}, ${ref.status}, conf ${ref.confidence.toFixed(2)}${verifiedSuffix}) — ${ref.title}${tagSuffix}`;
240
266
  }
267
+ /**
268
+ * What a redacted article or row is still named by: its slug and version,
269
+ * the only parts printed OUTSIDE the classified unit. The store validates
270
+ * both, but a store from another backend (or a planted index.json read by an
271
+ * older store) may not, so each is printed only when it is a slug and a
272
+ * positive whole number, and a fixed placeholder otherwise.
273
+ */
274
+ function redactedName(ref) {
275
+ return {
276
+ slug: isWikiSlug(ref.slug) ? ref.slug : "(an article with an invalid slug)",
277
+ version: typeof ref.version === "number" && Number.isSafeInteger(ref.version) && ref.version > 0
278
+ ? `v${ref.version}`
279
+ : "v?",
280
+ };
281
+ }
282
+ /** `slug (vN)`, from {@link redactedName}. */
283
+ function named(ref) {
284
+ const name = redactedName(ref);
285
+ return `${name.slug} (${name.version})`;
286
+ }
287
+ /**
288
+ * The most row text one classification sees at once, besides the row a chunk
289
+ * carries over from the one before it: well inside the classifier's window
290
+ * (64 KiB), so every row in a chunk is analysed in full.
291
+ */
292
+ const ROW_CHUNK_CHARS = 16 * 1024;
293
+ /** How many chunk or row classifications run at once. */
294
+ const ROW_CLASSIFY_CONCURRENCY = 8;
295
+ /**
296
+ * A Layer-3 stand-in that answers nothing: passed per call, it makes a
297
+ * classification run only the local layers (regex and structural).
298
+ */
299
+ const LOCAL_LAYERS_ONLY = async () => undefined;
300
+ /** Run `fn` over `items` in order, at most {@link ROW_CLASSIFY_CONCURRENCY} at a time. */
301
+ async function mapPooled(items, fn) {
302
+ const out = new Array(items.length);
303
+ let next = 0;
304
+ const worker = async () => {
305
+ for (let i = next++; i < items.length; i = next++)
306
+ out[i] = await fn(items[i], i);
307
+ };
308
+ await Promise.all(Array.from({ length: Math.min(ROW_CLASSIFY_CONCURRENCY, items.length) }, () => worker()));
309
+ return out;
310
+ }
311
+ /**
312
+ * Classify many list rows at origin `"memory"`, each row its own unit for
313
+ * the verdict, without paying one model call per row.
314
+ *
315
+ * 1. The local layers (regex, structural, decoded variants) run on each row
316
+ * alone, so a row's encoded payload is decoded however many rows sit
317
+ * beside it. A malicious verdict there is final: the model layer can
318
+ * raise a verdict but never lower one. This pass bypasses the verdict
319
+ * cache, so a local-only verdict never stands in for a full one.
320
+ * 2. The rows that pass are joined into chunks of at most
321
+ * {@link ROW_CHUNK_CHARS} and each chunk is classified in full, Layer 3
322
+ * included when the runtime registered one: one model call per chunk,
323
+ * not per row (a 400-row wiki_search used to make 400). Each chunk after
324
+ * the first starts with the row the one before it ended on, so every two
325
+ * neighbouring rows are classified side by side in some chunk.
326
+ * 3. Only inside a chunk that comes back malicious is each row classified
327
+ * in full on its own, so only the row that carries the injection is
328
+ * redacted, never its neighbours.
329
+ * 4. The rows of that chunk that pass on their own still go through
330
+ * {@link redactSplitInjections}: an injection split across two rows
331
+ * ("… ignore previous" / "instructions and …") passes row by row.
332
+ *
333
+ * A row that passes is tagged into the run's data lineage at `"memory"`.
334
+ */
335
+ async function classifyRows(rows, rc) {
336
+ const out = new Array(rows.length);
337
+ const local = await mapPooled(rows, (row) => classifyBoundary(row, {
338
+ origin: "memory",
339
+ llmClassifier: LOCAL_LAYERS_ONLY,
340
+ bypassCache: true,
341
+ }));
342
+ const chunks = [];
343
+ let chunk = [];
344
+ let chunkChars = 0;
345
+ let fresh = 0;
346
+ local.forEach((result, i) => {
347
+ if (result.action === "redact") {
348
+ out[i] = {
349
+ safe: false,
350
+ notice: result.redacted ?? buildRedactionNotice(result.verdict.hits),
351
+ };
352
+ return;
353
+ }
354
+ const length = rows[i].length + 1;
355
+ if (fresh > 0 && chunkChars + length > ROW_CHUNK_CHARS) {
356
+ chunks.push(chunk);
357
+ const last = chunk[chunk.length - 1];
358
+ chunk = [last];
359
+ chunkChars = rows[last].length + 1;
360
+ fresh = 0;
361
+ }
362
+ chunk.push(i);
363
+ chunkChars += length;
364
+ fresh++;
365
+ });
366
+ if (fresh > 0)
367
+ chunks.push(chunk);
368
+ const wholes = await mapPooled(chunks, (members) => classifyBoundary(members.map((i) => rows[i]).join("\n"), { origin: "memory" }));
369
+ // In chunk order, not completion order, so the output does not depend on
370
+ // which chunk's model call returned first.
371
+ const flaggedChunks = chunks.filter((_, c) => wholes[c]?.action === "redact");
372
+ const flagged = [...new Set(flaggedChunks.flat())];
373
+ const each = await mapPooled(flagged, (i) => classifyMemory(rows[i], undefined));
374
+ flagged.forEach((i, j) => {
375
+ const result = each[j];
376
+ if (!result.safe)
377
+ out[i] = result;
378
+ });
379
+ // One flagged chunk at a time: the check pools its own pair calls, so this
380
+ // keeps at most ROW_CLASSIFY_CONCURRENCY classifications in flight. A row
381
+ // carried between two flagged chunks takes the first chunk's notice.
382
+ for (const members of flaggedChunks) {
383
+ const passed = members.filter((i) => out[i] === undefined);
384
+ for (const [k, notice] of await redactSplitInjections(passed.map((i) => rows[i]))) {
385
+ const i = passed[k];
386
+ out[i] ??= { safe: false, notice };
387
+ }
388
+ }
389
+ return rows.map((row, i) => {
390
+ const result = out[i] ?? { safe: true, text: row };
391
+ if (result.safe && rc !== undefined)
392
+ tagContent(rc, result.text, "memory");
393
+ return result;
394
+ });
395
+ }
396
+ /**
397
+ * An injection split across rows. Each of `rows` passed classification on its
398
+ * own, but an attacker who can plant two articles that list side by side can
399
+ * split one phrase between them, so each half classifies clean and the model
400
+ * still reads one sentence. The rows are classified joined with "\n" at
401
+ * TrustOrigin `"memory"`: the raw rows, not the rendered ` • ` lines, whose
402
+ * bullets break the detector's phrase rules without breaking the sentence for
403
+ * the model. When the join is malicious, each pair of neighbours is classified
404
+ * and both rows of a malicious pair are redacted. When no pair reproduces the
405
+ * verdict, or the rows left still join malicious (a split over three rows, or
406
+ * weak hits that only add up across the chunk), every row is redacted: fail
407
+ * closed. runtime-core's `renderRecalledMemory` runs the same check on
408
+ * auto-recalled lines. Returns the notice for each row to redact, by index
409
+ * into `rows`.
410
+ */
411
+ async function redactSplitInjections(rows) {
412
+ const redacted = new Map();
413
+ if (rows.length < 2)
414
+ return redacted;
415
+ const joined = await classifyBoundary(rows.join("\n"), { origin: "memory" });
416
+ if (joined.action !== "redact")
417
+ return redacted;
418
+ const pairs = await mapPooled(rows.slice(1), (_row, i) => classifyBoundary(`${rows[i]}\n${rows[i + 1]}`, { origin: "memory" }));
419
+ pairs.forEach((pair, i) => {
420
+ if (pair.action !== "redact")
421
+ return;
422
+ const notice = pair.redacted ?? buildRedactionNotice(pair.verdict.hits);
423
+ redacted.set(i, notice);
424
+ redacted.set(i + 1, notice);
425
+ });
426
+ const left = rows.filter((_, i) => !redacted.has(i));
427
+ const residual = redacted.size === 0
428
+ ? joined
429
+ : left.length > 1
430
+ ? await classifyBoundary(left.join("\n"), { origin: "memory" })
431
+ : undefined;
432
+ if (residual?.action === "redact") {
433
+ const notice = residual.redacted ?? buildRedactionNotice(residual.verdict.hits);
434
+ for (let i = 0; i < rows.length; i++) {
435
+ if (!redacted.has(i))
436
+ redacted.set(i, notice);
437
+ }
438
+ }
439
+ return redacted;
440
+ }
441
+ /**
442
+ * Many list rows, classified by {@link classifyRows}: each row is `prefix`
443
+ * (text from the article that the row shows before the ref line, e.g.
444
+ * wiki_list's `updatedAt`) and the ref line, as one unit. A row that carries
445
+ * an injection anywhere in it becomes its slug and version with the notice.
446
+ * `updatedAt` used to be printed outside the unit, so a planted article's
447
+ * timestamp reached the model unredacted while wiki_get redacted the same
448
+ * article.
449
+ */
450
+ async function safeRefLines(refs, rc, render = (_ref, line) => line, prefix = () => "") {
451
+ const verdicts = await classifyRows(refs.map((ref) => `${prefix(ref)}${refLine(ref)}`), rc);
452
+ return refs.map((ref, i) => {
453
+ const c = verdicts[i];
454
+ return render(ref, c.safe ? c.text : `${named(ref)} — ${c.notice}`);
455
+ });
456
+ }
241
457
  function hitHeader(hit) {
242
458
  const viaSuffix = hit.via === "link" ? ", via link" : "";
243
459
  return `--- ${refLine(hit.ref)} (score ${hit.score.toFixed(4)}${viaSuffix})`;
@@ -256,6 +472,13 @@ export function createWikiTools(opts) {
256
472
  now,
257
473
  });
258
474
  const requireSources = opts.requireSources === true;
475
+ // The ranking tools send the query and article text to the embedder. A
476
+ // store that does not say where its embedder is, but can rank
477
+ // semantically, is taken to reach one.
478
+ const networked = store.embedderLeavesProcess ?? typeof store.semanticSearch === "function";
479
+ const embedderScope = networked
480
+ ? { scope: "external", ioCapability: "network" }
481
+ : { scope: "internal" };
259
482
  function createdByFrom(ctx) {
260
483
  const rc = resolveRunContext(ctx);
261
484
  if (rc === undefined)
@@ -277,7 +500,7 @@ export function createWikiTools(opts) {
277
500
  description: "PRIMARY RECALL. Fetch the most relevant slice of the expert's own wiki for a query — a combined keyword + semantic-vector context bundle. Call this FIRST on every user question before answering.",
278
501
  inputSchema: recallSchema,
279
502
  readOnly: true,
280
- scope: "internal",
503
+ ...embedderScope,
281
504
  execute: async (input, ctx) => {
282
505
  const rc = resolveRunContext(ctx);
283
506
  const hits = await store.recall(input.query, input.limit ?? 6);
@@ -286,7 +509,9 @@ export function createWikiTools(opts) {
286
509
  }
287
510
  const lines = [`${hits.length} wiki hit(s) for "${input.query}":`];
288
511
  for (const hit of hits) {
289
- lines.push("", hitHeader(hit), await classifyBody(hit.body, rc));
512
+ // Header and body are one unit: the title and tags are free text too.
513
+ const c = await classifyMemory(`${hitHeader(hit)}\n${hit.body}`, rc);
514
+ lines.push("", ...(c.safe ? [c.text] : [`--- ${named(hit.ref)} — [article redacted]`, c.notice]));
290
515
  }
291
516
  return lines.join("\n");
292
517
  },
@@ -296,8 +521,9 @@ export function createWikiTools(opts) {
296
521
  description: "Vector/semantic search over the wiki. Use when a query is conceptual and keyword search would miss paraphrases.",
297
522
  inputSchema: semanticSearchSchema,
298
523
  readOnly: true,
299
- scope: "internal",
300
- execute: async (input) => {
524
+ ...embedderScope,
525
+ execute: async (input, ctx) => {
526
+ const rc = resolveRunContext(ctx);
301
527
  const k = input.limit ?? 6;
302
528
  if (store.semanticSearch === undefined) {
303
529
  // Thredz-parity degradation: keyword-only plans fall back too.
@@ -305,8 +531,8 @@ export function createWikiTools(opts) {
305
531
  const lines = [
306
532
  "no embedder configured — degraded to keyword search (configure memory.wiki.embedder for semantic ranking):",
307
533
  ];
308
- for (const ref of refs.slice(0, k))
309
- lines.push(` • ${refLine(ref)}`);
534
+ for (const row of await safeRefLines(refs.slice(0, k), rc))
535
+ lines.push(` • ${row}`);
310
536
  if (refs.length === 0)
311
537
  lines.push(` (no keyword matches for "${input.query}")`);
312
538
  return lines.join("\n");
@@ -316,9 +542,10 @@ export function createWikiTools(opts) {
316
542
  return `no semantic matches ≥ ${(input.minScore ?? 0.05).toFixed(2)} for "${input.query}"`;
317
543
  }
318
544
  const lines = [`${hits.length} semantic match(es) for "${input.query}":`];
319
- for (const hit of hits) {
320
- lines.push(` • (${hit.score.toFixed(3)}) ${refLine(hit.ref)}`);
321
- }
545
+ const rows = await safeRefLines(hits.map((hit) => hit.ref), rc);
546
+ hits.forEach((hit, i) => {
547
+ lines.push(` • (${hit.score.toFixed(3)}) ${rows[i]}`);
548
+ });
322
549
  return lines.join("\n");
323
550
  },
324
551
  });
@@ -328,13 +555,14 @@ export function createWikiTools(opts) {
328
555
  inputSchema: searchSchema,
329
556
  readOnly: true,
330
557
  scope: "internal",
331
- execute: async (input) => {
558
+ execute: async (input, ctx) => {
559
+ const rc = resolveRunContext(ctx);
332
560
  const refs = await store.search(input.query);
333
561
  if (refs.length === 0)
334
562
  return `no wiki articles matched "${input.query}"`;
335
563
  const lines = [`${refs.length} keyword match(es) for "${input.query}":`];
336
- for (const ref of refs)
337
- lines.push(` • ${refLine(ref)}`);
564
+ for (const row of await safeRefLines(refs, rc))
565
+ lines.push(` • ${row}`);
338
566
  return lines.join("\n");
339
567
  },
340
568
  });
@@ -350,10 +578,6 @@ export function createWikiTools(opts) {
350
578
  const article = await store.get(input.slug);
351
579
  if (article === null)
352
580
  return `no wiki article with slug "${input.slug}"`;
353
- const safeBody = await classifyBody(article.body, rc);
354
- const body = input.concise === true && safeBody.length > CONCISE_CHARS
355
- ? `${safeBody.slice(0, CONCISE_CHARS).trimEnd()}\n… (concise — call wiki_get without concise for the full body)`
356
- : safeBody;
357
581
  const header = [
358
582
  `# ${article.title}`,
359
583
  `slug: ${article.slug} · v${article.version} · ${article.status} · confidence ${article.confidence.toFixed(2)}${article.verified ? " · ✓verified" : ""}`,
@@ -361,6 +585,16 @@ export function createWikiTools(opts) {
361
585
  article.sources.length > 0 ? `sources: ${article.sources.join(" · ")}` : "",
362
586
  `updated: ${article.updatedAt}${article.supersedes !== undefined ? ` (supersedes v${article.supersedes})` : ""}`,
363
587
  ].filter((l) => l !== "");
588
+ // The title, tags and sources are free text too: the whole article is
589
+ // one unit, classified before anything is rendered.
590
+ const c = await classifyMemory(`${header.join("\n")}\n\n${article.body}`, rc);
591
+ if (!c.safe) {
592
+ const name = redactedName(article);
593
+ return `slug: ${name.slug} · ${name.version} — [article redacted]\n\n${c.notice}`;
594
+ }
595
+ const body = input.concise === true && article.body.length > CONCISE_CHARS
596
+ ? `${article.body.slice(0, CONCISE_CHARS).trimEnd()}\n… (concise — call wiki_get without concise for the full body)`
597
+ : article.body;
364
598
  return `${header.join("\n")}\n\n${body}`;
365
599
  },
366
600
  });
@@ -425,7 +659,8 @@ export function createWikiTools(opts) {
425
659
  inputSchema: listSchema,
426
660
  readOnly: true,
427
661
  scope: "internal",
428
- execute: async (input) => {
662
+ execute: async (input, ctx) => {
663
+ const rc = resolveRunContext(ctx);
429
664
  const limit = input.limit ?? 25;
430
665
  const sort = input.sort ?? "updated";
431
666
  const order = input.order ?? "asc"; // thredz default: stalest first
@@ -462,8 +697,11 @@ export function createWikiTools(opts) {
462
697
  `${rows.length}/${refs.length} article(s) (sort ${sort === "title" ? "title" : "updated"} ${order}):`,
463
698
  ...notes,
464
699
  ];
465
- for (const ref of rows)
466
- lines.push(` • ${ref.updatedAt} ${refLine(ref)}`);
700
+ // The timestamp is article text too (a planted file sets it), so it is
701
+ // classified with the row it heads.
702
+ for (const row of await safeRefLines(rows, rc, undefined, (ref) => `${ref.updatedAt} `)) {
703
+ lines.push(` • ${row}`);
704
+ }
467
705
  if (rows.length === 0)
468
706
  lines.push(" (none)");
469
707
  return lines.join("\n");
@@ -474,8 +712,9 @@ export function createWikiTools(opts) {
474
712
  description: "Find articles related to a slug by tags + semantic similarity. Use in reflection to detect duplicates or contradictions to reconcile.",
475
713
  inputSchema: relatedSchema,
476
714
  readOnly: true,
477
- scope: "internal",
478
- execute: async (input) => {
715
+ ...embedderScope,
716
+ execute: async (input, ctx) => {
717
+ const rc = resolveRunContext(ctx);
479
718
  let refs;
480
719
  try {
481
720
  refs = await store.related(input.slug);
@@ -486,8 +725,9 @@ export function createWikiTools(opts) {
486
725
  if (refs.length === 0)
487
726
  return `no articles related to "${input.slug}"`;
488
727
  const lines = [`${refs.length} article(s) related to "${input.slug}":`];
489
- for (const ref of refs)
490
- lines.push(` • (${ref.relatedScore.toFixed(2)}) ${refLine(ref)}`);
728
+ const rows = await safeRefLines(refs, rc, (ref, row) => `(${ref.relatedScore.toFixed(2)}) ${row}`);
729
+ for (const row of rows)
730
+ lines.push(` • ${row}`);
491
731
  return lines.join("\n");
492
732
  },
493
733
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/tool-wiki",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "description": "0.3.0 memory release §3.2 — the thredz-identical wiki tool vocabulary (wiki_recall … log_knowledge_gap) over @crewhaus/wiki-store, with memory-origin boundary classification on every recalled body.",
6
6
  "main": "dist/index.js",
@@ -8,19 +8,20 @@
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
12
13
  }
13
14
  },
14
15
  "scripts": {
15
16
  "test": "bun test src"
16
17
  },
17
18
  "dependencies": {
18
- "@crewhaus/boundary-classifier": "0.6.0",
19
- "@crewhaus/event-log": "0.6.0",
20
- "@crewhaus/run-context": "0.6.0",
21
- "@crewhaus/tool-builder": "0.6.0",
22
- "@crewhaus/tool-catalog": "0.6.0",
23
- "@crewhaus/wiki-store": "0.6.0",
19
+ "@crewhaus/boundary-classifier": "0.8.0",
20
+ "@crewhaus/event-log": "0.8.0",
21
+ "@crewhaus/run-context": "0.8.0",
22
+ "@crewhaus/tool-builder": "0.8.0",
23
+ "@crewhaus/tool-catalog": "0.8.0",
24
+ "@crewhaus/wiki-store": "0.8.0",
24
25
  "zod": "^3.23.8"
25
26
  },
26
27
  "devDependencies": {
@@ -44,5 +45,8 @@
44
45
  "publishConfig": {
45
46
  "access": "public"
46
47
  },
47
- "files": ["dist", "README.md", "LICENSE", "NOTICE"]
48
+ "files": ["dist", "README.md", "LICENSE", "NOTICE"],
49
+ "engines": {
50
+ "bun": ">=1.3.14"
51
+ }
48
52
  }