kotlin
Seeded Kotlin defects — GlobalScope launches, swallowed CancellationException, leaked Context.
17 deliberate defects, each with a marker naming the rule in standards/ it breaks. It measures recall: how many of the defects the reviewer flags at their severity or higher. The pass condition is that all 10 BLOCKER defects are flagged.
Because each marker names a rule, the scorer also grades attribution. A reviewer that finds the bug but cites the wrong rule counts as caught, and separately as misattributed.
Running it
Nothing here is ever merged. Open a throwaway pull request on a pilot repository already onboarded for this stack, adding this directory and seeded/clean together, wait for the automated review to finish, then score it.
seed-canary.yml does exactly this on a schedule and closes the pull request afterwards, including when the run fails.
$ GH_TOKEN=... npx redlinegate metrics score-seeds --repo <org>/<repo> --pr <n>
Flags and output are on redline metrics.
The 17 defects
| Seed | Severity | Rule it violates | Defect |
|---|---|---|---|
| 1 | BLOCKER | kotlin/todo-stub | placeholder ships and crashes the first call |
| 1 | BLOCKER | core/hardcoded-secrets | hardcoded credential |
| 2 | BLOCKER | kotlin/context-leak | Activity Context held by a singleton — leaks the whole view tree |
| 4 | BLOCKER | kotlin/globalscope | GlobalScope leaks work past the owner's lifecycle |
| 5 | BLOCKER | kotlin/blocking-in-coroutine | blocking call inside a coroutine |
| 6 | BLOCKER | core/customer-data-in-logs | customer identifier in logs |
| 7 | BLOCKER | kotlin/broad-catch-cancellation | broad catch also swallows CancellationException |
| 8 | BLOCKER | kotlin/force-unwrap | !! non-null assertion in a production path |
| 9 | BLOCKER | kotlin/blocking-in-coroutine | runBlocking inside a suspend function |
| 11 | BLOCKER | core/type-checker-suppression | compiler warning suppressed with no explanation and no ticket |
| 2 | HIGH | kotlin/dropped-exception-cause | only the message survives |
| 3 | HIGH | kotlin/downcast-readonly-collection | read-only list cast to mutable |
| 3 | HIGH | kotlin/public-mutable-state | public MutableStateFlow — callers can mutate UI state directly |
| 4 | HIGH | kotlin/else-on-exhaustive-when | new variants fall into the default |
| 5 | HIGH | kotlin/print-stack-trace | bypasses crash reporting |
| 6 | HIGH | kotlin/implicit-default-locale | machine-read amount formatted in the device locale |
| 10 | HIGH | kotlin/data-class-var | data class with var properties breaks copy/equality semantics |
Scoring
The score reports BLOCKER recall, false positives, and rule attribution. The canary appends it to data/seed-scores.jsonl and fails the run if BLOCKER recall drops below 1.0 or the clean corpus attracts a false positive. Of the 17 defects here, 10 BLOCKER and 7 HIGH — only the BLOCKER count is a pass condition.
Adding a defect
Add the code and one marker line above it. The format, and what scripts/validate.mjs checks, are on Seeded corpus.
Related
- The kotlin standard — every rule these defects test.
- seed-canary.yml — runs this corpus every week.
- Telemetry & validation — where the scores end up.
The full file
// DO NOT MERGE — Redline validation seed (anti-slop rules).
package seeded
class CardGateway : PaymentGateway {
// SEED 1 [BLOCKER] (kotlin/todo-stub) placeholder ships and crashes the first call
override fun charge(amount: Money): Receipt = TODO("Not yet implemented")
}
fun load(api: Api, id: String) = try { api.fetch(id) } catch (e: java.io.IOException) {
// SEED 2 [HIGH] (kotlin/dropped-exception-cause) only the message survives
throw RepositoryException(e.message)
}
fun addToCart(state: CartState, item: Item) {
// SEED 3 [HIGH] (kotlin/downcast-readonly-collection) read-only list cast to mutable
(state.items as MutableList<Item>).add(item)
}
fun render(state: PaymentState) = when (state) {
is PaymentState.Failed -> showError(state.reason)
// SEED 4 [HIGH] (kotlin/else-on-exhaustive-when) new variants fall into the default
else -> showSuccess()
}
fun push(sync: SyncService, changes: List<Change>) {
try { sync.push(changes) }
// SEED 5 [HIGH] (kotlin/print-stack-trace) bypasses crash reporting
catch (e: java.io.IOException) { e.printStackTrace() }
}
// SEED 6 [HIGH] (kotlin/implicit-default-locale) machine-read amount formatted in the device locale
fun body(total: Double) = mapOf("amount" to "%.2f".format(total))
// DO NOT MERGE — Redline validation seed.
// Every `SEED n [SEVERITY] (rule-id)` marker must be flagged at that severity or higher,
// citing that rule id. Score with scripts/score-seeds.mjs.
package com.redline.seed
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
object SeededViolations {
// SEED 1 [BLOCKER] (core/hardcoded-secrets) hardcoded credential
private const val API_KEY = "acme-live-7Fq2Rd9Km4Tz6Hb3Vy8Wn"
// SEED 2 [BLOCKER] (kotlin/context-leak) Activity Context held by a singleton — leaks the whole view tree
var context: Context? = null
// SEED 3 [HIGH] (kotlin/public-mutable-state) public MutableStateFlow — callers can mutate UI state directly
val state = MutableStateFlow("")
fun load(msisdn: String) {
// SEED 4 [BLOCKER] (kotlin/globalscope) GlobalScope leaks work past the owner's lifecycle
GlobalScope.launch {
// SEED 5 [BLOCKER] (kotlin/blocking-in-coroutine) blocking call inside a coroutine
Thread.sleep(1000)
// SEED 6 [BLOCKER] (core/customer-data-in-logs) customer identifier in logs
println("loading balance for $msisdn")
try {
fetch(msisdn)
} catch (e: Exception) {
// SEED 7 [BLOCKER] (kotlin/broad-catch-cancellation) broad catch also swallows CancellationException
}
}
}
// SEED 8 [BLOCKER] (kotlin/force-unwrap) !! non-null assertion in a production path
fun title(map: Map<String, String>): String = map["title"]!!
private suspend fun fetch(msisdn: String): String {
// SEED 9 [BLOCKER] (kotlin/blocking-in-coroutine) runBlocking inside a suspend function
return runBlocking(Dispatchers.Main) { "$API_KEY/$msisdn" }
}
}
// SEED 10 [HIGH] (kotlin/data-class-var) data class with var properties breaks copy/equality semantics
data class Account(var id: String, var balance: Int)
// SEED 11 [BLOCKER] (core/type-checker-suppression) compiler warning suppressed with no explanation and no ticket
@Suppress("UNCHECKED_CAST")
fun <T> coerce(value: Any): T = value as T