Metadata-Version: 2.5
Name: bekci-guard
Version: 0.1.0
Summary: See what your coding agent can do without asking you: a read-only audit and a PreToolUse guard for Claude Code and Codex.
License-Expression: MIT
License-File: LICENSE
Keywords: ai-agents,claude-code,codex,devops,hooks,security
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# Bekci

**See what your coding agent can do without asking you.**
One command. Read-only. Nothing leaves your machine.

```sh
python3 bekci.py audit --card bekci-card.svg
```

![Recording of bekci audit](docs/demo.svg)

<sub>A real run, recorded with `script` and rendered by `docs/make_demo.py`.
The 12-second scan is shortened to 2 seconds; the first 25 lines are shown.</sub>

## Audit: what you get

This is a real run on my machine (3 Dec 2026, last 7 days, Claude Code + Codex,
my own rules plus the DevOps pack):

```text
Bekci audit · last 7 days · Claude Code + Codex

Allowed without asking                   1
  too broad                              0
  network access                         0
  can delete                             0
  contain a secret                       0
Shell commands replayed               7565
  Bekci would have asked               123
  Bekci would have denied                6
File and MCP calls replayed           4249
  Bekci would have asked                11
  Bekci would have denied                0
Guard hooks installed (of 2)             2
Unpinned MCP servers                     0
```

![Bekci audit card](docs/audit-card.svg)

The SVG card holds numbers only: no commands, no paths, no secrets. Its
renderer accepts integers and nothing else, and a test checks that commands,
paths and tokens from the session history never reach it. Share it or keep it.

What `audit` reads:

- **Allow rules** in `~/.claude/settings.json`, `settings.local.json`, the
  project settings of every project listed in `~/.claude.json`, and Codex
  `~/.codex/rules/*.rules` (`prefix_rule(... decision="allow")`). Each rule is
  flagged as too broad (any command, or a general-purpose program such as
  `curl:*` or `python3 *`), network, delete, or containing a secret.
- **Settings that skip approval**: Claude Code `bypassPermissions`, Codex
  `approval_policy = "never"`, `danger-full-access`, sandbox network access,
  and an automatic approvals reviewer. These are listed in the terminal.
- **History replay**: tool calls from the last N days in
  `~/.claude/projects/**/*.jsonl` (subagents included) and
  `~/.codex/sessions/**/*.jsonl`, run through Bekci's built-in rules and your
  Markdown rules. The terminal also shows the count per rule.
- **Setup**: whether a `PreToolUse` hook is installed for each tool, and MCP
  servers launched through `npx`, `uvx`, `bunx` or `docker` without a pinned
  version.

Options: `--days N` (default 7), `--card PATH` (the only file it writes),
`--home DIR` (audit another home directory). It never writes the decision
log and needs no network. Reading Codex `config.toml` needs Python 3.11+
(`tomllib`); on older Pythons that part is skipped and the summary says so.

Limits:

- Replay uses today's rules on past calls. It tells you what Bekci would
  decide now, not what happened then.
- Codex records a command only when it runs. Commands blocked before running
  are recovered from literal `exec_command({cmd: "..."})` calls in the session;
  commands built from variables at run time cannot be replayed.
- `codex exec --ephemeral` keeps no session file, so those runs cannot be
  replayed. If the hook's decision log exists (`BEKCI_LOG`), the summary
  prints its ask/deny/warn counts for the same window for comparison.
- Session formats change between versions. These were checked against the
  files Claude Code and Codex wrote on 3 Dec 2026.

## The hook

Bekci is a small Python `PreToolUse` safety hook for Claude Code and Codex. It
checks shell commands, file access, and supported MCP actions before a tool
runs. Built-in protections cover credential files, AI attribution trailers,
external actions, and destructive commands. Optional Markdown rules can add
`deny`, `ask`, or `warn` decisions; they cannot weaken built-in protections.
An active malformed rule fails closed: it produces an `ask` decision for its
event type (or all event types when `olay` is invalid). Codex converts that
`ask` to `deny`, like other approval requests.

## Requirements

- Python 3.10 or newer
- Claude Code or Codex with support for `PreToolUse` hooks

## Install

Clone or copy this directory to a stable location. Configure `BEKCI_LOG` and
`BEKCI_KURALLAR` if you want the log and rules stored elsewhere. By default,
the hook uses `bekci.log` and `bekci-kurallar.md` plus `bekci-devops.md` beside `bekci.py`.

### Claude Code

Add a `PreToolUse` command hook to your Claude Code settings. Replace
`/path/to/bekci` with the directory where you installed the files:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "python3 -I /path/to/bekci/bekci.py"
          }
        ]
      }
    ]
  }
}
```

### Codex

Add a `PreToolUse` command hook to `~/.codex/hooks.json` (or the hooks file
selected by your Codex setup). Replace `/path/to/bekci` with the install path:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python3 -I /path/to/bekci/bekci.py"
          }
        ]
      }
    ]
  }
}
```

The hook reads event JSON from standard input and writes a decision JSON to
standard output. Codex has no approval prompt for this hook, so `ask` decisions
are converted to `deny`.

## Additional rule format

Edit `bekci-kurallar.md` or set `BEKCI_KURALLAR` to another Markdown file.
Each rule uses a heading and four fields:

```markdown
### block-dangerous-command
- olay: bash
- karar: deny
- desen: `\bexample-command\b`
- mesaj: This command is disabled by local policy.
```

`olay` accepts `bash`, `file`, `yaz`, `mcp`, or `all`. `karar` accepts
`deny`, `ask`, or `warn`. Add `- kapalı` to disable a rule. Malformed active
rules are reported as `ask` decisions and logged as `kural-hata:<id>`; disabled
rules remain inactive. Validate the file before installing changes:

```sh
python3 -I bekci.py --check [path]
```

The command prints each malformed rule and exits 1 if any are found, otherwise
exits 0.

### DevOps rule pack

`bekci-devops.md` adds 25 rules for kubectl, Helm, Terraform/OpenTofu,
Docker, AWS/GCP/Azure CLIs, systemd, databases and git history rewrites.
They ask before deletes, production changes, applies, pushes to registries
and commands that print cloud tokens; printing Kubernetes secret values is
denied. It is on by default together with `bekci-kurallar.md`. If you set
`BEKCI_KURALLAR` yourself, list every file you want (separated by `:`):

```sh
export BEKCI_KURALLAR=/path/to/bekci/bekci-kurallar.md:/path/to/bekci/bekci-devops.md
```

A tool name only counts at the start of a command, so mentions in messages
are ignored. On 7 days of my own history (about 7,600 shell commands) the pack
flagged 3 commands; all three were quoted text, not real runs. Regex rules
cannot tell every quote from a command.

## Package

The `bekci-guard` package on PyPI has no dependencies and installs the
`bekci` and `bekci-guard` commands with both rule files. To run the audit
without installing anything (Python 3.10+; reading Codex TOML settings needs
3.11+):

```sh
uvx bekci-guard audit
```

From a checkout:

```sh
uvx --from . bekci-guard audit
```

When installed as a package, set `BEKCI_LOG` to a path you choose; the
default log sits next to `bekci.py`, inside the package's environment.

## Run tests

```sh
python3 -I test_bekci.py
python3 -I test_audit.py
python3 -I test_devops.py
```

## Configuration

- `BEKCI_KURALLAR`: additional Markdown rules files, separated by `:`
- `BEKCI_LOG`: path for decision logs; logs contain tool and rule identifiers,
  not command content

## License

MIT; see [LICENSE](LICENSE).
