Skip to main content

candor_classify/
lib.rs

1//! candor-classify — the curated effect classifier (crate+path -> effect), extracted to a STABLE
2//! crate so both the nightly `rustc_private` lint AND a stable backend share ONE source of truth
3//! (no drift). Pure string logic; no rustc internals. The effect vocabulary lives in candor-report.
4
5use candor_report::EFFECTS;
6
7/// The canonical CANDOR_POLICY DSL parser (SPEC §6.2), shared by the nightly gate and candor-query.
8pub mod policy;
9
10/// Project-supplied rules, consulted only when the built-in `classify` returns None.
11pub fn classify_extra(
12    crate_name: &str,
13    path: &str,
14    extra: &[(&'static str, bool, String)],
15) -> Option<&'static str> {
16    for (eff, is_crate, prefix) in extra {
17        let hit = if *is_crate { crate_name.starts_with(prefix.as_str()) } else { path.starts_with(prefix.as_str()) };
18        if hit {
19            return Some(eff);
20        }
21    }
22    None
23}
24
25/// The exact third-party crates `classify` has effect rules for, and the crate-name
26/// PREFIXES it recognizes. This is the single source of truth for "what candor knows":
27/// it is emitted beside the JSON report (`<prefix>.calibrated.json`) so the Claude Code
28/// receipt's coverage check reads candor's real coverage instead of a hand-copied list.
29/// Keep in lockstep with `classify` below — the `db_crates_are_calibrated` and
30/// `calibrated_crates_are_live` tests (in this crate's `tests` module) enforce both directions.
31pub const CALIBRATED_CRATES: [&str; 59] = [
32    // network (aws_config resolves credentials over the network on `.load()`;
33    // git2 remote ops — fetch/push/connect — contact the network; async_net is smol's net layer;
34    // pnet is raw L2/L3 packet capture)
35    "reqwest", "isahc", "ureq", "curl", "aws_config", "git2", "tokio_tcp", "tokio_udp", "async_net",
36    "async_nats", "lapin", "lettre", "tungstenite", "elasticsearch", "tonic", "rdkafka", "pnet",
37    // directory traversal (ignore = gitignore-aware walker, powers ripgrep/fd; its walk executors are Fs)
38    // + filesystem watching (notify = inotify/FSEvents/kqueue wrapper; powers watchexec/cargo-watch)
39    "ignore", "notify",
40    // database (see DB_CRATES in classify)
41    "sqlx", "rusqlite", "postgres", "tokio_postgres", "diesel", "redis", "mongodb",
42    "mysql", "mysql_async", "sea_orm", "deadpool_postgres",
43    // filesystem (async_fs = smol; fs_err = std::fs wrapper; tempfile; glob) / entropy /
44    // subprocess (async_process = smol; duct) / env (dotenvy/dotenv) / clock (time) / log / clipboard
45    "memmap2", "fs_err", "async_fs", "tempfile", "glob",
46    "rand", "getrandom", "fastrand",
47    // entropy: the password-hashing tier (salt mints + bcrypt's internal salt) + the OsRng source
48    "argon2", "bcrypt", "scrypt", "pbkdf2", "password_hash", "rand_core",
49    "portable_pty", "async_process", "duct",
50    "dotenvy", "dotenv",
51    "chrono", "time", "tracing", "log", "arboard",
52    // compiler diagnostic emission (a dylint lint's output) — see the Log rules in classify
53    "rustc_lint", "rustc_errors",
54    // raw syscalls via FFI — the syscall-name table that lights up the FFI-thin tier (nix is routed
55    // through the same table by leaf name, so a consumer of nix is covered without nix's own source)
56    "libc", "nix", "rustix",
57];
58
59pub const CALIBRATED_PREFIXES: [&str; 3] = ["aws_sdk_", "aws_smithy", "cap_"];
60
61/// Crates `classify` matches by PATH prefix rather than crate-name equality (their effectful modules
62/// are recognised, e.g. `tokio::net::`/`async_std::fs::`/`mio::net::`), so they're absent from
63/// `CALIBRATED_CRATES` (which the liveness test probes by crate name). The coverage check must still
64/// treat them as *covered* — otherwise it would mislabel the most common async crates as blind spots.
65pub const PATH_CALIBRATED_CRATES: [&str; 3] = ["tokio", "async_std", "mio"];
66
67/// Representative path tails (each appended to a crate name) that the `calibrated_crates_are_live`
68/// liveness test probes: at least one must match for every `CALIBRATED_CRATES` entry, else the entry is
69/// dead. Exported as ONE source of truth because the nightly lint crate (`src/lib.rs`) runs the SAME
70/// liveness test — when the two probe lists were duplicated they drifted, and a rule keyed on a
71/// distinctive tail (pnet `::datalink::channel`, ignore `::WalkBuilder::build_parallel`, notify
72/// `::RecommendedWatcher::new`) added to only one list silently broke the other crate's `cargo test`.
73pub const CALIBRATION_PROBE_TAILS: &[&str] = &[
74    "::X::send", "::X::execute", "::X::call", "::X::query", "::X::fetch_one", "::Remote::fetch",
75    "::datalink::channel", "::WalkBuilder::build_parallel", "::RecommendedWatcher::new",
76    "::X::connect", "::Utc::now", "::X::load", "::__private_api::log", "::tempfile", "::glob",
77    "::X::run", "::dotenv", "::random", "::emit", "::X::emit_span_lint", "::X::anything",
78    "::SaltString::generate", "::hash", "::OsRng::fill_bytes",
79    // verb-precise crates whose whole-crate rules were narrowed to the effectful surface (the pure
80    // accessors/ctors/data-types now return None), so the liveness probe must name an EFFECTFUL path:
81    "::Mmap::map", "::event", "::u32", "::Clipboard::get_text", "::spawn_command",
82];
83
84/// Database client crates whose execution verbs are I/O (see the DB branch in `classify`).
85/// Module-level so `db_crates_are_calibrated` can enforce `DB_CRATES ⊆ CALIBRATED_CRATES`.
86pub const DB_CRATES: [&str; 11] = [
87    "sqlx", "rusqlite", "postgres", "tokio_postgres", "diesel", "redis", "mongodb",
88    "mysql", "mysql_async", "sea_orm", "deadpool_postgres",
89];
90
91/// Pure file-descriptor *ownership-transfer* leaves. These ADOPT an already-open descriptor
92/// (`from_raw_fd`/`from_raw_socket`/`from_raw_handle`), EXTRACT/BORROW one
93/// (`into_raw_fd`/`into_raw_socket`/`into_raw_handle`, `as_raw_fd`/`as_raw_socket`/`as_raw_handle`),
94/// or UNWRAP an async wrapper back to its std type (`into_std`) — none of them issue a syscall or
95/// perform I/O. candor's cardinal sin is calling a PURE function effectful, and these collide with the
96/// coarse std-type PREFIX rules (`std::net::TcpStream`/`std::fs::File`/`std::os::unix::net` → Net/Fs/Ipc)
97/// even though the descriptor was opened ELSEWHERE. The portable_pty/async_process Exec rule already
98/// exempts `from_raw_fd`; this generalises the same carve-out across the net/fs/ipc prefix rules.
99/// (Found by a real-world sweep of tokio: `TcpStream::into_std`, `*::from_raw_fd`, `*::as_raw_fd` all
100/// fabricated Net/Fs/Ipc.)
101const PURE_FD_TRANSFER: &[&str] = &[
102    "from_raw_fd", "from_raw_socket", "from_raw_handle",
103    "into_raw_fd", "into_raw_socket", "into_raw_handle",
104    "as_raw_fd", "as_raw_socket", "as_raw_handle",
105    "into_std",
106    // `SocketAddr::from_pathname` (std/async-std unix net) builds an address STRUCT from a path —
107    // it opens no socket. The `std::os::unix::net` prefix rule below would otherwise fabricate Ipc
108    // on it. (Found sweeping socket2: `SockAddr::as_unix` → `from_pathname` reported Ipc.)
109    "from_pathname",
110];
111
112/// Classify a resolved callee by the crate it belongs to and its full path.
113pub fn classify(crate_name: &str, path: &str) -> Option<&'static str> {
114    // Pure fd ownership-transfer/extraction leaves are never an effect, regardless of which std I/O
115    // type they hang off — exempt them BEFORE the coarse prefix rules can fabricate Net/Fs/Ipc.
116    if PURE_FD_TRANSFER.contains(&path.rsplit("::").next().unwrap_or(path)) {
117        return None;
118    }
119    if crate_name.starts_with("aws_sdk_") || crate_name.starts_with("aws_smithy") {
120        // Only request dispatch is network I/O; builder setters/accessors are pure.
121        if path.ends_with("::send") || path.ends_with("::send_with") {
122            return Some("Net");
123        }
124        return None;
125    }
126    // aws-config resolves credentials/region on `.load()` — it reaches the IMDS metadata
127    // endpoint / STS over the network (and reads ~/.aws + env). Builders (`defaults()`,
128    // `SdkConfig::builder()`, `BehaviorVersion::latest()`) are pure; the `load` is the I/O.
129    // (Found hardening on a real app, ebman: `builder.load().await` was classified pure.)
130    if crate_name == "aws_config" {
131        if path.ends_with("::load") || path.ends_with("::load_defaults") {
132            return Some("Net");
133        }
134        return None;
135    }
136    // git2 (libgit2 FFI): remote operations contact the network; everything else is local
137    // to the .git directory. Match the remote verbs precisely — NOT bare `::clone`, which is
138    // the `Clone`-trait dup of a `Remote` handle (pure), not `Repository::clone`. (Found
139    // hardening on gitui: `remote.fetch`/`remote.push` were classified network-free — a git
140    // client reporting it makes no network calls.)
141    if crate_name == "git2" {
142        if path.ends_with("::fetch")
143            || path.ends_with("::push")
144            || path.ends_with("::download")
145            || path.ends_with("::connect")
146            || path.ends_with("::connect_auth")
147            || path.ends_with("::ls")
148            || path.ends_with("::upload")
149        {
150            return Some("Net");
151        }
152        return None;
153    }
154    // libc — raw syscalls via FFI. The FFI-thin tier (nix, and the syscall layer beneath rusqlite/git2)
155    // is invisible to a name classifier unless we model libc directly: a 35-crate calibration
156    // (eval/calibration) showed nix reporting ZERO library effects because every wrapper bottoms out in
157    // an unrecognised `libc::*` call. Classify by syscall name, but ONLY the UNAMBIGUOUS ones — the
158    // socket family is Net, path/dir syscalls are Fs, spawn/exec/wait is Exec, SysV/pipe IPC is Ipc,
159    // env/clock/entropy each their own. We deliberately SKIP the generic file-descriptor ops
160    // (read/write/close/lseek/dup/fcntl/ioctl/poll/select/epoll*/mmap): they operate on ANY fd — file,
161    // socket, or pipe — so a fixed label would mis-categorise as often as it helps. An honest
162    // no-classify (under-report) beats emitting the WRONG effect. Pure conversions (htons/inet_pton/
163    // gmtime) are also skipped.
164    //
165    // `nix` (the idiomatic SAFE libc wrapper, in ~every Rust systems/CLI crate) is routed through the
166    // SAME table: its functions keep the syscall leaf name (`nix::fcntl::open`, `nix::sys::socket::connect`,
167    // `nix::unistd::execvp`). Without this, a CONSUMER of nix analysed without nix's own source (the
168    // stable scanner, single-crate) sees `nix::*` cross-crate and under-reports — serialport-rs opens its
169    // device via `nix::fcntl::open` and reported ZERO Fs. The nightly lint reaches `libc::*` THROUGH nix's
170    // body; this gives the scanner the same coverage directly. (Found sweeping serialport-rs.)
171    // `rustix` is the same shape as nix but does RAW syscalls (no libc underneath), so its functions MUST
172    // be classified directly. Its leaf names are the syscall names too (`rustix::time::clock_settime`,
173    // `rustix::fs::mkfifoat`/`symlink`/`stat`, `rustix::net::connect`) — route it through the same table.
174    // The rustix-specific `*at`/variant leaves it doesn't share with libc just under-report (the safe
175    // direction). VALIDATED, not speculative: coreutils' `date` reads/sets the clock via
176    // `rustix::time::clock_getres`/`clock_settime` and reported Clock=0; the file I/O that goes through
177    // std::fs was already correct, which is why only the rustix-only effects (Clock/Ipc) were missing.
178    if crate_name == "libc" || crate_name == "nix" || crate_name == "rustix" {
179        let f = path.rsplit("::").next().unwrap_or(path);
180        // path / directory / metadata syscalls (incl. *64 and *at variants)
181        const FS: &[&str] = &[
182            "open", "open64", "openat", "openat2", "creat", "creat64", "stat", "stat64", "lstat",
183            "lstat64", "fstatat", "fstatat64", "newfstatat", "statx", "access", "faccessat",
184            "faccessat2", "mkdir", "mkdirat", "rmdir", "unlink", "unlinkat", "rename", "renameat",
185            "renameat2", "link", "linkat", "symlink", "symlinkat", "readlink", "readlinkat", "chmod",
186            "fchmodat", "chown", "lchown", "fchownat", "truncate", "truncate64", "ftruncate",
187            "ftruncate64", "opendir", "fdopendir", "readdir", "readdir64", "readdir_r", "closedir",
188            "rewinddir", "seekdir", "telldir", "scandir", "mkstemp", "mkstemps", "mkostemp", "mkdtemp",
189            "mknod", "mknodat", "chdir", "fchdir", "getcwd", "get_current_dir_name", "chroot",
190            "pivot_root", "statfs", "statfs64", "fstatfs", "fstatfs64", "statvfs", "fstatvfs", "mount",
191            "umount", "umount2", "fsync", "fdatasync", "sync", "syncfs", "sync_file_range", "fallocate",
192            "posix_fallocate", "posix_fadvise", "sendfile", "sendfile64", "copy_file_range", "flock",
193            "getdents", "getdents64", "utime", "utimes", "lutimes", "futimens", "utimensat", "futimesat",
194            "realpath",
195        ];
196        // socket family — these operate only on sockets, so Net is unambiguous (AF_UNIX domain isn't
197        // visible at the call, so a Unix socket reads as Net rather than Ipc; acceptable over-general).
198        const NET: &[&str] = &[
199            "socket", "setsockopt", "getsockopt", "bind", "listen", "accept", "accept4", "connect",
200            "shutdown", "send", "sendto", "sendmsg", "sendmmsg", "recv", "recvfrom", "recvmsg",
201            "recvmmsg", "getpeername", "getsockname", "getaddrinfo", "freeaddrinfo", "getnameinfo",
202        ];
203        // process creation / replacement / reaping
204        const EXEC: &[&str] = &[
205            "fork", "vfork", "clone", "clone3", "execl", "execlp", "execle", "execv", "execvp",
206            "execvpe", "execve", "execveat", "fexecve", "posix_spawn", "posix_spawnp", "system",
207            "popen", "pclose", "wait", "waitpid", "wait3", "wait4", "waitid",
208        ];
209        // pipes / FIFOs / SysV + POSIX message queues, semaphores, shared memory; socketpair (AF_UNIX)
210        const IPC: &[&str] = &[
211            "pipe", "pipe2", "mkfifo", "mkfifoat", "socketpair", "msgget", "msgsnd", "msgrcv", "msgctl",
212            "semget", "semop", "semtimedop", "semctl", "shmget", "shmat", "shmdt", "shmctl", "mq_open",
213            "mq_send", "mq_receive", "mq_timedsend", "mq_timedreceive", "mq_close", "mq_unlink",
214        ];
215        const ENV: &[&str] = &["getenv", "secure_getenv", "setenv", "putenv", "unsetenv", "clearenv"];
216        const CLOCK: &[&str] = &[
217            "time", "gettimeofday", "clock_gettime", "clock_getres", "nanosleep", "clock_nanosleep",
218            // SETTING the system clock is a clock effect too (was unclassified — found on coreutils `date`,
219            // which sets it via `clock_settime`).
220            "clock_settime", "settimeofday", "stime", "adjtime", "adjtimex", "clock_adjtime",
221        ];
222        const RAND: &[&str] = &["getrandom", "getentropy", "arc4random", "arc4random_buf", "arc4random_uniform"];
223        if FS.contains(&f) {
224            return Some("Fs");
225        }
226        if NET.contains(&f) {
227            return Some("Net");
228        }
229        if EXEC.contains(&f) {
230            return Some("Exec");
231        }
232        if IPC.contains(&f) {
233            return Some("Ipc");
234        }
235        if ENV.contains(&f) {
236            return Some("Env");
237        }
238        if CLOCK.contains(&f) {
239            return Some("Clock");
240        }
241        if RAND.contains(&f) {
242            return Some("Rand");
243        }
244        return None;
245    }
246    // C-library FFI bindings: libsqlite3 (under rusqlite) and libgit2 (under git2). Like the libc tier,
247    // these crates are thin Rust over a C library, so their real I/O is invisible until the C entry
248    // points are named. Match by the DISTINCTIVE C function name (`sqlite3_*` / `git_*`) via the call's
249    // LEAF — independent of the binding crate's alias: rusqlite calls `ffi::sqlite3_step`, git2 calls
250    // `raw::git_remote_fetch`, and the nightly lint resolves the same to `libsqlite3_sys`/`libgit2_sys`;
251    // all spellings share the leaf. Only the I/O-performing entry points are listed — the in-memory
252    // accessors (`sqlite3_bind_*`/`sqlite3_column_*`, `git_*_oid`/strarray/options builders) stay pure,
253    // so a non-listed `sqlite3_`/`git_` leaf returns None (under-report, never a wrong effect). Calibrated
254    // + validated against rusqlite 0.39 / git2 0.20 source (eval/calibration).
255    {
256        let leaf = path.rsplit("::").next().unwrap_or(path);
257        if let Some(rest) = leaf.strip_prefix("sqlite3_") {
258            let _ = rest;
259            // SQLite C API operations that touch the database (open/exec/step/prepare/backup/blob/wal).
260            const DB: &[&str] = &[
261                "sqlite3_open", "sqlite3_open_v2", "sqlite3_open16", "sqlite3_close", "sqlite3_close_v2",
262                "sqlite3_exec", "sqlite3_step", "sqlite3_prepare", "sqlite3_prepare_v2",
263                "sqlite3_prepare_v3", "sqlite3_prepare16", "sqlite3_prepare16_v2", "sqlite3_prepare16_v3",
264                "sqlite3_get_table", "sqlite3_backup_init", "sqlite3_backup_step", "sqlite3_backup_finish",
265                "sqlite3_blob_open", "sqlite3_blob_read", "sqlite3_blob_write", "sqlite3_blob_reopen",
266                "sqlite3_load_extension", "sqlite3_wal_checkpoint", "sqlite3_wal_checkpoint_v2",
267            ];
268            return DB.contains(&leaf).then_some("Db");
269        }
270        if leaf.starts_with("git_") {
271            // libgit2: remote/transport operations contact the network … (incl. submodule clone/update,
272            // which `git_clone`/fetch the subrepo over its remote — `allow_fetch` defaults on; an A/B on
273            // git2 0.20 caught `Submodule::update`/`clone` reporting no `Net`).
274            const NET: &[&str] = &[
275                "git_clone", "git_remote_connect", "git_remote_connect_ext", "git_remote_fetch",
276                "git_remote_download", "git_remote_upload", "git_remote_push", "git_remote_ls",
277                "git_submodule_clone", "git_submodule_update",
278            ];
279            // … and repository/index/odb/checkout/ref/config operations touch the on-disk .git store.
280            const FS: &[&str] = &[
281                "git_repository_open", "git_repository_open_ext", "git_repository_open_bare",
282                "git_repository_init", "git_repository_init_ext", "git_repository_discover",
283                "git_checkout_tree", "git_checkout_head", "git_checkout_index", "git_index_read",
284                "git_index_write", "git_index_write_tree", "git_index_write_tree_to",
285                "git_index_add_bypath", "git_index_add_all", "git_odb_open", "git_odb_read",
286                "git_odb_write", "git_odb_open_wstream", "git_odb_open_rstream",
287                "git_blob_create_fromdisk", "git_blob_create_fromworkdir", "git_blob_create_from_disk",
288                "git_blob_create_from_workdir", "git_blob_create_from_stream", "git_commit_create",
289                "git_commit_create_v", "git_reference_create", "git_reference_set_target",
290                "git_reference_delete", "git_config_open_default", "git_config_open_ondisk",
291                "git_config_add_file_ondisk", "git_tag_create", "git_treebuilder_write",
292                "git_packbuilder_write",
293            ];
294            if NET.contains(&leaf) {
295                return Some("Net");
296            }
297            if FS.contains(&leaf) {
298                return Some("Fs");
299            }
300            return None;
301        }
302        if leaf.starts_with("curl_") {
303            // libcurl (under the `curl` crate, called `curl_sys::curl_*`). Only the entry points that
304            // PERFORM network I/O: the blocking transfer (`curl_easy_perform`), raw socket send/recv,
305            // the HTTP/2 keepalive PING (`upkeep`), and the multi-interface transfer pumps. The large
306            // pure surface (setopt/init/cleanup/reset/getinfo/escape/multi_add_handle/fdset/info_read)
307            // stays unclassified, as do `curl_multi_wait`/`poll` (readiness WAIT on sockets, no payload —
308            // the loop's `perform` is the tagged boundary, per the I/O-boundary principle). An A/B on
309            // curl 0.4 caught the whole crate reporting ZERO Net (`Easy::perform` read as pure).
310            const NET: &[&str] = &[
311                "curl_easy_perform", "curl_easy_send", "curl_easy_recv", "curl_easy_upkeep",
312                "curl_multi_perform", "curl_multi_socket_action",
313            ];
314            return NET.contains(&leaf).then_some("Net");
315        }
316        if let Some(op) = leaf.strip_prefix("SSL_") {
317            // OpenSSL (libssl, under the `openssl`/`native-tls` crates, called `ffi::SSL_*`). The TLS
318            // handshake and record I/O run over the peer socket -> Net. Unlike libc read/write, an SSL_*
319            // op is ~always over a network BIO (the rare memory-BIO/sans-IO case is the honest exception
320            // we accept). The crypto surface (EVP_*/SHA*/AES*) and pure setup (SSL_CTX_new/SSL_set_fd) are
321            // NOT here; `BIO_*` is skipped (a BIO may be memory or socket). Validated vs openssl 0.9 source.
322            const SSL_NET: &[&str] = &[
323                "connect", "accept", "do_handshake", "read", "read_ex", "write", "write_ex", "peek",
324                "peek_ex", "shutdown",
325            ];
326            return SSL_NET.contains(&op).then_some("Net");
327        }
328    }
329    // HTTP clients use the same builder pattern as the AWS SDK: only the dispatch is
330    // I/O. (Found by the eval: ebman's reqwest calls to the Anthropic API + webhooks
331    // were silently classified network-free because reqwest wasn't recognized.)
332    if crate_name == "reqwest" || crate_name == "isahc" {
333        // The builder chain is pure; the dispatch (`::send`/`::execute`) is the I/O. PLUS the one-shot
334        // CONVENIENCE functions `reqwest::get` / `reqwest::blocking::get` / `isahc::get`, which send
335        // immediately — they're not the `Client::get` builder (a different path, `reqwest::Client::get`),
336        // so an exact match avoids false-positiving the builder. (Found running on `xh`: a one-shot
337        // `reqwest::get(url)` was classified network-free.)
338        if path.ends_with("::send")
339            || path.ends_with("::execute")
340            || path == "reqwest::get"
341            || path == "reqwest::blocking::get"
342            || path == "isahc::get"
343        {
344            return Some("Net");
345        }
346        return None;
347    }
348    if crate_name == "ureq" && path.ends_with("::call") {
349        return Some("Net");
350    }
351    // The `curl` crate (libcurl's safe binding — cargo's own HTTP client): the dispatch verbs are
352    // `perform` (Easy/Easy2/Transfer/Multi), raw-socket `send`/`recv`, the keepalive `upkeep`, and the
353    // multi-interface `action` (socket_action). The big setopt-style builder surface stays pure.
354    // `Multi::timeout` is deliberately NOT matched: `Easy::timeout` is a pure CURLOPT_TIMEOUT setter
355    // sharing the leaf — an under-report on the rare event-loop kick beats mis-tagging every consumer
356    // that sets a timeout. (Consumer-side companion to the curl_* FFI tier, same A/B finding.)
357    if crate_name == "curl"
358        && (path.ends_with("::perform")
359            || path.ends_with("::send")
360            || path.ends_with("::recv")
361            || path.ends_with("::upkeep")
362            || path.ends_with("::action"))
363    {
364        return Some("Net");
365    }
366    // The modern async-HTTP / TLS / QUIC / DNS stack — the LAYER reqwest/ureq/isahc build on, and that
367    // crates use DIRECTLY. Found by the independent-method differential on `oha` (2026-06-17): candor
368    // honestly DISCLOSED these as blind but never CLASSIFIED them, leaving real Net reaches uncovered.
369    // Verb-keyed (the pure type/builder/codec surface stays None) and CRATE-GATED, so generic verbs
370    // (request/connect/get/read/write/accept) never fabricate across unrelated crates. Same precision
371    // discipline as the reqwest/curl rules above; complements the scan_builder_entry_effect entries.
372    match crate_name {
373        // hyper 1.x client connection I/O (the builder/Body/Request types stay pure).
374        "hyper" if path.ends_with("::send_request") || path.ends_with("::handshake") => return Some("Net"),
375        // hyper-util's pooled legacy Client + its TCP connectors.
376        "hyper_util" if path.ends_with("::request") || path.ends_with("::connect") => return Some("Net"),
377        // hickory (trust-dns) resolver — issues DNS queries over the network.
378        "hickory_resolver"
379            if path.ends_with("::lookup_ip") || path.ends_with("::lookup") || path.ends_with("_lookup")
380                || path.ends_with("::resolve") => return Some("Net"),
381        // HTTP/3 over QUIC.
382        "h3" if path.ends_with("::send_request") || path.ends_with("::recv_data")
383            || path.ends_with("::recv_response") || path.ends_with("::send_data") => return Some("Net"),
384        // QUIC transport (UDP socket send/recv): connection setup, datagrams, AND the stream byte I/O
385        // (`RecvStream::read*` / `SendStream::write*` / `finish`). Opening a stream is caught above, but a
386        // fn that only HOLDS a stream and reads/writes it would otherwise read silent-pure (review: a Net
387        // under-report). Crate-gated to quinn, where these verbs are unambiguously the socket I/O.
388        "quinn" if path.ends_with("::connect") || path.ends_with("::accept") || path.ends_with("::open_bi")
389            || path.ends_with("::open_uni") || path.ends_with("::accept_bi") || path.ends_with("::accept_uni")
390            || path.ends_with("::send_datagram") || path.ends_with("::read_datagram")
391            || path.ends_with("::read") || path.ends_with("::read_chunk") || path.ends_with("::read_chunks")
392            || path.ends_with("::read_to_end") || path.ends_with("::write") || path.ends_with("::write_all")
393            || path.ends_with("::write_chunk") || path.ends_with("::write_chunks")
394            || path.ends_with("::finish") => return Some("Net"),
395        // TLS-over-TCP stream adapters — the actual socket handshake/I/O (the config/cert types stay pure).
396        "tokio_rustls" | "native_tls"
397            if path.ends_with("::connect") || path.ends_with("::accept") || path.ends_with("::handshake") =>
398            return Some("Net"),
399        // AF_VSOCK host<->guest sockets — inter-process / VM comms.
400        "tokio_vsock" if path.ends_with("::connect") || path.ends_with("::bind") || path.ends_with("::accept") =>
401            return Some("Ipc"),
402        // Loads the OS trust store from disk (cert files / keychain).
403        "rustls_native_certs" if path.ends_with("::load_native_certs") => return Some("Fs"),
404        // `rlimit` reads/mutates the process's kernel resource limits — the closest bucket is Env (host/
405        // process config); no dedicated process-state bucket exists, so getrlimit (read) and setrlimit
406        // (mutate) share it. NOTE: `num_cpus::get`/`get_physical` are deliberately NOT modeled — asking the
407        // OS for the CPU count is a near-pure topology query, and std's equivalent `thread::
408        // available_parallelism` classifies pure; modeling it as Env would spray Env over every thread-pool
409        // constructor (review: a high-noise over-report) for no capability a reviewer cares about.
410        "rlimit" if path.ends_with("::getrlimit") || path.ends_with("::setrlimit")
411            || path.ends_with("::increase_nofile_limit") => return Some("Env"),
412        _ => {}
413    }
414    // Message-queue clients fully encapsulate the socket (the underlying tokio::net lives
415    // inside the crate, unseen), so a user's connect/publish/consume calls ARE the I/O
416    // boundary — to a remote broker, hence Net. Match the broker round-trip verbs (snake_case
417    // methods); the CamelCase option/property builders stay pure. (Found hardening on consumer
418    // apps: lapin `basic_publish`/`queue_declare` and async-nats `publish`/`subscribe` were
419    // classified pure — a message-queue client reporting no I/O.)
420    if crate_name == "async_nats" {
421        if path.ends_with("::connect")
422            || path.contains("::publish")
423            || path.ends_with("::subscribe")
424            || path.ends_with("::queue_subscribe")
425            || path.contains("::request")
426            || path.ends_with("::flush")
427        {
428            return Some("Net");
429        }
430        return None;
431    }
432    if crate_name == "lapin" {
433        if path.ends_with("::connect")
434            || path.ends_with("::create_channel")
435            || path.contains("::basic_")
436            || path.contains("::queue_")
437            || path.contains("::exchange_")
438            || path.contains("::tx_")
439            || path.ends_with("::confirm_select")
440            || path.ends_with("::close")
441        {
442            return Some("Net");
443        }
444        return None;
445    }
446    // SMTP email — lettre's `Transport::send` is the network dispatch; Message building is
447    // pure. (Found hardening on a lettre consumer: `mailer.send(&email)` classified pure.)
448    if crate_name == "lettre" {
449        if path.ends_with("::send") || path.ends_with("::send_raw") {
450            return Some("Net");
451        }
452        return None;
453    }
454    // WebSockets — tungstenite (the modern successor to the old `websocket` crate). connect
455    // and the socket read/write/send are network; Message constructors are pure. (Found on a
456    // tungstenite consumer: connect + send + read classified pure.)
457    if crate_name == "tungstenite" {
458        if path.ends_with("::connect")
459            || path.ends_with("::read")
460            || path.ends_with("::write")
461            || path.ends_with("::send")
462            || path.ends_with("::close")
463            || path.ends_with("::flush")
464            || path.ends_with("::read_message")
465            || path.ends_with("::write_message")
466        {
467            return Some("Net");
468        }
469        return None;
470    }
471    // elasticsearch: request builders are pure; only the `.send()` dispatch is HTTP I/O
472    // (same shape as reqwest / the AWS SDK). (Found on an elasticsearch consumer.)
473    if crate_name == "elasticsearch" && path.ends_with("::send") {
474        return Some("Net");
475    }
476    // gRPC — tonic. The transport connect and the Grpc client RPC dispatch are network;
477    // codecs and request/response wrappers are pure. (connect repro-confirmed on a consumer;
478    // the unary/streaming RPC verbs are from the tonic::client::Grpc API.)
479    if crate_name == "tonic" {
480        if path.ends_with("::connect")
481            || path.ends_with("::unary")
482            || path.ends_with("::server_streaming")
483            || path.ends_with("::client_streaming")
484            || path.ends_with("::streaming")
485        {
486            return Some("Net");
487        }
488        return None;
489    }
490    // Kafka — rdkafka (FFI to librdkafka). Producer send + consumer poll/recv/subscribe/
491    // commit are network round-trips to the brokers. (API-calibrated + unit-tested; a real
492    // repro needs librdkafka/cmake, deferred.)
493    if crate_name == "rdkafka" {
494        if path.ends_with("::send")
495            || path.ends_with("::send_result")
496            || path.ends_with("::recv")
497            || path.ends_with("::poll")
498            || path.ends_with("::subscribe")
499            || path.ends_with("::commit")
500            || path.ends_with("::commit_message")
501            || path.ends_with("::commit_consumer_state")
502            || path.ends_with("::store_offset")
503            || path.ends_with("::seek")
504            || path.ends_with("::fetch_metadata")
505            || path.ends_with("::fetch_watermarks")
506            || path.ends_with("::flush")
507        {
508            return Some("Net");
509        }
510        return None;
511    }
512    // cap-std: capability-oriented std. I/O goes *through* a held capability handle
513    // (Dir/Pool/Clock/...), so these calls ARE the effect. Recognising them means a
514    // cap-std project's real I/O is detected and matches the capability it declared
515    // (via `declared_caps`/`capstd_cap`) — conformance against unforgeable capabilities.
516    if crate_name.starts_with("cap_") {
517        if path.contains("::net::Unix") || path.contains("::os::") {
518            return Some("Ipc");
519        }
520        if path.contains("::net") {
521            return Some("Net");
522        }
523        if path.contains("::time") {
524            return Some("Clock");
525        }
526        if path.contains("::fs") || crate_name == "cap_tempfile" || crate_name == "cap_directories" {
527            return Some("Fs");
528        }
529        return None;
530    }
531    // Local IPC (Unix-domain sockets) is I/O but not *network* — keep it distinct so
532    // CANDOR_NO_AMBIENT and audits don't conflate it with internet access. async-std puts its
533    // Unix sockets under `os::unix::net` (mirroring std); async-net (smol's net layer) under
534    // `unix`.
535    if path.starts_with("tokio::net::Unix")
536        || path.starts_with("std::os::unix::net")
537        || path.starts_with("async_std::os::unix::net")
538        || path.starts_with("async_net::unix")
539    {
540        return Some("Ipc");
541    }
542    // Raw packet capture / raw sockets — libpnet (the dominant low-level networking crate; powers
543    // bandwhich, sniffers, custom-protocol tools). `datalink::channel` opens an L2 socket and
544    // `transport::transport_channel` an L3/L4 raw socket — both ARE network I/O. Packet construction
545    // (pnet_packet / pnet_base, MacAddr, Ethernet frames…) is pure and stays unclassified. The actual
546    // frame read/write happens via methods on the returned Sender/Receiver (trait-object dispatch the
547    // syntactic backend can't resolve), so the channel-open call is the precise Net boundary. (Found
548    // scanning bandwhich — a packet sniffer — which reported Net 0.)
549    if crate_name == "pnet" || crate_name == "pnet_datalink" || crate_name == "pnet_transport" {
550        if path.ends_with("::channel") || path.ends_with("::transport_channel") {
551            return Some("Net");
552        }
553        return None;
554    }
555    // Directory traversal — `ignore` (BurntSushi's gitignore-aware walker; powers ripgrep, fd). The walk
556    // EXECUTORS read the directory tree from disk = Fs. Type-precise on purpose: the configuration builders
557    // (`OverrideBuilder::build`, `GitignoreBuilder::build`, the `WalkBuilder` setters) and `DirEntry`
558    // accessors are PURE — only `WalkBuilder::build`/`build_parallel` (which kick off the walk) and
559    // `WalkParallel::run` (which drives it) touch the filesystem. A bare `build` would wrongly flag the
560    // config builders. (Found scanning fd — a file finder — which reported Fs 2: its own `fs::read_dir`
561    // was caught, but the `ignore`-based traversal that IS fd was invisible cross-crate.)
562    if crate_name == "ignore" {
563        if path == "ignore::WalkBuilder::build"
564            || path == "ignore::WalkBuilder::build_parallel"
565            || path.ends_with("::WalkParallel::run")
566            // `add_ignore(path)` LOOKS like a config setter but reads that ignore file from disk at call
567            // time (it returns the read error) — unlike the pure `add_custom_ignore_filename(name)` which
568            // only stores a filename string. The lone Fs-touching builder method in the otherwise-pure setter
569            // surface, so it was silently pure under the covered-crate floor.
570            || path == "ignore::WalkBuilder::add_ignore"
571        {
572            return Some("Fs");
573        }
574        return None;
575    }
576    // Filesystem watching — `notify` (the de-facto fs-watch crate: watchexec, cargo-watch, mdbook). A
577    // watcher opens an OS notification handle (inotify / FSEvents / kqueue / ReadDirectoryChanges) and
578    // registers paths — observing filesystem state changes = Fs. The lifecycle boundary: any
579    // `*Watcher::new` constructor (RecommendedWatcher/PollWatcher/INotifyWatcher/FsEventWatcher/…), the
580    // `recommended_watcher` convenience fn, and the `watch`/`unwatch` registration verbs. `Config`/`Event`/
581    // `EventKind` data types stay pure. (Found scanning watchexec: its watcher-`create` read Fs 0.)
582    if crate_name == "notify" {
583        if path.ends_with("Watcher::new")
584            || path.ends_with("::recommended_watcher")
585            || path.ends_with("::watch")
586            || path.ends_with("::unwatch")
587        {
588            return Some("Fs");
589        }
590        return None;
591    }
592    // std DNS resolution — `("host", 80).to_socket_addrs()` / `std::net::lookup_host("host")` perform a
593    // real getaddrinfo query (Net), but the classify table covered only the socket I/O *types*, so they
594    // floored silently (sweep [37]; the syntactic engine modelled DNS only at the libc layer).
595    if path.ends_with("::to_socket_addrs")
596        || path == "std::net::lookup_host"
597        || path.ends_with("ToSocketAddrs::to_socket_addrs")
598    {
599        return Some("Net");
600    }
601    // Raw sockets. Match the I/O *types* only — `std::net` also holds pure data types
602    // (SocketAddr, IpAddr, …) whose construction must NOT be flagged.
603    if path.starts_with("std::net::TcpStream")
604        || path.starts_with("std::net::TcpListener")
605        || path.starts_with("std::net::UdpSocket")
606        || path.starts_with("tokio::net::")
607    {
608        // …but the PURE accessors read back local/option state — no network I/O — so the whole-type Net
609        // rule fabricated Net on them (sweep [24], the cardinal sin; mirrors the arboard/memmap2 accessor
610        // carve-outs). local_addr/peer_addr return bound/connected addresses; nodelay/ttl/take_error read
611        // socket options/state. Every genuine verb (connect/read/write/send/recv/accept) stays Net.
612        if path.ends_with("::local_addr")
613            || path.ends_with("::peer_addr")
614            || path.ends_with("::nodelay")
615            || path.ends_with("::ttl")
616            || path.ends_with("::take_error")
617        {
618            return None;
619        }
620        return Some("Net");
621    }
622    // Legacy tokio 0.1 socket crates — `tokio_tcp`/`tokio_udp` are *entirely* networking
623    // (no pure types to over-flag), so the whole crate is Net. (Found hardening on websocat,
624    // which is still on tokio 0.1: its `tokio_tcp::TcpStream::connect` was classified
625    // network-free — a network tool confidently reporting 0 Net.)
626    if matches!(crate_name, "tokio_tcp" | "tokio_udp") {
627        return Some("Net");
628    }
629    // The other async runtimes mirror tokio's module layout, and their `net` modules hold only
630    // socket I/O types (the pure `SocketAddr`/`IpAddr` are re-exports that resolve to `std::net`,
631    // so they're excluded by def-path). `mio` is the low-level non-blocking-socket layer under
632    // tokio/others; `async_net` is smol's net crate. Closes the async-std/smol/mio gap the
633    // tokio_tcp note flagged. (Calibrated by module structure — these crates ARE networking — not
634    // a live repro; the TCP/UDP types are defined in-crate so the def-path prefix is exact.)
635    if path.starts_with("async_std::net::")
636        || path.starts_with("mio::net::")
637        || crate_name == "async_net"
638    {
639        return Some("Net");
640    }
641    // Database clients. Like the AWS/HTTP builders, only the execution verbs are I/O;
642    // query *construction* is pure. Best-effort across crates (tune via CANDOR_CONFIG).
643    // Note: bare `::query` is deliberately omitted — it executes in postgres/rusqlite but
644    // only *builds* in sqlx, so including it would false-positive sqlx's `query()` builder.
645    if DB_CRATES.contains(&crate_name) {
646        // Postgres / SQLite-family clients: `query`/`batch_execute`/`prepare`/etc. ARE the
647        // execution (round-trips to the server). sqlx is the outlier where bare `query()`
648        // only BUILDS — it keeps the narrow set below. (Found by running on a real
649        // tokio-postgres app, pgman: candor had reported only 4 of ~20 DB call sites.)
650        if matches!(crate_name, "postgres" | "tokio_postgres" | "deadpool_postgres" | "rusqlite") {
651            const PG: [&str; 19] = [
652                "::query", "::query_one", "::query_opt", "::query_raw", "::execute",
653                "::batch_execute", "::simple_query", "::prepare", "::prepare_typed",
654                "::copy_in", "::copy_out", "::transaction", "::connect",
655                // rusqlite's dialect of the same verbs (a verb-probe found the CANONICAL rusqlite
656                // consumer API classifying pure): `query_row` is the one-row read, `query_map`/
657                // `query_and_then` the many-row reads, `execute_batch` is rusqlite's name for
658                // batch_execute, `prepare_cached` round-trips like prepare. `query_typed` is
659                // tokio_postgres 0.7.10+.
660                "::query_row", "::query_map", "::query_and_then", "::execute_batch",
661                "::prepare_cached", "::query_typed",
662            ];
663            if PG.iter().any(|v| path.ends_with(v)) {
664                return Some("Db");
665            }
666            // rusqlite only: opening the database IS the connection establishment (`Connection::
667            // open`/`open_in_memory`/`open_with_flags` — the embedded analog of `::connect`).
668            if crate_name == "rusqlite"
669                && (path.ends_with("::open")
670                    || path.ends_with("::open_in_memory")
671                    || path.ends_with("::open_with_flags"))
672            {
673                return Some("Db");
674            }
675            return None;
676        }
677        // redis: the way redis is ACTUALLY used is the high-level `Commands`/`AsyncCommands`
678        // traits (`con.get`/`set`/`hset`/`lpush`/…) — every method is a round-trip — plus
679        // connection establishment. The shared VERBS below only catch the low-level
680        // `cmd("GET").query(con)`, so without this a normal redis user's calls classify as
681        // PURE. (Found hardening on redis-rs: a fn doing `con.get`/`set` reported no effects.)
682        if crate_name == "redis"
683            && (path.contains("Commands::")
684                || path.contains("::get_connection")
685                || path.contains("::get_async_connection")
686                || path.contains("::get_multiplexed_async_connection")
687                // a live `ConnectionManager` round-trips (Db), but `ConnectionManagerConfig` is a pure
688                // in-memory builder (set_number_of_retries/set_max_delay) — exclude it (adversarial review).
689                // `ConnectionManager::clone` is an Arc refcount bump — no Db round-trip (sweep [27]).
690                || (path.contains("ConnectionManager") && !path.contains("ConnectionManagerConfig")
691                    && !path.ends_with("::clone"))
692                || path.ends_with("::query")
693                || path.ends_with("::query_async")
694                || path.ends_with("::req_command")
695                || path.ends_with("::req_packed_command")
696                || path.ends_with("::req_packed_commands"))
697        {
698            return Some("Db");
699        }
700        // mongodb: a document-store API with none of the SQL verbs — the user calls
701        // `coll.find_one`/`insert_one`/`aggregate`/… and `Client::with_uri_str`. Without
702        // these a mongodb user's calls classify PURE. (Found hardening: a fn doing
703        // `find_one`+`insert_one` reported no effects.) Handle accessors (name/namespace)
704        // and option/doc builders don't match these verbs, so they stay pure.
705        if crate_name == "mongodb" {
706            const MONGO: [&str; 27] = [
707                "::with_uri_str", "::connect", "::find", "::find_one", "::insert_one",
708                "::insert_many", "::update_one", "::update_many", "::delete_one",
709                "::delete_many", "::replace_one", "::aggregate", "::count_documents",
710                "::estimated_document_count", "::count", "::distinct", "::run_command",
711                "::find_one_and_update", "::find_one_and_delete", "::find_one_and_replace",
712                "::list_collections", "::list_collection_names", "::list_databases",
713                "::list_database_names", "::create_collection", "::create_index", "::watch",
714            ];
715            if MONGO.iter().any(|v| path.ends_with(v)) {
716                return Some("Db");
717            }
718            return None;
719        }
720        // mysql / mysql_async: the `query`/`exec` families + `get_conn`/`ping` execute
721        // immediately — no build-then-execute split like sqlx, so matching `::query` is safe
722        // here. Same DB-verb-dialect gap class as redis/mongodb; calibrated from the Queryable
723        // API (unit-tested; a real-app repro is the remaining confirmation).
724        if matches!(crate_name, "mysql" | "mysql_async") {
725            const MY: [&str; 16] = [
726                "::query", "::query_first", "::query_iter", "::query_map", "::query_fold",
727                "::query_drop", "::exec", "::exec_first", "::exec_iter", "::exec_map",
728                "::exec_fold", "::exec_drop", "::exec_batch", "::prep", "::ping", "::get_conn",
729            ];
730            if MY.iter().any(|v| path.ends_with(v)) {
731                return Some("Db");
732            }
733            return None;
734        }
735        // sea_orm: an ORM whose execution is split from building (like sqlx). The query
736        // BUILDERS (`Entity::find`, `Entity::insert`) are pure; execution happens at `.all`/
737        // `.one`/`.count`/`.stream` and `Insert/Update/Delete::exec`. The write path via an
738        // ActiveModel (`model.insert(db)`) executes too — distinguished from the `EntityTrait`
739        // builder by the trait in the path (`ActiveModelTrait::`). (Found hardening on a
740        // sea_orm consumer app: `.all(db)` reads and `ActiveModel::insert` writes were pure.)
741        if crate_name == "sea_orm" {
742            // sea_orm RE-EXPORTS sea_query (`sea_orm::sea_query::…`), whose builder algebra collides with
743            // the execution verbs: `Func::count(col)` builds a COUNT() expr, `Condition::all()` AND-groups
744            // filters, `Expr::count(…)` — all PURE, none touch a db. The `::all`/`::count`/`::one` execution
745            // rule fabricated Db on them (sweep [5]). sea_query is pure query construction end-to-end, so
746            // exclude the whole re-exported namespace first.
747            if path.contains("sea_query") {
748                return None;
749            }
750            if path.ends_with("::all")
751                || path.ends_with("::one")
752                || path.ends_with("::count")
753                || path.ends_with("::stream")
754                || path.ends_with("::exec")
755                || path.ends_with("::exec_with_returning")
756                || path.ends_with("::exec_without_returning")
757                || path.ends_with("::connect")
758                || path.ends_with("::execute")
759                || path.ends_with("::execute_unprepared")
760                || path.ends_with("::query_one")
761                || path.ends_with("::query_all")
762                || path.ends_with("::fetch_page")
763                || path.ends_with("::num_items")
764                || path.contains("ActiveModelTrait::")
765            {
766                return Some("Db");
767            }
768            return None;
769        }
770        // (Reached by sqlx + diesel — the build-vs-execute-split crates.) `first` is diesel's
771        // LIMIT-1 round trip and `load_iter` its 2.x streaming execution; `fetch_many` is sqlx's
772        // multi-result stream. All crate-gated, so a std `Vec::first` never resolves here.
773        const VERBS: [&str; 19] = [
774            "::execute", "::query_row", "::query_map", "::query_one", "::fetch_one",
775            "::fetch_all", "::fetch_optional", "::fetch", "::fetch_many", "::connect",
776            "::acquire", "::begin", "::commit", "::rollback", "::load", "::load_iter",
777            "::first", "::get_result", "::get_results",
778        ];
779        if VERBS.iter().any(|v| path.ends_with(v)) {
780            return Some("Db");
781        }
782        return None;
783    }
784    // std::path::Path / PathBuf STAT-family methods hit the filesystem (each is a stat/readlink/
785    // readdir syscall) — unlike the rest of the std::path surface, which is pure string manipulation
786    // (join/file_name/extension/parent/…). Verb-precise so the scanner's receiver inference can safely
787    // route a `path.symlink_metadata()` method call here. (A blackout screen caught gix-dir — an entire
788    // directory WALKER — reporting ZERO Fs because all its I/O is Path-method calls; same class as
789    // fd's residual `Path::symlink_metadata` under-report.)
790    if let Some(m) = path
791        .strip_prefix("std::path::Path::")
792        .or_else(|| path.strip_prefix("std::path::PathBuf::"))
793    {
794        const STAT: &[&str] = &[
795            "metadata", "symlink_metadata", "canonicalize", "read_link", "read_dir", "exists",
796            "try_exists", "is_file", "is_dir", "is_symlink",
797        ];
798        return STAT.contains(&m).then_some("Fs");
799    }
800    // Filesystem. `tokio::fs`/`async_std::fs` are the async mirrors of `std::fs`; `async_fs` is
801    // smol's fs crate; `fs_err` is a drop-in `std::fs` wrapper (its whole surface is fs I/O).
802    if path.starts_with("std::fs::")
803        || path.starts_with("tokio::fs::")
804        || path.starts_with("async_std::fs::")
805        || crate_name == "async_fs"
806        || crate_name == "fs_err"
807    {
808        return Some("Fs");
809    }
810    // memmap2: only `MmapOptions::map*` (and the in-place `Mmap::flush`/`make_*` protection
811    // changes / `remap`) actually issue the mmap/msync/mprotect/mremap syscall = Fs. The rest of the
812    // crate is PURE: `MmapOptions::new`/setters BUILD the request, and once a region is mapped, reads
813    // over it (`Mmap::len`/`is_empty`/`as_ptr`/`as_mut_ptr`/`deref` into the byte slice) are plain
814    // memory access with no syscall. Whole-crate Fs fabricated Fs on those reads (a `m.len()` the
815    // scanner's receiver inference routes to `memmap2::Mmap::len`). Match the syscall-issuing verbs;
816    // everything else returns None (pure). `map*` covers `map`/`map_mut`/`map_exec`/`map_copy`/
817    // `map_copy_read_only`/`map_raw`/`map_raw_read_only`/`map_anon`.
818    if crate_name == "memmap2" {
819        let m = path.rsplit("::").next().unwrap_or(path);
820        if m.starts_with("map")
821            || m == "flush"
822            || m == "flush_async"
823            || m == "flush_range"
824            || m == "flush_async_range"
825            || m == "remap"
826            || m.starts_with("make_")
827            || m == "advise"
828            || m == "advise_range"
829            || m == "lock"
830            || m == "unlock"
831        {
832            return Some("Fs");
833        }
834        return None;
835    }
836    // tempfile: creating a temp file/dir touches the disk. Match the create/persist verbs (the
837    // `Builder` setters — prefix/suffix/rand_bytes — stay pure). `persist`/`keep` rename/retain
838    // the file on disk; `close` removes it.
839    if crate_name == "tempfile"
840        && (path.ends_with("::tempfile")
841            || path.ends_with("::tempfile_in")
842            || path.ends_with("::tempdir")
843            || path.ends_with("::tempdir_in")
844            || path.ends_with("NamedTempFile::new")
845            || path.ends_with("NamedTempFile::new_in")
846            || path.ends_with("TempDir::new")
847            || path.ends_with("TempDir::new_in")
848            || path.ends_with("::persist")
849            || path.ends_with("::persist_noclobber")
850            || path.ends_with("::keep"))
851    {
852        return Some("Fs");
853    }
854    // glob: walks the filesystem to expand a pattern (the returned iterator reads directories).
855    // `Pattern::matches` is pure string matching — match only the directory-walking entry points.
856    if crate_name == "glob" && (path.ends_with("::glob") || path.ends_with("::glob_with")) {
857        return Some("Fs");
858    }
859    // Password-hashing / KDF crates — the entropy tier (the TS engine's CTA lesson: an invisible
860    // argon2 landed on exactly the call a security review cares about). In this engine's
861    // verb-precise style the ENTROPY is the salt mint: `SaltString::generate(OsRng)` in the
862    // password-hash API family, and bcrypt's `hash`/`hash_with_result` (salt minted internally).
863    // Verification and explicit-salt hashing are deterministic recomputation — pure. `rand_core`
864    // carries the OsRng source itself (otherwise the most common salt mint is invisible).
865    if matches!(crate_name, "argon2" | "scrypt" | "pbkdf2" | "password_hash") {
866        if path.contains("SaltString::generate") {
867            return Some("Rand");
868        }
869        return None;
870    }
871    if crate_name == "bcrypt" {
872        if path.ends_with("::hash") || path.ends_with("::hash_with_result") {
873            return Some("Rand");
874        }
875        return None;
876    }
877    if crate_name == "rand_core" {
878        if path.contains("OsRng")
879            || path.ends_with("::next_u32")
880            || path.ends_with("::next_u64")
881            || path.ends_with("::fill_bytes")
882        {
883            return Some("Rand");
884        }
885        return None;
886    }
887    // Randomness / entropy. `getrandom`/`fastrand` are effectful end-to-end. `rand` is NOT — it
888    // mixes entropy/generation (effectful) with *pure* distribution constructors (`Uniform::new`,
889    // `Normal::new`) and deterministic-seed constructors (`seed_from_u64`). Flagging the whole crate
890    // over-reported those as `Rand`; match only the calls that actually consume randomness — the
891    // entropy sources (`OsRng`, `thread_rng`/`rng`, `from_entropy`/`from_os_rng`) and the generation
892    // verbs (`gen*`/`random*`/`fill*`/`sample*`/`next_u*`). A `Uniform::new` is now correctly pure.
893    if crate_name == "getrandom" {
894        return Some("Rand");
895    }
896    // fastrand: like `rand`, it mixes entropy-consuming generation (effectful) with PURE deterministic
897    // pieces. `Rng::with_seed(42)` is a DETERMINISTIC seeded constructor (consumes no entropy — the same
898    // seed gives the same stream), and `Rng::fork`/`Rng::clone` just split/copy existing state. Those are
899    // PURE; whole-crate Rand fabricated Rand on them. The effect is the value-drawing methods (`u32`/
900    // `usize`/`bool`/`f64`/`char`/`alphanumeric`/`choice`/`choose_multiple`/`shuffle`/`fill`/the range
901    // forms) AND the entropy-seeded entry points: bare `Rng::new()` (seeds from the global entropy-backed
902    // generator), `fastrand::seed`, and the top-level `fastrand::u32(..)` free functions (which draw from
903    // the thread-local generator). `with_seed` is exempted explicitly; any other method on an `Rng`
904    // (i.e. a value draw) is Rand.
905    if crate_name == "fastrand" {
906        let m = path.rsplit("::").next().unwrap_or(path);
907        // Provably pure: deterministic seeded ctor + state split/copy.
908        if m == "with_seed" || m == "fork" || m == "clone" {
909            return None;
910        }
911        // Everything else fastrand exposes either draws a value or seeds from entropy → Rand. (The crate
912        // has no pure data types beyond the `Rng` handle itself, so a non-draw stray would have to be a
913        // method we don't recognise — keep the effect, the safe direction.)
914        return Some("Rand");
915    }
916    if crate_name == "rand" {
917        let rng_verb = path.ends_with("::gen")
918            || path.ends_with("::gen_range")
919            || path.ends_with("::gen_bool")
920            || path.ends_with("::gen_ratio")
921            || path.ends_with("::random")
922            || path.ends_with("::random_range")
923            || path.ends_with("::random_bool")
924            || path.ends_with("::random_ratio")
925            || path.ends_with("::random_iter") // rand 0.9 iterator generator
926            || path.ends_with("::gen_iter")
927            || path.ends_with("::fill")
928            || path.ends_with("::fill_bytes")
929            || path.ends_with("::try_fill")
930            || path.ends_with("::try_fill_bytes")
931            || path.ends_with("::sample")
932            || path.ends_with("::sample_iter")
933            || path.ends_with("::next_u32")
934            || path.ends_with("::next_u64")
935            || path.ends_with("::thread_rng")
936            || path.ends_with("::rng")
937            || path.ends_with("::from_entropy")
938            || path.ends_with("::from_os_rng");
939        // `OsRng` is the OS entropy SOURCE, but `clone`/`fork`/`default` just copy or construct the
940        // (zero-sized) handle and draw no entropy — pure, exactly like the `fastrand` arm's clone/fork
941        // exemption above. The actual draws (`fill_bytes`/`next_u*`/…) are caught by `rng_verb`. Without
942        // this exemption the blanket `contains("OsRng")` fabricated `Rand` on `OsRng::clone` (adversarial
943        // review: OsRng is a unit struct, cloning consumes nothing).
944        let m = path.rsplit("::").next().unwrap_or(path);
945        let os_rng = path.contains("OsRng") && !matches!(m, "clone" | "fork" | "default");
946        if rng_verb || os_rng {
947            return Some("Rand");
948        }
949        return None;
950    }
951    // Subprocess spawning. `tokio::process` is the async mirror of `std::process` — it exists
952    // only to spawn/control subprocesses (`Command`/`Child`, no pure data types like std's
953    // `Stdio`/`ExitStatus`/`exit`), so spawning through it is Exec just the same. Without this an
954    // async app's `tokio::process::Command::new(..).spawn()` classified pure — a silent under-report
955    // of subprocess execution, the dangerous direction (mirrors the tokio::fs/tokio::net coverage).
956    if path.starts_with("std::process::Command")
957        || path.starts_with("std::process::Child")
958        || path.starts_with("tokio::process::Command")
959        || path.starts_with("tokio::process::Child")
960        || path.starts_with("async_std::process::Command")
961        || path.starts_with("async_std::process::Child")
962    {
963        // PURE read-backs of the builder's stored fields / the cached pid — no spawn, no syscall — so the
964        // whole-type Exec rule fabricated Exec on them (sweep [23]; mirrors the portable_pty getter carve-
965        // out just below). get_program/get_args/get_envs/get_current_dir read the Command; Child::id reads
966        // the cached pid. Every genuine verb (new/spawn/output/status/wait/kill) stays Exec.
967        if path.ends_with("::get_program")
968            || path.ends_with("::get_args")
969            || path.ends_with("::get_envs")
970            || path.ends_with("::get_current_dir")
971            || path.ends_with("Child::id")
972        {
973            return None;
974        }
975        return Some("Exec");
976    }
977    // portable_pty / async_process are whole-crate Exec EXCEPT for the proven-pure surface they expose:
978    // the `CommandBuilder` GETTERS (`get_argv`/`get_cwd`/`get_env`/`as_unix_command_line`…) read back
979    // configuration, and the PURE DATA types (`PtySize::default`, `ExitStatus`/`Stdio`/`CommandBuilder`
980    // construction/setters). The earlier `is_cmd_naming_method` fix stopped the head-refinement LEAK, but
981    // the BASE Exec still fabricated on these accessors (a `cmd.get_cwd()` the scanner routes to
982    // `portable_pty::CommandBuilder::get_cwd`). Subtract the read-back getters and the obvious pure
983    // ctors/setters; the spawn/wait/exec surface (`spawn_command`/`openpty`/`wait`/`kill`/`exec`…) keeps
984    // Exec. SUBTRACT only what is provably pure — when unrecognised, KEEP Exec (the safe direction).
985    if crate_name == "async_process" || crate_name == "portable_pty" {
986        let m = path.rsplit("::").next().unwrap_or(path);
987        // configuration read-back getters — pure (no spawn).
988        if m.starts_with("get_") || m == "as_unix_command_line" {
989            return None;
990        }
991        // pure data-type ctors/setters/derives that NAME no program and spawn nothing.
992        if matches!(
993            m,
994            "default" | "new" | "piped" | "null" | "inherit" | "from_raw_fd"
995                | "arg" | "args" | "arg0" | "env" | "envs" | "env_clear" | "env_remove"
996                | "cwd" | "current_dir" | "rows" | "cols"
997                | "clone" | "fmt" | "eq" | "ne" | "hash"
998        ) {
999            return None;
1000        }
1001        return Some("Exec");
1002    }
1003    // duct: a subprocess-orchestration crate. `cmd()`/`cmd!` only *build* an Expression; the
1004    // spawn/wait happens at `run`/`read`/`start`. Match the execution verbs, not the builder.
1005    if crate_name == "duct"
1006        && (path.ends_with("::run")
1007            || path.ends_with("::read")
1008            || path.ends_with("::start")
1009            || path.ends_with("::read_chars"))
1010    {
1011        return Some("Exec");
1012    }
1013    if path.starts_with("std::env::") {
1014        return Some("Env");
1015    }
1016    // dotenvy / dotenv: load environment variables (reading a `.env` file and mutating the process
1017    // environment). Match the load/read entry points; `Error`/builder types stay pure.
1018    if matches!(crate_name, "dotenvy" | "dotenv")
1019        && (path.ends_with("::dotenv")
1020            || path.ends_with("::dotenv_override")
1021            || path.ends_with("::from_path")
1022            || path.ends_with("::from_path_override")
1023            || path.ends_with("::from_filename")
1024            || path.ends_with("::from_filename_override")
1025            || path.ends_with("::from_read")
1026            || path.ends_with("::from_read_override")
1027            || path.ends_with("::load")
1028            || path.ends_with("::var")
1029            || path.ends_with("::vars"))
1030    {
1031        return Some("Env");
1032    }
1033    // Wall-clock reads. Match the `now` accessor precisely (ends_with), not any path
1034    // containing the substring "now". The `time` crate (distinct from `std::time`/`chrono`)
1035    // reads the clock via `now_utc`/`now_local` (and the deprecated `Instant::now`).
1036    if (crate_name == "chrono" || path.starts_with("std::time::")) && path.ends_with("::now") {
1037        return Some("Clock");
1038    }
1039    if crate_name == "time"
1040        && (path.ends_with("::now_utc") || path.ends_with("::now_local") || path.ends_with("::now"))
1041    {
1042        return Some("Clock");
1043    }
1044    // `tracing`: same principle as the `log` facade below — the crate's TYPES are pure data, so match
1045    // the emit, not the whole crate. The actual program output is the macro-expanded
1046    // `Subscriber::event`/`event!`/`Span::*enter*` dispatch and the `Span::new*`/`Span::record`
1047    // recording path that drives the subscriber. The data-type accessors — `Level::as_str`,
1048    // `Span::is_disabled`/`metadata`/`id`, and constructing/reading `Level`/`LevelFilter`/`Span`/
1049    // `Event`/`Metadata`/`Field`/`FieldSet`/`Id` — are PURE (no output is produced), so whole-crate Log
1050    // fabricated Log on them. Match the emit verbs; everything else returns None.
1051    if crate_name == "tracing" {
1052        let m = path.rsplit("::").next().unwrap_or(path);
1053        // The user-facing emit MACROS (`tracing::info!`/`warn!`/…) — candor-scan is pre-expansion, so it
1054        // sees the raw macro path `tracing::info`, not the expanded `__tracing`/`Subscriber::event` the
1055        // deep (post-expansion) engine sees. Only the macro names; the pure DATA types (Level/Span/Event)
1056        // have other tails and stay None.
1057        if m == "trace" || m == "debug" || m == "info" || m == "warn" || m == "error"
1058            || m == "trace_span" || m == "debug_span" || m == "info_span" || m == "warn_span"
1059            || m == "error_span" || m == "span"
1060            || m == "event"
1061            || m == "new_span"
1062            || m == "record"
1063            || m == "record_follows_from"
1064            || m == "enter"
1065            || m == "exit"
1066            || m == "in_scope"
1067            || m == "entered"
1068            || path.contains("::__macro_support")
1069            || path.contains("::__tracing")
1070            || path.contains("Subscriber::event")
1071            || path.contains("Subscriber::new_span")
1072            || path.contains("Subscriber::enter")
1073            || path.contains("Subscriber::exit")
1074        {
1075            return Some("Log");
1076        }
1077        return None;
1078    }
1079    // The `log` facade: its macros route through `log::__private_api`; the crate's types
1080    // (`Level`, `LevelFilter`) are pure, so match the logging entry, not the whole crate.
1081    if crate_name == "log" {
1082        // Expanded macro form (deep engine) OR the raw user-facing macro names (candor-scan, pre-expansion).
1083        // `log::Level`/`LevelFilter`/`Record`/`Metadata` have other tails, so the type surface stays pure.
1084        let m = path.rsplit("::").next().unwrap_or(path);
1085        if path.contains("::__private_api")
1086            || m == "error" || m == "warn" || m == "info" || m == "debug" || m == "trace" || m == "log"
1087        {
1088            return Some("Log");
1089        }
1090    }
1091    // Compiler diagnostic emission — the ONE genuinely effectful operation in the otherwise-pure
1092    // rustc_* surface (a dylint lint's actual OUTPUT: it writes warnings/errors to the compiler's
1093    // diagnostic sink). Classified `Log` (same family as `tracing`/`log` — program output). Match the
1094    // emission verbs precisely; rustc_lint/rustc_errors are mostly pure types (Lint, LintId, the Diag
1095    // BUILDERS), and only the terminal `emit`/`emit_span_lint` actually produces output.
1096    if crate_name == "rustc_lint"
1097        && (path.ends_with("::emit_span_lint")
1098            || path.ends_with("::span_lint")
1099            || path.ends_with("::span_lint_hir"))
1100    {
1101        return Some("Log");
1102    }
1103    if crate_name == "rustc_errors"
1104        && (path.ends_with("::emit")
1105            || path.ends_with("::emit_diagnostic")
1106            || path.ends_with("::emit_now"))
1107    {
1108        return Some("Log");
1109    }
1110    // arboard: the effectful surface is the `Clipboard` handle's read/write verbs (each talks to the
1111    // OS clipboard / X11/Wayland/Win32/NSPasteboard server). The data types — chiefly `arboard::Error`
1112    // (whose `Display`/`to_string` formatting is pure) and the `ImageData`/`GetExtLinux`/`SetExtLinux`
1113    // option types — are PURE, so whole-crate Clipboard fabricated Clipboard on e.g. an error
1114    // `to_string()`. Match the handle verbs; everything else returns None. `Clipboard::new` opens the
1115    // connection to the clipboard server, so it's an effect too; `get`/`set` return the
1116    // builder-then-read `Get`/`Set` cursors whose `text`/`image`/`html` terminals do the I/O.
1117    if crate_name == "arboard" {
1118        let m = path.rsplit("::").next().unwrap_or(path);
1119        if m == "new"
1120            || m == "get"
1121            || m == "set"
1122            || m == "clear"
1123            || m == "get_text"
1124            || m == "set_text"
1125            || m == "set_html"
1126            || m == "get_image"
1127            || m == "set_image"
1128            || m == "text"
1129            || m == "image"
1130            || m == "html"
1131        {
1132            return Some("Clipboard");
1133        }
1134        return None;
1135    }
1136    None
1137}
1138
1139pub fn cap_from_name(name: &str) -> Option<&'static str> {
1140    EFFECTS.iter().copied().find(|e| *e == name)
1141}
1142
1143/// Refine the `Exec` cliff (spec §4 ⟨0.5⟩): the effects a *literal, statically-known* subprocess
1144/// head implies, matched by basename (`/usr/bin/curl` → `curl`). The head's effects are ADDED to a
1145/// caller that already carries `Exec` (a subprocess is still spawned — `Exec` is never dropped); an
1146/// unrecognised or dynamically-built head returns `&[]` and keeps the bare cliff (never guess). A
1147/// **candor engine** reads `Fs`/`Env` only — spec §7 item 12 (the analyzer self-boundary) guarantees
1148/// that, so that case is spec-supplied, not curation. The rest is a small curated table under the
1149/// same under-report rule as the crate classifier. INVARIANT: every head here is an external tool
1150/// that does NOT run the analysed project's own code (so `make`/`npm`/`cargo` are deliberately
1151/// absent — they stay the cliff). The reference engines share this table so the `Exec` boundary —
1152/// the one boundary every engine hits — refines identically (the §4-consistency argument).
1153pub fn classify_command_head(cmd: &str) -> &'static [&'static str] {
1154    // Only UNAMBIGUOUS single-effect tools belong here. A multi-modal head (`git status` is local,
1155    // `git push` is Net; `rsync` local-vs-remote) would FABRICATE the effect for its common case —
1156    // the under-report rule forbids it, so such heads keep the bare cliff.
1157    match cmd.rsplit(['/', '\\']).next().unwrap_or(cmd) {
1158        "curl" | "wget" | "http" | "ssh" | "scp" | "sftp" | "ftp" | "telnet" => &["Net"],
1159        "psql" | "mysql" | "sqlite3" | "mongosh" | "mongo" | "redis-cli" | "cqlsh" | "influx" => &["Db"],
1160        // candor engines — Fs/Env only, guaranteed by spec §7 item 12 (the analyzer self-boundary)
1161        "candor" | "candor-run.sh" | "candor-scan" | "candor-query" | "candor-java"
1162        | "candor-classify" | "candor-report" | "cargo-candor" => &["Env", "Fs"],
1163        _ => &[],
1164    }
1165}
1166
1167/// Whether a subprocess-builder method only MODIFIES the command (`.arg`, `.env`, `.current_dir`)
1168/// rather than NAMING the program (`Command::new`, `duct::cmd`). A WHOLE-CRATE-Exec crate
1169/// (`portable_pty`, `duct`, `async_process`) classifies *every* method as `Exec`, so the
1170/// head-refinement must skip these: an arg or env-var-name literal that happened to match a head
1171/// (`.env("psql", …)`, `.arg("curl")`) would FABRICATE that effect — the §1 under-report rule. The
1172/// method is the call path's last segment.
1173pub fn is_cmd_builder_method(method: &str) -> bool {
1174    matches!(
1175        method,
1176        "arg" | "args" | "arg0" | "env" | "envs" | "env_clear" | "env_remove" | "current_dir"
1177            | "cwd" | "stdin" | "stdout" | "stderr" | "pre_exec" | "creation_flags" | "uid" | "gid"
1178            | "groups" | "process_group"
1179    )
1180}
1181
1182/// Whether a subprocess method NAMES the program (so its first string literal IS the command head to
1183/// refine): `Command::new("curl")`, `duct::cmd("curl", …)`. The head-refinement must fire ONLY here —
1184/// an ALLOWLIST, not "any method except known modifiers". A whole-crate-Exec crate classifies EVERY
1185/// method as `Exec`, so a denylist leaked NON-naming methods that aren't modifiers — a getter like
1186/// `CommandBuilder::get_env("psql")` (reading back an env-var KEY, not a program) fed `"psql"` to the
1187/// head classifier and FABRICATED `Db` (review find). Only `new`/`cmd` name a program; everything else
1188/// (modifiers, getters `get_*`, custom builder methods) keeps the bare `Exec` cliff — under-refine
1189/// (safe) rather than fabricate. `std::process::Command` is verb-precise so getters never fire `Exec`
1190/// there anyway; the allowlist makes the whole-crate-Exec crates safe too.
1191pub fn is_cmd_naming_method(method: &str) -> bool {
1192    matches!(method, "new" | "cmd")
1193}
1194
1195/// The masking guard (AS-EFF-008): a Net call whose method takes the HOST/URL as an argument is
1196/// "establishing" — a classified Net call here with no captured host literal leaves the endpoint
1197/// structurally INVISIBLE (a runtime-built host), so the surface is incomplete and the gate must fail
1198/// closed (else a benign sibling literal masks the runtime endpoint). An ALLOWLIST of connection-
1199/// establishing verbs — the SAFE direction: a USE-verb on an already-connected socket
1200/// (`stream.write`/`read`/`flush`, `socket.send`/`recv`) is NOT here, so a missing literal there (the
1201/// host was fixed at `connect`) never false-positives. Under-catching an unusual establishing verb is a
1202/// missed mask (sound-with-disclosure), never a broken gate. The arg is the method (path's last segment).
1203pub fn is_net_establishing(method: &str) -> bool {
1204    matches!(
1205        method,
1206        "connect"
1207            | "connect_timeout"
1208            | "get"
1209            | "post"
1210            | "put"
1211            | "patch"
1212            | "delete"
1213            | "head"
1214            | "request"
1215            | "send_to"
1216            | "lookup_host"
1217            | "to_socket_addrs"
1218    )
1219}
1220
1221/// Map a cap-std capability *type* to the effect it authorises. Holding one of these
1222/// (e.g. `&Dir`) is the real, unforgeable right to perform that effect — so candor
1223/// treats it as a declared capability, exactly like its own `&Fs` token.
1224pub fn capstd_cap(crate_name: &str, type_name: &str) -> Option<&'static str> {
1225    if !crate_name.starts_with("cap_") {
1226        return None;
1227    }
1228    Some(match type_name {
1229        "Dir" => "Fs",
1230        "TcpListener" | "TcpStream" | "UdpSocket" | "Pool" => "Net",
1231        "UnixListener" | "UnixStream" | "UnixDatagram" => "Ipc",
1232        "SystemClock" | "MonotonicClock" => "Clock",
1233        _ => return None,
1234    })
1235}
1236
1237/// Table names a SQL string literal STATICALLY reaches — the `Db` analog of the `Net` host /
1238/// `Exec` command / `Fs` path literal surface (feeds `allow Db in <scope> <table>…`, AS-EFF-008).
1239/// Conservative by construction, because a wrong capture here would FABRICATE: the string must
1240/// open with a SQL statement keyword, and only identifiers in table position are taken —
1241/// `FROM`/`JOIN` anywhere, `INTO` anywhere, statement-leading `UPDATE`/`TRUNCATE`, and
1242/// `TABLE` (create/drop/alter), skipping `ONLY`/`IF NOT EXISTS`. `UPDATE` mid-statement is
1243/// deliberately ignored (`FOR UPDATE SKIP LOCKED` must not yield a table "skip"). A
1244/// dynamically-built query yields nothing — the gate's opaque case — never a guess.
1245/// Output is lower-cased, quote/backtick-stripped, `schema.table` kept qualified, deduped.
1246/// SPEC §2 pins this algorithm token-for-token across engines; the cross-impl vector battery
1247/// (candor-spec conformance/tables/vectors.json, run.sh Part 4b) enforces the JVM/TS mirrors.
1248pub fn tables_in_sql(sql: &str) -> Vec<String> {
1249    const STMT: &[&str] =
1250        &["select", "insert", "update", "delete", "create", "drop", "alter", "truncate", "merge", "replace", "with"];
1251    // Tokens that can FOLLOW a table-introducing keyword without being a table.
1252    const SKIP: &[&str] = &["only", "if", "not", "exists", "table"];
1253    // Identifier-position tokens that are grammar, not a table (subqueries, locking clauses…).
1254    const STOP: &[&str] = &[
1255        "select", "set", "where", "values", "on", "using", "group", "order", "by", "limit",
1256        "returning", "as", "inner", "outer", "left", "right", "cross", "lateral", "natural",
1257        "union", "all", "distinct", "case", "when", "null", "default", "skip", "nowait", "of",
1258        "from", "join", "into", "update", "delete", "insert",
1259    ];
1260    // `,` survives as its OWN token (not a space): it's what lets `FROM t1, t2` continue the table
1261    // list without fabricating from other comma-ridden positions (column lists, ON clauses).
1262    let cleaned: String = sql
1263        .to_lowercase()
1264        .chars()
1265        .flat_map(|c| match c {
1266            '(' | ')' | ';' => vec![' '],
1267            ',' => vec![' ', ',', ' '],
1268            _ => vec![c],
1269        })
1270        .collect();
1271    let toks: Vec<&str> = cleaned.split_whitespace().collect();
1272    let Some(first) = toks.first() else { return Vec::new() };
1273    if !STMT.contains(first) {
1274        return Vec::new(); // not SQL — nothing to certify, nothing fabricated
1275    }
1276    let ident = |t: &str| -> Option<String> {
1277        let t = t.trim_matches(|c| matches!(c, '"' | '`' | '\''));
1278        let mut chars = t.chars();
1279        let ok_first = chars.next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
1280        let ok_rest = t.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '$' | '"' | '`'));
1281        (ok_first && ok_rest && !STOP.contains(&t)).then(|| t.replace(['"', '`'], ""))
1282    };
1283    let mut out: Vec<String> = Vec::new();
1284    let mut push = |t: Option<String>| {
1285        if let Some(t) = t {
1286            if !out.contains(&t) {
1287                out.push(t);
1288            }
1289        }
1290    };
1291    for (i, tok) in toks.iter().enumerate() {
1292        let table_pos = match *tok {
1293            "from" | "join" | "into" | "table" => true,
1294            // statement-leading only (see doc comment): `update t set …`, `truncate [table] t`.
1295            "update" | "truncate" => i == 0,
1296            _ => false,
1297        };
1298        if !table_pos {
1299            continue;
1300        }
1301        let mut j = i + 1;
1302        while j < toks.len() && SKIP.contains(&toks[j]) {
1303            j += 1;
1304        }
1305        let Some(next) = toks.get(j) else { continue };
1306        let Some(first) = ident(next) else { continue };
1307        push(Some(first));
1308        // Comma-ADJACENT continuation only: `FROM t1, t2, t3` takes all three, while an alias breaks
1309        // the chain (`FROM t1 a, t2` keeps just t1 — an under-report, never a guess: skipping an
1310        // alias to chase the comma would fabricate tables out of `INSERT INTO t (a, b)`'s column
1311        // list, whose parens are spaces by the time we tokenize).
1312        while j + 2 < toks.len() && toks[j + 1] == "," {
1313            let Some(more) = ident(toks[j + 2]) else { break };
1314            push(Some(more));
1315            j += 2;
1316        }
1317    }
1318    out
1319}
1320
1321#[cfg(test)]
1322mod tests {
1323    #[test]
1324    fn sql_table_extraction_is_conservative() {
1325        use super::tables_in_sql as t;
1326        assert_eq!(t("SELECT id FROM users WHERE x = 1"), vec!["users"]);
1327        assert_eq!(t("select * from ledger.entries e join customers c on c.id = e.cid"),
1328                   vec!["ledger.entries", "customers"]);
1329        assert_eq!(t("INSERT INTO audit_log (a) VALUES (?1)"), vec!["audit_log"]);
1330        assert_eq!(t("UPDATE accounts SET v = ?"), vec!["accounts"]);
1331        assert_eq!(t("DELETE FROM sessions WHERE id = ?"), vec!["sessions"]);
1332        assert_eq!(t("CREATE TABLE IF NOT EXISTS cache (k TEXT)"), vec!["cache"]);
1333        assert_eq!(t("TRUNCATE TABLE staging"), vec!["staging"]);
1334        // FOR UPDATE locking clause must not yield a phantom table (mid-statement update ignored)
1335        assert_eq!(t("SELECT * FROM jobs FOR UPDATE SKIP LOCKED"), vec!["jobs"]);
1336        // a subquery in FROM position yields nothing for that position
1337        assert_eq!(t("SELECT * FROM (SELECT 1) q"), Vec::<String>::new());
1338        // not SQL -> nothing (never fabricate)
1339        assert_eq!(t("/tmp/some/path"), Vec::<String>::new());
1340        assert_eq!(t("hello world from nowhere"), Vec::<String>::new());
1341        // comma-ADJACENT continuation: a FROM list takes every table in the chain…
1342        assert_eq!(t("SELECT a FROM t1, t2, s.t3 WHERE x = 1"), vec!["t1", "t2", "s.t3"]);
1343        // …but an alias breaks it (under-report, never a guess)…
1344        assert_eq!(t("SELECT a FROM t1 a1, t2 WHERE x = 1"), vec!["t1"]);
1345        // …which is exactly what keeps a column list from fabricating (parens are spaces by now).
1346        assert_eq!(t("INSERT INTO t (a, b) VALUES (1, 2)"), vec!["t"]);
1347        // a subquery after the comma stops the chain too
1348        assert_eq!(t("SELECT a FROM t1, (SELECT 1) q"), vec!["t1"]);
1349    }
1350
1351    use super::*;
1352
1353    #[test]
1354    fn db_crates_are_calibrated() {
1355        // The calibrated set must cover every DB client the classifier knows, or the receipt's coverage
1356        // check would flag a recognized crate as a blind spot. (Was nightly-lint-only; now runs on stable.)
1357        for c in DB_CRATES {
1358            assert!(
1359                CALIBRATED_CRATES.contains(&c),
1360                "DB crate `{c}` is matched by classify() but missing from CALIBRATED_CRATES"
1361            );
1362        }
1363    }
1364
1365    #[test]
1366    fn calibrated_crates_are_live() {
1367        // Conversely, every crate advertised as calibrated must actually be matched by classify() for
1368        // some representative path — a dead entry would silently suppress a real coverage warning.
1369        for c in CALIBRATED_CRATES {
1370            assert!(
1371                CALIBRATION_PROBE_TAILS.iter().any(|t| classify(c, &format!("{c}{t}")).is_some()),
1372                "calibrated crate `{c}` is matched by no path in classify() — dead list entry"
1373            );
1374        }
1375    }
1376
1377    #[test]
1378    fn async_http_stack_classifies() {
1379        // The modern async-HTTP/TLS/QUIC/DNS stack (found by the independent-method differential on oha):
1380        // verb-keyed Net/Ipc/Fs/Env, crate-gated so generic verbs never fabricate across crates.
1381        assert_eq!(classify("hyper", "hyper::client::conn::http1::SendRequest::send_request"), Some("Net"));
1382        assert_eq!(classify("hyper", "hyper::client::conn::http1::handshake"), Some("Net"));
1383        assert_eq!(classify("hyper_util", "hyper_util::client::legacy::Client::request"), Some("Net"));
1384        assert_eq!(classify("hickory_resolver", "hickory_resolver::Resolver::lookup_ip"), Some("Net"));
1385        assert_eq!(classify("quinn", "quinn::Endpoint::connect"), Some("Net"));
1386        assert_eq!(classify("quinn", "quinn::RecvStream::read_to_end"), Some("Net")); // stream byte I/O, not just open
1387        assert_eq!(classify("quinn", "quinn::SendStream::write_all"), Some("Net"));
1388        assert_eq!(classify("tokio_rustls", "tokio_rustls::TlsConnector::connect"), Some("Net"));
1389        assert_eq!(classify("native_tls", "native_tls::TlsConnector::connect"), Some("Net"));
1390        assert_eq!(classify("tokio_vsock", "tokio_vsock::VsockStream::connect"), Some("Ipc"));
1391        assert_eq!(classify("rustls_native_certs", "rustls_native_certs::load_native_certs"), Some("Fs"));
1392        assert_eq!(classify("rlimit", "rlimit::setrlimit"), Some("Env"));
1393        // num_cpus is deliberately PURE (consistency with std::thread::available_parallelism; avoids Env spray)
1394        assert_eq!(classify("num_cpus", "num_cpus::get"), None);
1395        assert_eq!(classify("num_cpus", "num_cpus::get_physical"), None);
1396        // pure surface stays None (no fabrication): builder/type/config paths, and other crates' generic verbs
1397        assert_eq!(classify("hyper", "hyper::Request::builder"), None);
1398        assert_eq!(classify("hyper", "hyper::body::Bytes::new"), None);
1399        assert_eq!(classify("native_tls", "native_tls::TlsConnectorBuilder::min_protocol_version"), None);
1400        assert_eq!(classify("serde", "serde::Deserialize::request"), None); // generic verb, wrong crate
1401    }
1402
1403    #[test]
1404    fn log_tracing_emit_macros_classify_pre_expansion() {
1405        // candor-scan is pre-expansion: it sees the raw macro path (`log::info`, `tracing::warn`), not the
1406        // expanded dispatch the deep engine sees. Both the user-facing macro names AND the type surface:
1407        assert_eq!(classify("log", "log::info"), Some("Log"));
1408        assert_eq!(classify("log", "log::error"), Some("Log"));
1409        assert_eq!(classify("tracing", "tracing::warn"), Some("Log"));
1410        assert_eq!(classify("tracing", "tracing::info_span"), Some("Log"));
1411        // pure data-type surface stays None (no fabricated Log)
1412        assert_eq!(classify("log", "log::Level::as_str"), None);
1413        assert_eq!(classify("tracing", "tracing::Level::INFO"), None);
1414    }
1415
1416    #[test]
1417    fn classify_core_effects() {
1418        // A representative smoke test of the classifier's main families, so the published crate is not
1419        // shipped untested (these used to live only in the nightly-only src/lib.rs).
1420        assert_eq!(classify("std", "std::fs::read_to_string"), Some("Fs"));
1421        // std::path stat-family methods are Fs (each is a stat/readdir syscall); the pure
1422        // string-manipulation surface stays unclassified (the blackout screen's gix-dir find).
1423        assert_eq!(classify("std", "std::path::Path::symlink_metadata"), Some("Fs"));
1424        assert_eq!(classify("std", "std::path::PathBuf::read_dir"), Some("Fs"));
1425        assert_eq!(classify("std", "std::path::Path::exists"), Some("Fs"));
1426        assert_eq!(classify("std", "std::path::Path::join"), None); // pure string manipulation
1427        assert_eq!(classify("std", "std::path::PathBuf::file_name"), None);
1428        assert_eq!(classify("std", "std::path::Path::parent"), None);
1429        assert_eq!(classify("std", "std::process::Command::new"), Some("Exec"));
1430        assert_eq!(classify("std", "std::env::var"), Some("Env"));
1431        assert_eq!(classify("reqwest", "reqwest::Client::execute"), Some("Net"));
1432        // one-shot convenience fns send immediately → Net; the `Client::get` builder stays pure.
1433        assert_eq!(classify("reqwest", "reqwest::get"), Some("Net"));
1434        assert_eq!(classify("reqwest", "reqwest::blocking::get"), Some("Net"));
1435        assert_eq!(classify("reqwest", "reqwest::Client::get"), None);
1436        assert_eq!(classify("reqwest", "reqwest::RequestBuilder::header"), None);
1437        // nix routes through the libc syscall table (same leaves): I/O classified, generic fd ops skipped.
1438        assert_eq!(classify("nix", "nix::fcntl::open"), Some("Fs"));
1439        assert_eq!(classify("nix", "nix::sys::socket::connect"), Some("Net"));
1440        assert_eq!(classify("nix", "nix::unistd::execvp"), Some("Exec"));
1441        assert_eq!(classify("nix", "nix::unistd::write"), None); // generic fd op — deliberately unclassified
1442        assert_eq!(classify("nix", "nix::unistd::getpid"), None); // not I/O
1443        // rustix does raw syscalls (no libc underneath) → classified directly by leaf, same table.
1444        assert_eq!(classify("rustix", "rustix::time::clock_settime"), Some("Clock"));
1445        assert_eq!(classify("rustix", "rustix::fs::symlink"), Some("Fs"));
1446        assert_eq!(classify("rustix", "rustix::net::connect"), Some("Net"));
1447        assert_eq!(classify("rustix", "rustix::io::read"), None); // generic fd op
1448        // pnet raw packet capture: channel openers are Net, packet construction stays pure.
1449        assert_eq!(classify("pnet", "pnet::datalink::channel"), Some("Net"));
1450        assert_eq!(classify("pnet", "pnet::transport::transport_channel"), Some("Net"));
1451        assert_eq!(classify("pnet_datalink", "pnet_datalink::channel"), Some("Net"));
1452        assert_eq!(classify("pnet", "pnet::packet::ethernet::EthernetPacket::new"), None);
1453        assert_eq!(classify("pnet_base", "pnet_base::MacAddr::new"), None);
1454        // ignore (gitignore-aware walker): walk executors are Fs, config builders stay pure.
1455        assert_eq!(classify("ignore", "ignore::WalkBuilder::build_parallel"), Some("Fs"));
1456        assert_eq!(classify("ignore", "ignore::WalkBuilder::build"), Some("Fs"));
1457        assert_eq!(classify("ignore", "ignore::WalkParallel::run"), Some("Fs"));
1458        assert_eq!(classify("ignore", "ignore::WalkBuilder::add_ignore"), Some("Fs")); // reads the ignore file
1459        assert_eq!(classify("ignore", "ignore::overrides::OverrideBuilder::build"), None); // pure config
1460        assert_eq!(classify("ignore", "ignore::gitignore::GitignoreBuilder::build"), None); // pure config
1461        assert_eq!(classify("ignore", "ignore::DirEntry::path"), None); // pure accessor
1462        // notify fs-watching: watcher constructors + watch/unwatch are Fs, data types stay pure.
1463        assert_eq!(classify("notify", "notify::RecommendedWatcher::new"), Some("Fs"));
1464        assert_eq!(classify("notify", "notify::PollWatcher::new"), Some("Fs"));
1465        assert_eq!(classify("notify", "notify::recommended_watcher"), Some("Fs"));
1466        assert_eq!(classify("notify", "notify::INotifyWatcher::watch"), Some("Fs"));
1467        assert_eq!(classify("notify", "notify::Config::default"), None); // pure config
1468        assert_eq!(classify("notify", "notify::Event::new"), None); // pure data type
1469        assert_eq!(classify("rusqlite", "rusqlite::Connection::execute"), Some("Db"));
1470        // the rusqlite verb DIALECT (a verb probe found the canonical consumer API classifying pure):
1471        assert_eq!(classify("rusqlite", "rusqlite::Connection::query_row"), Some("Db"));
1472        assert_eq!(classify("rusqlite", "rusqlite::Statement::query_map"), Some("Db"));
1473        assert_eq!(classify("rusqlite", "rusqlite::Connection::execute_batch"), Some("Db"));
1474        assert_eq!(classify("rusqlite", "rusqlite::Connection::prepare_cached"), Some("Db"));
1475        assert_eq!(classify("rusqlite", "rusqlite::Connection::open"), Some("Db"));
1476        assert_eq!(classify("rusqlite", "rusqlite::Connection::open_in_memory"), Some("Db"));
1477        // …but `open` stays rusqlite-only (postgres has no open; nothing else may borrow it):
1478        assert_eq!(classify("postgres", "postgres::Client::open"), None);
1479        assert_eq!(classify("tokio_postgres", "tokio_postgres::Client::query_typed"), Some("Db"));
1480        // diesel's LIMIT-1 + streaming executions; sqlx's multi-result stream:
1481        assert_eq!(classify("diesel", "diesel::RunQueryDsl::first"), Some("Db"));
1482        assert_eq!(classify("diesel", "diesel::RunQueryDsl::load_iter"), Some("Db"));
1483        assert_eq!(classify("sqlx", "sqlx::query::Query::fetch_many"), Some("Db"));
1484        // sqlx's bare `query()` builder must STAY pure (the original sqlx lesson):
1485        assert_eq!(classify("sqlx", "sqlx::query"), None);
1486        // tracing: the emit/span-lifecycle dispatch is Log; the pure DATA-type accessors are not
1487        // (whole-crate Log fabricated Log on `Level::as_str` / `Span::is_disabled` — the data types are
1488        // pure, same principle as the `log` facade).
1489        assert_eq!(classify("tracing", "tracing::event"), Some("Log"));
1490        assert_eq!(classify("tracing", "tracing::Span::new_span"), Some("Log"));
1491        assert_eq!(classify("tracing", "tracing::Span::record"), Some("Log"));
1492        assert_eq!(classify("tracing", "tracing::Span::enter"), Some("Log"));
1493        assert_eq!(classify("tracing", "tracing::Level::as_str"), None); // pure accessor
1494        assert_eq!(classify("tracing", "tracing::Span::is_disabled"), None); // pure state read
1495        assert_eq!(classify("tracing", "tracing::Span::metadata"), None); // pure accessor
1496        assert_eq!(classify("tracing", "tracing::metadata::Level::TRACE"), None); // pure data type
1497        assert_eq!(classify("tracing", "tracing::field::Field::name"), None); // pure data type
1498        // memmap2: only the syscall-issuing map/flush/protect verbs are Fs; reads over an already-mapped
1499        // region (len/as_ptr/is_empty) and the request builder are PURE (whole-crate Fs fabricated Fs).
1500        assert_eq!(classify("memmap2", "memmap2::MmapOptions::map"), Some("Fs"));
1501        assert_eq!(classify("memmap2", "memmap2::MmapOptions::map_mut"), Some("Fs"));
1502        assert_eq!(classify("memmap2", "memmap2::Mmap::flush"), Some("Fs"));
1503        assert_eq!(classify("memmap2", "memmap2::MmapMut::make_read_only"), Some("Fs"));
1504        assert_eq!(classify("memmap2", "memmap2::Mmap::len"), None); // length read — pure
1505        assert_eq!(classify("memmap2", "memmap2::Mmap::is_empty"), None); // pure
1506        assert_eq!(classify("memmap2", "memmap2::Mmap::as_ptr"), None); // pointer — pure
1507        assert_eq!(classify("memmap2", "memmap2::MmapOptions::new"), None); // request builder — pure
1508        // arboard: the Clipboard handle's read/write verbs are Clipboard; `arboard::Error` formatting
1509        // and option data types are PURE (whole-crate Clipboard fabricated Clipboard on `Error::to_string`).
1510        assert_eq!(classify("arboard", "arboard::Clipboard::new"), Some("Clipboard"));
1511        assert_eq!(classify("arboard", "arboard::Clipboard::get_text"), Some("Clipboard"));
1512        assert_eq!(classify("arboard", "arboard::Clipboard::set_text"), Some("Clipboard"));
1513        assert_eq!(classify("arboard", "arboard::Clipboard::clear"), Some("Clipboard"));
1514        assert_eq!(classify("arboard", "arboard::Error::to_string"), None); // error formatting — pure
1515        assert_eq!(classify("arboard", "arboard::Error::fmt"), None); // Display impl — pure
1516        assert_eq!(classify("arboard", "arboard::ImageData::to_owned_img"), None); // pure data type
1517        // fastrand: value draws + entropy-seeded entry points are Rand; the DETERMINISTIC seeded ctor
1518        // `with_seed` and state split/copy (`fork`/`clone`) are PURE (whole-crate Rand fabricated Rand).
1519        assert_eq!(classify("fastrand", "fastrand::u32"), Some("Rand")); // top-level draw
1520        assert_eq!(classify("fastrand", "fastrand::Rng::usize"), Some("Rand"));
1521        assert_eq!(classify("fastrand", "fastrand::Rng::shuffle"), Some("Rand"));
1522        assert_eq!(classify("fastrand", "fastrand::Rng::new"), Some("Rand")); // entropy-seeded
1523        assert_eq!(classify("fastrand", "fastrand::Rng::with_seed"), None); // deterministic ctor — pure
1524        assert_eq!(classify("fastrand", "fastrand::Rng::fork"), None); // state split — pure
1525        assert_eq!(classify("fastrand", "fastrand::Rng::clone"), None); // state copy — pure
1526        // portable_pty / async_process: spawn/wait keep Exec; config GETTERS and pure data ctors/setters
1527        // do NOT (base Exec fabricated on `CommandBuilder::get_cwd` / `PtySize::default` / `Stdio::piped`).
1528        assert_eq!(classify("portable_pty", "portable_pty::PtySystem::openpty"), Some("Exec"));
1529        assert_eq!(classify("portable_pty", "portable_pty::SlavePty::spawn_command"), Some("Exec"));
1530        assert_eq!(classify("portable_pty", "portable_pty::CommandBuilder::get_argv"), None); // getter
1531        assert_eq!(classify("portable_pty", "portable_pty::CommandBuilder::get_cwd"), None); // getter
1532        assert_eq!(classify("portable_pty", "portable_pty::PtySize::default"), None); // pure data type
1533        assert_eq!(classify("portable_pty", "portable_pty::CommandBuilder::new"), None); // builder ctor
1534        assert_eq!(classify("async_process", "async_process::Command::spawn"), Some("Exec"));
1535        assert_eq!(classify("async_process", "async_process::Command::output"), Some("Exec"));
1536        assert_eq!(classify("async_process", "async_process::Stdio::piped"), None); // pure data type
1537        assert_eq!(classify("async_process", "async_process::Stdio::null"), None); // pure data type
1538        // FFI tiers (matched by distinctive leaf, alias-independent)
1539        assert_eq!(classify("libc", "libc::open"), Some("Fs"));
1540        assert_eq!(classify("libc", "libc::connect"), Some("Net"));
1541        assert_eq!(classify("libc", "libc::read"), None); // generic fd op — deliberately unclassified
1542        assert_eq!(classify("ffi", "ffi::sqlite3_step"), Some("Db"));
1543        assert_eq!(classify("raw", "raw::git_remote_fetch"), Some("Net"));
1544        // libgit2 clone + submodule clone/update fetch over the network (an A/B on git2 0.20 caught
1545        // `Submodule::update`/`clone` and `Repository::clone` reporting no Net — the latter because the
1546        // `src/build.rs` module was being dropped as if it were the Cargo build script).
1547        assert_eq!(classify("raw", "raw::git_clone"), Some("Net"));
1548        assert_eq!(classify("raw", "raw::git_submodule_clone"), Some("Net"));
1549        assert_eq!(classify("raw", "raw::git_submodule_update"), Some("Net"));
1550        assert_eq!(classify("raw", "raw::git_submodule_open"), None); // local subrepo open — not Net
1551        // libcurl: the transfer/raw-socket entry points are Net (an A/B on curl 0.4 caught the whole
1552        // crate reporting ZERO Net); the big setopt/init/getinfo surface — and the readiness-wait
1553        // multi_wait/poll — stay unclassified (the loop's perform is the boundary).
1554        assert_eq!(classify("curl_sys", "curl_sys::curl_easy_perform"), Some("Net"));
1555        assert_eq!(classify("curl_sys", "curl_sys::curl_easy_send"), Some("Net"));
1556        assert_eq!(classify("curl_sys", "curl_sys::curl_multi_perform"), Some("Net"));
1557        assert_eq!(classify("curl_sys", "curl_sys::curl_multi_socket_action"), Some("Net"));
1558        assert_eq!(classify("curl_sys", "curl_sys::curl_easy_setopt"), None); // in-memory option write
1559        assert_eq!(classify("curl_sys", "curl_sys::curl_easy_init"), None); // handle alloc
1560        assert_eq!(classify("curl_sys", "curl_sys::curl_multi_wait"), None); // readiness wait, no payload
1561        // consumer-side `curl` crate rule: the dispatch verbs are Net, the setopt builders pure.
1562        assert_eq!(classify("curl", "curl::easy::Easy::perform"), Some("Net"));
1563        assert_eq!(classify("curl", "curl::multi::Multi::perform"), Some("Net"));
1564        assert_eq!(classify("curl", "curl::easy::Easy::send"), Some("Net"));
1565        assert_eq!(classify("curl", "curl::easy::Easy::url"), None); // CURLOPT setter — pure
1566        assert_eq!(classify("curl", "curl::easy::Easy::timeout"), None); // pure setter; Multi::timeout under-reported by design
1567        assert_eq!(classify("ffi", "ffi::SSL_connect"), Some("Net"));
1568        // pure crates stay pure
1569        assert_eq!(classify("serde", "serde::Serialize::serialize"), None);
1570        assert_eq!(classify("std", "std::vec::Vec::push"), None);
1571
1572        // ── sweep 2026-06-17: fabrication carve-outs + DNS coverage (each fails pre-fix) ──
1573        // [24] std::net socket accessors are pure; the I/O verbs stay Net.
1574        assert_eq!(classify("std", "std::net::TcpStream::connect"), Some("Net"));
1575        assert_eq!(classify("std", "std::net::TcpStream::local_addr"), None);
1576        assert_eq!(classify("std", "std::net::TcpStream::nodelay"), None);
1577        assert_eq!(classify("std", "std::net::TcpStream::ttl"), None);
1578        assert_eq!(classify("std", "std::net::UdpSocket::peer_addr"), None);
1579        // [37] std DNS resolution is Net (was floored).
1580        assert_eq!(classify("std", "std::net::lookup_host"), Some("Net"));
1581        assert_eq!(classify("std", "core::net::ToSocketAddrs::to_socket_addrs"), Some("Net"));
1582        // [23] std::process getters are pure; spawn/new stay Exec.
1583        assert_eq!(classify("std", "std::process::Command::get_program"), None);
1584        assert_eq!(classify("std", "std::process::Command::get_args"), None);
1585        assert_eq!(classify("std", "std::process::Child::id"), None);
1586        assert_eq!(classify("std", "std::process::Command::spawn"), Some("Exec"));
1587        // [27] redis ConnectionManager::clone is an Arc bump (pure); a query round-trips.
1588        assert_eq!(classify("redis", "redis::aio::ConnectionManager::clone"), None);
1589        assert_eq!(classify("redis", "redis::aio::ConnectionManager::send_packed_command"), Some("Db"));
1590        // [5] sea_orm re-exported sea_query builder algebra is pure; execution verbs stay Db.
1591        assert_eq!(classify("sea_orm", "sea_orm::sea_query::Func::count"), None);
1592        assert_eq!(classify("sea_orm", "sea_orm::sea_query::Condition::all"), None);
1593        assert_eq!(classify("sea_orm", "sea_orm::Select::all"), Some("Db"));
1594    }
1595
1596    #[test]
1597    fn rand_osrng_handle_ops_are_pure_but_draws_are_rand() {
1598        // Adversarial-review fabrication: the blanket `contains("OsRng")` tagged `OsRng::clone` Rand,
1599        // but OsRng is a unit struct — clone/fork/default draw no entropy. The real draws still fire.
1600        assert_eq!(classify("rand", "rand::rngs::OsRng::clone"), None);
1601        assert_eq!(classify("rand", "rand::rngs::OsRng::default"), None);
1602        assert_eq!(classify("rand", "rand::rngs::OsRng::fill_bytes"), Some("Rand")); // a real draw
1603        assert_eq!(classify("rand", "rand::rngs::OsRng::next_u32"), Some("Rand"));
1604        assert_eq!(classify("rand", "rand::Rng::gen"), Some("Rand")); // verb path unaffected
1605        assert_eq!(classify("rand", "rand::distributions::Uniform::new"), None); // pure ctor still pure
1606    }
1607
1608    #[test]
1609    fn redis_connection_manager_config_builder_is_pure() {
1610        // Adversarial-review fabrication: `contains("ConnectionManager")` hit the pure *Config* builder.
1611        assert_eq!(classify("redis", "redis::aio::ConnectionManagerConfig::new"), None);
1612        assert_eq!(classify("redis", "redis::aio::ConnectionManagerConfig::set_max_delay"), None);
1613        // the LIVE manager still round-trips (Db).
1614        assert_eq!(classify("redis", "redis::aio::ConnectionManager::new"), Some("Db"));
1615        assert_eq!(classify("redis", "redis::Commands::get"), Some("Db"));
1616    }
1617
1618    #[test]
1619    fn pure_fd_transfer_is_not_an_effect() {
1620        // ADOPTING / EXTRACTING / BORROWING an already-open descriptor (or unwrapping an async type back
1621        // to its std type) issues NO syscall — it must be PURE even though it hangs off a std I/O type
1622        // whose prefix rule would otherwise fire Net/Fs/Ipc. (Real tokio sweep: `into_std`, `from_raw_fd`,
1623        // `as_raw_fd` all fabricated effects.)
1624        assert_eq!(classify("std", "std::net::TcpStream::from_raw_fd"), None);
1625        assert_eq!(classify("std", "std::net::TcpStream::into_raw_fd"), None);
1626        assert_eq!(classify("std", "std::net::TcpStream::as_raw_fd"), None);
1627        assert_eq!(classify("std", "std::net::TcpListener::from_raw_fd"), None);
1628        assert_eq!(classify("std", "std::net::UdpSocket::from_raw_socket"), None);
1629        assert_eq!(classify("std", "std::fs::File::from_raw_fd"), None);
1630        assert_eq!(classify("std", "std::fs::File::into_raw_fd"), None);
1631        assert_eq!(classify("std", "std::fs::File::as_raw_handle"), None);
1632        assert_eq!(classify("std", "std::os::unix::net::UnixStream::from_raw_fd"), None);
1633        // `SocketAddr::from_pathname` builds an address struct, opens no socket — pure. (socket2 sweep.)
1634        assert_eq!(classify("std", "std::os::unix::net::SocketAddr::from_pathname"), None);
1635        assert_eq!(classify("tokio", "tokio::net::TcpStream::from_raw_fd"), None);
1636        assert_eq!(classify("tokio", "tokio::net::TcpStream::into_std"), None); // unwrap → std type, pure
1637        assert_eq!(classify("tokio", "tokio::fs::File::into_std"), None);
1638        // …but a REAL open/connect on the SAME types still fires the effect — the carve-out is leaf-precise.
1639        assert_eq!(classify("std", "std::net::TcpStream::connect"), Some("Net"));
1640        assert_eq!(classify("std", "std::fs::File::open"), Some("Fs"));
1641        assert_eq!(classify("std", "std::fs::read"), Some("Fs"));
1642        assert_eq!(classify("std", "std::os::unix::net::UnixStream::connect"), Some("Ipc"));
1643        assert_eq!(classify("tokio", "tokio::net::TcpStream::connect"), Some("Net"));
1644    }
1645
1646    #[test]
1647    fn command_head_refines_the_exec_cliff() {
1648        use super::classify_command_head as h;
1649        // unambiguous external tools classify by basename (spec §4 ⟨0.5⟩)
1650        assert_eq!(h("curl"), &["Net"]);
1651        assert_eq!(h("telnet"), &["Net"]);
1652        assert_eq!(h("sftp"), &["Net"]);
1653        assert_eq!(h("/usr/local/bin/psql"), &["Db"]); // basename match strips the path
1654        assert_eq!(h("mongo"), &["Db"]);
1655        assert_eq!(h("cqlsh"), &["Db"]);
1656        // a candor engine is Fs/Env — spec-SUPPLIED by §7 item 12, not curation
1657        assert_eq!(h("candor-scan"), &["Env", "Fs"]);
1658        assert_eq!(h("candor-run.sh"), &["Env", "Fs"]);
1659        // an unrecognised head adds nothing — the bare Exec cliff stands (never guess). `make`/`npm`
1660        // run the project's own code; `git`/`rsync` are multi-modal (local vs remote) — all keep the
1661        // cliff rather than fabricate an effect for the common case.
1662        assert_eq!(h("some-unknown-tool"), &[] as &[&str]);
1663        assert_eq!(h("make"), &[] as &[&str]);
1664        assert_eq!(h("npm"), &[] as &[&str]);
1665        assert_eq!(h("git"), &[] as &[&str]);
1666        assert_eq!(h("rsync"), &[] as &[&str]);
1667        // a builder MODIFIER (`.arg`/`.env`) names no program — its literal must NOT refine (a
1668        // whole-crate-Exec crate classifies every method; `.env("psql",..)` must not fabricate Db).
1669        assert!(is_cmd_builder_method("env") && is_cmd_builder_method("arg") && is_cmd_builder_method("current_dir"));
1670        assert!(!is_cmd_builder_method("new")); // Command::new NAMES the program
1671        assert!(!is_cmd_builder_method("cmd")); // duct::cmd NAMES the program
1672        // The gate that ADMITS a literal to classify_command_head is an ALLOWLIST of program-NAMING
1673        // methods, not the builder denylist. Inversion matters: a whole-crate-Exec crate (portable_pty)
1674        // classifies EVERY method as Exec, so a getter like `cmd.get_env("psql")` — absent from the
1675        // builder denylist — would have leaked "psql" to the head and FABRICATED Db. Only `new`/`cmd`
1676        // name a program, so only they may refine.
1677        assert!(is_cmd_naming_method("new") && is_cmd_naming_method("cmd"));
1678        assert!(!is_cmd_naming_method("get_env")); // a GETTER, not a namer — the leak this closes
1679        assert!(!is_cmd_naming_method("arg") && !is_cmd_naming_method("env") && !is_cmd_naming_method("current_dir"));
1680    }
1681
1682    #[test]
1683    fn net_establishing_allowlist() {
1684        // sweep [3]/[7]: the masking guard's establishing-verb allowlist — host-bearing connect/request
1685        // verbs establish (a runtime host there is invisible); USE-verbs on a connected socket do NOT.
1686        assert!(is_net_establishing("connect") && is_net_establishing("connect_timeout"));
1687        assert!(is_net_establishing("get") && is_net_establishing("post") && is_net_establishing("request"));
1688        assert!(is_net_establishing("send_to") && is_net_establishing("to_socket_addrs"));
1689        // use-verbs (host fixed at connect) must NOT be establishing — else `connect("h").write()` flags.
1690        assert!(!is_net_establishing("write") && !is_net_establishing("read") && !is_net_establishing("send"));
1691        assert!(!is_net_establishing("flush") && !is_net_establishing("recv") && !is_net_establishing("peek"));
1692    }
1693}