mirror of
https://github.com/Monkopedia/rouse-context.git
synced 2026-11-29 11:13:05 +00:00
* Gate: a broad catch must be preceded by a cancellation clause (#674) `CancellationException` extends `IllegalStateException` on the JVM, so all four of `Exception`, `Throwable`, `RuntimeException` and `IllegalStateException` receive it. Source order within one try/catch chain is the only thing that decides delivery, and the Kotlin compiler diagnoses neither an unreachable nor a mis-ordered clause -- which is why six human-directed sweeps in two days (#660, #662/#665, #666/#669, #670/#673, #638/#672, #667) each found what the previous one had not looked at. The gate: scripts/lib/kotlin-catch-chains.awk per-file scanner scripts/check-cancellation-catch-order.sh scripts/tests/check-cancellation-catch-order-test.sh scripts/cancellation-catch-allowlist.tsv reasoned exemptions (1 entry) scripts/cancellation-catch-backlog.tsv known-unfixed defects (60) A scanner rather than a regex, because the property is order WITHIN ONE CHAIN and neither half survives a line match: this tree has a four-line clause (McpTool.kt:170) no line regex parses, and adjacent chains a line regex would credit to each other. Comments and string literals are blanked in place, then tokens are walked tracking brace depth. RED ON A KNOWN-BAD HISTORICAL TREE ---------------------------------- Run against9b50f469, the commit before #660: exit 1, 87 findings. Run against8479cf56(#660 applied): exit 1, 75 findings. The 12 that disappear are exactly the twelve #660's own commit message lists, file for file and count for count: CertProvisioningFlow.kt 2 -> 1 CtLogMonitor.kt 3 -> 1 (check x2) KtorWebSocketFactory.kt 1 -> 0 MuxDemux.kt 1 -> 0 (sendPingAwaitPong) OnboardingFlow.kt 1 -> 0 RelayApiClient.kt 2 -> 1 (executeRequest) SelfCertVerifier.kt 3 -> 0 TunnelClientImpl.kt 2 -> 0 (runKeepaliveLoop, healthCheck) net -12 The positive fires on the defect and the negative goes quiet on the fix. A reduced form of the pre-#660 CtLogMonitor shape is in the self-test, so that pairing is pinned in CI rather than observed once. TWO CONTROLS, EVERYWHERE ------------------------ Every rule is asserted in both directions -- an empty pattern matches everything and a filtered search reaches nothing, and both look clean. All four broad types, three narrow types, all three spellings of the guard, guard-after-broad, a guard on a neighbouring try, a guard earlier in the same chain with a clause between, the multi-line annotated clause, prose in a KDoc, a guard quoted in a string literal, `Flow.catch { }`. Part 6 mutates the gate itself: a copy that recognises no broad type, and a copy that ignores the cancellation guard. Each must make the suite go red and SAY so. Without those, an `expect 1` proves only that something went red and an `expect 0` only that something was quiet. The gate also refuses to report clean for a tree in which it parsed zero catch clauses: a scanner that reaches nothing and a tree that has nothing print the same empty result. WHAT IT FINDS ON MAIN --------------------- 61 violations across 54 <path, function> keys, from 177 catch clauses in 278 files across 12 production source sets. One is exempt with a written reason (IntegrationSetupViewModel.pushIntegrationSecrets -- no suspension point in the try, pinned by IntegrationSecretsPersistCancellationTest). The other 60 are recorded in the backlog file, which is not an allowlist: they are defects, they cite #667, and the gate prints the outstanding count on every run rather than reporting the tree as clean. None of them is fixed here; the gate is the deliverable. Both files are exact-match ratchets in BOTH directions, so fixing a site fails the gate until its entry is lowered or removed. That is what stops either file becoming the place things go to be forgotten, and it is asserted by the self-test. Two scanner defects found and fixed while measuring, both of which had mis-attributed real findings: an ordinary `"` string whose `${ ... }` template spans lines (TestRelayFixture.kt:392) was read as code from the newline on, shifting the depth counter for the rest of the file; and `fun interface`, brace-less interface members and default parameter values each disarmed the function-header parser. Closes #674. Claude-Session: https://claude.ai/code/session_01YYSPuh9bZEJkrzvnEpY1QK Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * Repoint the backlog rows at #722, and say what the citation check does not do All 53 backlog rows cited #667, which closed on 2026-09-02 -- the day before this branch's head commit -- and which was scoped to its own six `app/src/main` sites, never to this list. #674 closes when this PR merges. So on the day this landed, 60 recorded defects would have cited two closed issues and no open issue would have tracked them: invisible to any open-state triage, in a file whose header says its purpose is not to become the place things go to be forgotten. #722 is the open tracker for this debt, and it records the triage rule (reachability before severity), so the rows are not read as 60 mechanical edits. Every row now cites it. Also removes the two remaining places #667 was used as a POINTER rather than as history: the allowlist entry's reason, which now stands on the in-tree comment and `IntegrationSecretsPersistCancellationTest` alone, and the gate's developer-facing failure text. The remaining mentions are factual provenance -- the list of six sweeps, and which issue established that both spellings of `CancellationException` are live -- and the sweep list now says outright that all six are closed and that #722 is the tracker. The gate itself cannot catch this class, and now says so at length rather than implying otherwise. `read_ledger` matches `#[0-9]+` and never asks whether the number resolves to anything, let alone to anything open; `#999999` passes. Resolving it means a GitHub API call, and this gate runs from a plain checkout with no token, offline on a developer machine, and inside the throwaway repo its own self-test builds -- which has no remote. A network check would have to fail closed in all three (a gate nobody can run) or skip silently (a gate that reports clean without having looked, which is the exact failure mode this file exists to prevent). So the check is not made, the gap is written down, and the reviewer of a citation is a human. The related ceiling problem is noted as #721 and is not fixed here. No change to the gate's logic, the scanner, the self-test's assertions or any production Kotlin. The self-test's two ledger fixtures move to #722 for consistency; they only ever exercised the `#[0-9]+` shape. Closes #674. Claude-Session: https://claude.ai/code/session_01YYSPuh9bZEJkrzvnEpY1QK Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Monkopedia <monkopedia@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
244 lines
11 KiB
Awk
244 lines
11 KiB
Awk
# Emits one record per `catch` clause in ONE Kotlin file, for the #674 gate.
|
|
#
|
|
# Read scripts/check-cancellation-catch-order.sh first: it owns the rule, the
|
|
# allowlist and the backlog. This file owns exactly one question -- "for each
|
|
# catch clause, what does it catch, and does an earlier clause of the SAME
|
|
# try/catch chain catch a CancellationException?" -- and answers it from source
|
|
# order, which is the only thing that decides delivery (the Kotlin compiler
|
|
# diagnoses neither an unreachable nor a mis-ordered catch clause).
|
|
#
|
|
# Output, tab-separated, on stdout:
|
|
#
|
|
# CATCH <line> <type> <guarded:0|1> <enclosing-function>
|
|
# TOTAL <count>
|
|
#
|
|
# `<guarded>` is 1 iff some EARLIER clause of the same chain catches a type
|
|
# whose last dotted component is `CancellationException`. Both spellings in
|
|
# this tree -- bare, and qualified `kotlinx.coroutines.` / `java.util.concurrent.`
|
|
# -- therefore count, which a regex over one spelling would not (#667).
|
|
#
|
|
# `TOTAL` exists so the caller can refuse to report "clean" for a tree it never
|
|
# read. A scanner that reaches nothing and one that finds nothing print the
|
|
# same empty violation list.
|
|
#
|
|
# WHY A SCANNER AND NOT A REGEX
|
|
# -----------------------------
|
|
# The property is ORDER WITHIN ONE CHAIN, and neither half survives a line
|
|
# regex. `} catch (e: Exception) {` on the line after a
|
|
# `} catch (e: CancellationException) {` may belong to a different try, three
|
|
# lines of a nested block apart; and this tree already contains a chain whose
|
|
# clause spans four lines:
|
|
#
|
|
# } catch (
|
|
# @Suppress("TooGenericExceptionCaught")
|
|
# t: Throwable
|
|
# ) {
|
|
#
|
|
# So: strip comments and string literals (blanked, not deleted, so line numbers
|
|
# survive), then walk tokens tracking brace depth. A frame opened right after
|
|
# `try` or right after a `catch (...)` is part of a chain; when it closes, the
|
|
# next token decides whether the chain continues. Any other token ends it, which
|
|
# is what keeps a catch belonging to one try from being credited to another.
|
|
|
|
function is_ident_char(c) { return (c ~ /[A-Za-z0-9_$]/) }
|
|
|
|
BEGIN { inblock = 0; blockdepth = 0; inraw = 0; instr = ""; tmpl = 0; nlines = 0 }
|
|
|
|
# Pass 1: blank out comments, string literals and character literals, in place.
|
|
# Blanking rather than deleting keeps every character at its original line, so
|
|
# the reported line number is the line the `catch` keyword is actually on.
|
|
#
|
|
# The state (`inblock`, `inraw`, `instr`, `tmpl`) carries ACROSS lines, because
|
|
# three Kotlin constructs span them and one of them is not obvious:
|
|
#
|
|
# * block comments, which also nest, so the depth is counted;
|
|
# * `"""` raw strings;
|
|
# * an ordinary `"` string whose `${ ... }` template contains a newline. That
|
|
# is legal and this tree has one -- `TestRelayFixture.kt:392` interpolates a
|
|
# `runCatching { ... }` block across three lines. Blanking only to end of
|
|
# line there left `}.getOrDefault(-1)}) " +` to be read as CODE, and the two
|
|
# stray closing braces shifted the depth counter for the rest of the file:
|
|
# every chain decision after it was made at the wrong depth.
|
|
{
|
|
line = $0; out = ""; i = 1; n = length(line)
|
|
while (i <= n) {
|
|
c = substr(line, i, 1); c2 = substr(line, i, 2); c3 = substr(line, i, 3)
|
|
if (inblock) {
|
|
# Kotlin block comments nest, so this counts rather than scanning for the
|
|
# first `*/`.
|
|
if (c2 == "*/") { blockdepth--; if (blockdepth == 0) inblock = 0; out = out " "; i += 2; continue }
|
|
if (c2 == "/*") { blockdepth++; out = out " "; i += 2; continue }
|
|
out = out " "; i++; continue
|
|
}
|
|
if (inraw) {
|
|
if (c3 == "\"\"\"") { inraw = 0; out = out " "; i += 3; continue }
|
|
out = out " "; i++; continue
|
|
}
|
|
if (instr != "") {
|
|
if (tmpl > 0) {
|
|
# Inside `${ ... }`. Everything is blanked, INCLUDING the braces: they
|
|
# are balanced within the literal, so letting them reach the depth
|
|
# counter would be the bug this branch exists to avoid. They are counted
|
|
# here only to find where the template ends.
|
|
if (c == "{") tmpl++
|
|
else if (c == "}") tmpl--
|
|
out = out " "; i++; continue
|
|
}
|
|
if (c == "\\") { out = out " "; i += 2; continue }
|
|
if (c2 == "${") { tmpl = 1; out = out " "; i += 2; continue }
|
|
if (c == instr) { instr = ""; out = out " "; i++; continue }
|
|
out = out " "; i++; continue
|
|
}
|
|
if (c2 == "//") { while (i <= n) { out = out " "; i++ }; break }
|
|
if (c2 == "/*") { inblock = 1; blockdepth = 1; out = out " "; i += 2; continue }
|
|
if (c3 == "\"\"\"") { inraw = 1; out = out " "; i += 3; continue }
|
|
if (c == "\"" || c == "'") { instr = c; tmpl = 0; out = out " "; i++; continue }
|
|
out = out c; i++
|
|
}
|
|
# An ordinary string literal cannot contain a bare newline, so one still open
|
|
# here with no template pending is a lexing disagreement, not a continuation.
|
|
# Reset rather than swallow the rest of the file.
|
|
if (instr != "" && tmpl == 0) instr = ""
|
|
nlines++; L[nlines] = out
|
|
}
|
|
|
|
END {
|
|
# One buffer plus a character->line map, so a clause spanning lines is one
|
|
# token run rather than a per-line special case.
|
|
buf = ""; pos = 0
|
|
for (li = 1; li <= nlines; li++) {
|
|
s = L[li]; plen = length(s)
|
|
for (j = 1; j <= plen; j++) { pos++; lineat[pos] = li }
|
|
buf = buf s
|
|
pos++; lineat[pos] = li; buf = buf "\n"
|
|
}
|
|
parse()
|
|
print "TOTAL\t" total
|
|
}
|
|
|
|
function parse( i, n, c, tok, start, depth, pdepth,
|
|
pend_try, pend_type, pend_cancel,
|
|
chain_depth, chain_cancel, ty, guarded, who,
|
|
pend_fun, pend_name, pend_params, lastfun) {
|
|
n = length(buf); i = 1; depth = 0; pdepth = 0
|
|
pend_try = 0; pend_type = ""; pend_cancel = 0
|
|
# `chain_depth` is the depth at which a try/catch frame JUST closed, i.e. the
|
|
# only depth at which the very next token may continue a chain. Any other
|
|
# token resets it to -1, so a `catch` that follows unrelated code -- or that
|
|
# belongs to a different try -- starts a fresh chain with guarded = 0.
|
|
chain_depth = -1; chain_cancel = 0
|
|
pend_fun = 0; pend_name = ""; pend_params = 0; lastfun = ""
|
|
fname[0] = ""
|
|
while (i <= n) {
|
|
c = substr(buf, i, 1)
|
|
if (c == " " || c == "\t" || c == "\r" || c == "\n") { i++; continue }
|
|
if (is_ident_char(c)) {
|
|
start = i
|
|
while (i <= n && is_ident_char(substr(buf, i, 1))) i++
|
|
tok = substr(buf, start, i - start)
|
|
if (tok == "fun") { pend_fun = 1; pend_name = ""; pend_params = 0; chain_depth = -1; continue }
|
|
# `fun interface Foo { ... }` is a SAM declaration, not a function: its
|
|
# brace opens an interface body. Left armed, the next brace at top level
|
|
# is misread as that "function"'s body and every catch under it is
|
|
# attributed to the interface's name.
|
|
if (tok == "interface" && pend_fun && pend_params == 0) { pend_fun = 0; pend_name = ""; chain_depth = -1; continue }
|
|
# Only up to the parameter list: `fun invoke(...): ToolResult {` must
|
|
# name `invoke`, not its return type. A receiver or a type parameter
|
|
# before the name is overwritten by the name itself.
|
|
if (pend_fun && pdepth == 0 && pend_params == 0) { pend_name = tok }
|
|
if (tok == "try") { pend_try = 1; chain_depth = -1; continue }
|
|
if (tok == "catch") {
|
|
while (i <= n && substr(buf, i, 1) ~ /[ \t\r\n]/) i++
|
|
# `catch` not followed by `(` is not a catch clause -- an identifier of
|
|
# that name, or `Flow.catch { }`, which is a different construct with
|
|
# different semantics (#667 cleared those separately).
|
|
if (substr(buf, i, 1) != "(") { chain_depth = -1; continue }
|
|
ty = read_param(i); i = G_end
|
|
guarded = (chain_depth == depth) ? chain_cancel : 0
|
|
total++
|
|
who = fname[depth]
|
|
if (who == "") who = lastfun
|
|
# A catch in a property initialiser belongs to no function. Named
|
|
# rather than left blank: the ledger key is <path, function> and an
|
|
# empty field would collide with a parse failure.
|
|
if (who == "") who = "<top-level>"
|
|
print "CATCH\t" lineat[start] "\t" ty "\t" guarded "\t" who
|
|
pend_type = ty
|
|
pend_cancel = guarded
|
|
if (is_cancellation(ty)) pend_cancel = 1
|
|
chain_depth = -1
|
|
continue
|
|
}
|
|
chain_depth = -1
|
|
continue
|
|
}
|
|
if (c == "(") { if (pend_fun && pdepth == 0) pend_params = 1; pdepth++; chain_depth = -1; i++; continue }
|
|
if (c == ")") { pdepth--; chain_depth = -1; i++; continue }
|
|
if (c == "{") {
|
|
depth++
|
|
if (pend_try) { fkind[depth] = "try"; fcancel[depth] = 0; pend_try = 0 }
|
|
else if (pend_type != "") { fkind[depth] = "catch"; fcancel[depth] = pend_cancel; pend_type = "" }
|
|
else if (pend_fun && pdepth == 0) { fkind[depth] = "fun"; pend_fun = 0; lastfun = pend_name }
|
|
else { fkind[depth] = "other" }
|
|
fname[depth] = (fkind[depth] == "fun") ? pend_name : fname[depth - 1]
|
|
chain_depth = -1; i++; continue
|
|
}
|
|
if (c == "}") {
|
|
# A brace-less declaration -- `suspend fun obtain(x: ByteArray): Foo?` in
|
|
# an interface or an abstract member -- leaves the header armed with no
|
|
# body to consume it. The enclosing `}` is where it is known to be over;
|
|
# without this the NEXT brace at that level is claimed as its body.
|
|
pend_fun = 0
|
|
if (fkind[depth] == "try") { chain_depth = depth - 1; chain_cancel = 0 }
|
|
else if (fkind[depth] == "catch") { chain_depth = depth - 1; chain_cancel = fcancel[depth] }
|
|
else { chain_depth = -1 }
|
|
fkind[depth] = ""; fname[depth] = ""
|
|
depth--
|
|
i++; continue
|
|
}
|
|
# `=` ends a function header, so an expression-bodied `fun f() = ...` does
|
|
# not leave `pend_fun` armed for the next brace it meets. Its name is
|
|
# recorded anyway: an expression body opens no frame, and this tree puts
|
|
# whole try/catch chains in one (`KeypairDeviceCredentialProvider.kt:46`).
|
|
# Only at pdepth 0. A default parameter value -- `baseDomain: String =
|
|
# defaultBaseDomain` -- also contains `=`, and disarming there loses the
|
|
# header of every function that has one.
|
|
if (c == "=" && pdepth == 0 && pend_fun) { lastfun = pend_name; pend_fun = 0 }
|
|
chain_depth = -1
|
|
i++
|
|
}
|
|
}
|
|
|
|
# Reads the parenthesised clause starting at buf[i] == "(" and returns the
|
|
# caught TYPE. Sets G_end to the index just past the closing `)`.
|
|
#
|
|
# The type is what follows the last top-level `:`, so an annotated multi-line
|
|
# clause and a qualified type both parse. `<`/`>` are counted as nesting so a
|
|
# generic type argument cannot hide the colon that matters.
|
|
function read_param(i, n, d, start, inner, k, c, colon, dep) {
|
|
n = length(buf); d = 0; start = i
|
|
while (i <= n) {
|
|
c = substr(buf, i, 1)
|
|
if (c == "(") d++
|
|
else if (c == ")") { d--; if (d == 0) { i++; break } }
|
|
i++
|
|
}
|
|
G_end = i
|
|
inner = substr(buf, start + 1, (i - 2) - start)
|
|
colon = 0; dep = 0
|
|
for (k = 1; k <= length(inner); k++) {
|
|
c = substr(inner, k, 1)
|
|
if (c == "(" || c == "<" || c == "[") dep++
|
|
else if (c == ")" || c == ">" || c == "]") dep--
|
|
else if (c == ":" && dep == 0) colon = k
|
|
}
|
|
if (colon == 0) return "?"
|
|
inner = substr(inner, colon + 1)
|
|
gsub(/[ \t\r\n]/, "", inner)
|
|
return inner
|
|
}
|
|
|
|
# Matched on the LAST dotted component, so the bare and both qualified
|
|
# spellings all count. Grepping one spelling and missing the other is #667's
|
|
# named trap.
|
|
function is_cancellation(t) { return (t ~ /(^|\.)CancellationException$/) }
|