Skip to content

fix(detector): catch LAST_CALL_REPLAY via cursor-indexed reference replay#14

Closed
SinatrasC wants to merge 1 commit into
mainfrom
blue-cursor-replay-kg-red-16
Closed

fix(detector): catch LAST_CALL_REPLAY via cursor-indexed reference replay#14
SinatrasC wants to merge 1 commit into
mainfrom
blue-cursor-replay-kg-red-16

Conversation

@SinatrasC

Copy link
Copy Markdown
Collaborator

Summary

Targets red #16 under "Direction — Reference precompute replay".
Live kernelguard==0.2.2 classifies it valid:

_REFS = []
_idx = 0

def custom_kernel(data):
    global _idx
    if _idx < len(_REFS):
        out = _REFS[_idx]
        _idx += 1
        return out
    out = data.clone()
    _REFS.append(out)
    _idx += 1
    return out

The kernel pretends to populate a list of "reference outputs" on the warmup phase, then serves them on subsequent calls by walking a cursor through that list. The replay decision is a counter test that never looks at the input.

This shape is also a static FN against the open detector PRs:

Approach

A fourth pass in detect_last_call_replay, scoped tightly:

  1. Compute captured names — referenced in the function body but not parameters and not top-level local Name targets. global X declarations are exempt from the local-only set.
  2. Compute mutated captures — captures that the function mutates via AugAssign, Assign (only when the name is also global-declared), or attribute method call on the name (.append, .extend, .popleft, etc.).
  3. Walk every If whose body has no real-compute calls and whose test references no input parameter. Require the test to name at least one mutated capture.
  4. Require the if-body to return a Subscript on a mutated capture, either directly (return _REFS[_idx]) or via a local assigned from such a Subscript (out = _REFS[_idx]; ...; return out).

The "captured" + "mutated via method call" definition is what catches _REFS.append(out) — the previous flag-only pass treated only Assign / AugAssign as mutation, so a list registry filled via .append was invisible.

KernelGuard-Red-Submission: 16

Test plan

  • cursor_indexed (red fix(detector): catch function-attribute replay #16) → LAST_CALL_REPLAY, should_filter=True
  • cursor_indexed_direct_return (sibling shape) → flagged
  • Classic last_call_replay (existing test) → still flagged
  • workspace_lazy (if _buf is None: _buf = empty_like(data); return _buf + data) — stays valid (body has calls)
  • shape_dispatch (if data.shape[0] > 1024: ...) — stays valid (test references input)
  • config_lookup_legit (if data.dtype is _CFG['dtype']: ...) — stays valid (test references input)
  • plain_dict_lookup_legit (returns from a read-only dict, no mutation) — stays valid (no mutated captures)
  • plain_kernel — stays valid
  • tests/test_replay_regressions.py regressions pass

…play

Targets red #16 (CURSOR_INDEXED_REFERENCE_REPLAY) submitted under
'Direction - Reference precompute replay'. Live KG (kernelguard==0.2.2)
classifies it valid because:

    _REFS = []
    _idx = 0
    def custom_kernel(data):
        global _idx
        if _idx < len(_REFS):
            out = _REFS[_idx]
            _idx += 1
            return out
        out = data.clone()
        _REFS.append(out)
        _idx += 1
        return out

has no 'is'-comparison between input and a saved slot, the returned value
is a local Name (assigned from a Subscript), and the registry global
'_REFS' is mutated via method call ('.append') rather than Assign or
AugAssign. The earlier identity-replay passes don't fire.

Approach: a fourth pass in detect_last_call_replay that triggers on a
narrower-but-deeper shape:

  * compute the function's *captured* names (referenced but not param
    or top-level local Name target). 'global X' declarations are
    exempt from the local-only set.
  * compute *mutated* captures: AugAssign on a captured name, Assign
    on a captured name that is also globally declared, OR an
    attribute-method call on a captured name (so '.append', '.extend',
    '.popleft' all count as mutation).
  * walk every If with no real-compute calls in its body and a test
    that does not reference any input parameter; require the test to
    name at least one mutated capture.
  * require the if-body to return a Subscript on a mutated capture,
    either directly ('return _REFS[_idx]') or via a local that was
    assigned from such a Subscript ('out = _REFS[_idx]; ...; return out').

Verified: cursor_indexed and cursor_indexed_direct_return are now
flagged. Existing classic LAST_CALL_REPLAY still flagged. workspace_lazy,
shape_dispatch, plain_kernel, config_lookup_legit, plain_dict_lookup_legit
all stay valid - either the if-test references the input parameter, the
captured name is read-only, or the if-body has compute calls.
@SinatrasC SinatrasC temporarily deployed to kernelguard-api-control-plane May 1, 2026 13:01 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented May 1, 2026

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