Skip to content

feat: name program and target tokens in self-kill refusal - #16333

Closed
iamwhatever wants to merge 2 commits into
mainfrom
rebase/15055-selfkill-diag-offsets
Closed

iamwhatever wants to merge 2 commits into
mainfrom
rebase/15055-selfkill-diag-offsets

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #15055 (fork PR by @SaRedfiche). That PR had readiness: passed but
fell into conflict with main. This is an in-repo replacement rebased onto current
main, opened because fork PRs cannot consume /ai-review overrides. The author's
commit is preserved (authored by Robert Fisher, with a Co-authored-by: @SaRedfiche
trailer) so a Rebase-and-merge lands their work with credit. Full credit to
@SaRedfiche.

Problem / Motivation

Goal: Make the self-kill refusal say which token it read as the kill program and which as the target, so a reader can tell a real kill from a false positive without running the classifier.

When the self-protection floor blocks a command, its refusal diagnostic reports only a whole-command span=0..N. That is enough to see the floor fired, but not why — a reader cannot tell whether the block was a genuine pkill kirocrew or a false positive where an innocent argument (a glob path whose basename is *, next to a path carrying the product name) happened to look like a by-name kill of this gateway.

Why it matters

The self-protection floor is meant to stop the agent killing its own supervisor, but a false positive silently blocks ordinary work with no way to diagnose it short of reading the classifier source. Naming the two token offsets turns "this was refused, and here is exactly which two words tripped it" into something a reader can act on.

Not a goal

  • No other floor's diagnostic changes — this is self-protection-kill only.
  • The floor's decision (what it blocks) is untouched; only the refusal text gains fields.
  • No command bytes are exposed — the new fields carry offsets and a char-class census, never a substring of the command.

What changed (motivation → approach → change)

The self-kill refusal reported only a whole-command span, so a false positive gave a reader no way to see which two words the floor keyed on. The fix adds two optional fields to the refusal diagnostic — program=<a>..<b> and target=<c>..<d> — each with the same char-class census the existing span= already carries.

Locating those offsets is the work. A new _raw_word_spans() splitter walks the shared quote/escape state machine to break the command into shell words at their real source offsets, keeping quoted separators inside their word and an abutting operator as its own span. _self_kill_token_spans() then aligns those raw spans to the resolved frame's tokens by index — sound because both derive from the same word split, before per-word variable and tilde resolution — so a variable-resolved token like "$C"/gateway/* is bracketed at its true position even though the product name entered it by $C expansion.

Piece What it does
argv_spans._raw_word_spans splits the command into words with source offsets
argv_spans._self_kill_token_spans returns the program/target offsets the by-name leg keyed on, or None to fall back
diagnostics.RefusalTokenSpan + refusal_diagnostic() render the two additive fields; span= and the first two refusal lines are byte-identical when they are omitted
security/__init__.py the floor loop computes the spans for self-protection-kill only and threads them through

A leg with no single-token target (bare kill $(...)) returns None and keeps the whole-command span. docs/system-specs/modules/security.md documents the token-level exception in the same change.

Rebase note: on current main, argv_floor.py had grown to the edge of its per-module line cap, so the two new offset functions were placed in a new argv_spans sibling module (reached through the existing submodule facade, which keeps the one test seam that patches _data_consumer_exempt observable). This keeps argv_floor.py under its cap; the only behavioural change versus #15055 is the module the two private helpers live in. The kiro_crew.security package line budget in test_security_gate_liveness.py is re-pinned to the new measured total with a reason entry.

Backwards compatibility

Compatible: the fields are additive and omitted for every floor except self-protection-kill; the span= field and the first two refusal lines are byte-identical when they are absent, which the tests pin. The argv_spans split is internal to the kiro_crew.security package and changes no public name.

Tests

A TestSelfProtectionKillDiagnosticSpans class (13 tests) covers all shapes: pkill -f kirocrew and killall kirocrew bracket the program and target tokens; the variable-resolved glob command brackets its real source offsets; a quoted ERE pattern brackets its raw region; kill $(pgrep -f kirocrew) and a non-self-kill fall back with no fields; a multi-line command with a redirect (true\npkill -f kirocrew >out) keeps the whole-command span instead of naming the shifted -f/> tokens, while a multi-line command without a cancelling redirect still names the real tokens; every other floor's line stays byte-identical; and _refusal_diagnostic_id still reads the rule id. buluoray's three target-shift repros (<in, <<<x, NBSP+redirect) plus a fuzz case now assert the refusal falls back to the whole-command span with no program=/target= fields, an İ case asserts the length-shift fallback, and the plain case still names the real target; the target-slice check now kills the mutation (disabling it) that previously survived. The denied-command count pin and the keystone test_sandbox_governance_mask continue to hold. Run locally on this head: test_security_gate_liveness.py and test_denied_commands_security.py pass (1339 passed, 1 skipped), including the per-module cap and package-budget gates.

