Scalafix: Configuration Boundary Enforcement

Overview

  • Purpose: Keep configuration loading at the application edge and discourage direct exception handling in favour of Result[A].
  • Enforcement is global; legitimate exemptions are opt-in via // scalafix:off markers in source files.

Rules enforced (.scalafix.conf, DisableSyntax.regex)

  • NoConfigFactory - bans ConfigFactory references.
  • NoSysEnv - bans sys.env references.
  • NoSystemGetenv - bans System.getenv references.
  • NoPureConfigDefault - bans ConfigSource.default references.
  • NoKeywordTry / NoKeywordCatch / NoKeywordFinally - prefer Try(...).toResult and Result[A] combinators.

Configuration shape

  • All rules live under the canonical top-level path DisableSyntax.regex. scalafix only reads its regex list from this exact path; custom-named subblocks like DisableSyntax.MyRules { regex = [...] } are silently ignored. ScalafixConfigurationBoundarySpec enforces this structurally.
  • scalafix’s DisableSyntax rule has no excludePackages or per-rule fileFilter. To exempt a file, use a // scalafix:off marker (optionally with a rule id) in the source.

Plugin and rule type

  • Plugin: sbt-scalafix (see project/plugins.sbt).
  • Rule type: syntactic regex (no SemanticDB required; currently run on the Scala 3.7.1 build).

Running Scalafix

  • Compile-time enforcement: enabled in CI only (scalafixOnCompile := sys.env.getOrElse("CI", "false").toBoolean).
  • Manual run:
    • sbt scalafixAll to lint every module.
    • sbt core/scalafix to lint just core main sources.

