@crewhaus/wiki-store 0.7.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/dist/index.d.ts CHANGED
@@ -5,6 +5,25 @@ export { type AcquireLockOptions, type LockHandle, type LockPolicy, DEFAULT_LOCK
5
5
  export declare const DEFAULT_ROOT_DIR = ".crewhaus/wiki";
6
6
  /** The Thredz-parity conflict code carried by `WikiVersionConflictError`. */
7
7
  export declare const STALE_ARTICLE_VERSION = "stale_article_version";
8
+ /**
9
+ * Whether `value` is a wiki slug: kebab-case, 1–128 characters. Every slug
10
+ * the store returns is one; a caller that prints a slug outside a classified
11
+ * unit (tool-wiki's redaction notice) checks it again.
12
+ */
13
+ export declare function isWikiSlug(value: unknown): value is string;
14
+ /**
15
+ * What an article's `createdAt` / `updatedAt` reads as when the file holds
16
+ * no timestamp the store can read. It is printed as it stands
17
+ * (`updated: unknown`), sorts after every real timestamp in `list()`, and
18
+ * `Date.parse` gives `NaN` for it, so nothing downstream takes it for a date.
19
+ */
20
+ export declare const UNKNOWN_TIMESTAMP = "unknown";
21
+ /**
22
+ * The most of one article file the store reads or writes. Reads are capped
23
+ * before anything is buffered; a write past it is refused, so the store
24
+ * never writes an article it would then refuse to read.
25
+ */
26
+ export declare const ARTICLE_MAX_BYTES: number;
8
27
  export declare class WikiStoreError extends CrewhausError {
9
28
  readonly name: string;
10
29
  constructor(message: string, cause?: unknown);
@@ -47,7 +66,9 @@ export type WikiFrontmatter = {
47
66
  readonly supersedes?: number;
48
67
  readonly createdBy?: WikiCreatedBy;
49
68
  readonly status: WikiArticleStatus;
69
+ /** ISO-8601, or {@link UNKNOWN_TIMESTAMP} when the file holds none. */
50
70
  readonly createdAt: string;
71
+ /** ISO-8601, or {@link UNKNOWN_TIMESTAMP} when the file holds none. */
51
72
  readonly updatedAt: string;
52
73
  };
53
74
  export type WikiArticle = WikiFrontmatter & {
@@ -61,6 +82,7 @@ export type WikiRef = {
61
82
  readonly confidence: number;
62
83
  readonly verified: boolean;
63
84
  readonly version: number;
85
+ /** ISO-8601, or {@link UNKNOWN_TIMESTAMP} when the article holds none. */
64
86
  readonly updatedAt: string;
65
87
  /** Out-links extracted from `[[wikilinks]]` in the body (normalized slugs). */
66
88
  readonly links: readonly string[];
@@ -129,7 +151,18 @@ export type WikiRelatedRef = WikiRef & {
129
151
  */
130
152
  export interface WikiEmbedder {
131
153
  embed(texts: ReadonlyArray<string>): Promise<number[][]>;
154
+ /** `@crewhaus/embedder`'s provider id. Only `"mock"` is known to stay in
155
+ * the process; see {@link embedderLeavesProcess}. */
156
+ readonly provider?: string;
132
157
  }
158
+ /**
159
+ * Whether calling this embedder sends text out of the process — the rule
160
+ * `@crewhaus/memory-store` applies, kept identical here (memory-service's
161
+ * wiring test holds the two stores to the same answer). Only
162
+ * `@crewhaus/embedder`'s `mock` provider is known not to; an embedder that
163
+ * does not say what it is counts as one that does.
164
+ */
165
+ export declare function embedderLeavesProcess(embedder: WikiEmbedder | undefined): boolean;
133
166
  /** The design-§3.1 interface, verbatim (recall/search/get/write/list/related/setSignals/stats). */
134
167
  export interface WikiStore {
135
168
  /** Hybrid-ranked top-k WITH bodies (the recall-before-act bundle), after
@@ -163,6 +196,14 @@ export interface WikiStore {
163
196
  semanticSearch?(query: string, k?: number, minScore?: number): Promise<readonly WikiHit[]>;
164
197
  /** Diagnostic: where on disk this store lives. */
165
198
  path(): string;
199
+ /**
200
+ * True when `recall`, `related` and `semanticSearch` send the query and
201
+ * article text to an embedder outside the process (see
202
+ * {@link embedderLeavesProcess}). `@crewhaus/tool-wiki` reads it to flag
203
+ * those tools as network tools. Optional so another backend still
204
+ * type-checks; tool-wiki then decides from `semanticSearch`'s presence.
205
+ */
206
+ readonly embedderLeavesProcess?: boolean;
166
207
  }
167
208
  export type WikiStoreOptions = {
168
209
  readonly specName: string;
package/dist/index.js CHANGED
@@ -74,13 +74,15 @@
74
74
  * option), any resolved path outside the tenant's root throws before any
75
75
  * IO happens. The default root under a tenant is `<tenantRoot>/wiki`.
76
76
  */
77
- import { existsSync } from "node:fs";
78
- import { mkdir, readFile, readdir, rename, writeFile } from "node:fs/promises";
79
- import { dirname, join, resolve } from "node:path";
77
+ import { lstatSync, realpathSync, unlinkSync } from "node:fs";
78
+ import { mkdir, readdir } from "node:fs/promises";
79
+ import { isAbsolute, join, relative, resolve, sep } from "node:path";
80
80
  import { CrewhausError } from "@crewhaus/errors";
81
81
  import { assertSamePath, currentTenantContext } from "@crewhaus/tenancy";
82
+ import { openForRead, writeFileSafe } from "@crewhaus/tool-safety/fs";
82
83
  import YAML from "yaml";
83
84
  import { withLock } from "./lock";
85
+ import { wikilinkInners } from "./wikilinks";
84
86
  export { DEFAULT_LOCK_POLICY, WikiLockError, acquireLock, withLock, } from "./lock";
85
87
  export const DEFAULT_ROOT_DIR = ".crewhaus/wiki";
86
88
  /** The Thredz-parity conflict code carried by `WikiVersionConflictError`. */
@@ -91,7 +93,69 @@ const SPEC_NAME_REGEX = /^[a-zA-Z0-9_\-.]+$/;
91
93
  /** Slugs are kebab-case AND double as file names — fail-closed on anything
92
94
  * that could traverse (`..`, `/`, uppercase, spaces). */
93
95
  const SLUG_REGEX = /^[a-z0-9][a-z0-9-]{0,127}$/;
94
- const WIKILINK_REGEX = /\[\[([^\]]+)\]\]/g;
96
+ /**
97
+ * Whether `value` is a wiki slug: kebab-case, 1–128 characters. Every slug
98
+ * the store returns is one; a caller that prints a slug outside a classified
99
+ * unit (tool-wiki's redaction notice) checks it again.
100
+ */
101
+ export function isWikiSlug(value) {
102
+ return typeof value === "string" && SLUG_REGEX.test(value);
103
+ }
104
+ /**
105
+ * What an article's `createdAt` / `updatedAt` reads as when the file holds
106
+ * no timestamp the store can read. It is printed as it stands
107
+ * (`updated: unknown`), sorts after every real timestamp in `list()`, and
108
+ * `Date.parse` gives `NaN` for it, so nothing downstream takes it for a date.
109
+ */
110
+ export const UNKNOWN_TIMESTAMP = "unknown";
111
+ /** A calendar date: ECMAScript reads a date-only form as UTC midnight. */
112
+ const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
113
+ /**
114
+ * A date-time as `Date#toISOString` writes it, or as RFC 3339 and ISO 8601
115
+ * allow it by hand: a `T`, `t` or space between date (group 1) and time
116
+ * (group 2), a `.` or `,` before the fraction, and an offset that is `Z`,
117
+ * `±hh:mm`, `±hhmm` or `±hh` (groups 3–6), or none. Linear: no quantifier is
118
+ * nested or overlapping.
119
+ */
120
+ const DATE_TIME = /^(\d{4}-\d{2}-\d{2})[Tt ](\d{2}:\d{2}(?::\d{2}(?:[.,]\d{1,9})?)?)(?:([Zz])|([+-])(\d{2})(?::?(\d{2}))?)?$/;
121
+ /**
122
+ * `value` as an ISO-8601 timestamp, or {@link UNKNOWN_TIMESTAMP}. A timestamp
123
+ * is printed in list rows and sorted on, and a file on disk (which any agent
124
+ * with a write tool can edit) could put any text there, so only a timestamp
125
+ * is kept, written the one way `Date.parse` reads the same everywhere: `T`,
126
+ * `.`, and `Z` or `±hh:mm`. A date-time without an offset is read as UTC — a
127
+ * `Z` is appended — never as the host's local time, which is what
128
+ * `Date.parse` would do with it. The result is its own normal form.
129
+ */
130
+ function isoTimestampOr(value) {
131
+ if (typeof value !== "string")
132
+ return UNKNOWN_TIMESTAMP;
133
+ let iso;
134
+ if (ISO_DATE.test(value))
135
+ iso = value;
136
+ else {
137
+ const m = value.match(DATE_TIME);
138
+ if (m !== null) {
139
+ const [, date, time, z, sign, hh, mm] = m;
140
+ const zone = z !== undefined || sign === undefined ? "Z" : `${sign}${hh}:${mm ?? "00"}`;
141
+ iso = `${date}T${(time ?? "").replace(",", ".")}${zone}`;
142
+ }
143
+ }
144
+ return iso !== undefined && !Number.isNaN(Date.parse(iso)) ? iso : UNKNOWN_TIMESTAMP;
145
+ }
146
+ /** A timestamp's instant for sorting, or `undefined` when it is not known. */
147
+ function instantOf(timestamp) {
148
+ const ms = Date.parse(timestamp);
149
+ return Number.isNaN(ms) ? undefined : ms;
150
+ }
151
+ /**
152
+ * The most of one article file the store reads or writes. Reads are capped
153
+ * before anything is buffered; a write past it is refused, so the store
154
+ * never writes an article it would then refuse to read.
155
+ */
156
+ export const ARTICLE_MAX_BYTES = 16 * 1024 * 1024;
157
+ /** index.json is a derived cache: past this it is rebuilt from the articles instead of read. */
158
+ const INDEX_MAX_BYTES = 64 * 1024 * 1024;
95
159
  export class WikiStoreError extends CrewhausError {
96
160
  name = "WikiStoreError";
97
161
  constructor(message, cause) {
@@ -125,6 +189,16 @@ const ARTICLE_STATUSES = [
125
189
  "review",
126
190
  "archived",
127
191
  ];
192
+ /**
193
+ * Whether calling this embedder sends text out of the process — the rule
194
+ * `@crewhaus/memory-store` applies, kept identical here (memory-service's
195
+ * wiring test holds the two stores to the same answer). Only
196
+ * `@crewhaus/embedder`'s `mock` provider is known not to; an embedder that
197
+ * does not say what it is counts as one that does.
198
+ */
199
+ export function embedderLeavesProcess(embedder) {
200
+ return embedder !== undefined && embedder.provider !== "mock";
201
+ }
128
202
  // ---------------------------------------------------------------------------
129
203
  // frontmatter + wikilinks
130
204
  // ---------------------------------------------------------------------------
@@ -134,8 +208,7 @@ const ARTICLE_STATUSES = [
134
208
  export function extractWikilinks(body, self) {
135
209
  const out = [];
136
210
  const seen = new Set();
137
- for (const match of body.matchAll(WIKILINK_REGEX)) {
138
- const inner = match[1] ?? "";
211
+ for (const inner of wikilinkInners(body)) {
139
212
  const target = (inner.split("|")[0] ?? "").trim().toLowerCase().replace(/\s+/g, "-");
140
213
  if (!SLUG_REGEX.test(target))
141
214
  continue;
@@ -175,7 +248,7 @@ export function serializeArticle(article) {
175
248
  updatedAt: frontmatter.updatedAt,
176
249
  };
177
250
  const yamlText = YAML.stringify(fm).trimEnd();
178
- return `---\n${yamlText}\n---\n\n${body.replace(/\s+$/, "")}\n`;
251
+ return `---\n${yamlText}\n---\n\n${body.trimEnd()}\n`;
179
252
  }
180
253
  /** Parse an article file. Throws `WikiStoreError` on a malformed file. */
181
254
  export function parseArticle(raw) {
@@ -188,7 +261,7 @@ export function parseArticle(raw) {
188
261
  }
189
262
  const yamlText = raw.slice(4, end + 1);
190
263
  const rest = raw.slice(end + 4);
191
- const body = rest.replace(/^\r?\n+/, "").replace(/\s+$/, "");
264
+ const body = rest.replace(/^\r?\n+/, "").trimEnd();
192
265
  let fm;
193
266
  try {
194
267
  fm = YAML.parse(yamlText);
@@ -235,8 +308,8 @@ export function parseArticle(raw) {
235
308
  ...(typeof v["supersedes"] === "number" ? { supersedes: v["supersedes"] } : {}),
236
309
  ...(createdBy !== undefined ? { createdBy } : {}),
237
310
  status,
238
- createdAt: typeof v["createdAt"] === "string" ? v["createdAt"] : "1970-01-01T00:00:00.000Z",
239
- updatedAt: typeof v["updatedAt"] === "string" ? v["updatedAt"] : "1970-01-01T00:00:00.000Z",
311
+ createdAt: isoTimestampOr(v["createdAt"]),
312
+ updatedAt: isoTimestampOr(v["updatedAt"]),
240
313
  body,
241
314
  };
242
315
  }
@@ -359,31 +432,97 @@ export function createWikiStore(opts) {
359
432
  function articlePath(slug) {
360
433
  return fence(join(articlesDir, `${slug}.md`));
361
434
  }
435
+ /** A store path as the store names it in messages: `articles/notes.md`. */
436
+ function storeRel(absPath) {
437
+ return relative(storeDir, absPath).split("\\").join("/");
438
+ }
439
+ /**
440
+ * Write `content` at `absPath` without writing THROUGH anything planted in
441
+ * the store (security-2#0). The bytes go to an `O_EXCL|O_NOFOLLOW` temp
442
+ * under a random name in the file's physical directory, which must be
443
+ * inside the store, and the temp is renamed into place. A fixed temp name
444
+ * (`<slug>.md.tmp`, `index.json.tmp`) used to be opened with link
445
+ * following, so a symlink planted there — dangling or not — created or
446
+ * truncated a file anywhere the process could write. A symlinked article,
447
+ * a symlinked `versions/<slug>` directory leading out, or a FIFO at the
448
+ * leaf is refused, naming the store path.
449
+ */
362
450
  async function atomicWrite(absPath, content) {
363
451
  fence(absPath);
364
- await mkdir(dirname(absPath), { recursive: true });
365
- const tmpPath = `${absPath}.tmp`;
366
- await writeFile(tmpPath, content, { mode: 0o600 });
367
- await rename(tmpPath, absPath);
452
+ await mkdir(storeDir, { recursive: true });
453
+ const rel = storeRel(absPath);
454
+ const written = writeFileSafe(storeDir, rel, content, {
455
+ overwrite: true,
456
+ createParents: true,
457
+ mode: 0o600,
458
+ });
459
+ if (!written.ok) {
460
+ throw new WikiStoreError(written.code === "escapes-root"
461
+ ? outsideStoreMessage("write", rel)
462
+ : `refusing to write ${rel} in the wiki store: ${written.reason}`);
463
+ }
368
464
  }
369
- async function readArticle(slug) {
370
- const p = articlePath(slug);
371
- if (!existsSync(p))
372
- return null;
373
- let raw;
465
+ /**
466
+ * The refusal for a store path that resolves outside the store: a
467
+ * symlinked `articles/` or `versions/` directory (or file) leading out.
468
+ * tool-safety's own reason says "the workspace", which is the store here.
469
+ */
470
+ function outsideStoreMessage(op, rel) {
471
+ return `refusing to ${op} ${rel}: it resolves outside the wiki store, through a symlinked file or a symlinked articles/ or versions/ directory. Keep articles/ and versions/ as directories inside the store; to keep the wiki somewhere else, link the store's own directory instead.`;
472
+ }
473
+ /**
474
+ * Whether an existing store subdirectory resolves outside the store. The
475
+ * store root itself may be a link (it is resolved first); a subdirectory
476
+ * leading out is refused for every operation, so list() does not name
477
+ * articles that get() and search() would refuse to read.
478
+ */
479
+ function subdirLeavesStore(dir) {
480
+ let realStore;
481
+ let realDir;
374
482
  try {
375
- raw = await readFile(p, "utf8");
483
+ realStore = realpathSync(storeDir);
484
+ realDir = realpathSync(dir);
376
485
  }
377
486
  catch {
378
- return null;
487
+ return false; // not there (yet): nothing leads anywhere
379
488
  }
380
- return parseArticle(raw);
489
+ const rel = relative(realStore, realDir);
490
+ return rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel);
491
+ }
492
+ /**
493
+ * At most `maxBytes` of a regular file inside the store, never through a
494
+ * symlink (a planted `articles/<slug>.md -> ~/.ssh/...` is not read), or
495
+ * `null` when nothing is there.
496
+ */
497
+ async function readStoreFile(absPath, maxBytes) {
498
+ fence(absPath);
499
+ const rel = storeRel(absPath);
500
+ const read = await openForRead(storeDir, rel, { maxBytes, followLeafSymlink: false });
501
+ if (!read.ok) {
502
+ if (read.code === "not-found")
503
+ return null;
504
+ throw new WikiStoreError(read.code === "escapes-root"
505
+ ? outsideStoreMessage("read", rel)
506
+ : `refusing to read ${rel} in the wiki store: ${read.reason}`);
507
+ }
508
+ if (read.truncated) {
509
+ throw new WikiStoreError(`${rel} in the wiki store is larger than ${maxBytes} bytes, so it was not read`);
510
+ }
511
+ return read.text;
512
+ }
513
+ async function readArticle(slug) {
514
+ const raw = await readStoreFile(articlePath(slug), ARTICLE_MAX_BYTES);
515
+ return raw === null ? null : parseArticle(raw);
381
516
  }
382
517
  async function listArticleSlugs() {
383
518
  fence(articlesDir);
384
519
  let entries;
385
520
  try {
386
- entries = await readdir(articlesDir);
521
+ // Regular files only: a symlink or FIFO named like an article is not
522
+ // one of the store's articles, and reading it is refused anyway.
523
+ entries = (await readdir(articlesDir, { withFileTypes: true }))
524
+ .filter((e) => e.isFile())
525
+ .map((e) => e.name);
387
526
  }
388
527
  catch {
389
528
  return [];
@@ -421,29 +560,85 @@ export function createWikiStore(opts) {
421
560
  }
422
561
  return { version: 1, articles };
423
562
  }
424
- function isIndexFile(value) {
563
+ /**
564
+ * Whether `value` is an index this store could have written for the
565
+ * articles in `slugs`. index.json is a file any agent with a write tool
566
+ * can edit, and its entries become the rows every list tool prints, so
567
+ * each one is held to what {@link toIndexEntry} produces from a parsed
568
+ * article: the key is a slug with an article file behind it, the version a
569
+ * positive whole number, the status one of the four, the title, tags and
570
+ * links strings (each link a slug), the confidence a number in [0, 1],
571
+ * `verified` a boolean and `updatedAt` a normalised timestamp. Anything
572
+ * else — a planted key
573
+ * carrying text, a string version, an entry for an article that is not
574
+ * there — sends the load to a rebuild from the articles, which
575
+ * parseArticle validates.
576
+ *
577
+ * An article with no entry is not a reason to rebuild: the rebuild leaves
578
+ * out a malformed article, and whoever can leave one out of index.json can
579
+ * as well delete the article itself.
580
+ */
581
+ function isIndexFile(value, slugs) {
425
582
  if (typeof value !== "object" || value === null)
426
583
  return false;
427
584
  const v = value;
428
- return v["version"] === 1 && typeof v["articles"] === "object" && v["articles"] !== null;
585
+ if (v["version"] !== 1 || typeof v["articles"] !== "object" || v["articles"] === null) {
586
+ return false;
587
+ }
588
+ const entries = Object.entries(v["articles"]);
589
+ const onDisk = new Set(slugs);
590
+ const strings = (x) => Array.isArray(x) && x.every((s) => typeof s === "string");
591
+ return entries.every(([slug, raw]) => {
592
+ if (!onDisk.has(slug) || typeof raw !== "object" || raw === null)
593
+ return false;
594
+ const e = raw;
595
+ const { version, confidence, updatedAt } = e;
596
+ return (typeof e["title"] === "string" &&
597
+ strings(e["tags"]) &&
598
+ strings(e["links"]) &&
599
+ e["links"].every(isWikiSlug) &&
600
+ typeof confidence === "number" &&
601
+ confidence >= 0 &&
602
+ confidence <= 1 &&
603
+ typeof e["verified"] === "boolean" &&
604
+ typeof version === "number" &&
605
+ Number.isSafeInteger(version) &&
606
+ version >= 1 &&
607
+ ARTICLE_STATUSES.includes(e["status"]) &&
608
+ isoTimestampOr(updatedAt) === updatedAt);
609
+ });
429
610
  }
430
611
  /** Load index.json; on a missing/corrupt file, rebuild from the articles
431
612
  * (the crash-safe guarantee: the index is always derivable). */
432
613
  async function loadIndex() {
433
614
  fence(indexPath);
434
- if (existsSync(indexPath)) {
435
- try {
436
- const parsed = JSON.parse(await readFile(indexPath, "utf8"));
437
- if (isIndexFile(parsed))
615
+ if (subdirLeavesStore(articlesDir)) {
616
+ throw new WikiStoreError(outsideStoreMessage("read", "articles/"));
617
+ }
618
+ try {
619
+ const raw = await readStoreFile(indexPath, INDEX_MAX_BYTES);
620
+ if (raw !== null) {
621
+ const parsed = JSON.parse(raw);
622
+ if (isIndexFile(parsed, await listArticleSlugs()))
438
623
  return parsed;
439
624
  }
440
- catch {
441
- // fall through to rebuild
442
- }
625
+ }
626
+ catch {
627
+ // missing, corrupt, oversized or not a regular file: fall through to rebuild
443
628
  }
444
629
  return rebuildIndex();
445
630
  }
446
631
  async function persistIndex(index) {
632
+ // index.json is a derived cache, so a symlink planted in its place is
633
+ // removed (unlink never follows a link) rather than left to fail every
634
+ // write; the rebuilt index then lands as a regular file.
635
+ try {
636
+ if (lstatSync(fence(indexPath)).isSymbolicLink())
637
+ unlinkSync(indexPath);
638
+ }
639
+ catch {
640
+ // nothing there yet
641
+ }
447
642
  await atomicWrite(indexPath, `${JSON.stringify(index, null, 2)}\n`);
448
643
  }
449
644
  function refsOf(index) {
@@ -525,6 +720,7 @@ export function createWikiStore(opts) {
525
720
  .sort((a, b) => b.score - a.score || a.slug.localeCompare(b.slug));
526
721
  }
527
722
  return {
723
+ embedderLeavesProcess: embedderLeavesProcess(embedder),
528
724
  async recall(query, k = 6) {
529
725
  if (typeof query !== "string" || query.length === 0) {
530
726
  throw new WikiStoreError("recall(): query must be a non-empty string");
@@ -667,7 +863,11 @@ export function createWikiStore(opts) {
667
863
  body: input.body,
668
864
  };
669
865
  }
670
- await atomicWrite(articlePath(slug), serializeArticle(next));
866
+ const serialized = serializeArticle(next);
867
+ if (Buffer.byteLength(serialized, "utf8") > ARTICLE_MAX_BYTES) {
868
+ throw new WikiStoreError(`write(): article "${slug}" would be larger than ${ARTICLE_MAX_BYTES} bytes; split it into linked articles`);
869
+ }
870
+ await atomicWrite(articlePath(slug), serialized);
671
871
  // The index is a derived cache — rebuild from the authoritative
672
872
  // article scan so a previously stale/corrupt index self-heals on
673
873
  // the next mutation.
@@ -686,7 +886,18 @@ export function createWikiStore(opts) {
686
886
  refs = refs.filter((r) => wanted.every((t) => r.tags.includes(t)));
687
887
  }
688
888
  const dir = listOpts.staleFirst === true ? 1 : -1;
689
- refs.sort((a, b) => dir * (Date.parse(a.updatedAt) - Date.parse(b.updatedAt)) || a.slug.localeCompare(b.slug));
889
+ // An article whose timestamp is unknown sorts last in either order:
890
+ // it is neither the stalest nor the freshest, it is undated.
891
+ refs.sort((a, b) => {
892
+ const at = instantOf(a.updatedAt);
893
+ const bt = instantOf(b.updatedAt);
894
+ if (at === undefined || bt === undefined) {
895
+ if (at !== bt)
896
+ return at === undefined ? 1 : -1;
897
+ return a.slug.localeCompare(b.slug);
898
+ }
899
+ return dir * (at - bt) || a.slug.localeCompare(b.slug);
900
+ });
690
901
  return refs;
691
902
  },
692
903
  async related(slug) {
@@ -778,12 +989,16 @@ export function createWikiStore(opts) {
778
989
  let priorVersions = 0;
779
990
  fence(versionsDir);
780
991
  try {
781
- for (const slugDir of await readdir(versionsDir)) {
992
+ // Real directories and regular files only: a `versions/<slug>` link
993
+ // leading out of the store is not listed through.
994
+ for (const slugDir of await readdir(versionsDir, { withFileTypes: true })) {
995
+ if (!slugDir.isDirectory())
996
+ continue;
782
997
  try {
783
- priorVersions += (await readdir(join(versionsDir, slugDir))).filter((f) => f.endsWith(".md")).length;
998
+ priorVersions += (await readdir(join(versionsDir, slugDir.name), { withFileTypes: true })).filter((f) => f.isFile() && f.name.endsWith(".md")).length;
784
999
  }
785
1000
  catch {
786
- // not a directory / raced away — skip
1001
+ // raced away — skip
787
1002
  }
788
1003
  }
789
1004
  }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The text inside each `[[…]]` of `body`, in order: what
3
+ * `/\[\[([^\]]+)\]\]/g` captured, found with `indexOf`.
4
+ *
5
+ * The pattern rescanned the rest of the body from every `[[` that had no
6
+ * closing `]]` after it, so its cost was quadratic in a run of `[`: 40 000 of
7
+ * them took most of a second, and an article may hold 16 MiB. An article body
8
+ * is model-written, and the index rebuild reads every article's links.
9
+ *
10
+ * Each `[[` reads up to the first `]` after it, and a link is there only when
11
+ * the text between is not empty and a second `]` follows at once. When it is
12
+ * not, every `[[` before that `]` meets the same `]` and fails the same way,
13
+ * so the search resumes after it: each character is read a bounded number of
14
+ * times.
15
+ */
16
+ export declare function wikilinkInners(body: string): string[];
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The text inside each `[[…]]` of `body`, in order: what
3
+ * `/\[\[([^\]]+)\]\]/g` captured, found with `indexOf`.
4
+ *
5
+ * The pattern rescanned the rest of the body from every `[[` that had no
6
+ * closing `]]` after it, so its cost was quadratic in a run of `[`: 40 000 of
7
+ * them took most of a second, and an article may hold 16 MiB. An article body
8
+ * is model-written, and the index rebuild reads every article's links.
9
+ *
10
+ * Each `[[` reads up to the first `]` after it, and a link is there only when
11
+ * the text between is not empty and a second `]` follows at once. When it is
12
+ * not, every `[[` before that `]` meets the same `]` and fails the same way,
13
+ * so the search resumes after it: each character is read a bounded number of
14
+ * times.
15
+ */
16
+ export function wikilinkInners(body) {
17
+ const inners = [];
18
+ let from = 0;
19
+ for (;;) {
20
+ const open = body.indexOf("[[", from);
21
+ if (open === -1)
22
+ return inners;
23
+ const close = body.indexOf("]", open + 2);
24
+ if (close === -1)
25
+ return inners;
26
+ if (close > open + 2 && body.charCodeAt(close + 1) === 0x5d /* ] */) {
27
+ inners.push(body.slice(open + 2, close));
28
+ from = close + 2;
29
+ }
30
+ else {
31
+ from = close + 1;
32
+ }
33
+ }
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/wiki-store",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "description": "0.3.0 memory release §3.1 — local update-in-place wiki: versioned markdown articles (supersede, never delete), optimistic concurrency (stale_article_version), hybrid BM25+embedding recall with one-hop [[wikilink]] expansion.",
6
6
  "main": "dist/index.js",
@@ -8,16 +8,18 @@
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/errors": "0.7.0",
19
- "@crewhaus/infra-utils": "0.7.0",
20
- "@crewhaus/tenancy": "0.7.0",
19
+ "@crewhaus/errors": "0.8.0",
20
+ "@crewhaus/infra-utils": "0.8.0",
21
+ "@crewhaus/tenancy": "0.8.0",
22
+ "@crewhaus/tool-safety": "0.8.0",
21
23
  "yaml": "^2.6.0"
22
24
  },
23
25
  "devDependencies": {
@@ -42,5 +44,8 @@
42
44
  "publishConfig": {
43
45
  "access": "public"
44
46
  },
45
- "files": ["dist", "README.md", "LICENSE", "NOTICE"]
47
+ "files": ["dist", "README.md", "LICENSE", "NOTICE"],
48
+ "engines": {
49
+ "bun": ">=1.3.14"
50
+ }
46
51
  }