Skip to content

Commit 4bbcf91

Browse files
Fix agent scan silent about projects below (#2125) (#2126)
* Start agent scan projects-below note (#2125) Assisted-by: Claude Code:claude-fable-5-2 * Test agent scan naming projects below it A bare agent-mode scan above Bundler or PyPI projects with their own install directories must name the ones it left (#2125). These fail until the scan looks for them. Assisted-by: Claude Code:claude-fable-5-2 * Name projects an agent scan leaves below it A bare agent-mode scan patches the installed packages its crawl of --cwd reads, so it covers one project. It never looked for project directories below --cwd: from a monorepo root it printed "No packages found. Run your package manager's install first.", or a success summary for the root alone, and left api/ and web/ unpatched without a word (#2125). After the crawl it now finds the project directories below (the search a bare hosted scan uses) and keeps the ones where a crawl would read a PyPI, Cargo, RubyGems, Go or Composer install directory the crawl from --cwd did not. It names them in a projects_not_scanned warning with the command that reaches the first, on stderr and in --json warnings[]. An empty scan with such a warning points at it instead of telling the user to run an install. npm is not compared: its crawl walks every node_modules below --cwd. Assisted-by: Claude Code:claude-fable-5-2 * Name only projects with unread installs of own Review of the first cut found it too loose and too narrow at once. A Python project with no environment fell back to the machine's site-packages and was named on every scan, while an npm project the walk from --cwd skips (a hidden or build-output directory) never was. A directory is now named when it has an install directory of its own that the crawl from --cwd did not read: a node_modules, a Python environment or a Composer vendor directory inside the project, or a gem home (Bundler installs into the user-wide ones by default). The user-wide Python, Cargo and Go stores are not compared. Also: resolve the directories a few at a time, say when the tree was too large to search, list at most 10 directories, and suggest the command as the run was asked (--sync, an inferred mode). Assisted-by: Claude Code:claude-fable-5-2 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 89c482d commit 4bbcf91

7 files changed

Lines changed: 629 additions & 26 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ Pinned by `tests/scan/scan_proxy_fallback_e2e.rs`, `tests/cli/api_client_errors_
380380

381381
**Path-scoped scans (`scan [PATHS]...`, v5.0)**: what a PATH means depends on the mode.
382382

383-
**A bare hosted or vendored scan covers the checkout (additive/MINOR).** With no PATH, `scan` in hosted or vendored mode (the inferred default included) scans `--cwd` and every other project directory below it in the same git checkout, exactly as if each had been named as a PATH: its own lockfiles, ledgers, `.socket/` and inferred mode, one `== <dir> ==` heading each (`== . ==` for `--cwd` itself), the worst exit code, one `--max-new-patches` budget. A project directory is one the in-memory engine would detect as a root for the same tree: a directory holding a lockfile of a supported npm-family, PyPI, Cargo, RubyGems, Go or Composer format (a manifest alone never makes one; Maven, Gradle, sbt and NuGet projects are scanned only as `--cwd` or a named PATH), found by a walk that honours git's ignore rules (`.gitignore`, the repository's and the user's excludes; a `.ignore` file, which hides files from search tools and not from git, is not read), never follows a link, never enters `node_modules`, `.git`, `.socket`, `.yarn` or `vendor`, and never enters another repository: a directory below `--cwd` that holds a `.git` entry (a submodule, a nested clone) is left to a run of its own, since this checkout does not track its files and its own `socket.yml` was not read. A workspace member whose only lock is one the workspace root's run reads is not a project of its own: a Cargo workspace member's `Cargo.lock`, and a pnpm workspace member's `pnpm-lock.yaml` the root accounts for. Discovered directories are subject to the `socket.yml` path filters and the built-in `test/`, `tests/`, `fixtures/`, `__fixtures__/` and `testdata/` ignores, as glob-matched PATHs are; the ones left out are counted in one stderr note (`Note: N project directories under ignored paths ... were not scanned; name a directory to scan it.`), and naming a directory scans it. `--cwd` itself is scanned whenever it is a project, with or without a lockfile, as before. Nothing is searched for, and the run is the single-directory scan it always was, when: `--cwd` is not inside a git checkout (a scan from a home directory never goes looking for projects), a PATH is given, the scope is `--global`/`--global-prefix`, or the mode is agent. `--json` and `--vex` report on one project directory, so with either a bare scan scans `--cwd` alone and adds the run-level `projects_not_scanned` warning naming the project directories it found and left (`N project directories below this one were not scanned (a, b): --json takes one project directory. Run one scan per directory, e.g. ...`); exit code and status are unchanged. A tree with more than 500,000 entries is searched only that far, with a stderr warning.
383+
**A bare hosted or vendored scan covers the checkout (additive/MINOR).** With no PATH, `scan` in hosted or vendored mode (the inferred default included) scans `--cwd` and every other project directory below it in the same git checkout, exactly as if each had been named as a PATH: its own lockfiles, ledgers, `.socket/` and inferred mode, one `== <dir> ==` heading each (`== . ==` for `--cwd` itself), the worst exit code, one `--max-new-patches` budget. A project directory is one the in-memory engine would detect as a root for the same tree: a directory holding a lockfile of a supported npm-family, PyPI, Cargo, RubyGems, Go or Composer format (a manifest alone never makes one; Maven, Gradle, sbt and NuGet projects are scanned only as `--cwd` or a named PATH), found by a walk that honours git's ignore rules (`.gitignore`, the repository's and the user's excludes; a `.ignore` file, which hides files from search tools and not from git, is not read), never follows a link, never enters `node_modules`, `.git`, `.socket`, `.yarn` or `vendor`, and never enters another repository: a directory below `--cwd` that holds a `.git` entry (a submodule, a nested clone) is left to a run of its own, since this checkout does not track its files and its own `socket.yml` was not read. A workspace member whose only lock is one the workspace root's run reads is not a project of its own: a Cargo workspace member's `Cargo.lock`, and a pnpm workspace member's `pnpm-lock.yaml` the root accounts for. Discovered directories are subject to the `socket.yml` path filters and the built-in `test/`, `tests/`, `fixtures/`, `__fixtures__/` and `testdata/` ignores, as glob-matched PATHs are; the ones left out are counted in one stderr note (`Note: N project directories under ignored paths ... were not scanned; name a directory to scan it.`), and naming a directory scans it. `--cwd` itself is scanned whenever it is a project, with or without a lockfile, as before. Nothing is searched for, and the run is the single-directory scan it always was, when: `--cwd` is not inside a git checkout (a scan from a home directory never goes looking for projects), a PATH is given, the scope is `--global`/`--global-prefix`, or the mode is agent (an agent-mode scan still names the directories it left; see below). `--json` and `--vex` report on one project directory, so with either a bare scan scans `--cwd` alone and adds the run-level `projects_not_scanned` warning naming the project directories it found and left (`N project directories below this one were not scanned (a, b): --json takes one project directory. Run one scan per directory, e.g. ...`); exit code and status are unchanged. A bare agent-mode scan patches the installed packages its crawl of `--cwd` reads, so it covers the project at `--cwd` alone. It looks for the same project directories below `--cwd` and adds the `projects_not_scanned` warning (stderr `Warning:`, `--json` `warnings[]`) naming the ones whose installed packages that crawl did not read, and the command that reaches the first, asked as the run was (`scan --mode agent`, `scan --sync`, or a bare `scan` when the mode was inferred): `N project directories below this one were not scanned (a, b): an agent-mode scan patches the packages installed for the one project directory it runs in. Run one scan per directory, e.g. `socket-patch scan --mode agent --cwd a``. More than 10 directories are listed as the first 10 `, and N more`. Exit code and status are unchanged. A directory is named when it has an install directory of its own that the crawl from `--cwd` did not read: a `node_modules` the npm walk skips (one under a hidden or build-output directory), a Python environment or a Composer vendor directory inside the project, or a gem home (the project's `vendor/bundle`, or the user-wide homes Bundler installs into by default when `--cwd` holds no Bundler project to read them for). A directory is not named when the run did read its packages (every other `node_modules` below `--cwd`, a gem home or an activated environment both share) or when it has no install directory of its own; the user-wide Python, Cargo and Go stores are never compared. Nothing is looked for with a PATH, a global scope, a non-default `--manifest-path` or outside a git checkout. With nothing found at `--cwd`, a run that names such directories prints `No packages found (see the warning).` in place of the hint to run the package manager's install. A tree too large to search adds a `projects_below_not_searched` warning: some directories may not be named. A tree with more than 500,000 entries is searched only that far, with a stderr warning.
384384

385385
**What a bare scan patched at and below `--cwd` is listed and rolled back from `--cwd` (additive/MINOR).** A bare scan covers `--cwd` and the project directories below it, so `list` and a bare `rollback` cover the project at `--cwd` and the patched project directories below it. A bare `rollback` reaches exactly as far as a bare scan from the same directory: from an agent-mode project (whose scan covers that project alone) it rolls back that project alone and names the directories below in a `projects_not_rolled_back` warning. `vex`, `apply`, `repair`, `remove` and `rollback <target>` act on the one project at `--cwd`, and name the patched directories below when `--cwd` holds nothing for them. The directories below are the ones a bare scan covers (`commands/projects_below.rs`: the same root detection and walk, the same `socket.yml` path filters and built-in `test/`, `tests/`, `fixtures/`, `__fixtures__/` and `testdata/` ignores, applied before any file of a directory is read; `SOCKET_NO_SOCKET_YML` bypasses the file here as `--no-socket-yml` does for the scan), kept when they hold patch state: a manifest with records, a vendor ledger with entries, or hosted pins in their lockfiles (a store that cannot be read counts, so the directory's own run reports it). Nothing is searched for when `--cwd` is not inside a git checkout, the scope is `--global`/`--global-prefix`, or `--manifest-path` is not the default (it names the one project the run is about). With no patched directory below, every command is, byte for byte, the single-project run it always was.
386386

@@ -2301,7 +2301,7 @@ command's integration tests check its output.
23012301
| `nested_project_skipped` | `warnings[]` (warning) | scan `--mode hosted` / `--mode vendored` (npm family): the crawl found a patched package only in the `node_modules` of a nested, independent npm project below the scan root (a directory with its own npm-family lock that is not a workspace member whose package manager ignores that lock), the root's lockfiles do not list it, and the root's own install holds no copy. A hosted or vendored run rewires only the root's lockfiles, so the package is left out of the run (no `packages[]` row, no event, no VEX statement) instead of failing `vendor_lock_entry_not_found` or skipping `redirect_unconfirmed`. One warning per nested project; the detail names the purls, the project directory and the PATH to pass (`socket-patch scan --mode <mode> <dir>`). Exit and status unchanged (#1534). |
23022302
| `patched_projects_below` | `warnings[]` (warning) | `vex`, `apply`, `repair`, `remove` and `rollback <target>`, on the envelope of a run that found no patch at `--cwd` (`manifest_not_found`, vex's `no_patches`, apply's `noManifest`) or, for `rollback <target>`, none the target matches (`patch_not_found`): project directories below `--cwd` hold patches. The detail names them and the command for each (`... --cwd <dir>`); see "What a bare scan patched at and below `--cwd`" under Scan modes for which directories each command names. Exit code, status and `error` are unchanged. |
23032303
| `projects_not_rolled_back` | `warnings[]` (warning) | bare `rollback` at a project that holds something to undo, above patched project directories, when the run covers `--cwd` alone: under `--json` (it reports on one project directory), and from an agent-mode project (a bare scan from it covers that project alone, so its rollback does too). `--cwd` was rolled back alone, as before. The detail names the directories left and the command for one of them (`socket-patch rollback [--json] --cwd <dir>`). Exit and status unchanged. |
2304-
| `projects_below_not_searched` | `warnings[]` (warning) | `list`: the project directories below `--cwd` could not all be searched for patches (a `socket.yml` that cannot be honored, or a tree of more than 500,000 entries). Exit and status unchanged. |
2304+
| `projects_below_not_searched` | `warnings[]` (warning) | `list`: the project directories below `--cwd` could not all be searched for patches (a `socket.yml` that cannot be honored, or a tree of more than 500,000 entries). A bare agent-mode `scan`: the tree is too large to search for the project directories it left, so its `projects_not_scanned` list may be short. Exit and status unchanged. |
23052305
| `redirect_vlt_lock_unsupported` | `warnings[]` (warning) | scan/get `--mode hosted` (vlt): `vlt-lock.json` has a `lockfileVersion` other than absent, `0` or `1` (decided on the raw JSON token), is not a JSON object, starts with a UTF-8 BOM, or its `nodes` section is not vlt's one-node-per-line layout. Nothing rewritten; also refuses a vendored → hosted takeover of a `flavor: "vlt"` entry before its revert (`skipped` (hosted event)). Exit 0. |
23062306
| `redirect_takeover_kept_vendored` | `warnings[]` (warning) | scan/get `--mode hosted`: a vendored → hosted takeover the hosted rewrite would not pin was retracted (see **Staged takeover**): the package keeps its vendored wiring, ledger entry and artifact byte-identical and stays patched. The detail names the cause code, which is also the purl's `skipped` (hosted event). Exit 0. Replaces v5.0-pre `redirect_takeover_unpatched`, which reported a package left unpatched in both modes and is no longer emitted. |
23072307
| `redirect_takeover_not_pinned` | `skipped` (hosted event) | scan/get `--mode hosted`: the skip reason of a retracted takeover when no rewriter warning names the cause. |

‎crates/socket-patch-cli/src/commands/projects_below.rs‎

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,7 @@ impl PatchedProject {
316316
/// arguments, already shell-quoted) for this project, as the run
317317
/// `common` was asked to: a preview stays a preview.
318318
pub(crate) fn command(&self, common: &GlobalArgs, command: &str) -> String {
319-
// `--cwd .` joins to `./api`; the bare relative path names the same
320-
// directory.
321-
let dir = self.dir.strip_prefix(".").unwrap_or(&self.dir);
322-
let dry_run = if common.dry_run { " --dry-run" } else { "" };
323-
format!(
324-
"socket-patch {command}{dry_run} --cwd {}",
325-
shell_word(&dir.to_string_lossy())
326-
)
319+
project_command(&self.dir, common, command)
327320
}
328321

329322
/// Whether a command acting on `work` has anything to do here.
@@ -343,6 +336,123 @@ impl PatchedProject {
343336
}
344337
}
345338

339+
/// The command line that runs `command` (the subcommand and its arguments,
340+
/// already shell-quoted) for the project directory `dir`, as the run
341+
/// `common` was asked to: a preview stays a preview.
342+
pub(crate) fn project_command(dir: &Path, common: &GlobalArgs, command: &str) -> String {
343+
// `--cwd .` joins to `./api`; the bare relative path names the same
344+
// directory.
345+
let dir = dir.strip_prefix(".").unwrap_or(dir);
346+
let dry_run = if common.dry_run { " --dry-run" } else { "" };
347+
format!(
348+
"socket-patch {command}{dry_run} --cwd {}",
349+
shell_word(&dir.to_string_lossy())
350+
)
351+
}
352+
353+
/// What [`not_crawled`] found.
354+
#[derive(Default)]
355+
pub(crate) struct NotCrawled {
356+
/// The project directories a crawl from `--cwd` does not cover, in
357+
/// [`discovered`]'s (sorted) order.
358+
pub dirs: Vec<PathBuf>,
359+
/// The walk gave up before it had seen the whole tree: `dirs` may be
360+
/// short.
361+
pub truncated: bool,
362+
}
363+
364+
/// How many project directories [`not_crawled`] resolves install
365+
/// directories for at a time: a gem home lookup can run `gem env`.
366+
const PROBES_AT_ONCE: usize = 8;
367+
368+
/// The project directories below `--cwd` whose installed packages a crawl
369+
/// from `--cwd` does not read: the ones an agent-mode scan, which patches
370+
/// the copies its crawl finds, leaves unscanned. A directory is one of
371+
/// them when it has an install directory of its own
372+
/// ([`own_install_roots`]) that the crawl from `--cwd` does not read
373+
/// ([`crawled_install_roots`]). So a project with nothing installed in a
374+
/// directory of its own is not, nor one whose `node_modules` the walk
375+
/// from `--cwd` reaches, nor one whose gem home `--cwd` reads too.
376+
///
377+
/// Like [`patched`] it reaches only the directories [`discovered`]
378+
/// returns, and none outside [`searches_below`].
379+
///
380+
/// [`own_install_roots`]: crate::ecosystem_dispatch::own_install_roots
381+
/// [`crawled_install_roots`]: crate::ecosystem_dispatch::crawled_install_roots
382+
pub(crate) async fn not_crawled(
383+
common: &GlobalArgs,
384+
policy: &SelectionPolicy,
385+
repo_root: &Path,
386+
) -> NotCrawled {
387+
use crate::ecosystem_dispatch::{crawled_install_roots, own_install_roots};
388+
use futures_util::StreamExt as _;
389+
use socket_patch_core::crawlers::walk_pool::fd_limit_is_tight;
390+
use std::collections::HashSet;
391+
392+
let mut out = NotCrawled::default();
393+
if !searches_below(common) {
394+
return out;
395+
}
396+
let found = discovered(common, policy, repo_root).await;
397+
out.truncated = found.truncated;
398+
let below: Vec<PathBuf> = found
399+
.dirs
400+
.into_iter()
401+
.filter(|(_, explicit)| !explicit)
402+
.map(|(dir, _)| dir)
403+
.collect();
404+
if below.is_empty() {
405+
return out;
406+
}
407+
let only = common.ecosystems.as_deref();
408+
let options = common.crawler_options();
409+
// Each directory's own install directories, a few directories at a
410+
// time (one under a tight descriptor limit, like the crawl itself).
411+
let at_once = if fd_limit_is_tight() {
412+
1
413+
} else {
414+
PROBES_AT_ONCE
415+
};
416+
let own: Vec<(PathBuf, Vec<_>)> = futures_util::stream::iter(below)
417+
.map(|dir| {
418+
let mut options = options.clone();
419+
options.cwd = dir.clone();
420+
async move {
421+
let roots = own_install_roots(&options, only).await;
422+
(dir, roots)
423+
}
424+
})
425+
.buffered(at_once)
426+
.collect()
427+
.await;
428+
// What the crawl from `--cwd` read, for the ecosystems in question.
429+
let ecosystems: HashSet<_> = own
430+
.iter()
431+
.flat_map(|(_, roots)| roots.iter().map(|(ecosystem, _)| *ecosystem))
432+
.collect();
433+
if ecosystems.is_empty() {
434+
return out;
435+
}
436+
let real = |path: &Path| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
437+
let here: HashSet<PathBuf> = crawled_install_roots(&options, &ecosystems)
438+
.await
439+
.iter()
440+
.map(|root| real(root))
441+
.collect();
442+
for (dir, roots) in own {
443+
if roots.iter().any(|(_, root)| !here.contains(&real(root))) {
444+
out.dirs.push(dir);
445+
}
446+
}
447+
out
448+
}
449+
450+
/// The [`PROJECTS_BELOW_NOT_SEARCHED`] detail of an agent-mode scan whose
451+
/// search for the project directories it left gave up.
452+
pub(crate) const NOT_CRAWLED_TRUNCATED: &str =
453+
"this directory tree is too large to search for project directories; some that this \
454+
scan did not reach may not be named";
455+
346456
/// What [`patched`] found.
347457
#[derive(Default)]
348458
pub(crate) struct Below {
@@ -470,7 +580,7 @@ pub(crate) struct Advice {
470580
const SPELLED_OUT: usize = 10;
471581

472582
/// `a, b, c`, or the first [`SPELLED_OUT`] and `and N more`.
473-
fn spelled_out(names: &[&str]) -> String {
583+
pub(crate) fn spelled_out(names: &[&str]) -> String {
474584
match names.len().checked_sub(SPELLED_OUT) {
475585
Some(more @ 1..) => format!("{}, and {more} more", names[..SPELLED_OUT].join(", ")),
476586
_ => names.join(", "),

0 commit comments

Comments
 (0)