@autotelic/joggle 0.0.0-stage → 0.1.1
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/JOGGLE.md +634 -0
- package/LICENSE +21 -0
- package/README.md +88 -2
- package/bin/joggle.js +6 -0
- package/dist/answer-store.js +305 -0
- package/dist/architecture.js +190 -0
- package/dist/ask.js +184 -0
- package/dist/astro.js +75 -0
- package/dist/atoms.js +76 -0
- package/dist/bundles.js +156 -0
- package/dist/calibration.js +60 -0
- package/dist/candidates.js +561 -0
- package/dist/canonical.js +24 -0
- package/dist/cascade.js +98 -0
- package/dist/check.js +854 -0
- package/dist/cluster.js +69 -0
- package/dist/config.js +195 -0
- package/dist/content-mappers.js +83 -0
- package/dist/decision.js +203 -0
- package/dist/fingerprint.js +61 -0
- package/dist/git.js +127 -0
- package/dist/gitignore.js +65 -0
- package/dist/glob.js +51 -0
- package/dist/imports.js +153 -0
- package/dist/jsonc.js +95 -0
- package/dist/main.js +630 -0
- package/dist/manifest.js +42 -0
- package/dist/meta.js +170 -0
- package/dist/moves.js +47 -0
- package/dist/operation.js +133 -0
- package/dist/parsecache.js +264 -0
- package/dist/plans.js +425 -0
- package/dist/plugin.js +71 -0
- package/dist/plugins.js +136 -0
- package/dist/policy.js +676 -0
- package/dist/presets/composition.js +32 -0
- package/dist/report.js +474 -0
- package/dist/reporting.js +132 -0
- package/dist/result.js +28 -0
- package/dist/roles.js +303 -0
- package/dist/rule.js +194 -0
- package/dist/rules/append-only-honoured.js +190 -0
- package/dist/rules/bundle-conformance.js +245 -0
- package/dist/rules/call-pattern.js +209 -0
- package/dist/rules/cluster-verdict.js +700 -0
- package/dist/rules/compose-types.js +250 -0
- package/dist/rules/data-error-as-outage.js +482 -0
- package/dist/rules/declared-staleness.js +242 -0
- package/dist/rules/dependency-fit.js +297 -0
- package/dist/rules/deviation-has-a-shape.js +225 -0
- package/dist/rules/doc-matches-code.js +191 -0
- package/dist/rules/duplicate-call-run.js +240 -0
- package/dist/rules/duplicate-implementation.js +89 -0
- package/dist/rules/duplicate-meaning.js +130 -0
- package/dist/rules/fact-or-derivation.js +202 -0
- package/dist/rules/field-type-drift.js +373 -0
- package/dist/rules/generic-carries-a-caller.js +198 -0
- package/dist/rules/hoist-to-domain.js +288 -0
- package/dist/rules/import-architecture.js +393 -0
- package/dist/rules/index.js +105 -0
- package/dist/rules/inferred-over-recorded.js +214 -0
- package/dist/rules/intent-drift.js +286 -0
- package/dist/rules/invariant-has-a-reconciler.js +190 -0
- package/dist/rules/language-drift.js +234 -0
- package/dist/rules/meaning-switched-by-flag.js +189 -0
- package/dist/rules/module-direction.js +231 -0
- package/dist/rules/name-as-address.js +216 -0
- package/dist/rules/name-the-primitive.js +203 -0
- package/dist/rules/naming-drift.js +218 -0
- package/dist/rules/nullability-drift.js +220 -0
- package/dist/rules/object-shape.js +280 -0
- package/dist/rules/one-concept-one-type.js +243 -0
- package/dist/rules/page-needs-composition.js +227 -0
- package/dist/rules/reimplemented-primitive.js +283 -0
- package/dist/rules/reversal.js +264 -0
- package/dist/rules/rule-judgment.js +252 -0
- package/dist/rules/schema-excludes-domain-value.js +190 -0
- package/dist/rules/shallow-module.js +204 -0
- package/dist/rules/single-path.js +237 -0
- package/dist/rules/structural-collapse.js +198 -0
- package/dist/rules/temporal-coupling.js +205 -0
- package/dist/rules/types-over-logic.js +198 -0
- package/dist/rules/unaccounted-drop.js +190 -0
- package/dist/rules/unbounded-default-read.js +227 -0
- package/dist/run-cache.js +132 -0
- package/dist/schema.js +285 -0
- package/dist/similarity.js +119 -0
- package/dist/source.js +30 -0
- package/dist/sql.js +34 -0
- package/dist/stages.js +39 -0
- package/dist/state.js +65 -0
- package/dist/testing.js +131 -0
- package/dist/tsconfig-scope.js +32 -0
- package/dist/tsgo.js +127 -0
- package/dist/typefacts.js +455 -0
- package/dist/typetrace.js +283 -0
- package/dist/verdict.js +65 -0
- package/dist/vocabulary.js +365 -0
- package/dist/workspace.js +1963 -0
- package/extensions/joggle.ts +454 -0
- package/package.json +85 -3
package/dist/check.js
ADDED
|
@@ -0,0 +1,854 @@
|
|
|
1
|
+
import { Clock, Effect, FileSystem, Order, Path, Result } from "effect";
|
|
2
|
+
import * as AiError from "effect/ai/AiError";
|
|
3
|
+
import { layer as atomsLayer } from "./atoms.js";
|
|
4
|
+
import { isUnreachable, DecisionStats } from "./decision.js";
|
|
5
|
+
import { answerPlans, chunkPlans } from "./plans.js";
|
|
6
|
+
import { emptyTypeIndex, loadTypeFacts } from "./typetrace.js";
|
|
7
|
+
import { indexOfNodeTypes, loadDeclarationTypes, typesAtPositions, } from "./typefacts.js";
|
|
8
|
+
import { shortHash } from "./state.js";
|
|
9
|
+
import { canonical } from "./canonical.js";
|
|
10
|
+
import { sourceFingerprint } from "./fingerprint.js";
|
|
11
|
+
import { loadParses } from "./parsecache.js";
|
|
12
|
+
import { baselinePath, manifestOf, readBaseline, readStored, writeBaseline, writeStored, } from "./run-cache.js";
|
|
13
|
+
import { policy } from "./policy.js";
|
|
14
|
+
import { Rules } from "./rules/index.js";
|
|
15
|
+
import { foldSiteDuplicates, funnelNotes, rankDiagnostics } from "./report.js";
|
|
16
|
+
import { countUnjudged, everyFile, finding, inGraphScope, outcome, } from "./rule.js";
|
|
17
|
+
import { appliesAt, isEnabled, isIgnored, severityFor, unavailableFor } from "./config.js";
|
|
18
|
+
import { StoredRun, WorkspaceError, } from "./schema.js";
|
|
19
|
+
import { Service as Tsgo } from "./tsgo.js";
|
|
20
|
+
import { applyDeclarationTypes, discoverFiles, loadWorkspace } from "./workspace.js";
|
|
21
|
+
import { contentMappersOf } from "./content-mappers.js";
|
|
22
|
+
import { looksLikeSource } from "./source.js";
|
|
23
|
+
const reasonOf = (error) => error.message;
|
|
24
|
+
const typecheckFindings = (output) => output.map((diagnostic) => finding({
|
|
25
|
+
ruleId: "joggle/typecheck",
|
|
26
|
+
severity: diagnostic.severity === "error" ? "error" : "warn",
|
|
27
|
+
message: `${diagnostic.code}: ${diagnostic.message}`,
|
|
28
|
+
location: { file: diagnostic.file, line: diagnostic.line, column: diagnostic.column },
|
|
29
|
+
judged: false,
|
|
30
|
+
}));
|
|
31
|
+
/* -------------------------------------------------------------------------- */
|
|
32
|
+
/* Run manifest */
|
|
33
|
+
/* -------------------------------------------------------------------------- */
|
|
34
|
+
/**
|
|
35
|
+
* Which files moved since the stored run.
|
|
36
|
+
*
|
|
37
|
+
* A content hash cannot see a rename directly, but it can see one indirectly: a
|
|
38
|
+
* path that is new whose bytes match a path that is gone is a move, not a new
|
|
39
|
+
* declaration. Splitting it out is the same distinction the git scope makes, so
|
|
40
|
+
* `--changed` and `--pr` answer a move the same way.
|
|
41
|
+
*/
|
|
42
|
+
const changedSince = (stored, hashOf) => {
|
|
43
|
+
const before = new Map(stored.sources.map((source) => [source.path, source.hash]));
|
|
44
|
+
const byHash = new Map();
|
|
45
|
+
for (const [path, hash] of before) {
|
|
46
|
+
const list = byHash.get(hash);
|
|
47
|
+
if (list === undefined)
|
|
48
|
+
byHash.set(hash, [path]);
|
|
49
|
+
else
|
|
50
|
+
list.push(path);
|
|
51
|
+
}
|
|
52
|
+
const changed = new Set();
|
|
53
|
+
const moved = new Set();
|
|
54
|
+
for (const [file, hash] of hashOf) {
|
|
55
|
+
const prior = before.get(file);
|
|
56
|
+
if (prior === hash)
|
|
57
|
+
continue;
|
|
58
|
+
// A new path whose content matches a path that is gone is the same bytes
|
|
59
|
+
// under a new name.
|
|
60
|
+
const twin = (byHash.get(hash) ?? []).some((path) => !hashOf.has(path));
|
|
61
|
+
if (prior === undefined && twin)
|
|
62
|
+
moved.add(file);
|
|
63
|
+
else
|
|
64
|
+
changed.add(file);
|
|
65
|
+
}
|
|
66
|
+
for (const file of before.keys()) {
|
|
67
|
+
if (!hashOf.has(file))
|
|
68
|
+
changed.add(file);
|
|
69
|
+
}
|
|
70
|
+
return { changed: [...changed], renamed: [...moved] };
|
|
71
|
+
};
|
|
72
|
+
/** Exported declaration names by root-relative path. */
|
|
73
|
+
const exportsOf = (workspace) => {
|
|
74
|
+
const out = new Map();
|
|
75
|
+
for (const unit of workspace.units) {
|
|
76
|
+
if (!unit.exported)
|
|
77
|
+
continue;
|
|
78
|
+
const names = out.get(unit.file);
|
|
79
|
+
if (names === undefined)
|
|
80
|
+
out.set(unit.file, [unit.name]);
|
|
81
|
+
else
|
|
82
|
+
names.push(unit.name);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* A change to a file's EXPORTS can move what a dependent's type names resolve
|
|
88
|
+
* to, so dependents come into scope -- but only then.
|
|
89
|
+
*
|
|
90
|
+
* Closing over importers unconditionally looked harmless and was not: a
|
|
91
|
+
* body-only edit to one widely-imported file pulled 190 files into scope, which
|
|
92
|
+
* is most of the analysis back again for a change that could not have affected
|
|
93
|
+
* any of them.
|
|
94
|
+
*/
|
|
95
|
+
const affectedBy = ({ changed, imports, before, after, }) => {
|
|
96
|
+
const out = new Set(changed);
|
|
97
|
+
for (const file of changed) {
|
|
98
|
+
const was = [...(before.get(file) ?? [])].sort(Order.String).join(",");
|
|
99
|
+
const now = [...(after.get(file) ?? [])].sort(Order.String).join(",");
|
|
100
|
+
if (was === now)
|
|
101
|
+
continue;
|
|
102
|
+
for (const edge of imports.importersOf.get(file) ?? [])
|
|
103
|
+
out.add(edge.importer);
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
/* -------------------------------------------------------------------------- */
|
|
108
|
+
/* The run */
|
|
109
|
+
/* -------------------------------------------------------------------------- */
|
|
110
|
+
/**
|
|
111
|
+
* One pass over the workspace: deterministic candidates, then judgement for
|
|
112
|
+
* the rules that ask for it, then a single sorted report.
|
|
113
|
+
*/
|
|
114
|
+
export const checkRepository = Effect.fn("joggle.check")(function* (options) {
|
|
115
|
+
const started = yield* Clock.currentTimeMillis;
|
|
116
|
+
// The rule set comes from the context, where a plugin's rules have already
|
|
117
|
+
// been merged with the built-in ones. A run does not know or care where a rule
|
|
118
|
+
// came from: a plugin rule is selectable, configurable, severable and ignorable
|
|
119
|
+
// like any other, which is the whole point of a registry rather than an array.
|
|
120
|
+
const universe = yield* Rules;
|
|
121
|
+
const selected = (options.rules === undefined
|
|
122
|
+
? universe
|
|
123
|
+
: universe.filter((rule) => options.rules?.includes(rule.id) === true)).filter((rule) => isEnabled(options.config, rule.id, rule.severity));
|
|
124
|
+
const decisionStats = yield* DecisionStats;
|
|
125
|
+
const fs = yield* FileSystem.FileSystem;
|
|
126
|
+
const path = yield* Path.Path;
|
|
127
|
+
const discovered = options.paths.length === 0 && options.useTsgo
|
|
128
|
+
? yield* (yield* Tsgo).listFiles(options.cwd).pipe(Effect.orElseSucceed(() => undefined))
|
|
129
|
+
: undefined;
|
|
130
|
+
const discoverStarted = yield* Clock.currentTimeMillis;
|
|
131
|
+
const discovery = yield* discoverFiles(options.cwd, options.paths, discovered);
|
|
132
|
+
const files = discovery.files;
|
|
133
|
+
// A run that found no source files did not pass. Reporting "0 problems" for an
|
|
134
|
+
// empty file set is the worst output this program can produce, because it looks
|
|
135
|
+
// exactly like success -- and that is how the architecture rules measured
|
|
136
|
+
// nothing at all while appearing clean, when the tool was invoked with no path
|
|
137
|
+
// argument and TypeScript discovery came back empty.
|
|
138
|
+
if (files.length === 0) {
|
|
139
|
+
// Name the source of the emptiness. The old message blamed a missing path
|
|
140
|
+
// argument even when one was given, and even when the real cause was tsgo
|
|
141
|
+
// returning a list that did not survive the root comparison.
|
|
142
|
+
const where = discovered !== undefined
|
|
143
|
+
? "tsgo listed no source files under " + options.cwd
|
|
144
|
+
: options.paths.length === 0
|
|
145
|
+
? "no source files under " + options.cwd
|
|
146
|
+
: "no source files under " + options.paths.join(", ") + " in " + options.cwd;
|
|
147
|
+
return yield* WorkspaceError.make({ path: options.cwd, operation: "discover", cause: new Error(where) });
|
|
148
|
+
}
|
|
149
|
+
// Read once. The manifest needs the content, and a miss hands the same text to
|
|
150
|
+
// the parser rather than reading the tree twice.
|
|
151
|
+
const contents = new Map();
|
|
152
|
+
for (const absolute of files) {
|
|
153
|
+
const text = yield* Effect.orElseSucceed(fs.readFileString(absolute), () => undefined);
|
|
154
|
+
if (text !== undefined)
|
|
155
|
+
contents.set(absolute, text);
|
|
156
|
+
}
|
|
157
|
+
const discoverMs = (yield* Clock.currentTimeMillis) - discoverStarted;
|
|
158
|
+
// The tool's own source, which is also the parse cache's version: the parser is
|
|
159
|
+
// part of the tool, so an entry produced by a different parser is not a cache
|
|
160
|
+
// hit, it is a wrong answer.
|
|
161
|
+
const toolFingerprint = [
|
|
162
|
+
yield* sourceFingerprint(policy.analysisVersion),
|
|
163
|
+
...(options.plugins?.fingerprints ?? []),
|
|
164
|
+
].join("\u0000");
|
|
165
|
+
// The config and the run's own switches decide what is reported, so they travel
|
|
166
|
+
// in the manifest beside the rule ids. The rule set says WHICH rules ran; this
|
|
167
|
+
// says how they were configured to answer, and a stored run replayed across a
|
|
168
|
+
// config edit is a report for a repository that no longer exists. `canonical`
|
|
169
|
+
// rather than `JSON.stringify`, so a config that was only reordered is the same
|
|
170
|
+
// configuration.
|
|
171
|
+
const settings = shortHash(canonical({
|
|
172
|
+
config: options.config,
|
|
173
|
+
types: options.types,
|
|
174
|
+
typecheck: options.typecheck,
|
|
175
|
+
useTsgo: options.useTsgo,
|
|
176
|
+
runExternalCode: options.runExternalCode ?? false,
|
|
177
|
+
maxInputTokens: options.maxInputTokens ?? null,
|
|
178
|
+
}));
|
|
179
|
+
const manifest = manifestOf({
|
|
180
|
+
root: options.cwd,
|
|
181
|
+
files,
|
|
182
|
+
contents,
|
|
183
|
+
ruleIds: selected.map((rule) => rule.id),
|
|
184
|
+
tool: toolFingerprint,
|
|
185
|
+
settings,
|
|
186
|
+
});
|
|
187
|
+
const hashOf = new Map(files.map((file) => [path.relative(options.cwd, file), shortHash(contents.get(file) ?? "")]));
|
|
188
|
+
const finish = (report) => Effect.gen(function* () {
|
|
189
|
+
let diagnostics = report.diagnostics;
|
|
190
|
+
let note;
|
|
191
|
+
if (options.baselinePath !== undefined) {
|
|
192
|
+
const known = yield* readBaseline(fs, path, baselinePath(path, options.baselinePath));
|
|
193
|
+
if (known !== undefined) {
|
|
194
|
+
const before = diagnostics.length;
|
|
195
|
+
diagnostics = diagnostics.filter((entry) => entry.identity === undefined || !known.has(entry.identity));
|
|
196
|
+
note = {
|
|
197
|
+
ruleId: "joggle",
|
|
198
|
+
reason: `${before - diagnostics.length} finding(s) already in the baseline were not reported`,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (options.updateBaselinePath !== undefined) {
|
|
203
|
+
const identities = report.diagnostics
|
|
204
|
+
.map((entry) => entry.identity)
|
|
205
|
+
.filter((identity) => identity !== undefined);
|
|
206
|
+
yield* writeBaseline(fs, path, baselinePath(path, options.updateBaselinePath), identities);
|
|
207
|
+
}
|
|
208
|
+
return note === undefined ? { ...report, diagnostics } : { ...report, diagnostics, notes: [...report.notes, note] };
|
|
209
|
+
});
|
|
210
|
+
const runCache = options.runCacheDir ?? options.cacheDir;
|
|
211
|
+
const stored = yield* readStored(fs, path, runCache);
|
|
212
|
+
// A git-scoped run is not the whole repository, so the whole repository's
|
|
213
|
+
// stored report is not a replay of it.
|
|
214
|
+
if (options.replayUnchanged && options.changedPaths === undefined) {
|
|
215
|
+
if (stored !== undefined && stored.manifest === manifest) {
|
|
216
|
+
return yield* finish({
|
|
217
|
+
diagnostics: stored.diagnostics,
|
|
218
|
+
files: stored.files,
|
|
219
|
+
rules: stored.rules,
|
|
220
|
+
skipped: stored.skipped,
|
|
221
|
+
drops: stored.drops ?? [],
|
|
222
|
+
notes: [
|
|
223
|
+
{
|
|
224
|
+
ruleId: "joggle",
|
|
225
|
+
reason: `replayed the previous run: nothing it depends on changed across ${stored.files} files`,
|
|
226
|
+
},
|
|
227
|
+
// The stored run's own notes -- the funnel, the skipped judged rules,
|
|
228
|
+
// the counts -- are part of the report it replayed. Dropping them made
|
|
229
|
+
// a replay look like a run that had nothing to say.
|
|
230
|
+
...stored.notes,
|
|
231
|
+
],
|
|
232
|
+
structure: stored.structure ?? { concepts: 0, declarations: 0, duplicated: 0, overloaded: 0 },
|
|
233
|
+
timings: [{ phase: "replay-check", ms: discoverMs }],
|
|
234
|
+
// This run spent nothing, so it reports nothing spent.
|
|
235
|
+
decision: { requests: 0, replayed: 0, calls: 0, unavailable: 0, inputTokens: 0, outputTokens: 0 },
|
|
236
|
+
elapsedMs: (yield* Clock.currentTimeMillis) - started,
|
|
237
|
+
replayed: true,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// The compiler's view, when the caller asked for it. This typechecks the whole
|
|
242
|
+
// program, so it is opt-in and cached by the run manifest: an unchanged
|
|
243
|
+
// repository reuses the trace it already paid for.
|
|
244
|
+
const typeIssues = [];
|
|
245
|
+
let types = emptyTypeIndex;
|
|
246
|
+
let typesFrom = "none";
|
|
247
|
+
if (options.types === "trace") {
|
|
248
|
+
const loaded = yield* loadTypeFacts({
|
|
249
|
+
root: options.cwd,
|
|
250
|
+
cacheDir: runCache,
|
|
251
|
+
tool: toolFingerprint,
|
|
252
|
+
manifest,
|
|
253
|
+
});
|
|
254
|
+
types = loaded.index;
|
|
255
|
+
typesFrom = loaded.loadedFrom;
|
|
256
|
+
typeIssues.push(...loaded.issues);
|
|
257
|
+
}
|
|
258
|
+
const workspaceStarted = yield* Clock.currentTimeMillis;
|
|
259
|
+
// The parse cache is a performance artifact, like the run cache and the wire
|
|
260
|
+
// cache, so it lives in the machine cache and never in the repository.
|
|
261
|
+
const parseCache = yield* loadParses({ cacheDir: runCache, tool: toolFingerprint });
|
|
262
|
+
const workspace = yield* loadWorkspace(options.cwd, options.paths, {
|
|
263
|
+
discovered: files,
|
|
264
|
+
contents,
|
|
265
|
+
parses: parseCache.parses,
|
|
266
|
+
types,
|
|
267
|
+
});
|
|
268
|
+
// Written after the load, and only when something was actually parsed.
|
|
269
|
+
yield* parseCache.save;
|
|
270
|
+
// What each declaration IS, according to the compiler. On by default and
|
|
271
|
+
// separate from the trace: the trace is a whole-program side channel for the
|
|
272
|
+
// facts a guard needs, while this is the per-declaration answer every type
|
|
273
|
+
// rule reads. It needs tsgo, so a run that declined the compiler declines
|
|
274
|
+
// this too -- and says so, rather than resolving nothing silently.
|
|
275
|
+
let declarationFacts;
|
|
276
|
+
if (options.useTsgo && workspace.units.length > 0) {
|
|
277
|
+
const declarations = yield* loadDeclarationTypes({
|
|
278
|
+
root: options.cwd,
|
|
279
|
+
tsconfig: policy.typeFacts.tsconfig,
|
|
280
|
+
cacheDir: runCache,
|
|
281
|
+
tool: toolFingerprint,
|
|
282
|
+
manifest,
|
|
283
|
+
maxMembers: policy.typeFacts.maxMembers,
|
|
284
|
+
maxStrings: policy.typeFacts.maxStrings,
|
|
285
|
+
requests: workspace.units.map((unit) => ({ file: unit.file, position: unit.namePosition })),
|
|
286
|
+
});
|
|
287
|
+
for (const issue of declarations.issues)
|
|
288
|
+
typeIssues.push(issue);
|
|
289
|
+
declarationFacts = {
|
|
290
|
+
applied: applyDeclarationTypes(workspace, declarations.index),
|
|
291
|
+
from: declarations.resolvedFrom,
|
|
292
|
+
truncated: declarations.truncated,
|
|
293
|
+
unresolved: declarations.unresolved,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
// The checker's type at the nodes a rule asks about, by offset. One program,
|
|
297
|
+
// a bounded request list, and only with `--types`: it costs what the trace
|
|
298
|
+
// costs, and the join by offset is the one the declaration trace cannot make.
|
|
299
|
+
let nodeTypes;
|
|
300
|
+
if (options.types === "trace") {
|
|
301
|
+
const requests = workspace.files
|
|
302
|
+
.flatMap((file) => file.facts.returns.map((entry) => ({ file: file.path, position: entry.start })))
|
|
303
|
+
.slice(0, 2000);
|
|
304
|
+
if (requests.length > 0) {
|
|
305
|
+
const found = yield* Effect.tryPromise(() => typesAtPositions({ cwd: options.cwd, tsconfig: "tsconfig.json", requests })).pipe(Effect.orElseSucceed(() => ({ found: [], unresolved: 0 })));
|
|
306
|
+
nodeTypes = indexOfNodeTypes(found.found);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const timings = [
|
|
310
|
+
{ phase: "workspace", ms: (yield* Clock.currentTimeMillis) - workspaceStarted },
|
|
311
|
+
];
|
|
312
|
+
const diagnostics = [];
|
|
313
|
+
const skipped = [];
|
|
314
|
+
const notes = [];
|
|
315
|
+
for (const issue of typeIssues)
|
|
316
|
+
notes.push({ ruleId: "joggle", reason: issue });
|
|
317
|
+
// A project can declare content mappers for formats joggle cannot parse. The
|
|
318
|
+
// compiler may check them (when the run opted in), but the rule report does
|
|
319
|
+
// not index them, and a file count that silently omits a third of the tree is
|
|
320
|
+
// the failure `source.ts` exists to prevent.
|
|
321
|
+
const mappers = yield* contentMappersOf(options.cwd);
|
|
322
|
+
const declared = mappers.flatMap((mapper) => mapper.extensions);
|
|
323
|
+
const unindexed = declared.filter((extension) => !looksLikeSource("file" + extension));
|
|
324
|
+
if (unindexed.length > 0) {
|
|
325
|
+
notes.push({
|
|
326
|
+
ruleId: "joggle",
|
|
327
|
+
reason: "tsconfig declares content mappers for " +
|
|
328
|
+
unindexed.join(", ") +
|
|
329
|
+
"; joggle indexes TypeScript and Astro only, so those files are not in the rule report" +
|
|
330
|
+
(options.runExternalCode === true
|
|
331
|
+
? " (tsc ran the mappers, so typecheck diagnostics may still cover them)"
|
|
332
|
+
: " (tsc did not run them either: pass --run-external-code to enable the mapper)"),
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
// A file outside the root produces a "../" relative path. Its evidence has no
|
|
336
|
+
// business in a cache whose location nobody chose.
|
|
337
|
+
const escaping = workspace.files.filter((file) => file.path.startsWith(".."));
|
|
338
|
+
const breach = !options.cacheDirExplicit && escaping.length > 0;
|
|
339
|
+
if (breach) {
|
|
340
|
+
const first = escaping[0];
|
|
341
|
+
diagnostics.push(finding({
|
|
342
|
+
ruleId: "joggle/cache-boundary",
|
|
343
|
+
severity: "error",
|
|
344
|
+
message: `Analysed files live outside the project root (${first?.path ?? "?"}), so judgements would be persisted into a cache that does not own that code.`,
|
|
345
|
+
help: "Pass --cache-dir pointing outside the analysed tree, or run joggle from the analysed project's root.",
|
|
346
|
+
location: { file: first?.path ?? ".", line: 1, column: 1 },
|
|
347
|
+
judged: false,
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
const effective = breach ? selected.filter((rule) => !rule.judged) : selected;
|
|
351
|
+
if (breach) {
|
|
352
|
+
for (const rule of selected) {
|
|
353
|
+
if (rule.judged) {
|
|
354
|
+
skipped.push({ ruleId: rule.id, reason: "cache boundary: evidence would cross repositories" });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// Scope after the workspace is loaded: closing over the import graph needs it.
|
|
359
|
+
let scope = everyFile;
|
|
360
|
+
if (options.changedPaths !== undefined) {
|
|
361
|
+
const changed = new Set(options.changedPaths);
|
|
362
|
+
const moved = new Set(options.movedPaths ?? []);
|
|
363
|
+
scope = { changed, moved };
|
|
364
|
+
const base = options.changedBase === undefined ? "the git base" : options.changedBase;
|
|
365
|
+
notes.push({
|
|
366
|
+
ruleId: "joggle",
|
|
367
|
+
reason: changed.size === 0
|
|
368
|
+
? "the changed-file list for " + base + " is empty: nothing is in scope, so this run reports nothing"
|
|
369
|
+
: "scoped to " +
|
|
370
|
+
changed.size +
|
|
371
|
+
" file(s) that differ from " +
|
|
372
|
+
base +
|
|
373
|
+
(moved.size === 0
|
|
374
|
+
? ""
|
|
375
|
+
: " and " + moved.size + " pure rename(s), which the content rules skip") +
|
|
376
|
+
": this answers what the change introduced, not what is wrong with the repository",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
else if (options.changed) {
|
|
380
|
+
if (stored === undefined) {
|
|
381
|
+
notes.push({
|
|
382
|
+
ruleId: "joggle",
|
|
383
|
+
reason: "scoped run with no stored run to compare against: every file is in scope",
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
const since = changedSince(stored, hashOf);
|
|
388
|
+
const beforeExports = new Map((stored.sources ?? []).map((source) => [source.path, source.exports]));
|
|
389
|
+
const affected = affectedBy({
|
|
390
|
+
changed: new Set(since.changed),
|
|
391
|
+
imports: workspace.imports,
|
|
392
|
+
before: beforeExports,
|
|
393
|
+
after: exportsOf(workspace),
|
|
394
|
+
});
|
|
395
|
+
scope = { changed: affected, moved: new Set(since.renamed) };
|
|
396
|
+
notes.push({
|
|
397
|
+
ruleId: "joggle",
|
|
398
|
+
reason: `scoped to ${affected.size} file(s) that moved` +
|
|
399
|
+
(since.renamed.length === 0
|
|
400
|
+
? ""
|
|
401
|
+
: ` and ${since.renamed.length} pure rename(s), which the content rules skip`) +
|
|
402
|
+
": this answers what the change introduced, not what is wrong with the repository",
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const drops = [];
|
|
407
|
+
// The engine, in two phases.
|
|
408
|
+
//
|
|
409
|
+
// Phase one: every planned rule does its deterministic work and hands over its
|
|
410
|
+
// questions. Phase two: the engine answers every rule's questions, batched, and
|
|
411
|
+
// each rule reads its own answers. A rule that answered its own questions could
|
|
412
|
+
// not share a request with any other, which is the reason for the split.
|
|
413
|
+
//
|
|
414
|
+
// A plain rule -- deterministic, or judged but not batched -- runs as it always
|
|
415
|
+
// did. One atom store serves the whole run.
|
|
416
|
+
const isPlanned = (rule) => "plan" in rule;
|
|
417
|
+
const plannedRules = effective.filter(isPlanned);
|
|
418
|
+
const plainRules = effective.filter((rule) => !isPlanned(rule));
|
|
419
|
+
const engine = Effect.gen(function* () {
|
|
420
|
+
const ruleTimings = new Map();
|
|
421
|
+
const phases = yield* Effect.forEach(plannedRules, (rule) => Effect.gen(function* () {
|
|
422
|
+
const ruleStarted = yield* Clock.currentTimeMillis;
|
|
423
|
+
const result = yield* rule.plan(workspace, scope, { config: options.config, nodeTypes }).pipe(Effect.map((value) => Result.succeed(value)), Effect.catch((error) => Effect.succeed(Result.fail(error))));
|
|
424
|
+
ruleTimings.set(rule.id, (yield* Clock.currentTimeMillis) - ruleStarted);
|
|
425
|
+
return result;
|
|
426
|
+
}), { concurrency: "unbounded" });
|
|
427
|
+
// A plan is judged only when one of its files is in the run's scope. This is
|
|
428
|
+
// the enforcement, not a convention: a rule that forgets to filter still
|
|
429
|
+
// cannot spend a token on a candidate the run is not about, because the
|
|
430
|
+
// engine never sends it and its answer is never read.
|
|
431
|
+
const inScopePlan = (plan) => scope.changed === undefined || plan.concerns.some((file) => inGraphScope(scope, file));
|
|
432
|
+
const askedPerRule = phases.map((phase) => Result.isSuccess(phase) ? phase.success.plans.filter(inScopePlan) : []);
|
|
433
|
+
const allPlans = askedPerRule.flat();
|
|
434
|
+
// ONE request is the goal, and the provider's token ceiling is the reason it
|
|
435
|
+
// cannot always be one: a whole run's evidence can exceed it, and an oversized
|
|
436
|
+
// request comes back as max_tokens_exceeded, which every judged rule then
|
|
437
|
+
// reads as unreadable. So the plans are cut into requests that fit, and each is
|
|
438
|
+
// answered from the per-decision cache first, so a chunk boundary does not
|
|
439
|
+
// cost a re-judgement.
|
|
440
|
+
const chunks = yield* chunkPlans(allPlans, policy.decision.maxStateChars);
|
|
441
|
+
// The run's token budget. A chunk is estimated from the state it carries --
|
|
442
|
+
// the provider charges for the state, and it dominates -- so the run stops
|
|
443
|
+
// before a pathological change spends without bound. What it did not judge is
|
|
444
|
+
// reported as a budget drop, never dropped in silence.
|
|
445
|
+
const allowed = [];
|
|
446
|
+
const overBudget = [];
|
|
447
|
+
let estimated = 0;
|
|
448
|
+
for (const chunk of chunks) {
|
|
449
|
+
const cost = Math.ceil(chunk.chars / 4);
|
|
450
|
+
if (estimated + cost > (options.maxInputTokens ?? policy.decision.maxInputTokens)) {
|
|
451
|
+
overBudget.push(...chunk.plans);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
estimated += cost;
|
|
455
|
+
allowed.push(chunk);
|
|
456
|
+
}
|
|
457
|
+
// A chunk that fails is CUT AND RETRIED, not abandoned.
|
|
458
|
+
//
|
|
459
|
+
// The provider occasionally returns a distribution that does not sum to 1, and
|
|
460
|
+
// Effect's validation fails the whole request for it. With one candidate per
|
|
461
|
+
// request that cost one candidate; with a batch it costs the batch, and every
|
|
462
|
+
// rule reading it sees unreadable. Halving the request changes what the model
|
|
463
|
+
// is asked, so a bad answer usually does not repeat.
|
|
464
|
+
const answerChunk = (plans, retried = false) => answerPlans(plans).pipe(Effect.catch((error) =>
|
|
465
|
+
// A model that was never reached is not a bad answer: retrying it would
|
|
466
|
+
// ask the same unreachable model again, and the engine needs the error
|
|
467
|
+
// to know the rule was skipped rather than judged.
|
|
468
|
+
isUnreachable(error)
|
|
469
|
+
? Effect.fail(error)
|
|
470
|
+
: plans.length <= 1
|
|
471
|
+
// A lone request is one candidate, and the provider sometimes
|
|
472
|
+
// returns a distribution that does not sum to 1. That is not a
|
|
473
|
+
// judgement, so ask once more before giving the candidate up --
|
|
474
|
+
// the provider is nondeterministic, and losing a candidate to a
|
|
475
|
+
// malformed decimal is an instrument fault, not an answer.
|
|
476
|
+
? retried
|
|
477
|
+
? Effect.fail(error)
|
|
478
|
+
: answerChunk(plans, true)
|
|
479
|
+
: Effect.gen(function* () {
|
|
480
|
+
const half = Math.ceil(plans.length / 2);
|
|
481
|
+
const left = yield* answerChunk(plans.slice(0, half)).pipe(Effect.orElseSucceed(() => plans.slice(0, half).map(() => undefined)));
|
|
482
|
+
const right = yield* answerChunk(plans.slice(half)).pipe(Effect.orElseSucceed(() => plans.slice(half).map(() => undefined)));
|
|
483
|
+
return [...left, ...right];
|
|
484
|
+
})));
|
|
485
|
+
const chunkResults = yield* Effect.forEach(allowed, (chunk) => answerChunk(chunk.plans).pipe(Effect.map((values) => Result.succeed(values)), Effect.catch((error) => Effect.succeed(Result.fail(error)))), { concurrency: policy.decision.requestConcurrency });
|
|
486
|
+
const values = [];
|
|
487
|
+
let failure;
|
|
488
|
+
const allowedSet = new Set(allowed);
|
|
489
|
+
let resultIndex = 0;
|
|
490
|
+
for (const chunk of chunks) {
|
|
491
|
+
if (allowedSet.has(chunk)) {
|
|
492
|
+
const result = chunkResults[resultIndex];
|
|
493
|
+
resultIndex += 1;
|
|
494
|
+
if (result !== undefined && Result.isSuccess(result)) {
|
|
495
|
+
values.push(...result.success);
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (result !== undefined && Result.isFailure(result))
|
|
499
|
+
failure ??= result.failure;
|
|
500
|
+
}
|
|
501
|
+
for (let i = 0; i < chunk.plans.length; i += 1)
|
|
502
|
+
values.push(undefined);
|
|
503
|
+
}
|
|
504
|
+
const overBudgetSubjects = new Set(overBudget.map((plan) => plan.subject));
|
|
505
|
+
if (overBudget.length > 0) {
|
|
506
|
+
notes.push({
|
|
507
|
+
ruleId: "joggle",
|
|
508
|
+
reason: overBudget.length +
|
|
509
|
+
" candidate(s) were not judged: the run's token budget of " +
|
|
510
|
+
(options.maxInputTokens ?? policy.decision.maxInputTokens) +
|
|
511
|
+
" was reached",
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
const plannedOutcomes = [];
|
|
515
|
+
let offset = 0;
|
|
516
|
+
plannedRules.forEach((rule, index) => {
|
|
517
|
+
const phase = phases[index];
|
|
518
|
+
const ms = ruleTimings.get(rule.id) ?? 0;
|
|
519
|
+
if (phase === undefined) {
|
|
520
|
+
plannedOutcomes.push({ rule, result: Result.succeed(outcome([])), ms });
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (Result.isFailure(phase)) {
|
|
524
|
+
plannedOutcomes.push({ rule, result: Result.fail(phase.failure), ms });
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const planned = phase.success;
|
|
528
|
+
const asked = askedPerRule[index] ?? [];
|
|
529
|
+
const start = offset;
|
|
530
|
+
offset += asked.length;
|
|
531
|
+
// The run's answer for this rule: the rule's own setting, the run's
|
|
532
|
+
// `unavailable`, or the rule's declaration. `propagate` steps the rule
|
|
533
|
+
// aside; `count` keeps it but counts the candidates no judgement reached.
|
|
534
|
+
const unavailable = unavailableFor(options.config, rule.id, rule.onUnavailable);
|
|
535
|
+
// No judgement and a rule that cannot stand without one: the engine reports
|
|
536
|
+
// it as skipped. A rule with no questions is IDLE, not skipped.
|
|
537
|
+
if (asked.length > 0 && failure !== undefined && isUnreachable(failure) && unavailable === "propagate") {
|
|
538
|
+
plannedOutcomes.push({ rule, result: Result.fail(failure), ms });
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
// The rule's reader indexes its own full plan list, so the answers go back
|
|
542
|
+
// in that shape: the asked ones in order, undefined for the rest.
|
|
543
|
+
const askedValues = values.slice(start, start + asked.length);
|
|
544
|
+
let cursor = 0;
|
|
545
|
+
const aligned = planned.plans.map((plan) => (inScopePlan(plan) ? askedValues[cursor++] : undefined));
|
|
546
|
+
let result = planned.read(aligned);
|
|
547
|
+
if (asked.length > 0 && failure !== undefined && isUnreachable(failure) && unavailable === "count") {
|
|
548
|
+
result = countUnjudged(result, reasonOf(failure));
|
|
549
|
+
}
|
|
550
|
+
// An out-of-scope plan reads as no answer to a rule that never had to know
|
|
551
|
+
// the scope. That is the engine's bookkeeping, not a finding, so the drops
|
|
552
|
+
// it produced are removed. A plan the budget skipped is a budget drop, not
|
|
553
|
+
// an unreadable one.
|
|
554
|
+
const outside = new Set(planned.plans.filter((plan) => !inScopePlan(plan)).map((plan) => plan.subject));
|
|
555
|
+
const drops = result.drops
|
|
556
|
+
.filter((drop) => !(drop.stage === "unreadable" && outside.has(drop.subject)))
|
|
557
|
+
.map((drop) => drop.stage === "unreadable" && overBudgetSubjects.has(drop.subject)
|
|
558
|
+
? {
|
|
559
|
+
...drop,
|
|
560
|
+
stage: "budget",
|
|
561
|
+
reason: "the run's token budget was reached before this candidate",
|
|
562
|
+
}
|
|
563
|
+
: drop);
|
|
564
|
+
// The last guard, and the one that makes the invariant hold whatever a rule
|
|
565
|
+
// does: a finding about a file the run is not about is not reported. A
|
|
566
|
+
// fact-based rule reports an unverified finding when its answer is missing,
|
|
567
|
+
// so without this an out-of-scope cluster would leak into a scoped report.
|
|
568
|
+
const kept = result.diagnostics.filter((entry) => inGraphScope(scope, entry.location.file));
|
|
569
|
+
plannedOutcomes.push({ rule, result: Result.succeed({ ...result, diagnostics: kept, drops }), ms });
|
|
570
|
+
});
|
|
571
|
+
// The plain rules are independent, and the judged ones wait on the network.
|
|
572
|
+
// Running them concurrently overlaps those waits; the DecisionModel layer
|
|
573
|
+
// serializes its own cache writes, so two rules finishing at once cannot
|
|
574
|
+
// interleave a full write.
|
|
575
|
+
const plainResults = yield* Effect.forEach(plainRules, (rule) => Effect.gen(function* () {
|
|
576
|
+
const ruleStarted = yield* Clock.currentTimeMillis;
|
|
577
|
+
// The config goes in, because a rule that enforces a layering is
|
|
578
|
+
// entitled to know what the layering is.
|
|
579
|
+
const result = yield* rule.run(workspace, scope, { config: options.config, nodeTypes }).pipe(Effect.map((value) => Result.succeed(value)), Effect.catch((error) => Effect.succeed(Result.fail(error))));
|
|
580
|
+
return { rule, result, ms: (yield* Clock.currentTimeMillis) - ruleStarted };
|
|
581
|
+
}), { concurrency: policy.decision.requestConcurrency });
|
|
582
|
+
return { plannedOutcomes, plainResults };
|
|
583
|
+
});
|
|
584
|
+
const { plannedOutcomes, plainResults } = yield* engine.pipe(Effect.provide(atomsLayer));
|
|
585
|
+
const results = [...plannedOutcomes, ...plainResults];
|
|
586
|
+
for (const { rule, result, ms } of results) {
|
|
587
|
+
if (Result.isSuccess(result)) {
|
|
588
|
+
diagnostics.push(...result.success.diagnostics);
|
|
589
|
+
for (const note of result.success.notes)
|
|
590
|
+
notes.push({ ruleId: rule.id, reason: note });
|
|
591
|
+
drops.push(...result.success.drops);
|
|
592
|
+
}
|
|
593
|
+
else {
|
|
594
|
+
skipped.push({ ruleId: rule.id, reason: reasonOf(result.failure) });
|
|
595
|
+
}
|
|
596
|
+
timings.push({ phase: rule.id.replace("joggle/", ""), ms });
|
|
597
|
+
}
|
|
598
|
+
// What the run did not look at.
|
|
599
|
+
//
|
|
600
|
+
// The funnel rule applied to the one bound that had no report: every bound in
|
|
601
|
+
// this program is a decision not to look at something, and a bound nobody can
|
|
602
|
+
// see is indistinguishable from a bug. Discovery used to drop non-source files
|
|
603
|
+
// inside its own loop, so "287 files" read as the repository rather than as a
|
|
604
|
+
// fifth of it.
|
|
605
|
+
if (discovery.truncated) {
|
|
606
|
+
notes.push({
|
|
607
|
+
ruleId: "joggle",
|
|
608
|
+
reason: "the file walk hit its own limit: this run saw only part of the tree",
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
// A plugin that did not load is reported, not logged and forgotten. A rule
|
|
612
|
+
// that is silently absent makes the report look clean, which is the one
|
|
613
|
+
// failure mode this program keeps having to design against.
|
|
614
|
+
for (const failure of options.plugins?.failures ?? []) {
|
|
615
|
+
notes.push({
|
|
616
|
+
ruleId: "joggle",
|
|
617
|
+
reason: "plugin " + failure.specifier + " was not loaded: " + failure.reason,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
// What the run extracted, before a single rule filtered it.
|
|
621
|
+
//
|
|
622
|
+
// This note exists because its absence made a clean report unreadable. Run
|
|
623
|
+
// against a 110-file library, joggle reported "2 problems" and nothing else,
|
|
624
|
+
// and there was no way to tell whether the rules had looked at a clean codebase
|
|
625
|
+
// or at almost nothing. The extraction was fine -- 257 declarations -- but the
|
|
626
|
+
// only counts in the report were the FILTERED ones each rule chose to mention,
|
|
627
|
+
// so "11 type declaration(s)" read as the population when it was 11 of 53.
|
|
628
|
+
const kinds = new Map();
|
|
629
|
+
for (const unit of workspace.units)
|
|
630
|
+
kinds.set(unit.kind, (kinds.get(unit.kind) ?? 0) + 1);
|
|
631
|
+
const exported = workspace.units.filter((unit) => unit.exported).length;
|
|
632
|
+
notes.push({
|
|
633
|
+
ruleId: "joggle",
|
|
634
|
+
reason: "extracted " +
|
|
635
|
+
workspace.units.length +
|
|
636
|
+
" declaration(s) from " +
|
|
637
|
+
workspace.files.length +
|
|
638
|
+
" file(s): " +
|
|
639
|
+
[...kinds.entries()]
|
|
640
|
+
.sort((left, right) => right[1] - left[1])
|
|
641
|
+
.map(([kind, count]) => count + " " + kind)
|
|
642
|
+
.join(", ") +
|
|
643
|
+
"; " +
|
|
644
|
+
exported +
|
|
645
|
+
" exported, " +
|
|
646
|
+
(workspace.units.length - exported) +
|
|
647
|
+
" file-local",
|
|
648
|
+
});
|
|
649
|
+
if (parseCache.issues.length > 0) {
|
|
650
|
+
notes.push({
|
|
651
|
+
ruleId: "joggle",
|
|
652
|
+
reason: "the parse cache did not decode and was ignored: " + parseCache.issues[0],
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
// How many declarations the compiler could speak about. A trace that resolved
|
|
656
|
+
// nothing is a run whose type-aware rules must stay silent, and the two look
|
|
657
|
+
// identical in a report unless this says which happened.
|
|
658
|
+
if (declarationFacts === undefined) {
|
|
659
|
+
if (workspace.units.length > 0) {
|
|
660
|
+
notes.push({
|
|
661
|
+
ruleId: "joggle",
|
|
662
|
+
reason: "no declaration types were resolved: it needs the compiler, and this run declined it (--no-tsgo)",
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
const cut = declarationFacts.truncated === 0
|
|
668
|
+
? ""
|
|
669
|
+
: "; " + declarationFacts.truncated + " member list(s) were cut to the evidence budget";
|
|
670
|
+
const missed = declarationFacts.unresolved === 0 ? "" : "; " + declarationFacts.unresolved + " had no project";
|
|
671
|
+
notes.push({
|
|
672
|
+
ruleId: "joggle",
|
|
673
|
+
reason: "resolved " +
|
|
674
|
+
declarationFacts.applied +
|
|
675
|
+
" of " +
|
|
676
|
+
workspace.units.length +
|
|
677
|
+
" declaration(s) to a type (" +
|
|
678
|
+
declarationFacts.from +
|
|
679
|
+
")" +
|
|
680
|
+
cut +
|
|
681
|
+
missed,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
if (options.types === "trace") {
|
|
685
|
+
notes.push({
|
|
686
|
+
ruleId: "joggle",
|
|
687
|
+
reason: "declaration trace available (" + typesFrom + ") for the rules that read a guard's type",
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
const parsedFromCache = workspace.parses.hits();
|
|
691
|
+
const parsedAgain = workspace.parses.misses();
|
|
692
|
+
if (parsedFromCache > 0 || parsedAgain > 0) {
|
|
693
|
+
notes.push({
|
|
694
|
+
ruleId: "joggle",
|
|
695
|
+
reason: "parsed " +
|
|
696
|
+
parsedAgain +
|
|
697
|
+
" file(s) and reused " +
|
|
698
|
+
parsedFromCache +
|
|
699
|
+
" parse(s) from the cache",
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
if (workspace.testDeclarations > 0) {
|
|
703
|
+
notes.push({
|
|
704
|
+
ruleId: "joggle",
|
|
705
|
+
reason: workspace.testDeclarations +
|
|
706
|
+
" declaration(s) in test files are compared only against each other: a factory is supposed to be repeated",
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
if (workspace.excludedHelpers > 0) {
|
|
710
|
+
notes.push({
|
|
711
|
+
ruleId: "joggle",
|
|
712
|
+
reason: workspace.excludedHelpers +
|
|
713
|
+
" transpiler helper(s) were not analysed: they appear once per file by construction",
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
if (discovery.ignored > 0) {
|
|
717
|
+
notes.push({
|
|
718
|
+
ruleId: "joggle",
|
|
719
|
+
reason: "ignored by .gitignore: " +
|
|
720
|
+
discovery.ignored +
|
|
721
|
+
" file(s) and " +
|
|
722
|
+
discovery.ignoredDirectories +
|
|
723
|
+
" director" +
|
|
724
|
+
(discovery.ignoredDirectories === 1 ? "y" : "ies") +
|
|
725
|
+
" not analysed",
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
if (discovery.skippedExtensions.length > 0) {
|
|
729
|
+
const total = discovery.skippedExtensions.reduce((sum, entry) => sum + entry.count, 0);
|
|
730
|
+
const shown = discovery.skippedExtensions.slice(0, 4).map((entry) => entry.extension + " " + entry.count);
|
|
731
|
+
const rest = discovery.skippedExtensions.length - shown.length;
|
|
732
|
+
notes.push({
|
|
733
|
+
ruleId: "joggle",
|
|
734
|
+
reason: "not parsed: " +
|
|
735
|
+
total +
|
|
736
|
+
" file(s) outside the parser's extensions (" +
|
|
737
|
+
shown.join(", ") +
|
|
738
|
+
(rest > 0 ? ", and " + rest + " more" : "") +
|
|
739
|
+
")",
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
if (workspace.unparsed.length > 0) {
|
|
743
|
+
const example = workspace.unparsed[0];
|
|
744
|
+
notes.push({
|
|
745
|
+
ruleId: "joggle",
|
|
746
|
+
reason: workspace.unparsed.length +
|
|
747
|
+
" file(s) failed to parse, e.g. " +
|
|
748
|
+
(example === undefined ? "" : example.path + ": " + example.reason),
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
// The funnel, in the report rather than in a debug log.
|
|
752
|
+
//
|
|
753
|
+
// A rule that reports nothing is either a rule with nothing to look at or a
|
|
754
|
+
// rule whose gate is too tight, and the finding count cannot tell the two
|
|
755
|
+
// apart. This is what does -- and it is the only way a threshold can be tuned
|
|
756
|
+
// from evidence rather than from a guess.
|
|
757
|
+
notes.push(...funnelNotes(drops));
|
|
758
|
+
if (options.typecheck && options.useTsgo) {
|
|
759
|
+
const tsgo = yield* Tsgo;
|
|
760
|
+
const typeErrors = yield* tsgo.typecheck(options.cwd).pipe(Effect.orElseSucceed(() => []));
|
|
761
|
+
diagnostics.push(...typecheckFindings(typeErrors));
|
|
762
|
+
}
|
|
763
|
+
// The config decides how loudly a rule speaks and what is excepted. Both are
|
|
764
|
+
// applied here rather than inside the rules, so a rule cannot opt out of being
|
|
765
|
+
// configured.
|
|
766
|
+
const configured = diagnostics
|
|
767
|
+
.map((entry) => ({ entry, severity: severityFor(options.config, entry.ruleId, entry.severity) }))
|
|
768
|
+
.filter((item) => item.severity !== "off")
|
|
769
|
+
.map((item) => ({ ...item.entry, severity: item.severity }))
|
|
770
|
+
.filter((entry) => !isIgnored({ config: options.config, ruleId: entry.ruleId, path: entry.location.file }))
|
|
771
|
+
// Scoped out by configuration: this rule is not configured to speak about
|
|
772
|
+
// this file. A finding, not a rule, is what gets scoped.
|
|
773
|
+
.filter((entry) => appliesAt({ config: options.config, ruleId: entry.ruleId, path: entry.location.file }));
|
|
774
|
+
// One site, one move. `reversal` proves what the judged rules also report --
|
|
775
|
+
// a copy, a containment -- and the same location carrying both is one thing
|
|
776
|
+
// counted twice. The richer finding survives; the fact-only one is counted.
|
|
777
|
+
const folded = foldSiteDuplicates(configured);
|
|
778
|
+
if (folded.folded > 0) {
|
|
779
|
+
notes.push({
|
|
780
|
+
ruleId: "joggle",
|
|
781
|
+
reason: folded.folded +
|
|
782
|
+
" finding(s) reported a site another rule already reported with more evidence, and were folded into it",
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
// The workspace's own phases, alongside the per-rule timings. A run that says
|
|
786
|
+
// "workspace 4.4s" and nothing else is a run nobody can make faster.
|
|
787
|
+
timings.push(...workspace.phases);
|
|
788
|
+
// The shape of the codebase, before any rule filtered it. A change that adds
|
|
789
|
+
// declarations without adding concepts is the entropy this tool exists to
|
|
790
|
+
// remove, and the two numbers together are the only honest way to say it.
|
|
791
|
+
const names = new Map();
|
|
792
|
+
for (const unit of workspace.units) {
|
|
793
|
+
const list = names.get(unit.name);
|
|
794
|
+
if (list === undefined)
|
|
795
|
+
names.set(unit.name, [unit]);
|
|
796
|
+
else
|
|
797
|
+
list.push(unit);
|
|
798
|
+
}
|
|
799
|
+
let duplicated = 0;
|
|
800
|
+
let overloaded = 0;
|
|
801
|
+
for (const list of names.values()) {
|
|
802
|
+
if (list.length > 1)
|
|
803
|
+
duplicated += list.length;
|
|
804
|
+
const displays = new Set(list.map((unit) => unit.typeFacts?.display).filter((display) => display !== undefined));
|
|
805
|
+
if (displays.size > 1)
|
|
806
|
+
overloaded += 1;
|
|
807
|
+
}
|
|
808
|
+
const structure = {
|
|
809
|
+
concepts: names.size,
|
|
810
|
+
declarations: workspace.units.length,
|
|
811
|
+
duplicated,
|
|
812
|
+
overloaded,
|
|
813
|
+
};
|
|
814
|
+
const decisionTotals = yield* decisionStats.read;
|
|
815
|
+
const elapsedMs = (yield* Clock.currentTimeMillis) - started;
|
|
816
|
+
const report = {
|
|
817
|
+
diagnostics: rankDiagnostics(folded.diagnostics),
|
|
818
|
+
files: workspace.files.length,
|
|
819
|
+
rules: effective.length,
|
|
820
|
+
structure,
|
|
821
|
+
skipped,
|
|
822
|
+
notes,
|
|
823
|
+
drops,
|
|
824
|
+
timings,
|
|
825
|
+
decision: decisionTotals,
|
|
826
|
+
elapsedMs,
|
|
827
|
+
replayed: false,
|
|
828
|
+
};
|
|
829
|
+
// Stored before the baseline filter, so a later replay reproduces the whole
|
|
830
|
+
// run and the baseline is applied to it again rather than baked in.
|
|
831
|
+
// A scoped run is not a full run, so it must not become the baseline that the
|
|
832
|
+
// next full run is compared against.
|
|
833
|
+
if (!options.changed && options.changedPaths === undefined) {
|
|
834
|
+
yield* writeStored(fs, path, runCache, {
|
|
835
|
+
version: policy.version,
|
|
836
|
+
manifest,
|
|
837
|
+
sources: workspace.files.map((file) => ({
|
|
838
|
+
path: file.path,
|
|
839
|
+
hash: hashOf.get(file.path) ?? "",
|
|
840
|
+
exports: [...(exportsOf(workspace).get(file.path) ?? [])].sort(Order.String),
|
|
841
|
+
})),
|
|
842
|
+
diagnostics: report.diagnostics,
|
|
843
|
+
files: report.files,
|
|
844
|
+
rules: report.rules,
|
|
845
|
+
structure: report.structure,
|
|
846
|
+
skipped: report.skipped,
|
|
847
|
+
notes: report.notes,
|
|
848
|
+
drops: report.drops,
|
|
849
|
+
decision: decisionTotals,
|
|
850
|
+
elapsedMs,
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
return yield* finish(report);
|
|
854
|
+
});
|