@ontrails/regrade 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,170 @@
1
+ // Root package surface for `@ontrails/regrade`.
2
+ export {
3
+ literalRegradeTopo,
4
+ literalRegradeTrail,
5
+ } from './literal-transform.js';
6
+ export {
7
+ buildRegradeReport,
8
+ applyPreparedRegradeRun,
9
+ createTermRewriteClass,
10
+ createWardenTermRewriteClass,
11
+ loadWardenTermRewriteClasses,
12
+ prepareRegradeRun,
13
+ regradeReportOutput,
14
+ runRegrade,
15
+ selectRegradeClasses,
16
+ validatePreparedRegradeRun,
17
+ wardenTermRewriteClasses,
18
+ } from './downstream/report.js';
19
+ export {
20
+ deriveFileRenameCandidates,
21
+ runFileRenameRegrade,
22
+ } from './downstream/file-renames.js';
23
+ export { verifyDownstreamPackageSource } from './downstream/package-source.js';
24
+ /**
25
+ * Validate the package-source expectation shared by verifier and operator inputs.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * regradePackageSourceExpectationSchema.parse({
30
+ * kind: 'published',
31
+ * name: '@ontrails/core',
32
+ * version: '0.2.0',
33
+ * });
34
+ * ```
35
+ */
36
+ export { regradePackageSourceExpectationSchema } from './downstream/package-source-manifest.js';
37
+ export type {
38
+ RegradePackageSourceError,
39
+ RegradePackageSourceEvidence,
40
+ RegradePackageSourceExpectation,
41
+ } from './downstream/package-source.js';
42
+ export type {
43
+ FileRenameCandidate,
44
+ FileRenameRegradeRun,
45
+ } from './downstream/file-renames.js';
46
+ export {
47
+ cliAliasesExportRestructureClass,
48
+ createWardenExportRestructureClass,
49
+ exportRestructureClasses,
50
+ loadWardenRegradeClasses,
51
+ mcpTrailheadsExportRestructureClass,
52
+ } from './downstream/export-restructure.js';
53
+ export {
54
+ VOCABULARY_TRANSITION_RECORD_SCHEMA_VERSION,
55
+ buildVocabularyTransitionRecord,
56
+ deriveVocabularyFormProposals,
57
+ applyPreparedVocabularyRegradeRun,
58
+ prepareVocabularyRegradeRun,
59
+ readVocabularyTransitionRecord,
60
+ runVocabularyRegrade,
61
+ transitionRecordReportWithSummary,
62
+ vocabularyDispositionValues,
63
+ vocabularyRegradePlanSchema,
64
+ vocabularyRegradeRunOutput,
65
+ vocabularyTransitionRecordPath,
66
+ vocabularyTransitionRecordSchema,
67
+ writeVocabularyTransitionRecord,
68
+ } from './downstream/vocabulary.js';
69
+ export {
70
+ listVocabularyRegradePlansFromRegistry,
71
+ listVocabularyRegradeAuditPlansFromRegistry,
72
+ vocabularyRegradePlanFromTransition,
73
+ vocabularyRegradePlanForInput,
74
+ vocabularyRegradeTransitionForInput,
75
+ } from './downstream/vocabulary-registry.js';
76
+ export type {
77
+ RegradeApplySummary,
78
+ PreparedRegradeRun,
79
+ PreparedRegradeRunIdentity,
80
+ RegradeClass,
81
+ RegradeClassContext,
82
+ RegradeClassResult,
83
+ RegradeReport,
84
+ RegradeReportEntry,
85
+ RegradeReportEntrySelection,
86
+ RegradeReviewDetail,
87
+ RegradeReviewJudgment,
88
+ RegradeReviewSpan,
89
+ RegradeScanTargets,
90
+ RegradeSelection,
91
+ RegradeWardenClassSet,
92
+ } from './downstream/report.js';
93
+ export type {
94
+ RegradeScanDirectoryBucket,
95
+ RegradeScanExtensionBucket,
96
+ RegradeScanSummary,
97
+ } from './downstream/scan-summary.js';
98
+ export type {
99
+ VocabularyDisposition,
100
+ PreparedVocabularyRegradeRun,
101
+ VocabularyFileRename,
102
+ VocabularyFileRenameEvidence,
103
+ VocabularyFormProposal,
104
+ VocabularyOccurrence,
105
+ VocabularyOccurrenceSourceKind,
106
+ VocabularyPreserveInventoryEntry,
107
+ VocabularyPreserveRule,
108
+ VocabularyRegradePlan,
109
+ VocabularyRegradeScope,
110
+ VocabularyRegradeRun,
111
+ VocabularyScopePolicy,
112
+ VocabularyScopeTier,
113
+ VocabularyRunGate,
114
+ VocabularyRunLedger,
115
+ VocabularyRunReport,
116
+ VocabularyTransitionRecord,
117
+ VocabularyTransitionRecordEnvironment,
118
+ VocabularyTransitionRecordSummary,
119
+ VocabularyVerdict,
120
+ } from './downstream/vocabulary.js';
121
+ export {
122
+ createAstIdentifierRenameClass,
123
+ createAstRewriteClass,
124
+ createGovernedAstIdentifierRenameClasses,
125
+ } from './downstream/ast-rewrite.js';
126
+ export type {
127
+ AstIdentifierRenameClassOptions,
128
+ AstRewriteClassOptions,
129
+ AstRewriteContext,
130
+ AstRewriteMatch,
131
+ AstRewriteVisitResult,
132
+ } from './downstream/ast-rewrite.js';
133
+
134
+ /**
135
+ * Compact, canonical evidence for an applied Regrade run.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * const serialized = serializeRegradeHistoryReceipt(receipt);
140
+ * if (serialized.isErr()) throw serialized.error;
141
+ * ```
142
+ */
143
+ export {
144
+ REGRADE_HISTORY_RECEIPT_SCHEMA_VERSION,
145
+ canonicalRegradeJson,
146
+ regradeClassifiedStateHash,
147
+ regradeFormJudgmentSchema,
148
+ regradeHistoryReceiptSchema,
149
+ regradeReceiptContentHash,
150
+ regradeReceiptPlanContentHash,
151
+ regradeReceiptPlanSchema,
152
+ resolveRegradeHistoryReceipt,
153
+ serializeRegradeHistoryReceipt,
154
+ } from './history-receipt.js';
155
+ /**
156
+ * Type the authored and resolved sides of compact Regrade history.
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * const receipt: RegradeHistoryReceipt = parsed;
161
+ * ```
162
+ */
163
+ export type {
164
+ RegradeFormJudgment,
165
+ RegradeHistoryReceipt,
166
+ RegradeReceiptPlan,
167
+ RegradeReceiptPlanProvenance,
168
+ ResolvedRegradeHistoryReceipt,
169
+ ResolvedRegradeHistoryReceiptRun,
170
+ } from './history-receipt.js';
@@ -0,0 +1,124 @@
1
+ import { InternalError, Result, topo, trail } from '@ontrails/core';
2
+ import { z } from 'zod';
3
+
4
+ export const regradeTransformInput = z.object({
5
+ source: z.string().describe('Source text to transform'),
6
+ });
7
+
8
+ export const regradeTransformOutput = z.object({
9
+ changed: z.boolean().describe('Whether the transform changed the source'),
10
+ nextSource: z.string().describe('Transformed source text'),
11
+ notes: z.array(z.string()).describe('Tracer notes for the transform run'),
12
+ });
13
+
14
+ const childInput = z.object({
15
+ source: z.string(),
16
+ });
17
+
18
+ /**
19
+ * Parent input schema with its raw-to-implementation transform attached.
20
+ *
21
+ * Naming the transformed schema lets examples and tests reference both its raw
22
+ * pre-transform input (`z.input`) and its post-transform implementation input
23
+ * (`z.infer`) without restating either shape by hand.
24
+ */
25
+ const regradeTransformInputToChild = regradeTransformInput.transform(
26
+ ({ source }) => ({
27
+ child: { source },
28
+ })
29
+ );
30
+
31
+ /**
32
+ * Raw, pre-transform input accepted by {@link regradeTransformInputToChild}.
33
+ *
34
+ * Trail examples and `testExamples()` feed this shape through validation; the
35
+ * Zod transform then derives the implementation input.
36
+ */
37
+ type RegradeTransformRawInput = z.input<typeof regradeTransformInputToChild>;
38
+
39
+ /**
40
+ * Post-transform implementation input shape — the trail's inferred input type `I`.
41
+ *
42
+ * TRL-842: With a `.transform()` input schema, the trail's inferred input type
43
+ * `I` is the transform OUTPUT (the implementation input), while examples and
44
+ * `testExamples()` validate the raw pre-transform INPUT. Those two shapes are
45
+ * disjoint, so an authored example must carry raw input typed as the
46
+ * post-transform shape. A framework-level fix would thread a separate
47
+ * `z.input<>` raw-input type parameter through `TrailSpec`, `Trail`, and every
48
+ * `trail()` overload — a core-wide generics change beyond this tracer. Until
49
+ * then the divergence is captured by the two named types here and exercised by
50
+ * the runtime validation test in `__tests__/literal-transform.test.ts`.
51
+ */
52
+ type RegradeTransformImplementationInput = z.infer<
53
+ typeof regradeTransformInputToChild
54
+ >;
55
+
56
+ /**
57
+ * Raw example input, statically checked as valid pre-transform input. If the
58
+ * input schema changes so this literal is no longer valid raw input, source
59
+ * typecheck fails here instead of silently relying on the cast below.
60
+ */
61
+ const codeStringExampleInput: RegradeTransformRawInput = {
62
+ source: 'export const answer = 41;',
63
+ };
64
+
65
+ export const normalizeExportConstTrail = trail(
66
+ 'regrade.literal.normalize-export-const',
67
+ {
68
+ implementation: (input) => {
69
+ const nextSource = input.source.replaceAll('export const', 'export let');
70
+ return Result.ok({
71
+ changed: nextSource !== input.source,
72
+ nextSource,
73
+ notes:
74
+ nextSource === input.source
75
+ ? ['No export const declaration found.']
76
+ : ['Rewrote export const declarations to export let.'],
77
+ });
78
+ },
79
+ input: childInput,
80
+ output: regradeTransformOutput,
81
+ visibility: 'internal',
82
+ }
83
+ );
84
+
85
+ export const literalRegradeTrail = trail('regrade.literal.run', {
86
+ composes: [normalizeExportConstTrail],
87
+ examples: [
88
+ {
89
+ expected: {
90
+ changed: true,
91
+ nextSource: 'export let answer = 41;',
92
+ notes: ['Rewrote export const declarations to export let.'],
93
+ },
94
+ // TRL-842: author the raw pre-transform value (validated as
95
+ // RegradeTransformRawInput) and widen through `unknown` to the trail's
96
+ // inferred post-transform input type. The runtime still parses raw input;
97
+ // see RegradeTransformImplementationInput for why the two shapes diverge.
98
+ input:
99
+ codeStringExampleInput as unknown as RegradeTransformImplementationInput,
100
+ name: 'code-string fixture',
101
+ },
102
+ ],
103
+ implementation: async (input, ctx) => {
104
+ if (!ctx.compose) {
105
+ return Result.err(
106
+ new InternalError(
107
+ 'Literal Regrade tracer requires compose-capable execution.'
108
+ )
109
+ );
110
+ }
111
+ const implementationInput = input as RegradeTransformImplementationInput;
112
+ return await ctx.compose(
113
+ normalizeExportConstTrail,
114
+ implementationInput.child
115
+ );
116
+ },
117
+ input: regradeTransformInputToChild,
118
+ output: regradeTransformOutput,
119
+ });
120
+
121
+ export const literalRegradeTopo = topo('regrade-literal', {
122
+ literalRegradeTrail,
123
+ normalizeExportConstTrail,
124
+ });