Manual verification

N/A — unit coverage is sufficient; the change is a diagnostic string and its offset computation, both fully exercised by the new tests against real is_denied output.

Screenshots / video

N/A — no user-visible UI change; the change is a refusal-diagnostic string.

Related Issues

Fixes #14827. Supersedes #15055.

Pattern harvest

Not generalizable: this is a targeted diagnostic enrichment for one floor whose decision is already token-level; the offset-alignment approach is specific to the self-kill by-name leg and does not describe a defect class worth a lint or review rule.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

By submitting this pull request, I confirm that my contribution is made under the terms of the project's license.

@iamwhatever
iamwhatever requested a review from a team as a code owner October 3, 2026 00:14
@iamwhatever
iamwhatever requested a review from kyleseaman October 3, 2026 00:14
@iamwhatever
iamwhatever force-pushed the rebase/15055-selfkill-diag-offsets branch from 4e45193 to 2a937ff Compare October 3, 2026 00:35
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 3, 2026
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that the hand-copied by-name walk in argv_spans._self_kill_token_spans stays in step with _is_self_kill; only the conformance test catches drift.

Suggestions

  • One diagnostic line now holds three shape= keys (span=… shape=… program=… shape=… target=… shape=…), so the line no longer parses as a key=value map. Naming them program_shape= and target_shape= keeps the micro-format unambiguous for any future parser, at the cost of one extra word per field.

[DESIGN-REVIEWED] e1fd790

Description read

[DESCRIPTION-READ] 9fcea79f65696ee02e78c34aa19f50def220724f3b3bd6c2903ea38cd6f68366

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

Premise-level review of e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The span code re-derives token offsets with a second splitter and a copied walk, then adds guards that catch the two splitters disagreeing. It treats the symptom, not the cause.

Not justified as shipped

  • Item 2 — duplicate of argv_floor._is_self_kill (lines 871–904): the PR copies the by-name leg into argv_spans._self_kill_token_spans, and the diff itself says "Keep the two in lockstep." It counts 1 copy, and the copy has already drifted: it walks _shell_payload_walk where the floor walks _self_token_frames. The memo pin only requires disqualified to sit inside the frame loop, so the floor's own loop could return (i, j) without moving it.
    Subtraction: delete the copied walk and have the floor's by-name loop report the indices it matched.
    Clears when: one walk produces both the verdict and the indices.
  • Item 3 — symptom-level: offsets get lost because _self_tokens (shlex) drops them. The PR doesn't carry them through. It rebuilds them with _raw_word_spans and then stacks 4 guards for the cases where the two splitters disagree: the newline span, the single-word re-tokenise check, the target name check with its $ exception, and the len(lower) check. Each guard was added for a mismatch someone reported.
    Clears when: the description says this sits at symptom level and that offset-carrying tokens are deferred.

What this change ships

Inventory (6 items) — 4 justified

Intent: let a reader tell a real self-kill from a false positive by seeing which two tokens tripped the floor. ADDITION.

  1. The self-protection-kill refusal line gains program=/target= offsets and a character-class census — justified
  2. A copy of the floor's by-name walk that returns indices — duplicate of argv_floor._is_self_kill
  3. A raw word-span splitter plus checks that the alignment holds — symptom-level
  4. Falls back to the whole-command span on a length shift, a parser exception or no provable pair — justified
  5. The package line budget is raised to 28,921 — justified
  6. security.md documents the token-level exception — justified

[FIRST-PRINCIPLES-REVIEWED] e1fd790

Description read

[DESCRIPTION-READ] 812197ebd0bbbb9dc79fdc014b62fb63cd147c6bae1ae9a38cc490a0f9a222cf

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review -- ✅ PASS

Which legitimate operations does e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc newly refuse? Updated in place on each push. A script-confirmed regression blocks; the model proposes candidates and never decides a refusal.

Verdict basis: adjudicated, zero confirmed regressions.

Security scope review -- adjudicated candidates

  • macos-latest (platform posix): 10 classified, 0 newly refused, 10 still allowed.
  • ubuntu-latest (platform posix): 10 classified, 0 newly refused, 10 still allowed.
  • windows-latest (platform windows): 5 classified, 0 newly refused, 5 still allowed.

No candidate flipped from allowed to refused.

Scope-Verdict: PASS

TIGHTENING: Nothing gets stricter. The change only adds detail to a refusal message that was already being produced. The new token-span code runs only after the self-protection-kill floor has already said no, and any error inside it is caught (if rule_id == "self-protection-kill" and len(lower) == len(tool_name): try: spans = _submodule("argv_spans")._self_kill_token_spans(lower) except Exception: spans = None). From there it only changes the diagnostic line that _reason(...) receives through program_span=/target_span=. The threat this concerns is the existing one, a by-name self-kill of the product (pkill/killall aimed at a kiro[-.]?crew target), which base already refuses. _raw_word_spans, _self_kill_token_spans, refusal_diagnostic(..., program_span=, target_span=) and RefusalDiagnostic.as_line add no new match, tier or allow/deny branch.

ADJUDICATED: 15 candidates, 0 confirmed newly-refused

PLATFORM COVERAGE: The diff assumes posix shell word-splitting. _SHELL_WORD_BREAK_RAW = frozenset(";&|()<>"), the active-newline span and the quote/escape walk through _shell_normalizer._iter_shell_chars` all follow posix rules, though they only affect the diagnostic, never the verdict.

  • posix (10 rows): read-only neighbours of the self-kill (pgrep -f kirocrew, ps aux | grep -i kirocrew), and the same kill programs aimed at targets other than the product (pkill -f pytest, killall node). They also cover the shapes the new splitter special-cases: a redirect, a multi-line block with an active newline, and a bare kill with a substitution. Two more are product-path log reads that use a glob (grep -c -E 'error' ~/.kirocrew/logs/*.log, tail).
  • windows (5 rows): taskkill /IM node.exe /F, Stop-Process -Name pytest -Force, Get-Process -Name kirocrew, a tasklist /FI filter in cmd quoting, and a Get-Content $env:USERPROFILE\.kirocrew\logs\gateway.log read with \ separators. These probe the same kill and read neighbours in the Windows spellings, which the posix-only word splitter never parses for a verdict.
  • any: no rows, because every candidate carries a program name or path.

The diff makes no platform assumption on the verdict path, so no platform is left unprobed.

[SCOPE-REVIEWED] e1fd790

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc and found no blocking issues.

This comment is updated in place on each push.

Review details

FINDING -- src/kiro_crew/security/argv_spans.py:231 -- For pkill -f <'$' kirocrew # it's ok, the "$" in ... bypass makes is_denied() report target=10..13 (the redirect filename), not the actual target at 14..22, contradicting the alignment guarantee (recurring-defect-patterns) -> Fix: remove the blanket dollar-sign acceptance and return None when target alignment cannot be verified.
[GPT-REVIEWED] e1fd790

False positive or not applicable? A repository writer can comment:
/ai-review override gpt e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc: <one-sentence reason>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 3, 2026
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] e1fd790

Verdict parsed from the review's SHA-scoped output markers for commit e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc.

False positive or not applicable? A repository writer can comment:
/ai-review override fable e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc: <one-sentence reason>

@iamwhatever
iamwhatever force-pushed the rebase/15055-selfkill-diag-offsets branch from 2a937ff to b7513ef Compare October 3, 2026 01:53
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 3, 2026
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 3, 2026
@iamwhatever
iamwhatever enabled auto-merge (squash) October 3, 2026 06:02

@buluoray buluoray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Adds program=/target= offset+census fields to the self-protection-kill refusal diagnostic (#14827). The refusal itself is unchanged and nothing leaks: the new code runs only after the floor has already decided to deny, and it emits offsets and character-class counts, never bytes. The problem is the feature's own output. The raw-word/token index alignment in argv_spans._self_kill_token_spans treats "equal word counts" as proof the two lists line up, and that is false for common command shapes. On a multi-line command with a redirect, the refusal names -f as the kill program and > as the target. That is the exact misdiagnosis this PR exists to prevent, and nothing tells the reader it happened.

Verified (at b7513ef)

  • Refusal decision unchanged. src/kiro_crew/security/__init__.py:1435-1454: the span computation sits inside if hit:, after _emit_deny_event and before the unconditional return _reason(...). It cannot turn a deny into an allow. _is_self_kill (argv_floor.py:845) is untouched by the diff. Enforcement is fail-closed.
  • No new raise path seen. I fuzzed 20,000 random strings over the pkill/killall/quote/$()/operator/İ alphabet through _self_kill_token_spans and is_denied: 0 exceptions. raw_spans[i]/raw_spans[j] are guarded by the length-equality check, and refusal_diagnostic clamps the new spans.
  • No secret leakage. I probed AWS_SECRET_ACCESS_KEY=… pkill -f kirocrew, pkill -f kirocrew --token=ghp_…, pkill -f 'kirocrew.*ghp_…' and killall kirocrew; echo ghp_…. No secret, product-name or program bytes appear on the diagnostic line. Line 1 (Blocked by security policy: <pattern>) is unchanged.
    • One residual: when the target is a quoted pattern that embeds a secret, target= gives a per-token length and class census of that secret. Before, the whole-command census only had it mixed in with everything else. It is coarse and never bytes, so not blocking.
  • Mutation. I made _self_kill_token_spans return None immediately. 5 tests in test/test_denied_commands_security.py::TestSelfProtectionKillDiagnosticSpans went red: test_pkill_names_program_and_target, test_killall_names_program_and_target, test_variable_resolved_command_names_the_glob_tokens, test_no_new_field_contains_a_command_character and test_quoted_pattern_target_brackets_the_raw_quoted_region. Unmutated, the file gives 1325 passed, 1 skipped. I restored the file and git status is clean.
  • Spec. docs/system-specs/modules/security.md was updated in the same commit, as AGENTS.md requires. It names the wrong module (see follow-ups).
  • CI at the head SHA: 89 success, 10 skipped, 0 failure. Auto-merge is off, there are no human reviews, and the PR shows REVIEW_REQUIRED.
  • Tests: I ran only test/test_denied_commands_security.py locally. I did not run the full suite.

Blocking

  1. src/kiro_crew/security/argv_spans.py:110: equal counts are not an alignment proof, so the fields name the wrong tokens. _raw_word_spans and the floor's token walk break words differently:

    • an active newline becomes a ; token in the walk but gets no raw span;
    • >out and >/dev/null are one token in the walk but two raw spans.

    The differences can cancel, so the counts match while the lists are shifted. Reproduced at this head:

    • true\npkill -f kirocrew >out gives program=-f, target=>
    • ls -la\npkill -f kirocrew >/dev/null gives program=-f, target=>

    Agent bash commands are routinely multi-line and redirected, so this is not a corner case. A reader trusting program= is misled in exactly the way #14827 describes.

    Fix: make the raw splitter mirror the walk (emit a span for an active newline, and keep a redirect plus its target as one word), and check each paired slice before returning it: the raw slice must re-tokenize to its token, or at least contain the program basename and the product name. Return None on any disagreement. Add the two commands above as regression tests.

Bot claims disposition

  • Opus FINDING (argv_spans.py:110, misalignment): real. Reproduced as shown above. This is the blocking item.
  • GPT FINDING (argv_spans.py:110, same misalignment): real. Same defect, reproduced with its exact example.
  • Opus FINDING (__init__.py:1444, "lower-casing never changes length"): real. For grep İstanbul log.txt && pkill -f kirocrew, the fields bracket kill and irocrew, shifted by one, because 'İ'.lower() is two code points. It only affects the diagnostic, and only for case-expanding non-ASCII input, so it is a follow-up. Fix: skip the fields when len(lower) != len(tool_name), or render against lower, and correct the comment.
  • Opus and Fable design FINDING (spec names argv_floor._self_kill_token_spans): real. The symbol lives in argv_spans. The same wrong module appears in the budget re-pin comment at test/test_security_gate_liveness.py:156. Docs only, so a follow-up.
  • GPT FINDING (lazy from . import argv_floor at argv_spans.py:102): real but minor. argv_floor does not import argv_spans, so there is no cycle that would force a lazy import. The docstring's reason for it (keeping the dependency one-way) holds just as well at module scope. Style only, so a follow-up.
  • Fable design CONCERNS (mirror drift with no pointer back from _is_self_kill): real. The two loops are duplicated and nothing fails when they diverge. In fact they already differ in which frames they iterate: _self_token_frames in the floor versus _shell_payload_walk here. It only affects the diagnostic, so it is a follow-up: add a comment in _is_self_kill naming the mirror, or a conformance test.
  • First-principles CONCERNS (RefusalTokenSpan exported with zero consumers): real. A grep finds only its definition, the two export lists and a comment. It is a non-blocking follow-up: drop it from _EXPORTS, EXPORTED_NAMES and the TYPE_CHECKING import.
  • Security scope PASS: agree. No newly refused operation. The change is post-verdict.

Follow-ups (non-blocking)

  • Fix the İ length mismatch (see above).
  • Correct the module name in security.md and in the liveness re-pin comment.
  • Add a pointer from _is_self_kill to its mirror, or a conformance test.
  • Unexport RefusalTokenSpan.
  • Move the argv_floor import to module scope.
  • The new span computation is not wrapped. If a future edit makes it raise, the exception escapes is_denied from inside a deny branch. That breaks the "a diagnostic must never be the thing that raises inside a gate" principle stated in refusal_diagnostic. Consider a try/except Exception: spans = None.
  • The PR body says the new class has 10 tests; it has 9.

bolichen97
bolichen97 previously approved these changes Oct 3, 2026

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Non-blocking: _self_kill_token_spans runs on the deny path with no guard. refusal_diagnostic promises it never raises inside a gate; this new call can. Consider wrapping it in try/except that returns None, so a parser edge case keeps the plain whole-command span instead of turning a refusal into an exception. (Recent reverts #15232 / #16072 were floors that broke on real-world input; worth the belt-and-braces here.)

iamwhatever added a commit that referenced this pull request Oct 3, 2026
Addresses buluoray's review (blocking) and bolichen97's nit on #16333.

The self-kill diagnostic aligned raw word spans to the floor's resolved
token walk BY INDEX and trusted equal word counts as proof the two lists
lined up. They do not: the raw splitter and the walk break words by
different rules -- an active newline is a ';' separator token in the walk
but was no raw word, and a redirect such as '>out' is one walk token but
two raw words -- and the two differences can cancel so the counts match
while the slices are shifted. On 'true<newline>pkill -f kirocrew >out' the
shift named '-f' the kill program and '>' the target: the exact
misdiagnosis the feature exists to prevent.

Fix, in two parts:
- '_raw_word_spans' now emits a span for an active newline, mirroring the
  ';' separator token the walk produces, so the two lists no longer shift
  by one on a multi-line command.
- '_self_kill_token_spans' re-tokenises each picked slice before returning
  it and keeps the pair only when both are a single shell word and the
  program slice is itself a by-name kill program. A residual shift fails
  that check and falls back to the whole-command span. The check reads the
  resolved word, not its bytes, so a $VAR-resolved target still gets a
  precise offset.

Also wrap the span computation in 'is_denied' in a try/except that
degrades to the whole-command span, honouring the contract that a refusal
diagnostic must never be the thing that raises inside a gate
(bolichen97's nit).

Tests: two regression cases pin the multi-line+redirect fallback and the
multi-line-without-redirect precise span. Docs: correct the module name
('argv_spans', not 'argv_floor') in the spec and the liveness re-pin
comment, and re-pin the package line budget to the new measured total.

Co-authored-by: iamwhatever <12429733+iamwhatever@users.noreply.github.com>
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 3, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

@buluoray thanks — the blocking misalignment is fixed in 2ded6c7.

Blocking (equal counts are not alignment). You're right that _raw_word_spans and the token walk break words differently and the two differences cancel. Two changes:

  1. _raw_word_spans now emits a span for an active newline, mirroring the ; separator token the walk produces — so the two lists no longer shift by one on a multi-line command.
  2. Before returning a pair, _self_kill_token_spans re-tokenises each picked slice and keeps it only when both are a single shell word and the program slice is itself a by-name kill program. A residual shift (the slice at the program index being a flag such as -f) fails that check and falls back to the whole-command span. The check reads the resolved word, not its bytes, so a $VAR-resolved target still gets a precise offset.

Both commands you reproduced now keep the whole-command span with no program=/target= fields — the multi-line kill-with-redirect shapes (a true line then a by-name kill of the product with >out, and an ls -la line then the same with >/dev/null). Added both as regression tests, plus a multi-line-without-redirect case that still names the real tokens.

Follow-ups addressed in the same commit:

  • Corrected the module name: argv_spans._self_kill_token_spans (not argv_floor) in docs/system-specs/modules/security.md and in the test_security_gate_liveness.py re-pin comment.
  • Wrapped the span computation in is_denied in a try/except Exception that sets the spans to None, so a future parser edge case degrades to the whole-command span instead of raising inside the deny branch.
  • Corrected the test-count claim in the PR body (now 11 tests).

Follow-ups left for a separate change (non-blocking, as you flagged them): the dotted-capital case-expansion length mismatch (an __init__.py diagnostic-mapping change), unexporting RefusalTokenSpan, moving the argv_floor import to module scope, and the _is_self_kill mirror pointer / conformance test. Happy to fold any of these in here if you'd prefer.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

@bolichen97 thanks for the approve and the belt-and-braces nit — done in 2ded6c7. The span computation in is_denied is now wrapped in a try/except Exception that sets the spans to None, so a parser edge case keeps the plain whole-command span instead of letting a diagnostic raise inside the deny branch. That restores the "a diagnostic must never be the thing that raises inside a gate" contract refusal_diagnostic states.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 3, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Rebased onto the current main (head is now ea67cdbfb). The only conflict was in test/test_security_gate_liveness.py — the per-package line-budget changelog block and the _PACKAGE_LINE_BUDGET pin. I kept both sides' additive changelog entries and re-pinned the budget to the newly measured package total (28,873). No behavior change: the self-kill span machinery and the alignment fix are unchanged. The touched security tests pass locally (test_security_gate_liveness.py, test_denied_commands_security.py).

@buluoray this is a rebase-only update of the branch you reviewed — would you mind taking another look? Your earlier blocking point about the raw-span / token-walk alignment is addressed by the _raw_word_spans newline fix and the re-tokenise check in _self_kill_token_spans that are already on the branch.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Sensitive Change Review

Sensitive files changed:

  • src/kiro_crew/security/__init__.py

  • src/kiro_crew/security/argv_spans.py

  • src/kiro_crew/security/diagnostics.py

  • @chenmingwei23: missing the ## Sensitive change review heading

How to clear this check:

  1. The PR author posts the template below as a PR comment, with
    every field filled (N/A needs a reason). Keep the
    Head commit line: it must name the current head SHA.
  2. The check turns green within a minute, with no push.
  3. A new push resets this check: post it again for the new head.

Only the author's comment counts this way. A reviewer can instead
paste the filled template into an "Approve" review on this head.
Merging still needs a reviewer's approval.

## Sensitive change review
- Head commit: e1fd7909c1447e8ba7cc7db54d4f58926efd9ebc
- What rule changed:
- Before -> after (what was allowed/blocked, now):
- Worst case if wrong (breaks commands / leaks secret / locks user out):
- Evidence checked (which sandbox result proves it):
- No regression:
  - Tools still work (which tools/commands were run, result):
  - Backward compatible (old config / old data / old callers still work, how checked):
  - Existing tests (which suites passed on this head):
- How to undo:

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Oct 8, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Self-review (crew-pr-reviewer, read-only): CHANGES NEEDED

This PR adds program=/target= offsets to the self-protection-kill refusal, so a reader can tell a real self-kill from a false positive. The refusal decision itself is unchanged and stays fail-closed (the span call is wrapped in try/except after the deny). buluoray's CHANGES_REQUESTED at 0234181 is still open and NOT addressed. The current head ea67cdb is that same fix commit rebased onto main, and src/kiro_crew/security/argv_spans.py still only checks that the target slice re-tokenises to one word. It never checks that the word is the target token. I re-ran buluoray's repros on ea67cdb with is_denied, and all three still misname the target: pkill -f <in kirocrew # it's ok gives target=in, pkill -f <<<x kirocrew # it's fine gives target=x, and pkill -f \u00a0 kirocrew >/dev/null gives target=>. That is exactly the misdiagnosis this PR exists to prevent. Suggested fix, as buluoray proposed: before returning, run the target slice's word through the same _SELF_NAME_RE.search(_debracket(w)) or _SELF_NAME_RE.search(_normalize_operand(w)) test that picked j, fall back to comparing with tokens[j] for a $-expanded slice, return None otherwise, and add those commands as regression tests (the re-tokenise check currently survives mutation). Also still open: the İ length-expansion shift (__init__.py:1437, raised again by both GPT and Opus on this head), the zero-consumer RefusalTokenSpan export, and the Design/First Principles CONCERNS about the hand-copied _is_self_kill loop. This touches the security floor, Sensitive Change Review is red, and auto-merge is armed, so an approval would merge it immediately. — crew-pr-reviewer

@iamwhatever
iamwhatever marked this pull request as draft October 8, 2026 15:49
auto-merge was automatically disabled October 8, 2026 15:49

Pull request was converted to draft

@iamwhatever

Copy link
Copy Markdown
Contributor Author

Moved to draft by crew-pr-reviewer: the self-review above is CHANGES NEEDED. GitHub refuses a self-request-changes, and the readiness check ignores human reviews, so draft is how this PR is kept from merging (it also turns auto-merge off). Once the points above are fixed, mark it ready again with gh pr ready 16333 and it will be re-reviewed on the new head. — crew-pr-reviewer

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 8, 2026
The self-protection-kill refusal diagnostic now carries additive
program and target offset fields with the same char-class census, so a
reader can tell a real by-name kill from a glob-argument false positive
without running the classifier. A new raw-word-span splitter aligns the
resolved frame tokens to their real source offsets by index, so even a
shell-variable-resolved glob token is bracketed at its true position.
The span field and the first two refusal lines are byte-identical when
the fields are omitted, and no field carries a byte of the command.

Rebased onto main: the offset machinery moved into a new argv_spans
module so argv_floor stays under its per-module line cap, and the
security package line budget is re-pinned to the new measured total.

Co-authored-by: SaRedfiche <113142267+SaRedfiche@users.noreply.github.com>
iamwhatever added a commit that referenced this pull request Oct 8, 2026
Addresses buluoray's review (blocking) and bolichen97's nit on #16333.

The self-kill diagnostic aligned raw word spans to the floor's resolved
token walk BY INDEX and trusted equal word counts as proof the two lists
lined up. They do not: the raw splitter and the walk break words by
different rules -- an active newline is a ';' separator token in the walk
but was no raw word, and a redirect such as '>out' is one walk token but
two raw words -- and the two differences can cancel so the counts match
while the slices are shifted. On 'true<newline>pkill -f kirocrew >out' the
shift named '-f' the kill program and '>' the target: the exact
misdiagnosis the feature exists to prevent.

Fix, in two parts:
- '_raw_word_spans' now emits a span for an active newline, mirroring the
  ';' separator token the walk produces, so the two lists no longer shift
  by one on a multi-line command.
- '_self_kill_token_spans' re-tokenises each picked slice before returning
  it and keeps the pair only when both are a single shell word and the
  program slice is itself a by-name kill program. A residual shift fails
  that check and falls back to the whole-command span. The check reads the
  resolved word, not its bytes, so a $VAR-resolved target still gets a
  precise offset.

Also wrap the span computation in 'is_denied' in a try/except that
degrades to the whole-command span, honouring the contract that a refusal
diagnostic must never be the thing that raises inside a gate
(bolichen97's nit).

Tests: two regression cases pin the multi-line+redirect fallback and the
multi-line-without-redirect precise span. Docs: correct the module name
('argv_spans', not 'argv_floor') in the spec and the liveness re-pin
comment, and re-pin the package line budget to the new measured total.

Co-authored-by: iamwhatever <12429733+iamwhatever@users.noreply.github.com>
@iamwhatever
iamwhatever force-pushed the rebase/15055-selfkill-diag-offsets branch from ea67cdb to 15f762d Compare October 8, 2026 22:16
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Fixed at 15f762d9a653e9e949529534fc29742dd0de9ade.

@buluoray — blocking item (the target slice was never verified): done. Before returning a pair, the picked target word now runs through the same self-name predicate the loop used to pick j:

_SELF_NAME_RE.search(_debracket(w)) or _SELF_NAME_RE.search(_normalize_operand(w))

A slice whose resolved word still carries an unresolved $ expansion cannot be matched by name even when it is the target (a quoted "$C"/gateway/* re-tokenises to $c/gateway/*), so that one case is accepted — and no shift case has a $ to abuse it. Anything else returns None and keeps the whole-command span. All three of your repros, and the fuzz case, now fall back with no program=/target= fields:

  • the <in here-string + trailing comment case → whole span
  • the <<<x here-string + trailing comment case → whole span
  • the NBSP + redirect case → whole span
  • (your fuzz) the NBSP + ( + >| case → whole span

In every one the program slice was still correct; it was the target index that shifted onto in / x / > / (, which the new target check now rejects.

Coverage gap: closed. All four are regression tests now, and the mutation you described (disabling the re-tokenise/target check) is killed by them — I re-ran it with if False and (...) and the new target-shift test fails on the mutant, passes unmutated. The $-resolved target still names its real token.

Follow-ups, this round:

  • İ length shift (GPT + Opus, __init__.py:1437): fixed by a fallback, not an offset map. When command.lower() changes length (İ → i + combining dot) the diagnostic falls back to the whole-command span (len(lower) == len(tool_name) guard); the stale "lower-casing never changes length" comment is corrected.
  • Unused RefusalTokenSpan export: removed from _exports.py and the facade _EXPORTS map / TYPE_CHECKING block. The record stays internal to diagnostics; the facade export-consistency tests pass.
  • Copied _is_self_kill loop (Design / First Principles): I did not factor it into a shared helper, and here is why. _is_self_kill is held by a structural invariant — test_the_memo_is_declared_inside_the_frame_loop[_is_self_kill] requires the per-frame disqualified memo to be declared inside _is_self_kill's own for tokens frame loop, so a command-level answer is never carried from one frame into the next. Hoisting the walk into a shared generator moves that memo out of _is_self_kill and the invariant test fails (I tried it). Instead the span-module copy now carries a MIRROR comment pinning it to the floor's by-name leg, and TestSelfProtectionKillDiagnosticSpans pins that the two agree on which tokens fire. If a maintainer would rather relax that invariant to allow a shared helper, I'm happy to follow up — but changing a pinned security invariant is a maintainer call, not something I'll self-decide here.

The verdict (allow/deny) is unchanged — only the reader-facing program=/target= fields. test_denied_commands_security.py + test_security_gate_liveness.py: 1339 passed, 1 skipped locally on this head.

@buluoray re-requesting your review.

@iamwhatever
iamwhatever requested a review from buluoray October 8, 2026 22:18
@iamwhatever
iamwhatever marked this pull request as ready for review October 8, 2026 22:18
@iamwhatever
iamwhatever force-pushed the rebase/15055-selfkill-diag-offsets branch 2 times, most recently from 018efe4 to 2ae900a Compare October 8, 2026 22:42
Addresses buluoray's review across two rounds plus bolichen97's nit on
the self-kill diagnostic.

The diagnostic aligns raw word spans to the floor's resolved token walk
by index and must not trust equal word counts as proof the two lists line
up: the raw splitter and the walk break words by different rules (an
active newline is a separator token in the walk; a redirect such as a
trailing `>out` is one walk token but two raw words), and two such
differences can cancel so the counts match while the slices are shifted.

Alignment fix (first round):
- `_raw_word_spans` emits a span for an active newline, mirroring the
  separator token the walk produces, so the two lists stay index-aligned
  on a multi-line command.
- Before returning a pair, `_self_kill_token_spans` re-tokenises each
  picked slice and keeps it only when both are a single shell word and
  the program slice is itself a by-name kill program.
- The span computation in `is_denied` is wrapped in
  `try/except Exception: spans = None` so a parser edge case degrades to
  the whole-command span instead of raising inside the deny branch.
- Spec and the liveness re-pin comment name the correct module.

Target verification (second round, buluoray's remaining blocking item):
the program slice was verified but the target slice was not, so a shift
landing only on the target index (a redirect/here-string/NBSP/comment the
raw split and the walk break differently) could still name the wrong
token. The picked target word now runs through the same self-name
predicate that selects it; an unresolved `$` slice (which cannot be
name-matched but is still the resolved target) is accepted, and anything
else falls back to the whole-command span.

Also in this change:
- `__init__` falls back when case-lowering changes the command's length
  (a dotted-capital I expands to two code points), so an offset computed
  on the lower-cased string cannot misindex the submitted command.
- The facade no longer re-exports `RefusalTokenSpan` (no package-level
  consumer; the record is internal to `diagnostics`).
- A MIRROR comment pins the span module's by-name walk to its twin in
  `_is_self_kill`; the per-frame-memo invariant keeps that walk from being
  factored into a shared helper.
- Regression tests: buluoray's three target-shift repros plus a fuzz case
  assert the refusal falls back with no program/target fields; a
  dotted-capital case asserts the length-shift fallback; the plain case
  still names the real target. The target check kills a mutation that
  previously survived.

The verdict (allow/deny) is unchanged; only the reader-facing diagnostic
fields differ.

Co-authored-by: cixuuz <26782336+cixuuz@users.noreply.github.com>
@iamwhatever
iamwhatever force-pushed the rebase/15055-selfkill-diag-offsets branch from 2ae900a to e1fd790 Compare October 8, 2026 23:04
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 8, 2026
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 9, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Closing this. It never changes the allow/deny verdict -- it only adds a target name to the refusal message -- but it adds ~600 lines of command parsing to security code, and each fix has surfaced new edge cases (thanks @buluoray for the careful examples). The cost isn't worth the cosmetic gain. Thanks @chenmingwei23 for the review. Linked enhancement #14827 can stay open if someone wants a smaller approach.

@iamwhatever iamwhatever closed this Oct 9, 2026
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 9, 2026
@bolichen97
bolichen97 deleted the rebase/15055-selfkill-diag-offsets branch November 6, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security diagnostics: a self-protection-kill refusal reports the whole command as its span, so a false positive reads the same as a real kill

5 participants