Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AIPentest CyberStrikeAI Atomic Policy Swap

From Leeroopedia
Knowledge Sources
Domains Optimization, Concurrency, Agent_Design
Last Updated 2026-10-19 00:00 GMT

Overview

Swap fully-validated, immutable policy snapshots via `atomic.Pointer` so live tool-blocking checks never observe a half-updated rule set and never need a lock.

Description

The toolguard `Manager` guards every tool call with a regex-based blocking policy that can be reconfigured at runtime (admin edits rules via the UI/API). Instead of mutating rules in place under a mutex, the manager holds the active policy in an `atomic.Pointer[Policy]`. An update compiles and validates the entire new config into a fresh immutable `Policy` snapshot (rules pre-compiled into `*regexp.Regexp`), then atomically swaps the pointer with `Store`. Concurrent `Check` calls `Load` the current snapshot and read from it without any lock — the old snapshot stays consistent for any in-flight check until it finishes, then becomes garbage-collectable.

Usage

Apply this heuristic whenever a hot read path (per-tool-call policy evaluation) coexists with infrequent writes (admin reconfiguration). It is the standard pattern for the Toolguard_Manager_Check implementation and any live-reconfigurable guardrail whose reads must never block and whose writes must never tear.

The Insight (Rule of Thumb)

  • Action: Store the active policy in `atomic.Pointer[Policy]`; on update, compile + validate a new immutable snapshot, then `Store` it. Never mutate fields of the live policy in place.
  • Value: `Manager.policy atomic.Pointer[Policy]`; `Policy` is an immutable struct with pre-compiled `[]compiledRule`. Validation runs fully before the swap — a failed `Compile` leaves the old policy active.
  • Trade-off: Each update allocates a new snapshot and re-compiles every regex (O(rules)), but updates are rare and reads are lock-free and O(candidates × rules), so the read path wins decisively.
  • Pre-compile regexes once: Compile every rule's `*regexp.Regexp` at `Compile` time, never at `Check` time, so the hot path only runs `FindStringSubmatchIndex`.
  • Clone config on read: `Manager.Config()` returns a deep-cloned `Config` so callers cannot mutate the live snapshot by accident.

Reasoning

A mutex around `Check` would serialize every tool call across all agents behind admin reconfigurations; a plain field with no synchronization would let a concurrent `Update` tear a read mid-evaluation. The atomic-snapshot pattern gives:

  1. Lock-free reads: `Load` is a single atomic word read — zero contention on the hot per-tool-call path.
  2. Tear-free writes: the swap publishes a fully-validated snapshot or nothing; no reader ever sees a half-compiled rule set.
  3. Safe retention of old policy: if `Compile` fails, `Update` returns the error and never calls `Store`, so the previous good policy stays live (fail-closed on validation, fail-open on availability).
  4. Immutable snapshots enable safe concurrent inspection: in-flight `Check` calls hold a stable pointer even as a new one is published.

Observed in `internal/toolguard/toolguard.go:147-168` (`Manager` + `Update`) and `:180-185` (`Check` via `Load`), with `Policy` documented at `:54-58` as "an immutable snapshot safe for concurrent checks."

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment