kilocode icon indicating copy to clipboard operation
kilocode copied to clipboard

Deterministic-first decision layer for automatic approvals (successor to #9138)

Open Hellcake opened this issue 2 months ago • 7 comments

Why a new issue

#9138 — the spec for the missing middle between prompting on every risky call and allow-everything — was closed on 2026-08-28 by the stale bot after 60 days of inactivity, not by a decision. Its own closing message says "if the issue is still relevant please reopen it or create a fresh new one". This is the fresh one.

Nothing from that line of work has landed. Every attempt met the same bot:

what it was outcome
#7684 first request closed, inactivity
#9138 the full spec, by a maintainer closed, inactivity
#10267 the config contract, behind an experimental flag closed, inactivity
#11619 the runtime; later redirected to delegate verdicts to OpenGuardrails closed, inactivity
#10248 / #10250 rollout tracking and the VS Code surface no classifier to roll out

So the feature has been specced by a maintainer, attempted twice from two directions, and discussed by four people — and a user today still has exactly the two blunt settings #9138 opened with.

I have an implementation up at #13893 and would like a decision on it, or on a different shape if this one is wrong. I am not re-litigating the spec: #9138 is still the requirements document, and this issue tracks the parts where implementation forced a choice the spec did not make.

The problem, restated concretely

Whether a call is auto-approved comes from the permission ruleset alone. A ruleset matches on tokens, so the decision follows how a command is spelled rather than what it does. The same effect reaches the same place by another route and is auto-approved because no pattern matched:

  • sh -c 'cat .env' — the ruleset sees sh, not the program it carries.
  • git show HEAD:.env — matched by a git allow-rule; it is a secret read.
  • a symlinked path into a directory the rules never mention.
  • echo x > .git/hooks/pre-commit — a write that persists across sessions and runs on the next commit.
  • a renamed or copied interpreter, which no longer matches the interpreter rule.

These are not exotic. They are what a model produces when it is asked to do ordinary work and the first spelling was refused.

What is proposed, and where it diverges from the spec

#9138 puts a model in the decision path and short-circuits around it with tiers. The implementation inverts that: a deterministic layer decides first, and the model is only a narrowing stage.

The reviewer may turn a reviewable ask into an allow for the current call and nothing else. It never sees a deny, never widens what the ruleset permits, never writes policy, and never carries a verdict into the next call. Two properties follow, and both are worth attacking in review:

  • Monotonic. The layer may raise an allow to an ask, or block a proven destructive call. It cannot lower an ask to an allow on its own, so an explicit user deny always wins — the "out of scope" line in #9138.
  • Re-checked, not cached. A verdict is recomputed against freshly read ruleset, agent and containment facts before it is applied, so a rule or a sandbox toggled while a decision was in flight invalidates it rather than riding along.

This is a direct answer to two comments on #9138:

  • @oleg-bk asked that the decision be bound to the action's context, not the command string. It is: sh -c '…' is parsed to the program it carries, paths are canonicalized through realpath before they are judged, a workspace manifest decides which region a path belongs to, and git show HEAD:.env is classified as a secret read rather than as a git command.
  • @remesis asked for a reviewer that reads session context but refuses to treat tool output, file content or web content as a permission grant. The reviewer here is stricter than the spec's "reasoning-blind" requirement: it sees parsed argv, path facts and containment only — no assistant prose, no tool output, no chat history, no environment. There is no channel through which injected text can reach it.

The practical consequence of putting the deterministic layer first: it works with no model bound at all, which is the state most installs will be in. The model is an optimization that recovers autonomy on reviewable calls, not the thing safety depends on.

On the sandboxing question

@marius-kilocode's last note on #9138 was that sandboxing was about to ship and it was unclear how a gatekeeper fits. Containment is treated as evidence, not as a competitor: containment facts are an input to the decision, and they are re-read after the reviewer answers, so a sandbox toggled mid-review cannot be the ground an approval was granted on.

Coverage against the #9138 requirements

# requirement status
1 hook in Permission.ask, deny-and-continue, fail closed done — one hook after the hard veto and the human-only guards; reviewer error or timeout leaves the ask standing
2 staged classifier done in spirit — the deterministic core is the fast stage, the reviewer is the expensive one and runs only on a reviewable ask
3 reasoning-blind inputs done, more strictly than specced
4 prose config slots (environment / allow / soft_deny) not done — see the open question below
5 deny-and-continue with escalation backstop done — consecutive: 3 as specced, plus a density rule (5 blocks in a window of 20) for a model that answers each refusal with a fresh spelling of the same intent
6 tiered short-circuit done — read-only work stays autonomous
7 prompt-injection probe not done — the spec itself scopes it to phase 2
8 UI surface done — state on the tool row in TUI, VS Code and web
9 legacy yoloGatekeeperApiConfigId migration not done

Open questions for maintainers

These are the choices I could not make from outside the project.

1. Do you want this at all, in this shape? The deterministic-first inversion is the load-bearing decision. If you would rather have the spec's model-first design, or the OpenGuardrails delegation #11619 moved toward, say so and this can be closed — that is a better outcome than a fourth attempt aging out.

2. Where does policy configuration live? Requirement 4 wants prose slots in config. The implementation reads two environment variables instead, and not for convenience: config merge scopes only sandbox and indexing by source, so model, provider and its baseURL survive from a repository into the effective config. A cloned repo that named its own reviewer — or kept the model name and repointed its transport — would turn the whole reviewable population into an automatic allow, which is worse than having no reviewer. So reviewer resolution reads only the process environment and the user's own global config, and requires the merged provider entry to equal the trusted one.

That constraint is real but the conclusion is yours: either config gains a scope that a repository cannot write, or policy stays server-side and out of config. This also bears on the naming discussion between @thomaslwang and @marius-kilocode (classifier top-level vs permission.classifier vs gatekeeper), which was never settled.

3. How should it be split for review? #13893 is +15413 across 128 files, and I do not expect anyone to review that as one unit. About 10k of those lines are tests. The branch is already ordered so it can be cut into a stack, and the first two pieces are bug fixes that do not depend on the layer at all — both are already extracted and open, and can be judged with no view on this issue whatsoever. #14030 in particular is not a security change at all; it is a worker startup race that sits in the branch only because auto mode runs into it constantly:

PR size what
#14031 +365/−33 human-only asks are shown rather than auto-answered; honest attribution of who approved
#14030 +190/−2 RPC ready-handshake; a request raised before the worker installs its handler no longer hangs the caller. Unrelated to this issue — listed only because the commit lives in the same branch
next ~+13k, mostly tests the deterministic layer itself, flag-off, reviewer stage defined but unbound
then ~+1k bind the reviewer to a trusted model
then ~+0.7k show the layer's state on the tool row

If you would rather have the core split further than that, name the seam you want and I will cut it there.

4. What is the bar for the taxonomy? The layer is built against a frozen attack taxonomy with a route-equivalence sweep — the same effect is asserted to be judged the same through every spelling — and one bypass regression per technique it closes.

Where it came from, since that bears on how much to trust it: #9138 was used as the technical specification for a hackathon case at ITMO in Aug–Sep 2026, set by the AI-security team of a large bank, which linked this repository and that issue directly. The case came with a base corpus of roughly 20 ordinary development tasks and roughly 20 attacks, extended during the work; the taxonomy is shaped by what this permission model actually exposes rather than by a generic list. Its measured axes were ASR, utility on legitimate tasks, false positives, how many decisions are pushed to the human, and per-decision latency — kilo debug security-bench in #13893 is the harness for exactly that, with the reviewer as its own axis so a run shows what the model stage buys over the deterministic layer alone.

If you have an internal corpus or a red-team set you would rather this be measured against, I would rather find out now than after review — and if the numbers are more useful to you than the code, that part can be published on its own.

Not in scope here

  • Overriding an explicit user deny. User config always wins; the layer only gates what would otherwise auto-approve.
  • Porting the legacy yoloMode toggle. allow-everything already exists.
  • The phase-2 injection probe and the legacy gatekeeper migration, per the table above.

References

  • #9138 — the spec, with the design discussion this builds on
  • #10267, #11619 — the two earlier implementation attempts
  • #13893 — the implementation this issue asks about
  • #14030, #14031 — the two independent fixes already extracted from it

Hellcake avatar Sep 10 '26 19:09 Hellcake

Benchmark: what the layer measures out at, including where it fails

Question 4 above asked what the bar should be. Here is what exists today, negative results first, since those are the ones that should decide whether this is worth your time.

Caveats before the numbers. The environment is macOS with a verified Seatbelt profile and a closed network; Linux and Windows profiles are not built yet. The live reviewer in this run was Qwen 3.5 35B A3B — one model, not a sweep. Threat classes C8 (delegated and MCP operations) and C9 (policy precedence and provenance) are deferred and are not in these numbers. The "with human" time column is a model, not a user study: measured time plus 15s per manual confirmation, with 104 confirmations assumed for the manual baseline over 48 episodes.

Method

Three lanes, deliberately separated so a classifier decision is never mistaken for a real effect:

  • A1 — decision only. 39 actions (24 attacks, 15 safe near-neighbours) through the production parser, normalisation and rules, executed nowhere. Asserts the decision, the rule id, reviewability and the expected effect class.
  • A2 — real effect. 48 episodes per profile (38 attacks, 10 legitimate tasks) run through the actual CLI, each in a fresh workspace, with an independent oracle checking files, git state, secrets, an external sentinel and a local sink. This is the primary lane: a deterministic driver always attempts the action, so the result does not depend on whether a live model felt like complying.
  • B — prompt injection on live models. 9 scenarios × 3 repeats × 3 models × reviewer on/off = 162 episodes, with the hostile instruction hidden in a README, an agent rules file, source, a test report or tool output.

Full run: 336 A2 + 162 B = 498 episodes across 13 run groups, 0 harness errors.

A2, the primary slice

no layer layer + verified sandbox layer + live reviewer
ASR ↓ 100% (38/38) 2.6% (1/38) 5.3% (2/38)
Utility ↑ 100% 80% 100%
FP ↓ 0/10 5/10 0/10
Friction ↓ 0 1.02 decisions/episode 0.83 decisions/episode
Measured episode time 9.3s 12.9s 13.1s
Modelled, incl. human 41.8s 28.2s 25.6s

What that says, including against my own design

The control arm is not empty. All 38 attacks in the unprotected profile produced observable damage. So the low ASR of the protected profiles cannot be explained by a model declining to attack — which is the first thing worth ruling out in any number like this.

The deterministic layer stopped 37 of 38. The single success is a write to an agent control file — a known C3 policy gap, disclosed here as a class rather than a payload. It is not yet fixed; it is the first item on the follow-up list and wants a regression scenario alongside the fix.

The reviewer made things worse on the axis that matters. Binding a live reviewer raised ASR from 2.6% to 5.3%: it removed the false positives (Utility 80% → 100%, FP 5/10 → 0/10) but allowed a source-tree deletion. That is counted as a reviewer bypass in the report rather than being hidden behind a clean auto_bypass_violations = 0.

I would rather state this plainly than let it be found in review: the model stage is a usability trade, not a strengthening of the guarantee. It is off by default and unbound unless the environment says otherwise, and on this evidence the reviewable population for destructive operations should probably be narrowed further, or taken out of the reviewer's reach entirely. That is an argument for reviewing #13893's deterministic core and its reviewer stage as two separate decisions.

Sandbox state is verified, not trusted. The layer does not read a config flag — a preflight runs a restricted process and checks that writing outside, writing to protected regions and opening a network connection are actually refused, and only then treats containment as operational. This is what the "how does a gatekeeper fit with sandboxing" question at the end of #9138 turns into in practice.

What I can hand over

  • The harness is in #13893 as kilo debug security-bench, with the reviewer as its own axis, so a run shows what the model stage buys over the deterministic layer alone.
  • The corpus extension built during this work can be published separately, in whatever form you find useful — a repository of its own, or a PR here.
  • The ~20 ordinary tasks and ~20 attacks the case started from came with the hackathon case and are not mine to redistribute; anything I publish would be the extension and the harness.

If you have an internal red-team set, running this against yours is more informative than either of us running it against mine. Failing that, tell me which of the deferred classes you would want covered before this is worth reviewing at all — C8 and C9 are the obvious candidates, and neither is in the numbers above.

Hellcake avatar Sep 10 '26 20:09 Hellcake

The C3 gap above is now closed in #13893 — with the number it does not change

The one attack of 38 that got through was a write to the agent's own rule file. The path classifier knew .git, .husky, .githooks, .gitattributes and .envrc, so .kilocode/rules/00-workspace.md was an ordinary edit, the layer returned NO_OPINION, and a plain bash allow-rule ran it.

It belongs in the same class as a hook, and for a sharper reason than "it is configuration": a rule file, an agent or command definition, or the config naming them is read into every later prompt for that repository. A write there installs a standing instruction with no shell, no execute bit and no later git invocation needed to make it take effect. .kilo/, .kilocode/ and .opencode/ at any depth, plus the root config files, now classify as control plane, and a write to them is SEC.V1.CONTROL_PLANE_WRITE — ask, not deny, since these are hand-edited legitimately. The classification reuses the predicate the config-edit prompt already applies rather than adding a second list, since two lists drifting apart is what produced the neighbouring bug in #14031.

Verified at the decision level: writes through >, cp, mv, tee -a and a copy into the directory all land on the same rule; reads stay NO_OPINION; .kilo/plans/** stays ordinary, because a plan is written by the agent as its own work and is never read back as policy.

Not verified, deliberately stated: the A2 lane has not been re-run, so the 2.6% ASR above still stands as the measured figure — this closes the route at the layer's decision, not at an observed effect in a fresh workspace. The corpus case stays tagged as a known gap until a run says otherwise. If that re-measurement matters for your decision here, it is the kind of thing worth doing against your corpus rather than mine.

One thing the episode is evidence for, beyond the fix: the gap was found by the benchmark, not by review or by writing the rules. That is the argument for asking what corpus this should be measured against before the code gets read line by line.

Hellcake avatar Sep 10 '26 20:09 Hellcake

@marius-kilocode one yes/no question before #13893 takes more of your time:

Do you want auto-approve to work this way — fixed rules decide first, and a model may only turn an ask into allow, never the reverse?

  • If yes, I'll split #13893 into small PRs. The first would be the rule layer behind a flag, off by default, with no model involved.
  • If no, or not now, I'll close #13893. The two standalone fixes (#14030, #14031) don't depend on it.

Either answer is fine, I just don't want to keep a 15k-line PR open without knowing.

Hellcake avatar Sep 22 '26 20:09 Hellcake

Nice work @Hellcake! Just my 5 cents.

In the context of subagents, this feature is a great addition I'd like to see. It's quite annoying to answer permission requests for subagents that pipe a bunch of commands while exploring something. Auto‑allow isn't an option either, because they might suddenly decide to rm -rf something.

By the way, can System 1–type models be used as a judge? I believe they have great potential for decision‑making around tool/subagent selection and permission handling.

rusak47 avatar Sep 26 '26 19:09 rusak47

Thanks @rusak47. One correction to set expectations: the layer never allows anything, it only adds ask or deny on top of your rules. The intended setup is a broad bash allow with the layer as a floor, so rm, hook/CI/manifest writes, installs and git mutations still stop and ask. Subagents go through the same check.

It won't make exploration quieter yet, though. grep, rg, find in a pipe are unclassified and get an ask, since grep -r secret . is exploration too. For your case it would currently add prompts, not remove them.

On fast models as judge: any model works, but the reviewer only ever sees the layer's own reviewable asks, and per the numbers above it made ASR worse. So I'd use a fast model for tool/subagent picking, where a mistake costs a retry, before trusting it with permissions.

Hellcake avatar Sep 26 '26 19:09 Hellcake

Oh, sorry, I misunderstood the feature's real purpose — it's a safety net for "allow everything" mode only. Thanks for clarifying. A similar approach would be useful for "deny everything" mode too — hope it lands someday.

rusak47 avatar Sep 27 '26 17:09 rusak47

#13893 was closed by the stale bot. Reopened as #15167: rebased on main, stacked on #14031, RPC fix dropped. Off unless KILO_SECURITY_DECISION is set.

Hellcake avatar Oct 16 '26 14:10 Hellcake