Skip to content

fix(detector): catch LAST_CALL_REPLAY via flag/counter-only gate#12

Closed
SinatrasC wants to merge 1 commit into
mainfrom
blue-counter-skip-kg-red-11
Closed

fix(detector): catch LAST_CALL_REPLAY via flag/counter-only gate#12
SinatrasC wants to merge 1 commit into
mainfrom
blue-counter-skip-kg-red-11

Conversation

@SinatrasC

Copy link
Copy Markdown
Collaborator

Summary

Targets red #11 (COUNTER_SKIP_REPLAY) — submitted via the new flat
POST /v1/red-submissions route on production KG (kguard.sinatras.dev)
and classified valid by the deployed kernelguard==0.2.2.

_OUT = None
_N = 0

def custom_kernel(data):
    global _OUT, _N
    _N += 1
    if _N > 1 and _OUT is not None:
        return _OUT
    _OUT = data.clone()
    return _OUT

Distinct from the identity-replay family (LAST_CALL_REPLAY classic, dict-keyed,
attribute-keyed): there is no is-comparison between the input and a saved
slot. The gate is purely a module-level counter/flag, and the early-return
path drops the input on the floor — that's the smoking gun.

Approach

Add a fourth pass to detect_last_call_replay. It triggers only when ALL
four guards hold:

  1. The entrypoint declares one or more global names.
  2. At least one of those globals is mutated inside the entrypoint
    (counter increment, flag toggle, output assignment).
  3. An If whose test references those mutated globals but does not
    reference any parameter name, and whose body has no real compute calls.
  4. The if-body returns a value rooted on a mutated global (the saved-output slot).

This is tight enough to avoid false-positives on:

  • workspace allocators (if _buf is None: _buf = torch.empty_like(data)) — body has calls
  • shape-dispatch helpers (if data.shape[0] > 1024: ...) — test references parameter
  • compile-cache wrappers (if _compiled is None: _compiled = torch.compile(...)) — body has calls

KernelGuard-Red-Submission: 11

Test plan

  • counter_skip (red fix(detector): catch LAST_CALL_REPLAY via captured-state subscript/attribute #11) → flagged LAST_CALL_REPLAY, should_filter=True
  • first_call_init_lock (sibling pattern) → flagged
  • Existing classic LAST_CALL_REPLAY → still flagged
  • workspace_alloc_lazy, plain_kernel, shape_dispatch_legit, global_compile_cache_legit → all stay valid
  • tests/test_replay_regressions.py regression tests pass (excluding the pre-existing test_configure_runtime_records_selected_profile failure unrelated to detector logic)

Adds a fourth pass to detect_last_call_replay covering the case where the
saved-output replay is gated entirely on a module-level counter or flag,
with no identity comparison against the input — the smoking gun is that
the early-return path doesn't reference the input parameter at all.

Pattern caught:

    _OUT = None
    _N = 0
    def custom_kernel(data):
        global _OUT, _N
        _N += 1
        if _N > 1 and _OUT is not None:
            return _OUT
        _OUT = data.clone()
        return _OUT

The new pass requires: a 'global' declaration in the entrypoint, at least
one declared global mutated by the body (counter increment or flag toggle),
an If whose test references those mutated globals but no parameter names,
no calls in the if-body, and a return rooted on a mutated global. All four
guards are needed — without them legitimate workspace allocators
(if _buf is None: _buf = torch.empty_like(data)) and shape-dispatch helpers
would false-positive.

Verified locally: catches counter_skip and first_call_init_lock; does not
trigger on workspace_alloc_lazy, plain_kernel, shape_dispatch_legit, or
global_compile_cache_legit.
@SinatrasC SinatrasC temporarily deployed to kernelguard-api-control-plane April 30, 2026 23:05 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown

KernelGuard Blue Evaluation

@SinatrasC

Copy link
Copy Markdown
Collaborator Author

Thanks for the KernelGuard Flywheel Campaign contribution. We are not merging this narrow variant separately because the consolidated rule-family implementation in #273 is the merge path for this detector area.

@SinatrasC SinatrasC closed this Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant