@autotelic/joggle 0.0.0-stage → 0.1.2
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
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { Effect, Option, Schema } from "effect";
|
|
2
|
+
import { Decision, DecisionModel } from "effect/ai";
|
|
3
|
+
import { policy } from "../policy.js";
|
|
4
|
+
import { locator, messages, reporter } from "../reporting.js";
|
|
5
|
+
import { declined, defineRule, inScope, marginOfAnswer, outcome, qualityOf, } from "../rule.js";
|
|
6
|
+
import { pageDecisions, pageVerdictByRole } from "../vocabulary.js";
|
|
7
|
+
import { dirOf } from "../bundles.js";
|
|
8
|
+
const RULE_ID = "joggle/page-needs-composition";
|
|
9
|
+
const candidatesIn = (workspace) => {
|
|
10
|
+
const bundleDirs = new Set();
|
|
11
|
+
for (const file of workspace.files) {
|
|
12
|
+
if (file.facts.callSites.some((site) => site.name === "createContext" || site.name.endsWith(".createContext"))) {
|
|
13
|
+
bundleDirs.add(dirOf(file.path));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const { minLocalState, minInlineElements } = policy.pageNeedsComposition;
|
|
17
|
+
const pages = [];
|
|
18
|
+
for (const file of workspace.files) {
|
|
19
|
+
// Call SITES, not distinct names. This was counting a deduplicated set, so a
|
|
20
|
+
// file with thirty-four useState calls reported one and the gate below --
|
|
21
|
+
// which wants three -- could never be satisfied. The rule had never fired.
|
|
22
|
+
const localState = file.facts.callSites.filter((site) => site.name === "useState" || site.name.endsWith(".useState")).length;
|
|
23
|
+
const inlineElements = file.facts.jsx.length;
|
|
24
|
+
if (localState < minLocalState || inlineElements < minInlineElements)
|
|
25
|
+
continue;
|
|
26
|
+
pages.push({
|
|
27
|
+
source: file,
|
|
28
|
+
localState,
|
|
29
|
+
inlineElements,
|
|
30
|
+
lines: file.text.split("\n").length,
|
|
31
|
+
importsBundles: workspace.imports.edges.filter((edge) => edge.importer === file.path && edge.resolution === "resolved" && bundleDirs.has(dirOf(edge.to))).length,
|
|
32
|
+
rendersProvider: file.facts.jsx.some((element) => element.endsWith(".Provider")),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return pages;
|
|
36
|
+
};
|
|
37
|
+
const gaps = (page) => {
|
|
38
|
+
const found = [];
|
|
39
|
+
if (!page.rendersProvider)
|
|
40
|
+
found.push("no provider as the composition root");
|
|
41
|
+
if (page.importsBundles === 0)
|
|
42
|
+
found.push("no blocks from a composition bundle");
|
|
43
|
+
if (page.localState >= policy.pageNeedsComposition.minLocalState + 2) {
|
|
44
|
+
found.push(`${page.localState} useState calls that a provider would own`);
|
|
45
|
+
}
|
|
46
|
+
return found;
|
|
47
|
+
};
|
|
48
|
+
const PageEvidence = Schema.Struct({
|
|
49
|
+
page: Schema.Struct({
|
|
50
|
+
path: Schema.String,
|
|
51
|
+
lines: Schema.Number,
|
|
52
|
+
local_state_calls: Schema.Number,
|
|
53
|
+
inline_elements: Schema.Number,
|
|
54
|
+
imports_from_pattern_bundles: Schema.Number,
|
|
55
|
+
renders_a_provider: Schema.Boolean,
|
|
56
|
+
}),
|
|
57
|
+
gaps: Schema.Array(Schema.String),
|
|
58
|
+
role: Schema.optionalKey(Schema.String),
|
|
59
|
+
});
|
|
60
|
+
const PageRole = Decision.make({
|
|
61
|
+
input: PageEvidence,
|
|
62
|
+
decisions: { role: pageDecisions.role },
|
|
63
|
+
});
|
|
64
|
+
const findingFor = (report, page, answers) => {
|
|
65
|
+
const dropOf = (stage, reason) => ({
|
|
66
|
+
drop: { ruleId: RULE_ID, subject: page.source.path, stage, reason },
|
|
67
|
+
});
|
|
68
|
+
const verdict = answers["verdict"];
|
|
69
|
+
if (verdict === undefined || !("label" in verdict)) {
|
|
70
|
+
return dropOf("unreadable", "no verdict came back");
|
|
71
|
+
}
|
|
72
|
+
if (declined(verdict.label)) {
|
|
73
|
+
return dropOf("declined", "the state is this page's own");
|
|
74
|
+
}
|
|
75
|
+
// The yes/no question is a verdict, not a ranking: when it says the extraction
|
|
76
|
+
// is not worth a reviewer's time, there is no finding, however loudly the
|
|
77
|
+
// Choice said `extract_to_bundle`.
|
|
78
|
+
const worth = answers["worth_fixing"];
|
|
79
|
+
const probability = worth !== undefined && "probability" in worth ? worth.probability : undefined;
|
|
80
|
+
const confidence = verdict.confidence ?? 1;
|
|
81
|
+
const quality = qualityOf({
|
|
82
|
+
score: probability ?? confidence,
|
|
83
|
+
margin: marginOfAnswer(verdict),
|
|
84
|
+
confidence,
|
|
85
|
+
});
|
|
86
|
+
if (quality.quality === "drop")
|
|
87
|
+
return dropOf("gated", quality.reason);
|
|
88
|
+
const review = quality.quality === "review";
|
|
89
|
+
const gap = answers["primary_gap"];
|
|
90
|
+
const missing = gaps(page);
|
|
91
|
+
return {
|
|
92
|
+
diagnostic: report({
|
|
93
|
+
about: page.source,
|
|
94
|
+
messageId: "state_pressure",
|
|
95
|
+
data: {
|
|
96
|
+
file: page.source.path,
|
|
97
|
+
state: page.localState,
|
|
98
|
+
inline: page.inlineElements,
|
|
99
|
+
gaps: missing.join("; "),
|
|
100
|
+
start: gap === undefined || !("label" in gap) || declined(gap.label)
|
|
101
|
+
? ""
|
|
102
|
+
: ` Start with: ${gap.label.replace(/_/g, " ")}.`,
|
|
103
|
+
},
|
|
104
|
+
helpId: missing.length === 0 ? "state_pressure_help_none" : "state_pressure_help_gaps",
|
|
105
|
+
identity: [RULE_ID, page.source.path].join("\u0000"),
|
|
106
|
+
confidence,
|
|
107
|
+
score: probability ?? confidence,
|
|
108
|
+
judged: true,
|
|
109
|
+
severity: review ? "info" : "warn",
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export const pageNeedsComposition = defineRule({
|
|
114
|
+
id: RULE_ID,
|
|
115
|
+
severity: "warn",
|
|
116
|
+
description: "Pages whose own state and markup belong in a composition bundle.",
|
|
117
|
+
judged: true,
|
|
118
|
+
move: "expand",
|
|
119
|
+
messages: messages({
|
|
120
|
+
state_pressure: "{{file}} carries {{state}} useState call(s) and {{inline}} inline element(s) that may belong in a composition bundle.",
|
|
121
|
+
state_pressure_help_none: "See the composition pattern guide: a provider owns `{ state, actions, meta }` and blocks are exported by dot notation.",
|
|
122
|
+
state_pressure_help_gaps: "Gaps: {{gaps}}.{{start}}",
|
|
123
|
+
}),
|
|
124
|
+
run: Effect.fn("joggle/page-needs-composition")(function* (workspace, scope) {
|
|
125
|
+
const report = reporter(pageNeedsComposition, locator(workspace));
|
|
126
|
+
const pages = candidatesIn(workspace).filter((page) => inScope(scope, page.source.path));
|
|
127
|
+
if (pages.length === 0) {
|
|
128
|
+
return outcome([], [
|
|
129
|
+
"no file looks like a page under state pressure: the trigger is " +
|
|
130
|
+
policy.pageNeedsComposition.minLocalState +
|
|
131
|
+
" or more useState calls and " +
|
|
132
|
+
policy.pageNeedsComposition.minInlineElements +
|
|
133
|
+
" or more inline elements",
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
const budget = policy.pageNeedsComposition.maxPages;
|
|
137
|
+
const judged = pages.slice(0, budget);
|
|
138
|
+
const evidenceFor = (page) => ({
|
|
139
|
+
page: {
|
|
140
|
+
path: page.source.path,
|
|
141
|
+
lines: page.lines,
|
|
142
|
+
local_state_calls: page.localState,
|
|
143
|
+
inline_elements: page.inlineElements,
|
|
144
|
+
imports_from_pattern_bundles: page.importsBundles,
|
|
145
|
+
renders_a_provider: page.rendersProvider,
|
|
146
|
+
},
|
|
147
|
+
gaps: gaps(page),
|
|
148
|
+
});
|
|
149
|
+
// ROUND ONE: what is this file?
|
|
150
|
+
//
|
|
151
|
+
// Cheaper and more useful than asking the real question directly, because the
|
|
152
|
+
// answer decides which real question to ask. The first version of this rule
|
|
153
|
+
// asked every file under routes/ whether it should be a bundle, produced
|
|
154
|
+
// 1,216 candidates, and got 237 shrugs -- a model asked a question that does
|
|
155
|
+
// not apply to the thing in front of it does not say so, it says 0.2.
|
|
156
|
+
const classified = yield* Effect.forEach(judged, (page) => DecisionModel.decide(PageRole, { input: evidenceFor(page) }).pipe(Effect.map((result) => Option.some(result.answers.role.label)), Effect.orElseSucceed(() => Option.none())), { concurrency: policy.decision.requestConcurrency });
|
|
157
|
+
const roles = [];
|
|
158
|
+
const drops = pages.slice(budget).map((page) => ({
|
|
159
|
+
ruleId: RULE_ID,
|
|
160
|
+
subject: page.source.path,
|
|
161
|
+
stage: "budget",
|
|
162
|
+
reason: `the run judged ${budget} page(s) and this one was past the budget`,
|
|
163
|
+
}));
|
|
164
|
+
judged.forEach((page, index) => {
|
|
165
|
+
const role = classified[index];
|
|
166
|
+
if (role === undefined || Option.isNone(role)) {
|
|
167
|
+
drops.push({
|
|
168
|
+
ruleId: RULE_ID,
|
|
169
|
+
subject: page.source.path,
|
|
170
|
+
stage: "unreadable",
|
|
171
|
+
reason: "the response did not classify this file",
|
|
172
|
+
});
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (declined(role.value)) {
|
|
176
|
+
drops.push({
|
|
177
|
+
ruleId: RULE_ID,
|
|
178
|
+
subject: page.source.path,
|
|
179
|
+
stage: "declined",
|
|
180
|
+
reason: "the model classified it as not a page, modal or layout",
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
roles.push({ page, role: role.value });
|
|
185
|
+
});
|
|
186
|
+
if (roles.length === 0) {
|
|
187
|
+
return outcome([], ["every candidate was classified as something other than a page, modal or layout"], drops);
|
|
188
|
+
}
|
|
189
|
+
// ROUND TWO: the decision the classification selected, with the vocabulary
|
|
190
|
+
// that kind of file is judged by.
|
|
191
|
+
const results = yield* Effect.forEach(roles, ({ page, role }) => {
|
|
192
|
+
const definition = Decision.make({
|
|
193
|
+
input: PageEvidence,
|
|
194
|
+
decisions: {
|
|
195
|
+
verdict: {
|
|
196
|
+
...pageDecisions.verdict,
|
|
197
|
+
criteria: pageVerdictByRole[role] ?? pageDecisions.verdict.criteria,
|
|
198
|
+
},
|
|
199
|
+
primary_gap: pageDecisions.primary_gap,
|
|
200
|
+
worth_fixing: pageDecisions.worth_fixing,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
return DecisionModel.decide(definition, { input: { ...evidenceFor(page), role } }).pipe(Effect.map((result) => Option.some(result.answers)), Effect.orElseSucceed(() => Option.none()));
|
|
204
|
+
}, { concurrency: policy.decision.requestConcurrency });
|
|
205
|
+
const diagnostics = [];
|
|
206
|
+
// Only the files that survived classification are read here, and the index
|
|
207
|
+
// is into THAT list: round two asked about a different, shorter set.
|
|
208
|
+
roles.forEach(({ page }, index) => {
|
|
209
|
+
const result = results[index];
|
|
210
|
+
if (result === undefined || Option.isNone(result)) {
|
|
211
|
+
drops.push({
|
|
212
|
+
ruleId: RULE_ID,
|
|
213
|
+
subject: page.source.path,
|
|
214
|
+
stage: "unreadable",
|
|
215
|
+
reason: "the response contained nothing for this candidate",
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const decided = findingFor(report, page, result.value);
|
|
220
|
+
if (decided.diagnostic !== undefined)
|
|
221
|
+
diagnostics.push(decided.diagnostic);
|
|
222
|
+
if (decided.drop !== undefined)
|
|
223
|
+
drops.push(decided.drop);
|
|
224
|
+
});
|
|
225
|
+
return outcome(diagnostics, [], drops);
|
|
226
|
+
}),
|
|
227
|
+
});
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Decision } from "effect/ai";
|
|
3
|
+
import { Atoms } from "../atoms.js";
|
|
4
|
+
import { lineAt } from "../cascade.js";
|
|
5
|
+
import { policy } from "../policy.js";
|
|
6
|
+
import { verdictsOf } from "../plans.js";
|
|
7
|
+
import { locator, messages, reporter } from "../reporting.js";
|
|
8
|
+
import { inScope, marginOfAnswer, outcome, qualityOf, } from "../rule.js";
|
|
9
|
+
import { stripFile } from "./call-pattern.js";
|
|
10
|
+
import { lineStarts } from "../workspace.js";
|
|
11
|
+
const RULE_ID = "joggle/reimplemented-primitive";
|
|
12
|
+
/**
|
|
13
|
+
* A function that inlines exactly what an existing declaration already does.
|
|
14
|
+
*
|
|
15
|
+
* Two halves, and the split is the point of `docs/two-regimes.md`.
|
|
16
|
+
*
|
|
17
|
+
* The deterministic half is GENERATION: which pairs are even candidates. That is
|
|
18
|
+
* the resolved call graph -- a function whose whole body is a run of calls has a
|
|
19
|
+
* key, and two functions with the same key are a pair worth judging. The key is
|
|
20
|
+
* the resolved callees, coarse on purpose -- what each call READS is evidence the
|
|
21
|
+
* question sees, not part of the key -- so it is a high-recall filter and not the
|
|
22
|
+
* verdict.
|
|
23
|
+
*
|
|
24
|
+
* The judged half is the VERDICT: are these two the same operation? That is the
|
|
25
|
+
* question `same_behavior` used to ask and answered badly, because it was asked
|
|
26
|
+
* over text with no call sites in the state. Here the state IS the call graph --
|
|
27
|
+
* both bodies, their callees resolved to declarations, and the leaf inputs they
|
|
28
|
+
* read -- which is the evidence the old question was missing.
|
|
29
|
+
*
|
|
30
|
+
* A run with no model key reports the pair unverified rather than deciding it,
|
|
31
|
+
* which is what a high-recall candidate deserves.
|
|
32
|
+
*/
|
|
33
|
+
export const reimplementedPrimitive = {
|
|
34
|
+
id: RULE_ID,
|
|
35
|
+
severity: "info",
|
|
36
|
+
description: "A function that inlines exactly what an existing declaration already does.",
|
|
37
|
+
judged: true,
|
|
38
|
+
move: "contract",
|
|
39
|
+
onUnavailable: "report",
|
|
40
|
+
operations: ["replace"],
|
|
41
|
+
messages: messages({
|
|
42
|
+
inlined_helper: "{{name}} inlines what `{{helper}}` already does: {{count}} call(s) in the same order.",
|
|
43
|
+
inlined_helper_help: "Call `{{helper}}` in {{helperFile}} instead of repeating its body. The calls are {{calls}}.{{unverified}}",
|
|
44
|
+
}),
|
|
45
|
+
plan: Effect.fn("joggle/reimplemented-primitive")(function* (workspace, scope) {
|
|
46
|
+
const report = reporter(reimplementedPrimitive, locator(workspace));
|
|
47
|
+
const { minCalls, maxPairs } = policy.reimplementedPrimitive;
|
|
48
|
+
const fileOf = new Map(workspace.files.map((file) => [file.path, file]));
|
|
49
|
+
// A call is its callee AND its arguments. Keying on callee names alone
|
|
50
|
+
// reported `companyYearTotalsJson` as a re-implementation of
|
|
51
|
+
// `crewSummaryToJson` because both read `Number -> Number -> Number ->
|
|
52
|
+
// String`; the arguments are what told the two apart.
|
|
53
|
+
const signatureOf = (unit) => {
|
|
54
|
+
const file = fileOf.get(unit.file);
|
|
55
|
+
if (file === undefined)
|
|
56
|
+
return undefined;
|
|
57
|
+
const sites = file.facts.callSites.filter((site) => site.start >= unit.start && site.end <= unit.end);
|
|
58
|
+
// The pairing only holds when every call has its span.
|
|
59
|
+
if (sites.length !== unit.calls.length)
|
|
60
|
+
return undefined;
|
|
61
|
+
// The resolved callee, which is a fact. What each call READS is evidence --
|
|
62
|
+
// `callGraphEvidence` carries it -- not part of the key: collapsing it here
|
|
63
|
+
// decided whether two calls had "the same inputs", which is the question
|
|
64
|
+
// (docs/rule-coupling.md).
|
|
65
|
+
const keys = sites.map((site, index) => {
|
|
66
|
+
void site;
|
|
67
|
+
return unit.calls[index] ?? "";
|
|
68
|
+
});
|
|
69
|
+
return { keys, sites, text: file.text };
|
|
70
|
+
};
|
|
71
|
+
// A declaration whose WHOLE body is its call sequence. That is the thing that
|
|
72
|
+
// can be called instead of inlined; a longer declaration is not a primitive.
|
|
73
|
+
const bySequence = new Map();
|
|
74
|
+
for (const unit of workspace.units) {
|
|
75
|
+
if (unit.kind !== "function" || unit.calls.length < minCalls)
|
|
76
|
+
continue;
|
|
77
|
+
const signature = signatureOf(unit);
|
|
78
|
+
if (signature === undefined)
|
|
79
|
+
continue;
|
|
80
|
+
const key = signature.keys.join("\u0000");
|
|
81
|
+
if (!bySequence.has(key))
|
|
82
|
+
bySequence.set(key, unit);
|
|
83
|
+
}
|
|
84
|
+
if (bySequence.size === 0) {
|
|
85
|
+
return {
|
|
86
|
+
plans: [],
|
|
87
|
+
read: () => outcome([], [
|
|
88
|
+
"no function's whole body is a run of " + minCalls + " or more resolved calls",
|
|
89
|
+
]),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
// Every pair: a run of calls in one declaration matching the whole body of
|
|
93
|
+
// another. This is candidate generation and nothing more.
|
|
94
|
+
const pairs = [];
|
|
95
|
+
for (const unit of workspace.units) {
|
|
96
|
+
if (unit.kind !== "function" || unit.calls.length < minCalls)
|
|
97
|
+
continue;
|
|
98
|
+
if (!inScope(scope, unit.file))
|
|
99
|
+
continue;
|
|
100
|
+
const signature = signatureOf(unit);
|
|
101
|
+
if (signature === undefined)
|
|
102
|
+
continue;
|
|
103
|
+
for (let start = 0; start + minCalls <= unit.calls.length; start += 1) {
|
|
104
|
+
const length = unit.calls.length - start;
|
|
105
|
+
const helper = bySequence.get(signature.keys.slice(start, start + length).join("\u0000"));
|
|
106
|
+
if (helper === undefined || helper === unit)
|
|
107
|
+
continue;
|
|
108
|
+
// A function that already calls the helper is not inlining it.
|
|
109
|
+
if (unit.calls.includes(helper.file + "#" + helper.name))
|
|
110
|
+
continue;
|
|
111
|
+
pairs.push({ unit, helper, start, length });
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (pairs.length === 0) {
|
|
116
|
+
return {
|
|
117
|
+
plans: [],
|
|
118
|
+
read: () => outcome([], [
|
|
119
|
+
bySequence.size +
|
|
120
|
+
" declaration(s) whose whole body is a run of calls; no two share one",
|
|
121
|
+
]),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
// A bounded fan-out: one request per pair is the cache's requirement, and a
|
|
125
|
+
// pathological repository should cost a budget rather than an afternoon.
|
|
126
|
+
const atoms = yield* Atoms;
|
|
127
|
+
const planned = yield* Effect.forEach(pairs.slice(0, maxPairs), (pair) => Effect.gen(function* () {
|
|
128
|
+
const body = callGraphEvidence(pair.unit, workspace);
|
|
129
|
+
const helper = callGraphEvidence(pair.helper, workspace);
|
|
130
|
+
const id = yield* atoms.add({ declared: body, existing: helper, length: pair.length });
|
|
131
|
+
const plan = {
|
|
132
|
+
ruleId: RULE_ID,
|
|
133
|
+
subject: pair.unit.name + " (" + pair.unit.file + ")",
|
|
134
|
+
concerns: [pair.unit.file, pair.helper.file],
|
|
135
|
+
atoms: [id],
|
|
136
|
+
violations: { same: ["same_operation"] },
|
|
137
|
+
decisions: {
|
|
138
|
+
same: Decision.classify({
|
|
139
|
+
instructions: [
|
|
140
|
+
`Is \`atoms[${id}].declared\` the same operation as \`atoms[${id}].existing\`?`,
|
|
141
|
+
"Each side lists the calls it makes, resolved to the declaration each reaches, and the inputs each call reads.",
|
|
142
|
+
"Answer `same_operation` when the declared one could be replaced by a call to the existing one with no change in behaviour: the same work on the same inputs, in the same order.",
|
|
143
|
+
"Answer `different_inputs` when the two read different things, so they are two operations that happen to compose the same helpers.",
|
|
144
|
+
"Answer `different_order` when they call the same things in a different order, or interleave them differently.",
|
|
145
|
+
"Answer `different_work` when one does something the other does not.",
|
|
146
|
+
].join("\n"),
|
|
147
|
+
criteria: {
|
|
148
|
+
same_operation: "The same work on the same inputs. One can call the other.",
|
|
149
|
+
different_inputs: "Same helpers, different inputs. Two operations, not one.",
|
|
150
|
+
different_order: "Same calls, different order or interleaving.",
|
|
151
|
+
different_work: "They do not do the same thing.",
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
},
|
|
155
|
+
read: (answers) => answers,
|
|
156
|
+
};
|
|
157
|
+
return { pair, id, plan };
|
|
158
|
+
}), { concurrency: "unbounded" });
|
|
159
|
+
return {
|
|
160
|
+
plans: planned.map((entry) => entry.plan),
|
|
161
|
+
read: (answers) => {
|
|
162
|
+
const verdicts = verdictsOf(answers);
|
|
163
|
+
const diagnostics = [];
|
|
164
|
+
const drops = [];
|
|
165
|
+
planned.forEach((entry, index) => {
|
|
166
|
+
const { pair } = entry;
|
|
167
|
+
const subject = pair.unit.name + " (" + pair.unit.file + ")";
|
|
168
|
+
const answer = verdicts[index];
|
|
169
|
+
const same = answer === undefined ? undefined : answer["same"];
|
|
170
|
+
if (same === undefined || !("label" in same)) {
|
|
171
|
+
// No judgement: report the fact unverified. A high-recall candidate
|
|
172
|
+
// with no verdict is still a candidate a reader may want.
|
|
173
|
+
diagnostics.push(findingFor(report, pair, workspace, "no judgement was available", "no judgement was available"));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (same.label !== "same_operation") {
|
|
177
|
+
drops.push({
|
|
178
|
+
ruleId: RULE_ID,
|
|
179
|
+
subject,
|
|
180
|
+
stage: "declined",
|
|
181
|
+
reason: "the model read them as " + same.label.replace(/_/g, " "),
|
|
182
|
+
});
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const quality = qualityOf({
|
|
186
|
+
score: probabilityOfLabel(same),
|
|
187
|
+
margin: marginOfAnswer(same),
|
|
188
|
+
confidence: same.confidence,
|
|
189
|
+
});
|
|
190
|
+
if (quality.quality === "drop") {
|
|
191
|
+
drops.push({ ruleId: RULE_ID, subject, stage: "gated", reason: quality.reason });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
diagnostics.push(findingFor(report, pair, workspace, quality.quality === "act" ? undefined : quality.reason, "no judgement was available"));
|
|
195
|
+
});
|
|
196
|
+
return outcome(diagnostics, [], drops);
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}),
|
|
200
|
+
};
|
|
201
|
+
/** The probability the answer gave its own label. */
|
|
202
|
+
const probabilityOfLabel = (answer) => answer.probabilities?.[answer.label] ?? 0.7;
|
|
203
|
+
/**
|
|
204
|
+
* One declaration as a call graph, which is the state the question needs.
|
|
205
|
+
*
|
|
206
|
+
* `calls` are resolved to `file#name`; `inputs` are each call as written, which is
|
|
207
|
+
* EVIDENCE rather than the verdict. The model sees that two functions call the
|
|
208
|
+
* same three helpers and read different fields, and decides; a collapsed key
|
|
209
|
+
* alone used to decide it.
|
|
210
|
+
*/
|
|
211
|
+
const callGraphEvidence = (unit, workspace) => {
|
|
212
|
+
const file = workspace.files.find((candidate) => candidate.path === unit.file);
|
|
213
|
+
const sites = file === undefined
|
|
214
|
+
? []
|
|
215
|
+
: file.facts.callSites.filter((site) => site.start >= unit.start && site.end <= unit.end);
|
|
216
|
+
const inputs = sites.map((site) => file === undefined ? "()" : file.text.slice(site.start, Math.min(site.end, site.start + 120)));
|
|
217
|
+
return {
|
|
218
|
+
symbol: unit.name,
|
|
219
|
+
path: unit.file,
|
|
220
|
+
line: unit.location.line,
|
|
221
|
+
calls: unit.calls.map(stripFile),
|
|
222
|
+
inputs,
|
|
223
|
+
source: unit.text.slice(0, policy.evidence.maxSourceChars),
|
|
224
|
+
documented: unit.doc !== undefined,
|
|
225
|
+
doc: unit.doc?.slice(0, policy.evidence.maxDocChars) ?? null,
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
/** The finding for one candidate: verified, or the fact with the reason it is not. */
|
|
229
|
+
const findingFor = (report, pair, workspace, unverifiedReason, fallbackReason) => {
|
|
230
|
+
const { unit, helper, start, length } = pair;
|
|
231
|
+
const file = workspace.files.find((candidate) => candidate.path === unit.file);
|
|
232
|
+
const sites = file === undefined
|
|
233
|
+
? []
|
|
234
|
+
: file.facts.callSites.filter((site) => site.start >= unit.start && site.end <= unit.end);
|
|
235
|
+
const first = sites[start];
|
|
236
|
+
const starts = file === undefined ? [] : lineStarts(file.text);
|
|
237
|
+
const edits = [
|
|
238
|
+
{
|
|
239
|
+
file: unit.file,
|
|
240
|
+
line: first === undefined ? unit.location.line : lineAt(starts, first.start),
|
|
241
|
+
column: 1,
|
|
242
|
+
instruction: "replace these " + length + " call(s) with `" + helper.name + "` from " + helper.file,
|
|
243
|
+
},
|
|
244
|
+
];
|
|
245
|
+
if (helper.file !== unit.file) {
|
|
246
|
+
edits.push({
|
|
247
|
+
file: unit.file,
|
|
248
|
+
line: 1,
|
|
249
|
+
column: 1,
|
|
250
|
+
instruction: "import `" + helper.name + "` from `" + helper.file + "`",
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const verified = unverifiedReason === undefined;
|
|
254
|
+
return report({
|
|
255
|
+
about: unit,
|
|
256
|
+
messageId: "inlined_helper",
|
|
257
|
+
data: {
|
|
258
|
+
name: unit.name,
|
|
259
|
+
helper: helper.name,
|
|
260
|
+
helperFile: helper.file,
|
|
261
|
+
count: length,
|
|
262
|
+
calls: unit.calls
|
|
263
|
+
.slice(start, start + length)
|
|
264
|
+
.map(stripFile)
|
|
265
|
+
.join(" -> "),
|
|
266
|
+
unverified: verified ? "" : " Not verified: " + (unverifiedReason ?? fallbackReason) + ".",
|
|
267
|
+
},
|
|
268
|
+
helpId: "inlined_helper_help",
|
|
269
|
+
identity: [RULE_ID, unit.file, unit.name, helper.file, helper.name].join("\u0000"),
|
|
270
|
+
judged: verified,
|
|
271
|
+
severity: "info",
|
|
272
|
+
repair: {
|
|
273
|
+
operation: "replace",
|
|
274
|
+
keep: helper.location,
|
|
275
|
+
remove: [],
|
|
276
|
+
cascade: edits,
|
|
277
|
+
complete: true,
|
|
278
|
+
settled: verified
|
|
279
|
+
? "the model read the two call graphs as the same operation"
|
|
280
|
+
: "the call graphs match, so this is a candidate rather than a verdict",
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
};
|