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.

terminal
$ GH_TOKEN=... npx redlinegate metrics score-seeds --repo <org>/<repo> --pr <n>

Flags and output are on redline metrics.

The 17 defects

SeedSeverityRule it violatesDefect
1BLOCKERkotlin/todo-stubplaceholder ships and crashes the first call
1BLOCKERcore/hardcoded-secretshardcoded credential
2BLOCKERkotlin/context-leakActivity Context held by a singleton — leaks the whole view tree
4BLOCKERkotlin/globalscopeGlobalScope leaks work past the owner's lifecycle
5BLOCKERkotlin/blocking-in-coroutineblocking call inside a coroutine
6BLOCKERcore/customer-data-in-logscustomer identifier in logs
7BLOCKERkotlin/broad-catch-cancellationbroad catch also swallows CancellationException
8BLOCKERkotlin/force-unwrap!! non-null assertion in a production path
9BLOCKERkotlin/blocking-in-coroutinerunBlocking inside a suspend function
11BLOCKERcore/type-checker-suppressioncompiler warning suppressed with no explanation and no ticket
2HIGHkotlin/dropped-exception-causeonly the message survives
3HIGHkotlin/downcast-readonly-collectionread-only list cast to mutable
3HIGHkotlin/public-mutable-statepublic MutableStateFlow — callers can mutate UI state directly
4HIGHkotlin/else-on-exhaustive-whennew variants fall into the default
5HIGHkotlin/print-stack-tracebypasses crash reporting
6HIGHkotlin/implicit-default-localemachine-read amount formatted in the device locale
10HIGHkotlin/data-class-vardata 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.

The full file

seeded/kotlin/AntiSlop.kt · 33 lines · 1.3 KB
// 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))
seeded/kotlin/SeededViolations.kt · 57 lines · 2.3 KB
// 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