reading-stone 0.0.0-stage → 0.2.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 (72) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +11 -0
  3. package/README.md +274 -2
  4. package/dist/analysis.d.ts +341 -0
  5. package/dist/analysis.js +392 -0
  6. package/dist/cognitive.d.ts +163 -0
  7. package/dist/cognitive.js +380 -0
  8. package/dist/complexity.d.ts +102 -0
  9. package/dist/complexity.js +163 -0
  10. package/dist/coverage.d.ts +78 -0
  11. package/dist/coverage.js +635 -0
  12. package/dist/detector.d.ts +41 -0
  13. package/dist/detector.js +2134 -0
  14. package/dist/discovery.d.ts +330 -0
  15. package/dist/discovery.js +930 -0
  16. package/dist/doc-coverage.d.ts +155 -0
  17. package/dist/doc-coverage.js +543 -0
  18. package/dist/extract.d.ts +209 -0
  19. package/dist/extract.js +498 -0
  20. package/dist/index.d.ts +64 -0
  21. package/dist/index.js +54 -0
  22. package/dist/internal.d.ts +75 -0
  23. package/dist/internal.js +63 -0
  24. package/dist/main.d.ts +8 -0
  25. package/dist/main.js +39 -0
  26. package/dist/models.d.ts +185 -0
  27. package/dist/models.js +170 -0
  28. package/dist/program.d.ts +207 -0
  29. package/dist/program.js +662 -0
  30. package/dist/protocol.d.ts +88 -0
  31. package/dist/protocol.js +74 -0
  32. package/dist/quality/assertions.d.ts +81 -0
  33. package/dist/quality/assertions.js +425 -0
  34. package/dist/quality/collect.d.ts +66 -0
  35. package/dist/quality/collect.js +412 -0
  36. package/dist/quality/inference.d.ts +86 -0
  37. package/dist/quality/inference.js +603 -0
  38. package/dist/quality/matchers.d.ts +129 -0
  39. package/dist/quality/matchers.js +220 -0
  40. package/dist/quality/pairing.d.ts +133 -0
  41. package/dist/quality/pairing.js +770 -0
  42. package/dist/quality/pipeline.d.ts +59 -0
  43. package/dist/quality/pipeline.js +202 -0
  44. package/dist/quality/types.d.ts +127 -0
  45. package/dist/quality/types.js +92 -0
  46. package/dist/safeio.d.ts +107 -0
  47. package/dist/safeio.js +314 -0
  48. package/dist/server.d.ts +19 -0
  49. package/dist/server.js +451 -0
  50. package/dist/signals/adapter.d.ts +103 -0
  51. package/dist/signals/adapter.js +323 -0
  52. package/dist/signals/caller-index.d.ts +117 -0
  53. package/dist/signals/caller-index.js +147 -0
  54. package/dist/signals/caller.d.ts +76 -0
  55. package/dist/signals/caller.js +108 -0
  56. package/dist/signals/evidence.d.ts +233 -0
  57. package/dist/signals/evidence.js +578 -0
  58. package/dist/signals/interface.d.ts +68 -0
  59. package/dist/signals/interface.js +87 -0
  60. package/dist/signals/jsdoc.d.ts +27 -0
  61. package/dist/signals/jsdoc.js +233 -0
  62. package/dist/signals/naming.d.ts +26 -0
  63. package/dist/signals/naming.js +160 -0
  64. package/dist/signals/routing.d.ts +163 -0
  65. package/dist/signals/routing.js +416 -0
  66. package/dist/signals/types.d.ts +146 -0
  67. package/dist/signals/types.js +99 -0
  68. package/dist/signals/visibility.d.ts +23 -0
  69. package/dist/signals/visibility.js +82 -0
  70. package/dist/taxonomy.d.ts +89 -0
  71. package/dist/taxonomy.js +217 -0
  72. package/package.json +61 -4
