Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/coder-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ The causes were in the loop, not only in prompt wording:

Each new or changed test runs in the sandbox with and without the
change (`habits_check`). A test the runner finds nothing in goes back to the
coder, and the coder quotes the run's output in its Evidence. End-to-end code
coder, and the coder quotes the run's output in its Evidence.
These runs install through CI's job for the test files. Without one, a
workspace member installs from the workspace root. They have a budget
of their own: a short step owes them to a fresh step, which runs them
before any other check. How many ran, and why the rest didn't, is
counted on the run and in the pull request (`incidents::tq_tests_run`). End-to-end code
against another company's page uses only what a probe observed (#734).

A proof fix round that answers a gap by looking (a query, a page, a check
Expand Down
22 changes: 22 additions & 0 deletions docs/incidents/tq_tests_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# tq. `tq_tests_run`

**What happened:** Across the 53 most recent runs, 17 had new or changed tests to run with and without the change. Not one of them ran both ways:

- **6 runs: the install failed in the sandbox.** All six were in one pnpm workspace. The test files sat in a workspace member (`apps/web`) whose lockfile is the workspace root's, and the sandbox installed the member on its own: `cd 'apps/web' && pnpm install --ignore-workspace --frozen-lockfile`. That folder has no lockfile, so pnpm stopped with `ERR_PNPM_NO_LOCKFILE` before any test ran, on every run. The run's CI install (cached per manifest blob) was never used for these runs, and the failure didn't say why. One of the runs also listed a Playwright spec as a unit test.
- **7 runs: no step had the time.** The check asked once for a step of its own. The fresh step ran every other check first (the proof, the link checks, the new-feature rules, the live probes, the scope ledger), so it reached the sandbox runs with less than their 760 seconds left, and its one request was already spent. One run resumed for the runs at 16:00:42 and only reached them at 16:05:42.
- **2 runs: CI's test command wasn't read.** A data pipeline's CI runs a `python "$t"` loop over its tests. Its fixture JSON files were also listed as tests, and one file without a command stopped every file from running.
- **2 runs: no test to run.** One change was only tests; the other changed no test file.

**The guarantee:**
- **Install the way CI does.** The sandbox runs use CI's job for the changed files: its install lines in order, its Node and pnpm versions, through the cached environment. Without one, a workspace member installs from the workspace root, falling back to the member and what it depends on. A failed install says why, with its error lines.
- **Unit tests only.** An end-to-end spec and test data (fixtures, snapshots) are never run as unit tests.
- **A budget of their own.** When a step is short, the runs are owed to a fresh step. That step runs them first, before any other check, and records the result for the head. A run asks for such a step up to `MAX_PROOF_STEPS` times; only past that are the tests recorded as not run.
- **Read the test command.** Each file runs with CI's test step narrowed to it. One function narrows pytest, unittest, Vitest, Jest, `go test` and `cargo test`, and package scripts and make targets are read through to their runner. When CI's step can't be narrowed, the file's own package's test script runs it. A file with no command doesn't stop the others.
- **Counted.** Whether the tests ran, and why not, is recorded on the run's checks (`tests_ran`), written to its progress notes, and shown in the pull request.

**Where it's enforced:**
- `common::e2e::test_package::workspace_member` and `install_cmd`;
- `common::test_env` (`narrow`, `resolve_scripts`, `package_test_command`, `INSTALL_FAILED`);
- `habits_check::bench_plan`, `install_failure`, `proof_time` and `run_owed`;
- the ledger's `proof_runs` and `proof_owed`;
- `habits_check::TestsRan`.
120 changes: 115 additions & 5 deletions services/common/src/e2e/test_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ pub fn packages_outside(
out.push(TestPackage {
dir: dir.clone(),
manager: lock.map(|(_, m)| *m).unwrap_or(Manager::Npm),
lockfile: lock.map(|(n, _)| n.to_string()),
// From the repository root, as `package_of` names it: a lockfile
// outside the package's folder is a workspace's.
lockfile: lock.map(|(n, _)| join(&dir, n)),
has_tsconfig: tree.iter().any(|p| *p == join(&dir, "tsconfig.json")),
lint: has_eslint(tree, &dir, &pj),
runner: runner_of(&pj),
Expand Down Expand Up @@ -316,14 +318,65 @@ pub fn ensure_manager(m: Manager) -> &'static str {
}
}

/// Installs the package's dependencies on their own, never the parent
/// workspace's, after making its manager available.
/// Whether the package is a member of a workspace whose lockfile sits at the
/// workspace's root (`lockfile` names a file outside the package's folder):
/// its dependencies install from that root, the way its CI installs them.
/// A package with its own lockfile, or none, installs on its own.
pub fn workspace_member(p: &TestPackage) -> bool {
let dir = p.dir.trim_end_matches('/');
p.lockfile
.as_deref()
.is_some_and(|l| !dir.is_empty() && dir != "." && !l.starts_with(&format!("{dir}/")))
}

/// The folder the install runs in: the workspace's root for a member
/// ([`workspace_member`]), else the package's own.
pub fn install_dir(p: &TestPackage) -> String {
if workspace_member(p) {
p.lockfile
.as_deref()
.map(|l| parent(l).to_string())
.filter(|d| !d.is_empty())
.unwrap_or_else(|| ".".into())
} else {
p.dir.clone()
}
}

/// Installs the package's dependencies, after making its manager
/// available: a package on its own never reaches for the parent
/// workspace's; a workspace member installs the workspace from its root
/// lockfile (run in [`install_dir`]). A member's install that fails as a
/// whole falls back to the narrowest one that still runs its tests: the
/// member and what it depends on.
///
/// Six pull requests' new tests never ran because a member of a pnpm
/// workspace was installed on its own (`cd apps/web && pnpm install
/// --ignore-workspace --frozen-lockfile`): its folder has no lockfile, so
/// pnpm stopped with `ERR_PNPM_NO_LOCKFILE` before any test.
pub fn install_cmd(p: &TestPackage) -> String {
format!("{}{}", ensure_manager(p.manager), install_only(p))
}

fn install_only(p: &TestPackage) -> String {
let frozen = p.lockfile.is_some();
if workspace_member(p) {
let member = format!("./{}", p.dir.trim_start_matches("./"));
return match p.manager {
Manager::Npm => format!(
"(npm ci --no-audit --no-fund || npm ci --no-audit --no-fund --workspace {})",
q(&member).unwrap_or_else(|| "'.'".into())
),
Manager::Pnpm => match q(&format!("{member}...")) {
Some(f) => {
format!("(pnpm install --frozen-lockfile || pnpm install --frozen-lockfile --filter {f})")
}
None => "pnpm install --frozen-lockfile".into(),
},
Manager::Yarn => "yarn install --ignore-engines --frozen-lockfile".into(),
Manager::Bun => "bun install --frozen-lockfile".into(),
};
}
match p.manager {
Manager::Npm if frozen => "npm ci --no-audit --no-fund --workspaces=false".into(),
Manager::Npm => "npm install --no-audit --no-fund --workspaces=false".into(),
Expand Down Expand Up @@ -362,7 +415,7 @@ pub fn check_steps(p: &TestPackage) -> Option<Vec<Step>> {
let dir = q(&p.dir)?;
let mut steps = vec![Step {
kind: "install",
cmd: format!("cd {dir} && {}", install_cmd(p)),
cmd: format!("cd {} && {}", q(&install_dir(p))?, install_cmd(p)),
}];
if p.has_tsconfig {
steps.push(Step {
Expand Down Expand Up @@ -697,7 +750,7 @@ mod tests {
let mut p = TestPackage {
dir: "tests/e2e".into(),
manager: Manager::Pnpm,
lockfile: Some("pnpm-lock.yaml".into()),
lockfile: Some("tests/e2e/pnpm-lock.yaml".into()),
has_tsconfig: false,
lint: false,
runner: None,
Expand All @@ -710,6 +763,7 @@ mod tests {
ensure_manager(Manager::Pnpm)
)
);
assert_eq!(install_dir(&p), "tests/e2e");
p.manager = Manager::Yarn;
assert!(install_cmd(&p).contains("--frozen-lockfile"));
p.manager = Manager::Npm;
Expand All @@ -722,6 +776,62 @@ mod tests {
assert!(check_steps(&p).is_none());
}

/// A pnpm workspace's member (`apps/web`, lockfile at the root): it
/// installs the workspace from the root, never on its own, where pnpm
/// stops with `ERR_PNPM_NO_LOCKFILE`.
#[test]
fn a_workspace_member_installs_from_the_workspace_root() {
let t = tree(&[
"package.json",
"pnpm-lock.yaml",
"pnpm-workspace.yaml",
"apps/web/package.json",
"apps/web/src/test/a.test.ts",
"e2e/package.json",
"e2e/pnpm-lock.yaml",
"e2e/tests/a.spec.ts",
]);
let web = package_of(&t, "apps/web/src/test/a.test.ts", &|_| None).unwrap();
assert_eq!(web.dir, "apps/web");
assert_eq!(web.lockfile.as_deref(), Some("pnpm-lock.yaml"));
assert!(workspace_member(&web));
assert_eq!(install_dir(&web), ".");
let steps = check_steps(&web).unwrap();
assert!(steps[0].cmd.starts_with("cd '.' && "), "{}", steps[0].cmd);
assert!(
!steps[0].cmd.contains("--ignore-workspace"),
"{}",
steps[0].cmd
);
assert!(steps[0].cmd.contains("pnpm install --frozen-lockfile"));
// The narrowest fallback: the member and what it depends on.
assert!(steps[0].cmd.contains("--filter './apps/web...'"));
// Type-check and lint still run in the member's folder.
assert!(steps
.iter()
.skip(1)
.all(|s| s.cmd.starts_with("cd 'apps/web'")));
// A package with its own lockfile stays on its own.
let e2e = package_of(&t, "e2e/tests/a.spec.ts", &|_| None).unwrap();
assert!(!workspace_member(&e2e));
assert_eq!(install_dir(&e2e), "e2e");
assert!(check_steps(&e2e).unwrap()[0]
.cmd
.contains("pnpm install --ignore-workspace --frozen-lockfile"));
// As `packages_outside` names it, the same.
let out = packages_outside(&t, &["e2e/tests/a.spec.ts".into()], &[], &|_| None);
assert_eq!(out[0].lockfile.as_deref(), Some("e2e/pnpm-lock.yaml"));
assert!(!workspace_member(&out[0]));
// The root package installs where it is.
let root = package_of(
&tree(&["package.json", "pnpm-lock.yaml", "a.test.ts"]),
"a.test.ts",
&|_| None,
)
.unwrap();
assert!(!workspace_member(&root));
}

#[test]
fn repeat_commands_use_the_runner_and_the_target() {
let mut p = TestPackage {
Expand Down
Loading
Loading