fallow_types/output.rs
1//! Types that describe fallow's JSON output contract.
2//!
3//! Today the JSON serialization layer (`crates/cli/src/report/json.rs`) builds
4//! its output via `serde_json::json!` macros. The types defined here are the
5//! schema-side counterpart of that output: they document, with Rust's type
6//! system, the augmentations the JSON layer adds to each per-finding struct
7//! (the `actions` array on every finding, the optional `introduced` flag in
8//! audit-mode sub-results).
9//!
10//! The `schema-emit` binary derives `JsonSchema` for these types (gated by the
11//! `schema` cargo feature) so the public `docs/output-schema.json` stays in
12//! sync with the Rust source of truth. A future refactor will route the JSON
13//! emission path through these types directly, eliminating the drift class
14//! between the augmentation list here and the `serde_json::json!` builders.
15
16use serde::Serialize;
17
18/// A suggested action attached to a finding in the JSON output. Each finding
19/// carries an `actions` array; consumers (agents, IDE clients, CI bots) can
20/// dispatch on the `type` discriminant to choose the right remediation.
21///
22/// The discriminator is `type` (snake_case `type` field), the payload uses the
23/// matching kebab-case identifier per variant.
24///
25/// ## `auto_fixable` is per-finding, not per action type
26///
27/// Every action variant carries an `auto_fixable: bool` field. The value is
28/// evaluated PER FINDING, not per action type: the same action type may
29/// appear with `auto_fixable: true` on one finding and `auto_fixable: false`
30/// on another, depending on per-instance guards in the `fallow fix` applier.
31/// Agents that filter on `auto_fixable: true` must branch on the bool of
32/// each individual finding's action, not on the action `type` alone.
33///
34/// Current per-instance flips:
35///
36/// - `remove-catalog-entry` (`unused-catalog-entries`): `true` only when the
37/// finding's `hardcoded_consumers` array is empty. When a workspace
38/// package still pins a hardcoded version of the same package, `fallow fix`
39/// skips the entry to avoid breaking `pnpm install`, and the action is
40/// emitted with `auto_fixable: false`.
41/// - `remove-dependency` vs `move-dependency` (dependency findings): when the
42/// finding's `used_in_workspaces` array is non-empty, the primary action
43/// flips to `move-dependency` with `auto_fixable: false` (`fallow fix` will
44/// not remove a dependency that another workspace imports). On findings
45/// without cross-workspace consumers the action stays `remove-dependency`
46/// with `auto_fixable: true`.
47/// - `add-to-config` for `ignoreExports` (`duplicate-exports`): `true` when
48/// `fallow fix` can safely apply the action without further user setup.
49/// That is: a fallow config file exists on disk, OR no config exists AND
50/// the working directory is NOT inside a monorepo subpackage (in which
51/// case the applier creates `.fallowrc.json` from `fallow init`'s
52/// framework-aware scaffolding and layers the new rules on top).
53/// `false` inside a monorepo subpackage with no workspace-root config
54/// (the applier refuses to fragment per-package configs across the
55/// monorepo and points at the workspace root instead).
56/// - `update-catalog-reference` (`unresolved-catalog-references`): always
57/// `false` today (the catalog-switching applier is not wired in yet); the
58/// field is non-singleton so that future enablement does not require a
59/// schema change.
60///
61/// All `suppress-line` and `suppress-file` actions are uniformly
62/// `auto_fixable: false`. The field is non-singleton on the wire so that a
63/// future auto-applier (e.g. an LLM-driven suppression writer) can promote
64/// individual variants without a schema bump.
65#[derive(Debug, Clone, Serialize)]
66#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
67#[serde(untagged)]
68pub enum IssueAction {
69 /// A code-change fix the user can apply (auto-fixable by `fallow fix` for
70 /// some variants, manual for others).
71 Fix(FixAction),
72 /// Place a `// fallow-ignore-next-line ...` comment above the offending
73 /// line. Always manual.
74 SuppressLine(SuppressLineAction),
75 /// Place a `// fallow-ignore-file ...` comment at the top of the file.
76 /// Always manual.
77 SuppressFile(SuppressFileAction),
78 /// Add the offending finding to the fallow config (e.g.
79 /// `ignoreDependencies: ["lodash"]`). Auto-fixable for the array-shaped
80 /// `ignoreExports` variant when `fallow fix` can safely apply the
81 /// action (config file exists, or no config exists and the working
82 /// directory is not inside a monorepo subpackage); manual otherwise.
83 AddToConfig(AddToConfigAction),
84}
85
86/// A code-change fix. `type` is one of the kebab-case identifiers in
87/// [`FixActionType`].
88#[derive(Debug, Clone, Serialize)]
89#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
90pub struct FixAction {
91 /// Kebab-case identifier for the fix action.
92 #[serde(rename = "type")]
93 pub kind: FixActionType,
94 /// Whether `fallow fix` can apply this fix automatically. Evaluated PER
95 /// FINDING, not per action type: the same `type` may carry
96 /// `auto_fixable: true` on one finding and `auto_fixable: false` on
97 /// another when per-instance guards in the applier discriminate (e.g.
98 /// `remove-catalog-entry` flips on `hardcoded_consumers`, the primary
99 /// dependency action flips between `remove-dependency` /
100 /// `move-dependency` on `used_in_workspaces`). Filter on this bool of
101 /// each individual action, not on `type`. See the [`IssueAction`]
102 /// enum-level docs for the full list of per-instance flips.
103 pub auto_fixable: bool,
104 /// Human-readable description of the fix.
105 pub description: String,
106 /// Optional context note. Present on non-auto-fixable actions, and on
107 /// auto-fixable re-export findings to warn about public API surface.
108 #[serde(default, skip_serializing_if = "Option::is_none")]
109 pub note: Option<String>,
110 /// Only present on `update-catalog-reference` actions: catalogs in the
111 /// same workspace that DO declare the package, sorted lexicographically.
112 /// Lets agents pick the catalog to switch to without re-reading the
113 /// source.
114 #[serde(default, skip_serializing_if = "Option::is_none")]
115 pub available_in_catalogs: Option<Vec<String>>,
116 /// Only present on `update-catalog-reference` actions when exactly one
117 /// alternative catalog declares the package: the unambiguous switch
118 /// target. Lets deterministic (non-LLM) agents land the edit without
119 /// picking from a list. Absent when `available_in_catalogs` has zero
120 /// or more than one entry.
121 #[serde(default, skip_serializing_if = "Option::is_none")]
122 pub suggested_target: Option<String>,
123}
124
125/// Discriminant string for [`FixAction`]. Kebab-case per the JSON output
126/// contract.
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
128#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
129#[serde(rename_all = "kebab-case")]
130pub enum FixActionType {
131 /// Remove an export declaration from a source file.
132 RemoveExport,
133 /// Delete an entire unused file.
134 DeleteFile,
135 /// Remove an entry from `dependencies` / `devDependencies` in
136 /// `package.json`.
137 RemoveDependency,
138 /// Move an entry between `dependencies` and `devDependencies`.
139 MoveDependency,
140 /// Remove an enum member from a TypeScript enum.
141 RemoveEnumMember,
142 /// Remove a class member (method or property).
143 RemoveClassMember,
144 /// Resolve an unresolved import (manual).
145 ResolveImport,
146 /// Install a missing dependency.
147 InstallDependency,
148 /// Remove a duplicate export (the canonical action for
149 /// `duplicate-exports`).
150 RemoveDuplicate,
151 /// Move a production dependency to `devDependencies`
152 /// (used by type-only-dependency and test-only-dependency findings).
153 MoveToDev,
154 /// Break a circular dependency by refactoring imports.
155 RefactorCycle,
156 /// Break a re-export cycle by removing an `export * from` (or
157 /// `export { ... } from`) statement on any one member file. Re-export
158 /// cycles are structurally always bugs (chain propagation through the
159 /// loop is a no-op), so there is no auto-fix; the action is manual.
160 RefactorReExportCycle,
161 /// Resolve a boundary violation by refactoring the import.
162 RefactorBoundary,
163 /// Convert an import statement to a type-only import (used by
164 /// private-type-leak findings).
165 ExportType,
166 /// Remove an unused catalog entry from `pnpm-workspace.yaml`.
167 RemoveCatalogEntry,
168 /// Remove an empty named catalog group from `pnpm-workspace.yaml`.
169 RemoveEmptyCatalogGroup,
170 /// Update an existing `catalog:` reference in a workspace `package.json`
171 /// to point at a different (declared) catalog.
172 UpdateCatalogReference,
173 /// Add the missing entry to the referenced catalog.
174 AddCatalogEntry,
175 /// Remove the catalog reference from the workspace `package.json` and
176 /// replace it with a hardcoded version.
177 RemoveCatalogReference,
178 /// Remove an unused dependency override entry.
179 RemoveDependencyOverride,
180 /// Fix a misconfigured dependency override entry (unparsable key or empty
181 /// value).
182 FixDependencyOverride,
183 /// Replace a banned call or banned import flagged by a rule-pack rule
184 /// (manual; the rule's message usually names the sanctioned alternative).
185 ResolvePolicyViolation,
186 /// Move a server-only export out of a `"use client"` file into a
187 /// non-client module (manual; used by invalid-client-export findings).
188 MoveToServerModule,
189 /// Split a barrel that re-exports both client and server-only modules
190 /// into separate client and server barrels (manual; used by
191 /// mixed-client-server-barrel findings).
192 SplitMixedBarrel,
193 /// Hoist a misplaced `"use client"` / `"use server"` directive to the
194 /// leading prologue of the file (manual; used by misplaced-directive
195 /// findings).
196 HoistDirective,
197 /// Wire a server action to a project consumer or remove the unused action
198 /// export (manual; used by unused-server-action findings).
199 WireServerAction,
200 /// Add a provider for an injected key or remove the dead inject call
201 /// (manual; used by unprovided-inject findings).
202 ProvideInject,
203 /// Use a SvelteKit load-data key from the route UI or remove the unused
204 /// returned key (manual; used by unused-load-data-key findings).
205 UseLoadData,
206 /// Render a reachable component from project code or remove the component
207 /// (manual; used by unrendered-component findings).
208 RenderComponent,
209 /// Use a declared component prop or remove it from the component API
210 /// (manual; used by unused-component-prop findings).
211 UseComponentProp,
212 /// Emit a declared component event or remove it from the component API
213 /// (manual; used by unused-component-emit findings).
214 EmitComponentEvent,
215 /// Add or forward a Svelte custom-event listener, or remove the dispatch
216 /// (manual; used by unused-svelte-event findings).
217 WireSvelteEvent,
218 /// Resolve a Next.js App Router route collision by moving or merging one of
219 /// the files that own the same URL (manual; suppressing a guaranteed build
220 /// error is never the right fix, so this is the primary action).
221 ResolveRouteCollision,
222 /// Resolve a Next.js dynamic-segment name conflict by renaming the dynamic
223 /// segments at the conflicting position to a single consistent slug name
224 /// (manual).
225 ResolveDynamicSegmentNameConflict,
226}
227
228/// Inline-comment suppression for a single finding line.
229#[derive(Debug, Clone, Serialize)]
230#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
231pub struct SuppressLineAction {
232 /// Action type identifier.
233 #[serde(rename = "type")]
234 pub kind: SuppressLineKind,
235 /// Always false for suppress actions.
236 pub auto_fixable: bool,
237 /// Human-readable description of the suppression.
238 pub description: String,
239 /// The inline comment to place above the line (e.g.,
240 /// `// fallow-ignore-next-line unused-export`). When multiple
241 /// suppressible findings share the same path and line, this may contain a
242 /// comma-separated issue-kind list such as
243 /// `// fallow-ignore-next-line unused-export, complexity`.
244 pub comment: String,
245 /// Present on multi-location issue types (e.g., `duplicate_exports`) to
246 /// indicate the comment must be applied at each location.
247 #[serde(default, skip_serializing_if = "Option::is_none")]
248 pub scope: Option<SuppressLineScope>,
249}
250
251/// Singleton discriminant for [`SuppressLineAction`].
252#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
253#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
254#[serde(rename_all = "kebab-case")]
255pub enum SuppressLineKind {
256 /// `// fallow-ignore-next-line <kind>` directive.
257 SuppressLine,
258}
259
260/// Scope marker for line suppressions that span multiple locations.
261#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
262#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
263#[serde(rename_all = "kebab-case")]
264pub enum SuppressLineScope {
265 /// Apply the suppression comment at each location of the multi-location
266 /// finding (e.g., every `duplicate_exports` site).
267 PerLocation,
268}
269
270/// File-wide suppression placed at the top of the source file.
271#[derive(Debug, Clone, Serialize)]
272#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
273pub struct SuppressFileAction {
274 /// Action type identifier.
275 #[serde(rename = "type")]
276 pub kind: SuppressFileKind,
277 /// Always false for suppress actions.
278 pub auto_fixable: bool,
279 /// Human-readable description of the suppression.
280 pub description: String,
281 /// The file-level comment to place at the top of the file (e.g.,
282 /// `// fallow-ignore-file unused-file`).
283 pub comment: String,
284}
285
286/// Singleton discriminant for [`SuppressFileAction`].
287#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
288#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
289#[serde(rename_all = "kebab-case")]
290pub enum SuppressFileKind {
291 /// `// fallow-ignore-file <kind>` directive.
292 SuppressFile,
293}
294
295/// Edit a fallow config file (`.fallowrc.json`, `fallow.toml`, etc.) to
296/// add the offending value to an `ignore*` rule.
297#[derive(Debug, Clone, Serialize)]
298#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
299pub struct AddToConfigAction {
300 /// Action type identifier.
301 #[serde(rename = "type")]
302 pub kind: AddToConfigKind,
303 /// True when `fallow fix` can apply this config action automatically.
304 /// Evaluated PER FINDING, not per action type: `ignoreExports`
305 /// duplicate-export actions are auto-fixable when `fallow fix` can
306 /// safely write the rule, which today means EITHER a fallow config
307 /// file already exists OR no config exists and the working directory
308 /// is NOT inside a monorepo subpackage (in which case the applier
309 /// creates `.fallowrc.json` from `fallow init`'s framework-aware
310 /// scaffolding). The action is `false` inside a monorepo subpackage
311 /// with no workspace-root config because the applier refuses to
312 /// fragment per-package configs across the monorepo. Older scalar
313 /// config-ignore actions (e.g. `ignoreDependencies` on dependency
314 /// findings) are always manual today. Filter on this bool of each
315 /// individual action, not on the `type` alone. See the [`IssueAction`]
316 /// enum-level docs for the full list of per-instance flips.
317 pub auto_fixable: bool,
318 /// Human-readable description of the config change.
319 pub description: String,
320 /// The fallow config key to add the value to (e.g.,
321 /// `ignoreDependencies`).
322 pub config_key: String,
323 /// Value to add to the config key. Shape depends on `config_key`. For
324 /// scalar config keys (`ignoreDependencies`, others) this is a string
325 /// such as `"lodash"`. For `ignoreExports` this is an array of
326 /// `{ file, exports }` rule objects so the snippet can be merged into
327 /// the user's config verbatim. For `ignoreCatalogReferences` and
328 /// `ignoreDependencyOverrides` this is an object whose shape matches the
329 /// rule entry users add to their fallow config.
330 pub value: AddToConfigValue,
331 /// Optional URL pointing at a stable JSON Schema fragment that describes
332 /// the shape of `value`. Agents that intend to validate `value` before
333 /// writing it into a user's config can fetch the linked schema and run
334 /// it against `value`. The URL is a JSON Pointer fragment into fallow's
335 /// main config schema (e.g.
336 /// `schema.json#/properties/ignoreExports` for the ignoreExports
337 /// action, or `schema.json#/properties/ignoreDependencies/items` for
338 /// the per-package ignoreDependencies action). Strictly additive:
339 /// consumers that ignore the field keep working unchanged.
340 #[serde(default, skip_serializing_if = "Option::is_none")]
341 pub value_schema: Option<String>,
342}
343
344/// Singleton discriminant for [`AddToConfigAction`].
345#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
346#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
347#[serde(rename_all = "kebab-case")]
348pub enum AddToConfigKind {
349 /// Append a value into a fallow config `ignore*` list.
350 AddToConfig,
351}
352
353/// Value payload for [`AddToConfigAction::value`]. The variants line up with
354/// the documented per-`config_key` shapes; deserialization is untagged so
355/// downstream consumers can switch on the JSON value's type.
356#[derive(Debug, Clone, Serialize)]
357#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
358#[serde(untagged)]
359pub enum AddToConfigValue {
360 /// Scalar string value (e.g., a package name for
361 /// `ignoreDependencies: ["lodash"]`).
362 Scalar(String),
363 /// Array of file+export rule objects for `ignoreExports`.
364 ExportsRules(Vec<IgnoreExportsRule>),
365 /// Free-form object for rule-shaped keys like
366 /// `ignoreCatalogReferences` / `ignoreDependencyOverrides`. The shape
367 /// matches the rule entry users add to their fallow config; consumers
368 /// validate against the per-key schema referenced by `value_schema`.
369 RuleObject(serde_json::Map<String, serde_json::Value>),
370}
371
372/// Single `ignoreExports` rule entry. The fallow config accepts an array of
373/// these under the `ignoreExports` key.
374#[derive(Debug, Clone, Serialize)]
375#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
376pub struct IgnoreExportsRule {
377 /// File path (forward slashes, relative to project root) to which this
378 /// rule applies. Globs are accepted.
379 pub file: String,
380 /// Names of exports inside `file` to silently treat as used.
381 pub exports: Vec<String>,
382}
383
384/// A read-only follow-up command fallow surfaces from the current findings,
385/// emitted as the top-level `next_steps` array on each command's JSON envelope.
386///
387/// `next_steps` exists to point agents and humans sideways to fallow's adjacent
388/// verification capabilities (trace, complexity breakdown, audit, workspace
389/// scoping) that telemetry shows agents rarely discover, because they act on the
390/// output in front of them rather than on reference docs.
391///
392/// ## Two hard contracts
393///
394/// 1. **Read-only.** A `next_step` NEVER suggests `fallow fix` or any mutating
395/// command. Fallow surfaces evidence and verification paths; deciding and
396/// applying the remediation is the agent's job.
397/// 2. **Runnable, placeholder-free.** `command` is always runnable as-is. It
398/// never contains an angle-bracket placeholder (`<...>`); finding-derived
399/// values are filled in from a real, deterministically-selected finding, and
400/// any environment- or user-specific value that cannot be made concrete lives
401/// in `reason` instead. An agent can copy `command` and run it without edits.
402///
403/// Both contracts are enforced by unit tests in
404/// `crates/cli/src/report/suggestions.rs`.
405///
406/// Note: a SEPARATE, unrelated `next_steps` field exists on the
407/// `coverage setup` envelope (`CoverageSetupOutput.next_steps`) as a plain
408/// `Vec<String>` of human onboarding steps. Consumers that read multiple
409/// envelope kinds must route on the envelope's `kind` before interpreting a
410/// `next_steps` field: on analysis envelopes it is `Vec<NextStep>` objects, on
411/// `coverage setup` it is `Vec<String>`.
412#[derive(Debug, Clone, Serialize)]
413#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
414pub struct NextStep {
415 /// Stable kebab-case key for machine dispatch and de-duplication
416 /// (for example `"trace-unused-export"`). Identity is stable across runs;
417 /// the `command` and `reason` strings may vary with the findings.
418 pub id: String,
419 /// A runnable, read-only command string. Placeholder-free by contract.
420 pub command: String,
421 /// One short phrase explaining why this helps. Carries any value that
422 /// cannot be made concrete in `command`.
423 pub reason: String,
424}