docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment guidelines: delete redundant restatements, decorative banners, change narration, and end-of-line comments; keep and tighten the crucial ones (invariants, bug rationale, SAFETY blocks, ported-source attribution). No functional code changed. Every edit is proven comment-only against the prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a separate doctest-fence check. Where removing a comment made rustfmt or clippy want to re-lay-out adjacent code, the minimal triggering comment is restored so code tokens stay byte-identical. Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy --workspace --all-targets (0 warnings). Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for these guidelines (flags banners, end-of-line comments, change narration, and commented-out code).
This commit is contained in:
1 parent
ff0fb56c67
commit
a02b555e66
1458 files changed
+10729
-21750
No files matched your search
@@ -1,10 +1,8 @@
|
||||
//! Shared git-repo dir-chain primitive.
|
||||
//!
|
||||
//! One `git2` discovery + one cwd→root walk, reused across the many repo-local
|
||||
//! config marker checks the folder-trust gate runs back-to-back. Lives in its
|
||||
//! own module (rather than `discovery`) because it is a generic repo-walk
|
||||
//! primitive consumed cross-crate by `kigi-workspace`, not agent-definition
|
||||
//! discovery.
|
||||
//! Lives in its own module rather than `discovery` because it is a generic
|
||||
//! repo-walk primitive consumed cross-crate by `kigi-workspace`, not
|
||||
//! agent-definition discovery.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@@ -14,42 +12,28 @@ use std::path::{Path, PathBuf};
|
||||
///
|
||||
/// The folder-trust gate's `repo_configs_present` probes a dozen repo-local
|
||||
/// code-exec markers (`.mcp.json`, `.kigi/config.toml`, `.claude/settings.json`,
|
||||
/// project plugin/agent dirs, …) back-to-back on the agent startup path. Each
|
||||
/// marker walker used to run its own `discover` + cwd→root walk; sharing one
|
||||
/// `RepoDirChain` collapses that to a single traversal (each redundant syscall
|
||||
/// is taxed 10-100x on Windows, and on a non-git dir each `discover` walks to
|
||||
/// the filesystem root). Both the gate and the real loaders consume the same
|
||||
/// chain via `*_in` walker variants, so detection can't drift from loading.
|
||||
///
|
||||
/// The public cwd-taking delegators (`find_project_configs`,
|
||||
/// `project_plugin_dirs`, `project_agent_dirs`, …) now resolve through this
|
||||
/// chain too, so their non-gate callers (config watcher, reloader, the mcp/
|
||||
/// config loaders, inspect, upload, mcp_doctor) gain the per-level canonicalize
|
||||
/// below. That is deliberate: all those callers are cold (startup / file-change /
|
||||
/// session-setup / manual commands), never per-keystroke, and the canonical stop
|
||||
/// is strictly more correct.
|
||||
/// project plugin/agent dirs, …) back-to-back on the agent startup path, so a
|
||||
/// per-walker discovery + walk is a real cost: each redundant syscall is taxed
|
||||
/// 10-100x on Windows, and on a non-git dir each `discover` walks to the
|
||||
/// filesystem root. Both the gate and the real loaders consume the same chain
|
||||
/// via `*_in` walker variants, so detection can't drift from loading.
|
||||
///
|
||||
/// Outside a git repo `git_root` is `None` and `dirs` is just `[cwd]`, matching
|
||||
/// every walker's no-repo branch (probe `cwd` only).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RepoDirChain {
|
||||
/// Git worktree root (`workdir`), or `None` when `cwd` is not inside a repo.
|
||||
pub git_root: Option<PathBuf>,
|
||||
/// `cwd` up to and including `git_root`, cwd-first (`[cwd]` with no repo).
|
||||
pub dirs: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl RepoDirChain {
|
||||
/// Resolve the chain for `cwd`: ONE `git2` discovery + ONE upward walk.
|
||||
pub fn resolve(cwd: &Path) -> Self {
|
||||
let git_root = git2::Repository::discover(cwd)
|
||||
.ok()
|
||||
.and_then(|repo| repo.workdir().map(|p| p.to_path_buf()))
|
||||
// Home-is-a-git-repo (dotfiles in $HOME): a discovery that walks up
|
||||
// to $HOME must NOT treat the whole home subtree as one repo, or
|
||||
// home-level `.kigi`/`.mcp.json`/plugins would look repo-local. Drop
|
||||
// it so cwd is handled as no-repo (probe cwd only). Home is compared
|
||||
// canonically to match the symlink handling in the walk below.
|
||||
// Dotfiles in $HOME make home itself a repo; treating that subtree
|
||||
// as repo-local would promote home-level `.kigi`/`.mcp.json`/plugins
|
||||
// to project config. Dropping the root makes cwd behave as no-repo.
|
||||
.filter(|root| !is_home_dir(root));
|
||||
|
||||
let mut dirs = Vec::new();
|
||||
@@ -57,11 +41,10 @@ impl RepoDirChain {
|
||||
// Canonicalize only for the stop test so a symlinked cwd/ancestor
|
||||
// still halts AT the worktree root instead of over-walking to the
|
||||
// filesystem root; pushed dirs keep their original spelling (callers
|
||||
// `join` markers onto them, which resolve the same either way). The
|
||||
// per-level canonicalize is required to stop at root through a
|
||||
// symlinked ancestor while keeping raw spelling — do NOT reduce to a
|
||||
// 2-call `starts_with` variant (it would mis-handle a mid-chain
|
||||
// absolute symlink and reintroduce the over-walk).
|
||||
// `join` markers onto them, which resolve the same either way).
|
||||
// Canonicalizing per level is what makes that stop reliable — a
|
||||
// 2-call `starts_with` variant mis-handles a mid-chain absolute
|
||||
// symlink and over-walks.
|
||||
let root_canonical = dunce::canonicalize(root).unwrap_or_else(|_| root.clone());
|
||||
let mut current = Some(cwd.to_path_buf());
|
||||
while let Some(dir) = current {
|
||||
@@ -81,9 +64,9 @@ impl RepoDirChain {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `path` canonicalizes to the user's home directory. Local (not reused
|
||||
/// from `kigi-workspace`, which depends on THIS crate) to keep the dep edge
|
||||
/// one-way; backs the home-is-dotfiles guard in [`RepoDirChain::resolve`].
|
||||
/// Whether `path` canonicalizes to the user's home directory. Duplicated here
|
||||
/// instead of reused from `kigi-workspace`, which depends on THIS crate, to keep
|
||||
/// the dep edge one-way.
|
||||
fn is_home_dir(path: &Path) -> bool {
|
||||
let Some(home) = dirs::home_dir() else {
|
||||
return false;
|
||||
@@ -94,8 +77,7 @@ fn is_home_dir(path: &Path) -> bool {
|
||||
|
||||
/// Existing `<dir>/<subdir>` directories under each dir of a precomputed
|
||||
/// cwd→git-root chain ([`RepoDirChain::dirs`]), in chain order (cwd-first, then
|
||||
/// each `subdirs` entry in order). Shared body for the project plugin/agent dir
|
||||
/// walkers so the byte-identical double-loop lives in one place.
|
||||
/// each `subdirs` entry in order).
|
||||
pub(crate) fn existing_subdirs_along(chain_dirs: &[PathBuf], subdirs: &[&str]) -> Vec<PathBuf> {
|
||||
let mut found = Vec::new();
|
||||
for dir in chain_dirs {
|
||||
@@ -114,8 +96,8 @@ mod tests {
|
||||
use super::*;
|
||||
use serial_test::serial;
|
||||
|
||||
/// RAII guard: set an env var, restore the prior value (or unset) on drop,
|
||||
/// so a test never leaves process-global env pointing at a dropped tempdir.
|
||||
/// Restores the prior value (or unsets) on drop, so a test never leaves
|
||||
/// process-global env pointing at a dropped tempdir.
|
||||
struct EnvVarGuard {
|
||||
key: &'static str,
|
||||
prev: Option<std::ffi::OsString>,
|
||||
@@ -140,8 +122,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn resolve_in_repo_yields_cwd_to_root_chain() {
|
||||
// A git-init'd tmp with a 2-deep subdir: the chain is cwd→root inclusive,
|
||||
// cwd-first, in the dirs' original spelling, and `git_root` is the root.
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
git2::Repository::init(tmp.path()).unwrap();
|
||||
let nested = tmp.path().join("a").join("b");
|
||||
@@ -156,8 +136,8 @@ mod tests {
|
||||
tmp.path().to_path_buf(),
|
||||
]
|
||||
);
|
||||
// `git_root` is the canonical worktree root (git2's `workdir`); compare by
|
||||
// canonical form so a `/tmp`→`/private/tmp` symlink doesn't fail the test.
|
||||
// git2's `workdir` is canonical, so compare canonically or a
|
||||
// `/tmp`→`/private/tmp` symlink fails the test.
|
||||
let root = chain.git_root.expect("inside a repo");
|
||||
assert_eq!(
|
||||
dunce::canonicalize(&root).unwrap(),
|
||||
@@ -167,10 +147,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn resolve_outside_repo_is_cwd_only() {
|
||||
// A non-git tmp: no discovery hit, so the chain is just `[cwd]` and there
|
||||
// is no git root. Only assert the no-repo shape when the temp dir is
|
||||
// genuinely outside any repo (a dev/CI checkout may place $TMPDIR inside
|
||||
// a larger git worktree).
|
||||
// Only assert the no-repo shape when the temp dir is genuinely outside
|
||||
// any repo: a dev/CI checkout may place $TMPDIR inside a larger git
|
||||
// worktree.
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let plain = tmp.path().join("plain");
|
||||
std::fs::create_dir_all(&plain).unwrap();
|
||||
@@ -184,10 +163,8 @@ mod tests {
|
||||
#[test]
|
||||
#[serial(home_env)]
|
||||
fn resolve_treats_home_git_repo_as_no_repo() {
|
||||
// Home-is-a-git-repo (dotfiles in $HOME): discovery walks up to $HOME,
|
||||
// but the guard drops that root so a subdir resolves as no-repo (probe
|
||||
// cwd only) instead of spanning the whole home subtree. $HOME is guarded
|
||||
// (dirs::home_dir reads it) and canonicalized to match the guard.
|
||||
// $HOME is process-global (`dirs::home_dir` reads it) so it needs the
|
||||
// guard, and canonicalized to match the comparison in `is_home_dir`.
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let home = dunce::canonicalize(tmp.path()).unwrap();
|
||||
git2::Repository::init(&home).unwrap();
|
||||
@@ -203,8 +180,8 @@ mod tests {
|
||||
#[test]
|
||||
#[serial(home_env)]
|
||||
fn resolve_keeps_non_home_git_root() {
|
||||
// The guard is home-EXACT: a git root that is NOT $HOME still resolves
|
||||
// normally (no over-trigger), so $HOME points at an unrelated dir here.
|
||||
// The guard is home-EXACT, so $HOME points at an unrelated dir here to
|
||||
// prove a non-home git root still resolves normally.
|
||||
let home = tempfile::tempdir().unwrap();
|
||||
let _home_guard = EnvVarGuard::set("HOME", home.path());
|
||||
let repo = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user