@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,218 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Decision } from "effect/ai";
|
|
3
|
+
import { Atoms } from "../atoms.js";
|
|
4
|
+
import { describeOperation, permitted, settle } from "../operation.js";
|
|
5
|
+
import { policy } from "../policy.js";
|
|
6
|
+
import { expanded, namePairs, rankSpeculative } from "../candidates.js";
|
|
7
|
+
import { clusterOf, nameList } from "../cluster.js";
|
|
8
|
+
import { budgetNote, declined, marginOfAnswer, outcome, } from "../rule.js";
|
|
9
|
+
import { panelFromAnswers, planClusters, readClusters } from "./cluster-verdict.js";
|
|
10
|
+
import { layersFrom } from "../architecture.js";
|
|
11
|
+
import { nameVocabulary } from "../vocabulary.js";
|
|
12
|
+
/**
|
|
13
|
+
* The question naming-drift should always have been asking.
|
|
14
|
+
*
|
|
15
|
+
* It asks whether two NAMES denote one concept -- a question about words -- not
|
|
16
|
+
* whether one declaration is a redundant copy of the other, which is a question
|
|
17
|
+
* about bodies and is correctly answered "no" for two different things that
|
|
18
|
+
* merely share a name root. The state carries the expanded words for both, so
|
|
19
|
+
* the model compares meanings rather than strings, and `same_words` reports the
|
|
20
|
+
* case where only the spelling differs.
|
|
21
|
+
*/
|
|
22
|
+
const nameQuestionnaire = ({ cluster, described, workspace, }) => Effect.gen(function* () {
|
|
23
|
+
const atoms = yield* Atoms;
|
|
24
|
+
const operations = permitted(workspace, cluster.members);
|
|
25
|
+
const left = described[0];
|
|
26
|
+
const right = described[1];
|
|
27
|
+
const wordsOf = (unit) => (unit === undefined ? [] : expanded(unit.name));
|
|
28
|
+
const leftWords = wordsOf(left);
|
|
29
|
+
const rightWords = wordsOf(right);
|
|
30
|
+
const sameWords = leftWords.join(" ") === rightWords.join(" ");
|
|
31
|
+
const leftId = yield* atoms.add({
|
|
32
|
+
symbol: left?.name ?? null,
|
|
33
|
+
words: leftWords,
|
|
34
|
+
kind: left?.kind ?? null,
|
|
35
|
+
path: left?.file ?? null,
|
|
36
|
+
});
|
|
37
|
+
const rightId = yield* atoms.add({
|
|
38
|
+
symbol: right?.name ?? null,
|
|
39
|
+
words: rightWords,
|
|
40
|
+
kind: right?.kind ?? null,
|
|
41
|
+
path: right?.file ?? null,
|
|
42
|
+
});
|
|
43
|
+
const factsId = yield* atoms.add({ same_words: sameWords });
|
|
44
|
+
return {
|
|
45
|
+
atoms: [leftId, rightId, factsId],
|
|
46
|
+
// The Noul is the violation: its probability IS P(one concept), so the
|
|
47
|
+
// violating set is empty and calibration reduces it.
|
|
48
|
+
violations: { one_concept: [] },
|
|
49
|
+
decisions: {
|
|
50
|
+
one_concept: Decision.probability({
|
|
51
|
+
instructions: "Do `atoms[" + leftId + "].symbol` and `atoms[" + rightId + "].symbol` denote the same concept? Compare `atoms[" + leftId + "].words` and `atoms[" + rightId + "].words` word by word. Two names for one concept are a spelling problem; two names for two different things are not, however similar they read.",
|
|
52
|
+
criteria: nameVocabulary.oneConcept,
|
|
53
|
+
}),
|
|
54
|
+
verdict: Decision.classify({
|
|
55
|
+
instructions: "What should happen to these two names? Compare `atoms[" + leftId + "].symbol` and `atoms[" + rightId + "].symbol`. `atoms[" + leftId + "].words` and `atoms[" + rightId + "].words` are what each name means; `atoms[" + factsId + "].same_words` is true when only the spelling differs. Choose `no_issue` when the names denote two different concepts.",
|
|
56
|
+
criteria: {
|
|
57
|
+
same_use_left: nameVocabulary.standardize(left?.name ?? "left"),
|
|
58
|
+
same_use_right: `One concept, two spellings. Standardize on \`${right?.name ?? "right"}\`.`,
|
|
59
|
+
no_issue: "Two different concepts. Both names are correct. Change nothing.",
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
// The model's own read, over the operations the graph permits. A merge
|
|
63
|
+
// across a package boundary is not among them.
|
|
64
|
+
operation: Decision.classify({
|
|
65
|
+
instructions: [
|
|
66
|
+
"What should happen to these two names?",
|
|
67
|
+
"Answer with the one that fits what they are and where they live.",
|
|
68
|
+
"Choose `no_issue` when nothing should change.",
|
|
69
|
+
].join("\n"),
|
|
70
|
+
criteria: {
|
|
71
|
+
...Object.fromEntries(operations.map((operation) => [operation, describeOperation(operation)])),
|
|
72
|
+
no_issue: "Leave both names as they are.",
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
read: (answers) => {
|
|
77
|
+
const verdict = answers["verdict"];
|
|
78
|
+
if (verdict === undefined || !("label" in verdict))
|
|
79
|
+
return undefined;
|
|
80
|
+
const oneConcept = answers["one_concept"];
|
|
81
|
+
const score = oneConcept !== undefined && "probability" in oneConcept
|
|
82
|
+
? oneConcept.probability
|
|
83
|
+
: (verdict.confidence ?? 1);
|
|
84
|
+
const margin = marginOfAnswer(verdict);
|
|
85
|
+
const confidence = verdict.confidence ?? 1;
|
|
86
|
+
// The table here is one line, because a name pair has one repair: two
|
|
87
|
+
// spellings of one concept standardize on one of them. The model's own read
|
|
88
|
+
// is the second estimator, and the graph decides what it may propose.
|
|
89
|
+
const operationAnswer = answers["operation"];
|
|
90
|
+
const proposed = operationAnswer !== undefined &&
|
|
91
|
+
"label" in operationAnswer &&
|
|
92
|
+
(operationAnswer.label === "merge" || operationAnswer.label === "move")
|
|
93
|
+
? operationAnswer.label
|
|
94
|
+
: undefined;
|
|
95
|
+
const derived = score >= policy.decision.gates.probabilityFloor ? "merge" : undefined;
|
|
96
|
+
const settled = settle({
|
|
97
|
+
derived,
|
|
98
|
+
proposed,
|
|
99
|
+
margin: operationAnswer !== undefined && "label" in operationAnswer ? marginOfAnswer(operationAnswer) : 1,
|
|
100
|
+
confidence: operationAnswer !== undefined && "confidence" in operationAnswer
|
|
101
|
+
? operationAnswer.confidence
|
|
102
|
+
: undefined,
|
|
103
|
+
});
|
|
104
|
+
if (declined(verdict.label)) {
|
|
105
|
+
return {
|
|
106
|
+
keep: undefined,
|
|
107
|
+
confidence,
|
|
108
|
+
score,
|
|
109
|
+
redundancy: score,
|
|
110
|
+
margin,
|
|
111
|
+
derived,
|
|
112
|
+
panel: panelFromAnswers(answers, { derived, agreement: "drop" }),
|
|
113
|
+
operation: undefined,
|
|
114
|
+
agreement: "drop",
|
|
115
|
+
settled: "the model said " + verdict.label,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
keep: verdict.label === "same_use_right" ? 1 : 0,
|
|
120
|
+
confidence,
|
|
121
|
+
score,
|
|
122
|
+
// Names have no consequence axis: whether two spellings matter is what
|
|
123
|
+
// `one_concept` already asks.
|
|
124
|
+
redundancy: score,
|
|
125
|
+
margin,
|
|
126
|
+
derived,
|
|
127
|
+
panel: panelFromAnswers(answers, {
|
|
128
|
+
operation: settled.operation,
|
|
129
|
+
derived,
|
|
130
|
+
agreement: settled.quality,
|
|
131
|
+
}),
|
|
132
|
+
operation: settled.operation,
|
|
133
|
+
agreement: settled.quality,
|
|
134
|
+
settled: settled.reason,
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
const spec = {
|
|
140
|
+
ruleId: "joggle/naming-drift",
|
|
141
|
+
severity: "warn",
|
|
142
|
+
move: "contract",
|
|
143
|
+
onUnavailable: "propagate",
|
|
144
|
+
questionnaire: nameQuestionnaire,
|
|
145
|
+
nameOf: (cluster) => {
|
|
146
|
+
const names = [...new Set(cluster.members.map((member) => member.name))];
|
|
147
|
+
return names.length === 1
|
|
148
|
+
? `\`${names[0] ?? "?"}\` is spelled ${cluster.members.length} ways`
|
|
149
|
+
: `${names.join(", ")} may be one concept`;
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Pairs of names in one head-noun family, from the candidate engine.
|
|
154
|
+
*
|
|
155
|
+
* Exported names only, and the number is why: on one 2,456-file application
|
|
156
|
+
* removing this filter takes the candidate set from 23 to 77, three times the
|
|
157
|
+
* cost, and the recall it buys is unmeasured. The argument FOR removing it is
|
|
158
|
+
* real -- an agent greps a file-local name as readily as an exported one -- but
|
|
159
|
+
* the two duplications joggle found in a 110-file library were both file-local,
|
|
160
|
+
* and those were found by the near-duplicate rule, which has no such filter.
|
|
161
|
+
*/
|
|
162
|
+
const find = (workspace, scope) => {
|
|
163
|
+
const { minScore, minSharedWords } = policy.namingDrift;
|
|
164
|
+
return namePairs({
|
|
165
|
+
units: workspace.units,
|
|
166
|
+
minScore,
|
|
167
|
+
minSharedWords,
|
|
168
|
+
scope,
|
|
169
|
+
exportedOnly: true,
|
|
170
|
+
}).map((pair) => clusterOf([pair.left, pair.right], false, pair.score));
|
|
171
|
+
};
|
|
172
|
+
export const namingDrift = {
|
|
173
|
+
id: spec.ruleId,
|
|
174
|
+
severity: spec.severity,
|
|
175
|
+
description: "Two spellings of one concept across files.",
|
|
176
|
+
judged: true,
|
|
177
|
+
onUnavailable: spec.onUnavailable,
|
|
178
|
+
plan: Effect.fn("joggle/naming-drift")(function* (workspace, scope, context) {
|
|
179
|
+
const clusters = find(workspace, scope);
|
|
180
|
+
if (clusters.length === 0) {
|
|
181
|
+
return {
|
|
182
|
+
plans: [],
|
|
183
|
+
read: () => outcome([], ["no two exported names were near enough to be worth judging"]),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const budget = policy.namingDrift.maxClusters;
|
|
187
|
+
// Name pairs are a guess, so the budget is filled by a relevance judgement
|
|
188
|
+
// over a deterministic window rather than by a global sort-and-slice.
|
|
189
|
+
const ordered = yield* rankSpeculative({
|
|
190
|
+
budget,
|
|
191
|
+
items: clusters.map((cluster) => ({
|
|
192
|
+
item: cluster,
|
|
193
|
+
strength: cluster.overlap,
|
|
194
|
+
evidence: {
|
|
195
|
+
names: cluster.members.slice(0, 2).map((member) => member.name),
|
|
196
|
+
files: [...new Set(cluster.members.map((member) => member.file))].slice(0, 4),
|
|
197
|
+
signal: Math.round(cluster.overlap * 100) + "% shared name words",
|
|
198
|
+
},
|
|
199
|
+
})),
|
|
200
|
+
});
|
|
201
|
+
const phase = yield* planClusters(spec, ordered.slice(0, budget), scope, workspace);
|
|
202
|
+
const layers = layersFrom(context.config);
|
|
203
|
+
const unjudged = ordered.slice(budget);
|
|
204
|
+
const notes = budgetNote({
|
|
205
|
+
unitKind: "pairs",
|
|
206
|
+
judged: budget,
|
|
207
|
+
candidates: clusters.length,
|
|
208
|
+
sample: unjudged.slice(0, 3).map((cluster) => nameList({ cluster })),
|
|
209
|
+
});
|
|
210
|
+
return {
|
|
211
|
+
plans: phase.planned.map((entry) => entry.plan),
|
|
212
|
+
read: (answers) => {
|
|
213
|
+
const judged = readClusters(spec, workspace, phase, layers, answers);
|
|
214
|
+
return outcome(judged.diagnostics, notes, judged.drops);
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Decision } from "effect/ai";
|
|
3
|
+
import { Atoms } from "../atoms.js";
|
|
4
|
+
import { lineAt } from "../cascade.js";
|
|
5
|
+
import {} from "../plans.js";
|
|
6
|
+
import { verdictsOf } from "../plans.js";
|
|
7
|
+
import { locator, messages, reporter } from "../reporting.js";
|
|
8
|
+
import { verdictOf } from "../verdict.js";
|
|
9
|
+
import { outcome, qualityOf, } from "../rule.js";
|
|
10
|
+
import { lineStarts } from "../workspace.js";
|
|
11
|
+
const RULE_ID = "joggle/nullability-drift";
|
|
12
|
+
/** Which labels mean this rule is violated -- the read and the calibration share it. */
|
|
13
|
+
const VIOLATIONS = { verdict: ["drift"] };
|
|
14
|
+
// One column, two nullabilities.
|
|
15
|
+
//
|
|
16
|
+
// `field-type-drift` compares two TypeScript declarations. This compares a type
|
|
17
|
+
// whose nullability comes from somewhere else: the database. A column the
|
|
18
|
+
// migration leaves nullable and a schema that requires it is a decode that fails
|
|
19
|
+
// the first time real data arrives.
|
|
20
|
+
//
|
|
21
|
+
// The mapping is explicit and OPT-IN, because it is not derivable: the row schema
|
|
22
|
+
// reads `payroll_crew.shakti_user_id` into a field called `person_id`, an alias in
|
|
23
|
+
// the query. A repository annotates the field with
|
|
24
|
+
// `.annotate({ sourceColumn: 'table.column' })`.
|
|
25
|
+
//
|
|
26
|
+
// Both halves -- the migration's `.notNullable()` chain and the schema's
|
|
27
|
+
// `Schema.NullOr` -- are specifications read from the source, so the DISAGREEMENT
|
|
28
|
+
// is a fact. Whether it is a defect, or a null that is data the adapter handles,
|
|
29
|
+
// is the judgement, and it is the one the help text used to hand to the reader.
|
|
30
|
+
export const nullabilityDrift = {
|
|
31
|
+
id: RULE_ID,
|
|
32
|
+
severity: "warn",
|
|
33
|
+
description: "A column nullable in the database and required in a schema, or the reverse.",
|
|
34
|
+
judged: true,
|
|
35
|
+
move: "expand",
|
|
36
|
+
onUnavailable: "report",
|
|
37
|
+
messages: messages({
|
|
38
|
+
nullable_mismatch: "`{{source}}` is {{columnState}} in the migration and `{{field}}` is {{schemaState}} in the schema, so the database can hold a value the schema rejects{{tail}}.",
|
|
39
|
+
nullable_mismatch_help: "The column is declared at {{columnAt}}. Make the schema agree with the migration (or the migration with the schema), or, if a null here is data rather than drift, say what the adapter does with it and pin the decision.{{unverified}}",
|
|
40
|
+
}),
|
|
41
|
+
plan: Effect.fn("joggle/nullability-drift")(function* (workspace, scope) {
|
|
42
|
+
const report = reporter(nullabilityDrift, locator(workspace));
|
|
43
|
+
// Every column a migration declares, by `table.column`. Later migrations win:
|
|
44
|
+
// they are timestamp-named, so path order is chronological order.
|
|
45
|
+
const columns = new Map();
|
|
46
|
+
const files = [...workspace.files].sort((left, right) => left.path.localeCompare(right.path));
|
|
47
|
+
for (const file of files) {
|
|
48
|
+
const starts = lineStarts(file.text);
|
|
49
|
+
for (const column of file.facts.columns) {
|
|
50
|
+
columns.set(column.table + "." + column.column, {
|
|
51
|
+
nullable: column.nullable,
|
|
52
|
+
file: file.path,
|
|
53
|
+
line: lineAt(starts, column.start),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const candidates = [];
|
|
58
|
+
let annotated = 0;
|
|
59
|
+
for (const file of workspace.files) {
|
|
60
|
+
const starts = lineStarts(file.text);
|
|
61
|
+
for (const object of file.facts.objects) {
|
|
62
|
+
if (!object.declared)
|
|
63
|
+
continue;
|
|
64
|
+
for (const [field, source] of Object.entries(object.sources)) {
|
|
65
|
+
const column = columns.get(source);
|
|
66
|
+
if (column === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
annotated += 1;
|
|
69
|
+
if (scope.changed !== undefined && !scope.changed.has(file.path) && !scope.changed.has(column.file)) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const schemaNullable = object.nullable.includes(field);
|
|
73
|
+
if (schemaNullable === column.nullable)
|
|
74
|
+
continue;
|
|
75
|
+
candidates.push({
|
|
76
|
+
source,
|
|
77
|
+
field,
|
|
78
|
+
schemaNullable,
|
|
79
|
+
column,
|
|
80
|
+
schema: { file: file.path, line: lineAt(starts, object.start) },
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (annotated === 0) {
|
|
86
|
+
return {
|
|
87
|
+
plans: [],
|
|
88
|
+
read: () => outcome([], [
|
|
89
|
+
"no schema field names its source column, so there was nothing to compare -- annotate with .annotate({ sourceColumn: 'table.column' }) to opt in",
|
|
90
|
+
]),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (candidates.length === 0) {
|
|
94
|
+
return {
|
|
95
|
+
plans: [],
|
|
96
|
+
read: () => outcome([], [
|
|
97
|
+
annotated + " schema field(s) named a source column; all agree with the migration on nullability",
|
|
98
|
+
]),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const atoms = yield* Atoms;
|
|
102
|
+
const planned = yield* Effect.forEach(candidates, (candidate) => Effect.gen(function* () {
|
|
103
|
+
const id = yield* atoms.add({
|
|
104
|
+
source: candidate.source,
|
|
105
|
+
field: candidate.field,
|
|
106
|
+
schemaNullable: candidate.schemaNullable,
|
|
107
|
+
columnNullable: candidate.column.nullable,
|
|
108
|
+
schema: candidate.schema,
|
|
109
|
+
column: { file: candidate.column.file, line: candidate.column.line },
|
|
110
|
+
});
|
|
111
|
+
const plan = {
|
|
112
|
+
ruleId: RULE_ID,
|
|
113
|
+
subject: candidate.source + " / " + candidate.field,
|
|
114
|
+
concerns: [candidate.schema.file, candidate.column.file],
|
|
115
|
+
atoms: [id],
|
|
116
|
+
violations: VIOLATIONS,
|
|
117
|
+
decisions: {
|
|
118
|
+
verdict: Decision.classify({
|
|
119
|
+
instructions: [
|
|
120
|
+
`\`atoms[${id}].source\` is \`${candidate.source}\`. The migration declares it ${candidate.column.nullable ? "nullable" : "required"} (\`atoms[${id}].columnNullable\`), and the schema field \`atoms[${id}].field\` is ${candidate.schemaNullable ? "nullable" : "required"} (\`atoms[${id}].schemaNullable\`).`,
|
|
121
|
+
"Is that disagreement a defect, or is a null here data that the adapter handles?",
|
|
122
|
+
"Answer `drift` when the schema should agree with the migration: the database can hold a null the schema rejects, or the schema accepts a null the database cannot.",
|
|
123
|
+
"Answer `handled` when a null here is data rather than drift and the adapter does something deliberate with it.",
|
|
124
|
+
"Answer `mismapped` when the field does not in fact read that column, so the annotation is wrong.",
|
|
125
|
+
].join("\n"),
|
|
126
|
+
criteria: {
|
|
127
|
+
drift: "The two should agree. Make the schema (or the migration) match.",
|
|
128
|
+
handled: "A null is data, and the adapter handles it deliberately.",
|
|
129
|
+
mismapped: "The field does not read that column.",
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
},
|
|
133
|
+
read: (answers) => answers,
|
|
134
|
+
};
|
|
135
|
+
return { candidate, id, plan };
|
|
136
|
+
}), { concurrency: "unbounded" });
|
|
137
|
+
return {
|
|
138
|
+
plans: planned.map((value) => value.plan),
|
|
139
|
+
read: (answers) => {
|
|
140
|
+
const verdicts = verdictsOf(answers);
|
|
141
|
+
const diagnostics = [];
|
|
142
|
+
const drops = [];
|
|
143
|
+
planned.forEach((value, index) => {
|
|
144
|
+
const { candidate } = value;
|
|
145
|
+
const subject = candidate.source + " / " + candidate.field;
|
|
146
|
+
const answer = verdicts[index];
|
|
147
|
+
const verdict = verdictOf(answer?.["verdict"], VIOLATIONS.verdict);
|
|
148
|
+
if (verdict === undefined) {
|
|
149
|
+
diagnostics.push(findingFor({
|
|
150
|
+
report,
|
|
151
|
+
candidate,
|
|
152
|
+
confidence: undefined,
|
|
153
|
+
unverifiedReason: "no judgement was available",
|
|
154
|
+
}));
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (verdict.label !== "drift") {
|
|
158
|
+
drops.push({
|
|
159
|
+
ruleId: RULE_ID,
|
|
160
|
+
subject,
|
|
161
|
+
stage: "declined",
|
|
162
|
+
reason: verdict.label === "handled"
|
|
163
|
+
? "a null here is data the adapter handles"
|
|
164
|
+
: "the field does not read that column",
|
|
165
|
+
});
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const quality = qualityOf({
|
|
169
|
+
score: verdict.probability,
|
|
170
|
+
margin: verdict.margin,
|
|
171
|
+
confidence: verdict.confidence,
|
|
172
|
+
});
|
|
173
|
+
if (quality.quality === "drop") {
|
|
174
|
+
drops.push({
|
|
175
|
+
ruleId: RULE_ID,
|
|
176
|
+
subject,
|
|
177
|
+
stage: "gated",
|
|
178
|
+
reason: quality.reason,
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const review = quality.quality === "review";
|
|
183
|
+
diagnostics.push(findingFor({
|
|
184
|
+
report,
|
|
185
|
+
candidate,
|
|
186
|
+
confidence: verdict.confidence,
|
|
187
|
+
unverifiedReason: undefined,
|
|
188
|
+
review,
|
|
189
|
+
}));
|
|
190
|
+
});
|
|
191
|
+
return outcome(diagnostics, [
|
|
192
|
+
annotated +
|
|
193
|
+
" schema field(s) named a source column; " +
|
|
194
|
+
diagnostics.length +
|
|
195
|
+
" of " +
|
|
196
|
+
candidates.length +
|
|
197
|
+
" disagreement(s) reported",
|
|
198
|
+
], drops);
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}),
|
|
202
|
+
};
|
|
203
|
+
const findingFor = ({ report, candidate, confidence, unverifiedReason, review = false, }) => report({
|
|
204
|
+
about: { file: candidate.schema.file, line: candidate.schema.line, column: 1 },
|
|
205
|
+
messageId: "nullable_mismatch",
|
|
206
|
+
data: {
|
|
207
|
+
source: candidate.source,
|
|
208
|
+
field: candidate.field,
|
|
209
|
+
columnState: candidate.column.nullable ? "nullable" : "required",
|
|
210
|
+
schemaState: candidate.schemaNullable ? "nullable" : "required",
|
|
211
|
+
tail: candidate.column.nullable ? "" : ", or the schema accepts one the database cannot",
|
|
212
|
+
columnAt: candidate.column.file + ":" + String(candidate.column.line),
|
|
213
|
+
unverified: unverifiedReason === undefined ? "" : " Not verified: " + unverifiedReason + ".",
|
|
214
|
+
},
|
|
215
|
+
helpId: "nullable_mismatch_help",
|
|
216
|
+
identity: [RULE_ID, candidate.source, candidate.field].join("\u0000"),
|
|
217
|
+
judged: unverifiedReason === undefined,
|
|
218
|
+
confidence,
|
|
219
|
+
severity: review ? "info" : "warn",
|
|
220
|
+
});
|