Skip to content

Commit 33030f6

Browse files
committed
refactor(check): keep stale-install probe worktree-local
1 parent 3f02d56 commit 33030f6

10 files changed

Lines changed: 57 additions & 330 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
"fallow:baseline": "fallow dead-code --save-baseline fallow-baselines/dead-code.json --summary && fallow health --report-only --save-baseline fallow-baselines/health.json --summary",
131131
"check:fallow": "fallow audit",
132132
"check:affected": "node --experimental-strip-types scripts/check-affected/run.ts",
133-
"check:affected:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/check-affected/model.test.ts scripts/check-affected/platform-packages.test.ts scripts/check-affected/device-lanes.test.ts scripts/check-affected/run.test.ts",
133+
"check:affected:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/check-affected/model.test.ts scripts/check-affected/platform-packages.test.ts scripts/check-affected/device-lanes.test.ts scripts/check-affected/lockfile-install-sync.test.ts scripts/check-affected/run.test.ts",
134134
"gate": "node --experimental-strip-types scripts/gate/run.ts",
135135
"check:gate-manifest": "node --experimental-strip-types scripts/gate/check.ts",
136136
"check:gate-manifest:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/gate/*.test.ts",

src/utils/lockfile-install-sync.test.ts renamed to scripts/check-affected/lockfile-install-sync.test.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import path from 'node:path';
4-
import { test } from 'vitest';
5-
import { mkdtempForTestSync } from '../__tests__/test-utils/tmp-dir.ts';
4+
import { test } from 'node:test';
5+
import { mkdtempForTestSync } from '../../src/__tests__/test-utils/tmp-dir.ts';
66
import { checkLockfileInstallSync } from './lockfile-install-sync.ts';
77