@@ -0,0 +1,330 @@
1
+ import type { Writable } from "node:stream";
2
+ /**
3
+ * Deterministic tree walker for file discovery (FR-002/FR-003, research R2).
4
+ *
5
+ * Performs an async recursive descent over a project tree and returns every
6
+ * *walk candidate*: an entry that is neither a directory nor a symlink and
7
+ * whose name carries an in-scope TypeScript or JavaScript-family extension.
8
+ * Candidates are returned as root-relative POSIX-separator paths in
9
+ * deterministic depth-first order — each directory's entries are visited in
10
+ * UTF-16 code-unit order — so any downstream per-file diagnostic emitted in
11
+ * walk order is deterministic too (research R8), not just the sorted
12
+ * response lists.
13
+ *
14
+ * The walker decides only what is visited and what is collected.
15
+ * `discover` (T024) sequences the per-candidate precedence chain — safety
16
+ * skip, `.d.ts` exclusion, generated-marker exclusion — over the walk
17
+ * results and hands the survivors to `classifyFiles`, which applies
18
+ * test-vs-source classification and lexicographically sorts the two final
19
+ * lists. `detectFramework` (US4, FR-006) reports the test framework from
20
+ * configuration presence at the resolved root, reading `package.json`
21
+ * through the shared safe reader, and `hasGeneratedMarker` (US4, FR-007)
22
+ * is the exported single-boolean view of the leading-comment scan the
23
+ * chain runs. All four surfaces are exported and composable; `discover`
24
+ * below runs them behind root-path validation (FR-010) and assembles the
25
+ * wire-shaped result (FR-001).
26
+ */
27
+ /**
28
+ * File extensions considered by discovery (FR-002). Case-sensitive.
29
+ *
30
+ * Also the parse-only module-specifier resolution domain's extension
31
+ * enumeration (spec 004's imports pairing tier, FR-004): the pairing
32
+ * strategies import the SAME set so specifier resolution and file
33
+ * discovery can never disagree about which extensions name an in-root
34
+ * module — one enumeration, two consumers, no drift.
35
+ */
36
+ export declare const WALKED_EXTENSIONS: ReadonlySet<string>;
37
+ /**
38
+ * Walks the project tree rooted at `root` and returns all walk candidates
39
+ * as root-relative POSIX-separator paths in deterministic walk order.
40
+ *
41
+ * The caller (`discover`, T009) validates and realpath-resolves `root`
42
+ * before walking, and `resolveRoot`'s root-level readability probe (T024)
43
+ * has already proven the root readable by the time the walk starts; this
44
+ * function assumes an existing, accessible directory. A `readdir` failure
45
+ * on a subdirectory propagates to the caller: silently skipping an
46
+ * unreadable subtree would silently omit its files — a silent false
47
+ * negative the constitution forbids (Principle II).
48
+ *
49
+ * @param root Absolute path of an existing directory to walk.
50
+ * @returns Walk candidates in deterministic depth-first order. The list is
51
+ * NOT globally sorted — sorting the response lists is `classifyFiles`'s
52
+ * responsibility (FR-008).
53
+ */
54
+ export declare function walkTree(root: string): Promise<string[]>;
55
+ /**
56
+ * The disjoint result of classifying walk candidates (FR-005): every
57
+ * classified file lands in exactly one list.
58
+ *
59
+ * Field names are camelCase per convention pack CS-006 — this is an
60
+ * internal support type, not the protocol payload; the wire shape
61
+ * (`source_files` / `test_files`) is assembled by `discover` (T009) per
62
+ * contracts/discover.md.
63
+ */
64
+ export interface FileClassification {
65
+ readonly sourceFiles: readonly string[];
66
+ readonly testFiles: readonly string[];
67
+ }
68
+ /**
69
+ * Classifies walk candidates into disjoint, sorted source and test file
70
+ * lists (FR-004, FR-005, FR-008; research R8, R13).
71
+ *
72
+ * Precedence per research R13: a trailing-`.d.ts` basename is excluded
73
+ * from both lists first (silent documented policy, no diagnostic); then
74
+ * any test signal — name or directory — classifies the file into
75
+ * `testFiles`, because test wins over source; every other candidate is a
76
+ * source file. The chain's async stages — safety skip and
77
+ * generated-marker exclusion (FR-007) — run earlier, in `discover`'s
78
+ * screen (`screenCandidates`); a candidate arriving here from `discover`
79
+ * has already passed them, and the `.d.ts` check below is kept so direct
80
+ * callers of this composable surface get the same policy.
81
+ *
82
+ * Determinism (FR-008 / research R8): both lists are sorted with the
83
+ * default `Array.prototype.sort()` comparator — UTF-16 code-unit
84
+ * lexicographic, stable, locale-independent, and Node-version-stable.
85
+ * `localeCompare` is locale- and ICU-dependent and is never used.
86
+ *
87
+ * @param candidates Root-relative POSIX-separator walk candidates (the
88
+ * output of `walkTree`), in any order.
89
+ * @returns The disjoint, lexicographically sorted file lists.
90
+ */
91
+ export declare function classifyFiles(candidates: readonly string[]): FileClassification;
92
+ /**
93
+ * Detects the project's test framework from configuration presence at the
94
+ * resolved project root (FR-006; research R7; Constitution Principle I).
95
+ *
96
+ * The detection basis is exactly what the spec pins (Assumption 2):
97
+ * configuration presence at the root, non-recursive, independent of
98
+ * source-file contents and of the process environment — so the same tree
99
+ * reports the same framework on every run (US4 scenario 3):
100
+ *
101
+ * 1. `"vitest"` when any of the four vitest config basenames exists at
102
+ * the root;
103
+ * 2. otherwise `"jest"` when any of the five jest config basenames exists
104
+ * at the root, or the root `package.json` carries a `jest` key;
105
+ * 3. otherwise `"unknown"`.
106
+ *
107
+ * vitest wins when both frameworks are configured (FR-006). The
108
+ * `package.json` read happens only on the jest-key fallback — a vitest
109
+ * or jest config file has already decided the answer — and flows through
110
+ * the shared safe reader with its 16 MiB cap and one-diagnostic skip
111
+ * rules: an unreadable or malformed `package.json` writes exactly one
112
+ * stderr diagnostic and is treated as having no `jest` key (FR-017),
113
+ * while a root with no `package.json` is silent — absence is not a skip
114
+ * condition.
115
+ *
116
+ * Sequenced into `discover()` (T024): the call replaced the US1
117
+ * `"unknown"` placeholder, and the caller's stderr sink flows through —
118
+ * the server handler forwards the server's own `stderr` — so a skipped
119
+ * `package.json` diagnostic reaches the process stderr, never stdout.
120
+ *
121
+ * @param resolvedRoot Realpath-resolved absolute analysis root, the form
122
+ * `resolveRoot` returns; every probe and read is relative to it.
123
+ * @param stderr Injected diagnostic sink (Testability principle — the
124
+ * server passes its own `stderr`); stdout is never touched.
125
+ * @returns The lowercase framework string: `"vitest"`, `"jest"`, or
126
+ * `"unknown"` (spec Assumption 11).
127
+ */
128
+ export declare function detectFramework(resolvedRoot: string, stderr: Writable): Promise<"vitest" | "jest" | "unknown">;
129
+ /**
130
+ * Reports whether a walked file's leading comment block carries a
131
+ * generated-code marker (FR-007; research R6) — the signal that
132
+ * excludes the file from both discover lists.
133
+ *
134
+ * The read runs `readBounded`'s full guarded sequence (FR-017, FR-018)
135
+ * over the bounded 64 KiB prefix, so containment, the regular-file
136
+ * check, the 16 MiB size cap, and strict UTF-8 decoding all apply. A
137
+ * skipped file resolves `null` after `readBounded` itself has written
138
+ * the one `reading-stone: skipping <relPath>: <reason>` line; this
139
+ * function writes nothing on that path (FR-017's one-diagnostic
140
+ * contract) and reads the file as not marked — the safety skip already
141
+ * excludes it from both lists higher in the precedence chain (safety
142
+ * skip → `.d.ts` → generated marker → test → source), so a `false`
143
+ * here can never re-include it.
144
+ *
145
+ * A file that is merely marked — the exclusion this function reports —
146
+ * is a silent documented policy like `.d.ts` (FR-004, FR-007): no
147
+ * diagnostic, because nothing is wrong with the file; it is generated
148
+ * code, out of scope by policy.
149
+ *
150
+ * Sequenced into `discover()` (T024): the precedence chain runs this
151
+ * same scan through the shared internal `scanLeadingComments`, which
152
+ * keeps the safety-skip fact distinguishable from the marker verdict.
153
+ * The exported boolean view preserves T023's single-`false` contract: a
154
+ * safety-skipped file reads as not marked, sound in the chain because
155
+ * the skip itself — not the verdict — excludes the file from both lists.
156
+ *
157
+ * @param resolvedRoot Realpath-resolved absolute analysis root; the read
158
+ * is contained within it (FR-018).
159
+ * @param relPath Walk-built root-relative POSIX path, echoed verbatim in
160
+ * `readBounded`'s diagnostics.
161
+ * @param stderr Injected diagnostic sink (Testability principle — the
162
+ * server passes its own `stderr`); stdout is never touched.
163
+ * @returns `true` when the leading comment block's text contains the
164
+ * case-sensitive `@generated` or `Code generated` marker; `false` when
165
+ * it does not, when the prefix carries no comment trivia, or when the
166
+ * safe reader skipped the file.
167
+ */
168
+ export declare function hasGeneratedMarker(resolvedRoot: string, relPath: string, stderr: Writable): Promise<boolean>;
169
+ /**
170
+ * Applies the per-candidate precedence chain's excluding stages to the
171
+ * walk results (T024; FR-004, FR-007, FR-017; research R13) and returns
172
+ * the candidates that survive into path-only classification.
173
+ *
174
+ * The chain, in the spec's order — safety skip → `.d.ts` → generated
175
+ * marker — with test-vs-source classification (in `classifyFiles`) as
176
+ * the final stages:
177
+ *
178
+ * 1. **Safety skip** (FR-017): every walked candidate flows through
179
+ * `readBounded`'s guarded 64 KiB prefix read first — the scan's own
180
+ * read, so each file is read exactly once. A `null` return (FIFO,
181
+ * oversized, unreadable, decode error, symlink) excludes the file
182
+ * from both lists; readBounded has already written the one
183
+ * `reading-stone: skipping <relPath>: <reason>` line. This is why
184
+ * the read precedes even the `.d.ts` name check: the safety reader —
185
+ * not the name — decides whether a candidate named like a source
186
+ * file is readable (a FIFO named `types.d.ts` is diagnosed, not
187
+ * silently dropped).
188
+ * 2. **`.d.ts` exclusion** (FR-004): silent documented policy.
189
+ * 3. **Generated-marker exclusion** (FR-007): silent documented policy —
190
+ * a marked file is generated code, out of scope by policy, and the
191
+ * exclusion wins over test classification (spec edge case).
192
+ *
193
+ * Candidates are screened in deterministic walk order, so the skip
194
+ * diagnostics land in walk order too (research R8).
195
+ *
196
+ * @param resolvedRoot Realpath-resolved absolute analysis root.
197
+ * @param candidates Walk candidates (`walkTree`'s output), in walk order.
198
+ * @param stderr Injected diagnostic sink; stdout is never touched.
199
+ * @returns The candidates that survive all three excluding stages, in
200
+ * walk order, for `classifyFiles` to split into test and source lists.
201
+ */
202
+ export declare function screenCandidates(resolvedRoot: string, candidates: readonly string[], stderr: Writable): Promise<string[]>;
203
+ /**
204
+ * The error thrown when a discovery root fails validation (FR-010): the
205
+ * root does not exist, is not a directory, or cannot be accessed.
206
+ *
207
+ * All three failure modes deliberately share one message: the wire
208
+ * contract (contracts/discover.md error table) maps every root failure
209
+ * to the same `-32602` response — `Invalid params: root_path is not a
210
+ * directory: <path>` — so this error mirrors that unified phrasing and
211
+ * the server handler can produce the contract message without
212
+ * re-classifying the failure. The underlying fs error, when one exists,
213
+ * rides along as the standard `cause` for debugging and never reaches
214
+ * the wire.
215
+ *
216
+ * The carried path is the cwd-resolved root (the absolute form of the
217
+ * caller's `root_path`), not the realpath-canonicalized form:
218
+ * `path.resolve` never touches the filesystem, so the reported path is
219
+ * a deterministic, caller-predictable function of the input and never
220
+ * leaks platform canonicalization (macOS resolves `/var` to
221
+ * `/private/var`) into error messages.
222
+ */
223
+ export declare class InvalidRootPathError extends Error {
224
+ /** The cwd-resolved root path that failed validation. */
225
+ readonly rootPath: string;
226
+ /**
227
+ * @param rootPath The cwd-resolved root path that failed validation;
228
+ * embedded in the message and exposed as a field so the server
229
+ * handler can build the contract's `-32602` response (research R9
230
+ * layering: discovery never imports from server).
231
+ * @param options Standard error options; fs failures pass the
232
+ * underlying error as `cause` (`ENOENT`, `EACCES`, ...).
233
+ */
234
+ constructor(rootPath: string, options?: ErrorOptions);
235
+ }
236
+ /**
237
+ * Validates and resolves the discovery root (FR-010, FR-018; research
238
+ * R5) in a fixed, security-relevant order:
239
+ *
240
+ * 1. `path.resolve` the (possibly relative) `root_path` against the
241
+ * analyzer process working directory (spec Assumption 8). Pure
242
+ * string arithmetic: the filesystem is not touched and symlinks are
243
+ * not followed — canonicalization is step 2's job.
244
+ * 2. `fs.realpath` the resolved root exactly once: a symlinked root is
245
+ * followed at this boundary only, and the path is canonicalized. A
246
+ * nonexistent root (`ENOENT`) or an inaccessible one (`EACCES`)
247
+ * fails here.
248
+ * 3. `stat` the real path and require a directory — a file used as the
249
+ * root rejects.
250
+ * 4. `readdir` the real root once as a readability probe — `realpath`
251
+ * and `stat` both succeed on an unreadable (mode 000) directory, so
252
+ * only this probe surfaces a root-level `EACCES`/`EPERM` as an
253
+ * `InvalidRootPathError` (T024, the T005-flagged src fix) rather than
254
+ * letting the walk's root `readdir` fail as an unexpected internal
255
+ * error; every other probe failure rethrows, and mid-walk
256
+ * subdirectory `readdir` failures stay fail-loud (Principle II).
257
+ *
258
+ * Containment downstream (FR-018) is enforced against the returned
259
+ * resolved root: the walker only ever joins root-relative segments onto
260
+ * it and never follows symlinks, so no walked path can escape it
261
+ * (research R5's structural layer).
262
+ *
263
+ * @param rootPath Absolute or relative root path from the caller.
264
+ * @returns The realpath-resolved absolute root to walk.
265
+ * @throws InvalidRootPathError when the root does not exist, is not a
266
+ * directory, or cannot be accessed.
267
+ */
268
+ export declare function resolveRoot(rootPath: string): Promise<string>;
269
+ /**
270
+ * The `discover` result payload (FR-001) — the JSON-RPC wire shape,
271
+ * field for field, as contracts/discover.md defines it.
272
+ *
273
+ * Field names are snake_case by design: this is the protocol payload,
274
+ * not an internal support type, so the wire contract governs the naming
275
+ * (same convention as `InitializeResult` in protocol.ts) rather than
276
+ * convention-pack CS-006's camelCase rule for internal types. The
277
+ * camelCase classification fields (`FileClassification.sourceFiles` and
278
+ * `.testFiles`) map onto the wire names in `discover`.
279
+ */
280
+ export interface DiscoveryResult {
281
+ /**
282
+ * Lexicographically sorted, root-relative POSIX-separator source
283
+ * file paths.
284
+ */
285
+ readonly source_files: readonly string[];
286
+ /**
287
+ * Lexicographically sorted, root-relative POSIX-separator test file
288
+ * paths; disjoint from `source_files`.
289
+ */
290
+ readonly test_files: readonly string[];
291
+ /**
292
+ * The detected test framework: `"vitest"`, `"jest"`, or `"unknown"`;
293
+ * always present (contracts/discover.md).
294
+ */
295
+ readonly framework: "vitest" | "jest" | "unknown";
296
+ }
297
+ /**
298
+ * Discovers a project's source and test files (FR-001, FR-006, FR-007,
299
+ * FR-010; research R5) — the implementation behind the `discover`
300
+ * JSON-RPC method.
301
+ *
302
+ * Composes the exported building blocks: `resolveRoot` validates and
303
+ * realpath-resolves the root, `walkTree` collects the walk candidates in
304
+ * deterministic order, `screenCandidates` applies the per-candidate
305
+ * precedence chain's excluding stages (safety skip → `.d.ts` → generated
306
+ * marker), `classifyFiles` splits the survivors into the disjoint,
307
+ * sorted lists, and `detectFramework` reports the test framework from
308
+ * configuration presence at the root. The returned object is already the
309
+ * wire-shaped result, so the server handler returns it as the JSON-RPC
310
+ * result unchanged (byte-level determinism is the protocol layer's
311
+ * serializer).
312
+ *
313
+ * Diagnostics (FR-017) flow to the injected stderr sink — the server
314
+ * handler forwards the server's own `stderr`, so a skip line reaches the
315
+ * process stderr, never stdout; direct callers that omit the sink get
316
+ * `process.stderr`.
317
+ *
318
+ * @param rootPath Absolute or relative project root; a relative path
319
+ * resolves against the analyzer process working directory (spec
320
+ * Assumption 8), and a symlinked root is realpath-resolved exactly once
321
+ * at the boundary (FR-018 / research R5).
322
+ * @param stderr Diagnostic sink for safe-reader skip lines
323
+ * (Testability principle); defaults to `process.stderr`.
324
+ * @returns The discovery result: sorted, root-relative POSIX
325
+ * `source_files` and `test_files`, and the detected test `framework`.
326
+ * @throws InvalidRootPathError when the root does not exist, is not a
327
+ * directory, or cannot be accessed (FR-010); the server handler maps it
328
+ * to the contract's `-32602` response.
329
+ */
330
+ export declare function discover(rootPath: string, stderr?: Writable): Promise<DiscoveryResult>;