Suppressing a finding

  • File-wide: put // scalafix:off (silences all rules) or // scalafix:off DisableSyntax.NoSysEnv (silences one) at the top of the file, before the package declaration.
  • Block: wrap the region with // scalafix:off RuleId / // scalafix:on RuleId.
  • Name only the rules the region needs, and remove the marker when the code no longer trips them: .scalafix.conf sets lint.error = [UnusedScalafixSuppression], so a suppression that suppresses nothing fails scalafix. A Scala 3 brace-less try ... catch does not match NoKeywordTry/NoKeywordCatch (their patterns need a {), so it needs no marker.
  • Use sparingly; prefer fixing the violation. App-edge code (samples, workspace, the org.llm4s.config package) is the most common legitimate exemption.

Migration guidance

  • Load configuration at the app/test edge via Llm4sConfig.
  • Inject typed settings into core services rather than reading env vars or ConfigSource.default from library code.
  • Replace try/catch with Try(...).toResult and combinators on Result[A].

Migration rules

modules/scalafix-rules holds semantic Scalafix rules that rewrite user code across llm4s’s breaking changes (#2330). They are not published yet: publishing them as org.llm4s::llm4s-scalafix with the release workflow, and the one-line command in each migration guide, are the rest of #2330.

Llm4sMigrate_0_5 runs every rule for the 0.4.1 to 0.5.0 guide:

Rule Rewrites
CopyToWithSetters .copy(maxTokens = Some(500), temperature = 0.2) on a growth-prone type to .withMaxTokens(500).withTemperature(0.2); message.copy(contentOpt = Some(t)) to message.withContent(t)
MillisToFiniteDuration CrawlerConfig(delayMs = 500, timeoutMs = 30000) to CrawlerConfig(delay = 500.millis, timeout = 30.seconds) (also UrlLoader, HttpConfig, ShellConfig and the search tool configs), RateLimitError("openai", 1000L) to RateLimitError("openai", 1.second), and a read of config.timeoutMs to config.timeout.toMillis
IsRecoverableToTotal error.isRecoverable to LLMError.isRecoverable(error)
MovedConfigLoaders Llm4sConfig.loadBraveSearchTool() (and the DuckDuckGo and Exa loaders) to ToolsConfigLoader, Llm4sConfig.metrics() to MetricsConfigLoader.default(), Llm4sConfig.pgSearchIndex() to PgSearchIndexConfigLoader.default()
MessageExtractorToTypePattern case UserMessage(content) => to case userMessage: UserMessage =>, reading userMessage.content in the guard and body (also ToolMessage(content, toolCallId))
RemovedApis Lint only: the orchestration package (PlanRunner, DAG, TypedAgent, CancellationToken), org.llm4s.types.AgentId and PlanId, and getToolDefinitionsSafe, each with a link to the guide section

Where a rewrite cannot be made safely, the rule reports a lint instead: a copy with a positional argument, a field that has no setter any more (Completion.thinking, NamedProviderConfig.organization), or a message extractor nested inside another pattern. These 0.5 rules change names, not types; the Secret keys and the Instant of Completion.created are Llm4sMigrate_1_0’s.

Llm4sMigrate_1_0 runs every rule for the breaking changes since 0.5.1 (the notes in the Migration Guide), those since 0.6.0 among them, and grows as the 1.0 breaks land:

Rule Rewrites
CopyToWithSetters_1_0 .copy(field = value) to .withField(value) on the 95 types made growth-safe since 0.5.1 (ToolRetryPolicy, the OpenAI-compatible, Gemini, Vertex AI and Azure configs, TraceEvent and AssistantError cases, tool schemas, context types, …); a String apiKey is wrapped in Secret(...). Lints a positional copy and new X(...)
CredentialsToSecret OpenAIConfig(apiKey = key, ...) and config.withApiKey(key) to Secret(key) on the provider configs (Some(Secret(key)) where the key is an Option), and a read of config.apiKey to config.apiKey.reveal
FromValuesKeyFirst OpenAIConfig.fromValues(model, key, organization, baseUrl, ...) to fromValues(model, Secret(key), baseUrl, organization, ...), dropping a None organization; AzureConfig.fromValues(deployment, endpoint, key, apiVersion) to fromValues(deployment, Secret(key), endpoint, apiVersion)
CompletionCreatedToInstant Completion(created = seconds), withCreated(seconds) and toCompletion(seconds) to Instant.ofEpochSecond(seconds) (0L to Instant.EPOCH), and a read of completion.created to completion.created.getEpochSecond
PositionalPatternsGainFields A positional pattern on one of the 43 types whose extractor gained fields since 0.5.1 gets a _ for each: case ServiceError(m, s, p, r) to case ServiceError(m, s, p, r, _), also the TraceEvent cases, the provider configs and ModelCapabilities; 0.6.0’s ten-field Completion and eight-field ModelCallCompleted patterns gain finishReason and servedTier, its nine-field TraceEvent.ErrorOccurred pattern usage, its fourteen-field TraceEvent.CompletionReceived pattern servedTier, its TokenUsage, ModelPricing, TurnState and RAGConfig patterns their new last field, and its CompletionOptions patterns promptCache and serviceTier
DuplicateApisRemoved Deletes import ServiceError.ServiceErrorOps; Llm4sConfig.loadTextEmbeddingModel() to textEmbeddingModel()
RemovedApis_1_0 Lint only: org.llm4s.types.typeclass, CompletionChunk/ChunkDelta, OpenAIToolHelper, AnsiColors and the embeddings sample’s settings on Llm4sConfig, each with a link to its note
ThinkingReplacesBudgetTokens Since 0.6.0: withReasoning(ReasoningEffort.None) and CompletionOptions(reasoning = Some(ReasoningEffort.None)) to Thinking.Off, unless the same chain sets withReasoning again afterwards (Thinking.Off takes precedence over reasoning). Lints budgetTokens and withBudgetTokens (Thinking.budget(n) is a Result), any other ReasoningEffort.None, and a match on ReasoningEffort with no catch-all, which needs Minimal, XHigh and Max
AgentStatusGainsTruncated Since 0.6.0, lint only: a match (or partial function) on AgentStatus with no catch-all, which needs a case for AgentStatus.Truncated, the status of an answer cut off at the model’s output limit (note)
FinishReasonTyped Since 0.6.0: chunk.finishReason.contains("stop"), == Some("length"), exists(_ == "tool_calls") and case Some("stop") to the FinishReason cases; withFinishReason("stop") and StreamedChunk(..., finishReason = Some("stop")) likewise (0.6.0’s raw Bedrock or Gemini value, such as "end_turn", to the case it maps to now, another literal to FinishReason.Other(...)); any other read to chunk.finishReason.map(_.name), which keeps its type. The cases assume a client that maps its reason; a Spring AI model gives Other(raw) for any value. Lints a reason set from a non-literal String or a filter’s value, and, in the comparison a read is part of, a literal no rewrite gets right (and any stop, tool_calls or length beside it, left as a String): 0.6.0’s raw Bedrock and Gemini values (end_turn, tool_use, max_tokens, MAX_TOKENS, …), which now map to a case while a Spring AI model still gives Other(raw); a content filter’s (content_filter, SAFETY, …), a ContentFilteredError now; and Gemini’s STOP, Stop or ToolCalls
CompressionRuleTransform Since 0.6.0: CompressionRule’s field apply to transform: rule.apply(messages) to rule.transform(messages), withApply(f) to withTransform(f), and the named argument CompressionRule(name, apply = f) to transform = f (note)
AgentCopyToWithSetters Since 0.6.0: .copy(field = value) to .withField(value) on the 36 llm4s-agent types made growth-safe by #2778 (ContextWindowConfig, Handoff, the AgentEvents payloads, EventRecord, GraphSnapshot, Command and others); a positional copy and new X(...) are reported

A key passed by position (OpenAIConfig("sk-...", "gpt-4o")), and the other credentials that became Secrets (Langfuse, search-tool and store keys), still need a hand edit. The breaks that change behaviour or add cases have no rule: see Changes since 0.5.1 that no rule automates.

Llm4sMigrate_1_0 assumes code compiled against 0.5.x. CredentialsToSecret, CompletionCreatedToInstant and FromValuesKeyFirst cannot tell 0.6.x code, where the key is already a Secret and created an Instant, from 0.5.x code (Scalafix sees no types from Scala 3 dependencies), and would rewrite it again: config.apiKey.reveal to config.apiKey.reveal.reveal. Code on 0.6.x runs the rules for the breaks since 0.6.0 by name instead: ThinkingReplacesBudgetTokens, FinishReasonTyped, CompressionRuleTransform, AgentCopyToWithSetters, AgentStatusGainsTruncated and PositionalPatternsGainFields.

The rules are compiled with scalac 2.13, the only compiler scalafix-core is published for; they read the SemanticDB of Scala 3 sources. Scalafix’s Scala 3 support resolves the symbol at each name, which every rule here relies on, but has no synthetics or inferred types, so the rules match on the symbols the source names. Scala 3’s SemanticDB also records no symbol for some calls, such as the copy in CompletionOptions().copy(maxTokens = Some(500)); the rules then read the method from a named argument’s symbol (CompletionOptions#copy().(maxTokens)), so a positional copy on such a receiver is neither rewritten nor reported.

Each rule is tested by scalafixTests (sbt scalafixTests/test): the files in modules/scalafix-rules/input are compiled with SemanticDB against llm4s-core 0.4.1 from Maven Central, each rule named in a file’s header is run on it, and the result must equal the file of the same name in modules/scalafix-rules/output, which is compiled against the current sources. scalafixTests05 does the same for the Llm4sMigrate_1_0 rules, with modules/scalafix-rules/input-0.5 compiled against the 0.5.1 artifacts and its expected output under output/.../fix/v1, and scalafixTests06 for the breaks since 0.6.0, with modules/scalafix-rules/input-0.6 compiled against the 0.6.0 artifacts and its expected output under output/.../fix/v06.

Notes

  • CI and pre-commit run scalafix checks.
  • If you hit a false positive (e.g. a doc comment containing a banned token), prefer a // scalafix:off DisableSyntax.<RuleId> over loosening the regex.