88
function writeLockfile(root: string, content: string): void {
@@ -47,9 +47,6 @@ test('reports install-missing when a source checkout has no installed snapshot',
4747
test('reports install-missing for a fresh worktree that has no node_modules directory at all', () => {
4848
const root = mkdtempForTestSync('agent-device-lockfile-sync-fresh-worktree-');
4949
writeLockfile(root, 'lockfileVersion: 9.0\n');
50-
// Deliberately no node_modules directory — the exact state of a brand new
51-
// `git worktree add`, before its first `pnpm install`. This must stay a real
52-
// finding: it is the resolve-to-main-checkout trap #1963 names.
5350
assert.equal(fs.existsSync(path.join(root, 'node_modules')), false);
5451

5552
assert.deepEqual(checkLockfileInstallSync(root), {
@@ -58,30 +55,21 @@ test('reports install-missing for a fresh worktree that has no node_modules dire
5855
});
5956
});
6057

61-
test('reports no-source-checkout when there is no pnpm-lock.yaml — the packaged-install shape', () => {
62-
// A published agent-device: package.json `files` ships bin/ and dist/ but never the
63-
// lockfile, and an npm tarball never carries node_modules. Neither file is present,
64-
// and the answer must be "not applicable" rather than any kind of defect.
58+
test('reports no-source-checkout when there is no pnpm-lock.yaml', () => {
6559
const root = mkdtempForTestSync('agent-device-lockfile-sync-packaged-');
66-
fs.mkdirSync(path.join(root, 'dist'), { recursive: true });
6760
fs.writeFileSync(path.join(root, 'package.json'), '{"name":"agent-device"}\n');
6861

6962
assert.deepEqual(checkLockfileInstallSync(root), { status: 'no-source-checkout' });
7063
});
7164

72-
test('reports no-source-checkout even when a node_modules snapshot exists without a lockfile', () => {
73-
// The lockfile is the discriminator, not the snapshot: a consuming project could have
74-
// its own node_modules around the packaged install without that making our root a
75-
// source checkout to diagnose.
65+
test('reports no-source-checkout even when an installed snapshot exists without a lockfile', () => {
7666
const root = mkdtempForTestSync('agent-device-lockfile-sync-no-lockfile-');
7767
writeInstalledSnapshot(root, 'lockfileVersion: 9.0\n');
7868

7969
assert.deepEqual(checkLockfileInstallSync(root), { status: 'no-source-checkout' });
8070
});
8171

82-
test('is not fooled by two worktrees sharing the same repo but different lockfile states', () => {
83-
// The failure mode from #1963: a worktree's own node_modules must be compared against
84-
// that same worktree's own pnpm-lock.yaml, never another checkout's.
72+
test('checks each worktree against its own lockfile state', () => {
8573
const worktreeA = mkdtempForTestSync('agent-device-lockfile-sync-worktree-a-');
8674
const worktreeB = mkdtempForTestSync('agent-device-lockfile-sync-worktree-b-');
8775
writeLockfile(worktreeA, 'lockfileVersion: 9.0\nimporters:\n .: {}\n');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
const LOCKFILE_BASENAME = 'pnpm-lock.yaml';
5+
const INSTALLED_SNAPSHOT_RELATIVE_PATH = ['node_modules', '.pnpm', 'lock.yaml'];
6+
7+
export const STALE_NODE_MODULES_MESSAGE =
8+
"node_modules does not match this worktree's pnpm-lock.yaml";
9+
10+
export type LockfileInstallSyncResult =
11+
| { readonly status: 'no-source-checkout' }
12+
| { readonly status: 'in-sync' }
13+
| {
14+
readonly status: 'out-of-sync';
15+
readonly reason: 'install-missing' | 'stale';
16+
};
17+
18+
/**
19+
* Compares pnpm's installed lockfile snapshot with this worktree's lockfile.
20+
* This is intentionally synchronous and subprocess-free: it runs before any gate.
21+
*/
22+
export function checkLockfileInstallSync(repoRoot: string): LockfileInstallSyncResult {
23+
const lockfile = readFileIfExists(path.join(repoRoot, LOCKFILE_BASENAME));
24+
if (!lockfile) return { status: 'no-source-checkout' };
25+
26+
const installedSnapshot = readFileIfExists(
27+
path.join(repoRoot, ...INSTALLED_SNAPSHOT_RELATIVE_PATH),
28+
);
29+
if (!installedSnapshot) return { status: 'out-of-sync', reason: 'install-missing' };
30+
31+
return lockfile.equals(installedSnapshot)
32+
? { status: 'in-sync' }
33+
: { status: 'out-of-sync', reason: 'stale' };
34+
}
35+
36+
function readFileIfExists(filePath: string): Buffer | undefined {
37+
if (!fs.existsSync(filePath)) return undefined;
38+
return fs.readFileSync(filePath);
39+
}

‎scripts/check-affected/run.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import os from 'node:os';
99
import path from 'node:path';
1010
import { test } from 'node:test';
1111
import { runCmdSync } from '../../src/utils/exec.ts';
12-
import { STALE_NODE_MODULES_MESSAGE } from '../../src/utils/lockfile-install-sync.ts';
12+
import { STALE_NODE_MODULES_MESSAGE } from './lockfile-install-sync.ts';
1313
import { CHECK_CATALOG } from './checks.ts';
1414
import { DEFAULT_VITEST_MAX_WORKERS } from '../lib/vitest-concurrency.ts';
1515
import { selectChecks } from './model.ts';
@@ -348,6 +348,10 @@ test('runChecks names the real cause on stderr instead of surfacing as an unrela
348348
stderrChunks.some((chunk) => chunk.includes(STALE_NODE_MODULES_MESSAGE)),
349349
`expected stderr to name the stale-install cause, got: ${stderrChunks.join('')}`,
350350
);
351+
const stderr = stderrChunks.join('');
352+
assert.match(stderr, /Worktree: \.\n/);
353+
assert.match(stderr, /pnpm install --frozen-lockfile/);
354+
assert.doesNotMatch(stderr, /agent-device doctor/);
351355
} finally {
352356
process.stderr.write = originalWrite;
353357
}

‎scripts/check-affected/run.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
checkLockfileInstallSync,
1414
STALE_NODE_MODULES_MESSAGE,
1515
type LockfileInstallSyncResult,
16-
} from '../../src/utils/lockfile-install-sync.ts';
16+
} from './lockfile-install-sync.ts';
1717
import { parseScriptArgs } from '../lib/cli-args.ts';
1818
import { runEntrypoint } from '../lib/cli-entrypoint.ts';
1919
import {
@@ -198,13 +198,10 @@ export async function runChecks(
198198
): Promise<number> {
199199
const cwd = options.cwd ?? repoRoot;
200200
const checkLockfileSync = options.checkLockfileSync ?? checkLockfileInstallSync;
201-
// Fast preflight, before format (first in the catalog) or any other check gets a
202-
// chance to run: a stale install (#1956) makes oxfmt/oxlint/tsc misbehave in ways
203-
// that look like real diffs/failures on files the change never touched. Naming the
204-
// real cause here means an agent never "fixes" formatting that was never wrong.
201+
// Fail before any gate can misdiagnose a stale worktree install (#1956).
205202
const lockfileSync = checkLockfileSync(cwd);
206203
if (lockfileSync.status === 'out-of-sync') {
207-
reportStaleInstall(lockfileSync.reason);
204+
reportStaleInstall(lockfileSync.reason, cwd);
208205
return 1;
209206
}
210207
const execute = options.execute ?? streamingExecutor;
@@ -232,14 +229,15 @@ export async function runChecks(
232229
return 0;
233230
}
234231

235-
function reportStaleInstall(reason: 'install-missing' | 'stale'): void {
232+
function reportStaleInstall(reason: 'install-missing' | 'stale', cwd: string): void {
236233
process.stderr.write(`\ncheck:affected: ${STALE_NODE_MODULES_MESSAGE}\n`);
237234
process.stderr.write(
238235
reason === 'install-missing'
239236
? '(no node_modules/.pnpm/lock.yaml found — this checkout was never installed)\n'
240237
: '(node_modules/.pnpm/lock.yaml disagrees with pnpm-lock.yaml)\n',
241238
);
242-
process.stderr.write('Run `agent-device doctor` for more detail.\n');
239+
process.stderr.write(`Worktree: ${cwd}\n`);
240+
process.stderr.write('Run `pnpm install --frozen-lockfile` in this worktree, then retry.\n');
243241
}
244242

245243
// Where a check the local run skips is authoritative. A parked check has no automatic

‎src/daemon/handlers/__tests__/session-doctor-node-modules.test.ts‎

Lines changed: 0 additions & 69 deletions
This file was deleted.

‎src/daemon/handlers/session-doctor-node-modules.ts‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎src/daemon/handlers/session-doctor.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
resolveDoctorDeviceForAppCheck,
1616
} from './session-doctor-device.ts';
1717
import { probeMetro } from './session-doctor-metro.ts';
18-
import { nodeModulesLockfileCheck } from './session-doctor-node-modules.ts';
1918
import {
2019
readDoctorOptions,
2120
remoteConnectionChecks,
@@ -74,14 +73,6 @@ export async function handleDoctorCommand(params: {
7473
return doctorResponse(checks, options);
7574
}
7675

77-
// Local-only from here down. The worktree/lockfile probe must stay below this
78-
// return: under --remote the daemon's own root describes the server deployment,
79-
// not the caller's checkout, so the answer could not address #1963 anyway. The
80-
// probe also returns undefined when there is no source checkout (a packaged
81-
// install), leaving no check line rather than a meaningless one.
82-
const nodeModules = nodeModulesLockfileCheck();
83-
if (nodeModules) appendDoctorCheck(checks, nodeModules);
84-
8576
const inventory = await appendDeviceInventoryCheck(checks, req, session);
8677
await appendToolchainChecks(checks, session?.device.platform ?? inventory?.platform);
8778
const appCheckDevice = await appendLocalDoctorChecks({

‎src/utils/lockfile-install-sync.ts‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)