Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,13 @@ trees, on both platforms, without a device.
runner major is the one legitimate rewrite (`>=1.0.0 <2.0.0`, engines
patched): `version-packages` runs `scripts/restore-peer-ranges.ts` after
`changeset version`, which puts that in shape and leaves a wide range alone.
The same check fails when a package that depends on `agent-device` or an
`@agent-device/*` plugin pins another version than `@e2e-dev/mobile`, has
no `@e2e-dev/mobile` peer, or peers on it with a floor that does not ship
that pin: a tagged `@e2e-dev/mobile@<floor>` pinning another agent-device,
or an untagged floor below the engine's current version
(`scripts/agent-device-pins.ts`, reading the tags). An agent-device bump
moves every pin and every such floor in one change.
- The root `release` script publishes with no `--tag`, so versioned releases
land on `latest`. `changeset publish` passes `--tag` through to the publish
tool when given one, so `publishConfig.tag: "latest"` on every package is
Expand Down
136 changes: 136 additions & 0 deletions scripts/agent-device-pins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { agentDevicePins, type EngineReleases } from './agent-device-pins.ts';
import type { PackageManifest } from './public-packages.ts';

const engine: PackageManifest = { name: '@e2e-dev/mobile', version: '0.12.9', dependencies: { 'agent-device': '0.21.44' } };

/** Engine releases as tags would give them: each released version and the agent-device it pinned. */
function releases(history: Record<string, string>): EngineReleases {
return {
release: (version) => (version in history ? { tagged: true, agentDevice: history[version] } : { tagged: false }),
releasedWith: (pin) => Object.keys(history).filter((version) => history[version] === pin),
};
}

/** Tags up to 0.12.9, which pinned 0.21.42; the pin 0.21.44 is not released yet, so it ships in 0.12.10. */
const history = releases({ '0.12.8': '0.21.39', '0.12.9': '0.21.42' });

/** A provider package pinning `driver` at `version`, peering on the engine at `floor`. */
function provider(name: string, driver: string, version: string, floor: string | undefined): PackageManifest {
return {
name,
dependencies: { [driver]: version },
...(floor === undefined ? {} : { peerDependencies: { '@e2e-dev/mobile': `>=${floor} <1` } }),
};
}

describe('agentDevicePins', () => {
it('passes when every driver package pins the engine version and peers on the release that ships it', () => {
const result = agentDevicePins([
engine,
provider('@e2e-dev/testmu', '@agent-device/testmu', '0.21.44', '0.12.10'),
provider('@e2e-dev/e2b', 'agent-device', '0.21.44', '0.12.10'),
{ name: '@e2e-dev/eas', peerDependencies: { '@e2e-dev/mobile': '>=0.12.9 <1' } },
], history);
assert.deepEqual(result.problems, []);
assert.equal(result.version, '0.21.44');
assert.deepEqual(result.pinned, ['@e2e-dev/e2b', '@e2e-dev/mobile', '@e2e-dev/testmu']);
});

it('accepts a floor on a tagged release that ships the pin', () => {
const released = releases({ '0.12.9': '0.21.42', '0.12.10': '0.21.44' });
const { problems } = agentDevicePins([{ ...engine, version: '0.12.10' }, provider('@e2e-dev/limrun', 'agent-device', '0.21.44', '0.12.10')], released);
assert.deepEqual(problems, []);
});

it('flags a package a bump missed, by name and both versions', () => {
const { problems } = agentDevicePins([engine, provider('@e2e-dev/revyl', '@agent-device/revyl', '0.21.43', '0.12.10')], history);
assert.equal(problems.length, 1);
assert.match(problems[0]!, /@e2e-dev\/revyl depends on @agent-device\/revyl "0\.21\.43"; @e2e-dev\/mobile pins agent-device "0\.21\.44"/);
});

it('flags a bump that moved every pin and no floor', () => {
// 0.12.10 is released with 0.21.44; every pin moved to 0.21.45, every floor stayed.
const released = releases({ '0.12.9': '0.21.42', '0.12.10': '0.21.44' });
const bumped = { ...engine, version: '0.12.10', dependencies: { 'agent-device': '0.21.45' } };
const { problems } = agentDevicePins([
bumped,
provider('@e2e-dev/limrun', 'agent-device', '0.21.45', '0.12.10'),
provider('@e2e-dev/testmu', '@agent-device/testmu', '0.21.45', '0.12.10'),
], released);
assert.equal(problems.length, 2);
assert.match(problems[0]!, /@e2e-dev\/mobile 0\.12\.10 pins agent-device "0\.21\.44", not "0\.21\.45": raise it to >=0\.12\.11/);
});

it('points a floor on an older release at the first release that ships the pin, as #2304 needed', () => {
// Every pin moved 0.21.42 -> 0.21.43 while 0.12.9 (the newest tag) still ships 0.21.42.
const moved = { ...engine, dependencies: { 'agent-device': '0.21.43' } };
const { problems } = agentDevicePins([
moved,
provider('@e2e-dev/revyl', '@agent-device/revyl', '0.21.43', '0.12.9'),
provider('@e2e-dev/e2b', 'agent-device', '0.21.43', '0.12.6'),
], history);
assert.equal(problems.length, 2);
for (const problem of problems) assert.match(problem, /raise it to >=0\.12\.10/);
});

it('accepts an untagged floor at the current engine version, as the release commit has it before publish tags it', () => {
const { problems } = agentDevicePins([{ ...engine, version: '0.12.10' }, provider('@e2e-dev/limrun', 'agent-device', '0.21.44', '0.12.10')], history);
assert.deepEqual(problems, []);
});

it('flags an untagged floor below the current engine version', () => {
const { problems } = agentDevicePins([engine, provider('@e2e-dev/limrun', 'agent-device', '0.21.44', '0.12.7')], history);
assert.equal(problems.length, 1);
assert.match(problems[0]!, /no @e2e-dev\/mobile 0\.12\.7 release is tagged/);
});

it('reports only the mis-pinned package, not its floor', () => {
const { problems } = agentDevicePins([
engine,
provider('@e2e-dev/limrun', 'agent-device', '0.21.44', '0.12.10'),
provider('@e2e-dev/revyl', '@agent-device/revyl', '0.21.45', '0.12.6'),
], history);
assert.equal(problems.length, 1);
assert.match(problems[0]!, /@e2e-dev\/revyl depends on @agent-device\/revyl "0\.21\.45"/);
});

it('holds an optional dependency on agent-device to the pin, over a regular one of the same name', () => {
const { problems, pinned } = agentDevicePins([
engine,
{
name: '@e2e-dev/cloud',
dependencies: { '@agent-device/cloud': '0.21.44' },
optionalDependencies: { '@agent-device/cloud': '0.21.40' },
peerDependencies: { '@e2e-dev/mobile': '>=0.12.10 <1' },
},
], history);
assert.deepEqual(pinned, ['@e2e-dev/cloud', '@e2e-dev/mobile']);
assert.equal(problems.length, 1);
assert.match(problems[0]!, /@e2e-dev\/cloud depends on @agent-device\/cloud "0\.21\.40"/);
});

it('flags a driver package with no engine peer', () => {
const { problems } = agentDevicePins([engine, provider('@e2e-dev/limrun', 'agent-device', '0.21.44', undefined)], history);
assert.equal(problems.length, 1);
assert.match(problems[0]!, /has no @e2e-dev\/mobile peer/);
});

it('checks pins but notes the floors it could not check in a checkout without tags', () => {
const { problems, notes } = agentDevicePins([engine, provider('@e2e-dev/e2b', 'agent-device', '0.21.44', '0.12.6')], undefined);
assert.deepEqual(problems, []);
assert.equal(notes.length, 1);
});

it('reports a missing engine pin instead of passing', () => {
const { problems } = agentDevicePins([{ name: '@e2e-dev/mobile' }], history);
assert.equal(problems.length, 1);
});

it('ignores packages that do not drive agent-device', () => {
const { problems, pinned } = agentDevicePins([engine, { name: '@e2e-dev/web', dependencies: { 'playwright-core': '1.65.1' } }], history);
assert.deepEqual(problems, []);
assert.deepEqual(pinned, ['@e2e-dev/mobile']);
});
});
108 changes: 108 additions & 0 deletions scripts/agent-device-pins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* The agent-device version every package that drives a device through it
* must pin. `@e2e-dev/mobile` pins `agent-device` exactly, and a provider
* that starts its own daemon (`agent-device`) or loads a device-cloud plugin
* (`@agent-device/<cloud>`) checks at the first lease that its copy is the
* engine's (`agentDevicePlugin().assertVersion`). npm cannot see that check,
* so a pin one package missed in a bump installs cleanly and fails the run.
*
* The provider's peer floor on the engine is what keeps a user's install in
* step, so it must name an engine release that ships the same pin: a tagged
* `@e2e-dev/mobile@<floor>` whose manifest pins it, or, with no such tag, the
* release still to come, at or above the engine's current version.
*/

import { wideRangeFloor, type PackageManifest } from './public-packages.ts';

const ENGINE = '@e2e-dev/mobile';

/** What the engine's release history says about one version: the agent-device it pinned, or that no release of it is tagged. */
export type EngineRelease = { readonly tagged: true; readonly agentDevice: string | undefined } | { readonly tagged: false };

/** The engine's releases, read from its git tags; `undefined` when the checkout has none of them. */
export interface EngineReleases {
release(version: string): EngineRelease;
/** The versions released with `pin`, oldest first. */
releasedWith(pin: string): readonly string[];
}

/** Whether `name` is agent-device or one of its device-cloud plugins. */
function isAgentDevicePackage(name: string): boolean {
return name === 'agent-device' || name.startsWith('@agent-device/');
}

/** Compares two `major.minor.patch` versions, ignoring any prerelease. */
export function compareVersions(a: string, b: string): number {
const parts = (version: string) => version.split(/[.-]/).slice(0, 3).map(Number);
const [x, y] = [parts(a), parts(b)];
for (let index = 0; index < 3; index += 1) {
const diff = (x[index] ?? 0) - (y[index] ?? 0);
if (diff !== 0) return diff;
}
return 0;
}

/** The next patch release after `version`. */
function nextPatch(version: string): string {
const [major, minor, patch] = version.split(/[.-]/).slice(0, 3).map(Number);
return `${major}.${minor}.${(patch ?? 0) + 1}`;
}

/** What `agentDevicePins` found: the pin, the packages on it, every disagreement as one line, and notes that do not fail the check. */
export interface AgentDevicePins {
readonly version: string | undefined;
readonly pinned: readonly string[];
readonly problems: readonly string[];
readonly notes: readonly string[];
}

/**
* Checks that every package depending on agent-device or a plugin of it pins
* the version `@e2e-dev/mobile` pins, exactly, and that every such package
* other than the engine peers on the engine with a floor that ships that pin.
* Without `releases` (a checkout with no engine tags), the floors are not
* checked and a note says so.
*/
export function agentDevicePins(manifests: readonly PackageManifest[], releases: EngineReleases | undefined): AgentDevicePins {
const engine = manifests.find((manifest) => manifest.name === ENGINE);
const version = engine?.dependencies?.['agent-device'];
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
if (engine === undefined || version === undefined) {
return { version: undefined, pinned: [], problems: [`${ENGINE} does not depend on agent-device, so no pin can be checked`], notes: [] };
}
const current = engine.version ?? '0.0.0';
const shipped = releases?.releasedWith(version)[0];
const expected = shipped ?? nextPatch(current);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
const problems: string[] = [];
const notes: string[] = [];
const pinned: string[] = [];
for (const manifest of manifests) {
// npm installs an optional dependency over a same-named regular one, so it is the one held to the pin.
const drivers = Object.entries({ ...manifest.dependencies, ...manifest.optionalDependencies }).filter(([name]) => isAgentDevicePackage(name));
if (drivers.length === 0) continue;
pinned.push(manifest.name);
const offPin = drivers.filter(([, range]) => range !== version);
for (const [name, range] of offPin) {
problems.push(`${manifest.name} depends on ${name} ${JSON.stringify(range)}; ${ENGINE} pins agent-device ${JSON.stringify(version)}`);
}
if (manifest.name === ENGINE) continue;
const range = manifest.peerDependencies?.[ENGINE];
if (range === undefined) {
problems.push(`${manifest.name} drives agent-device but has no ${ENGINE} peer, so nothing keeps the engine it runs beside on the same agent-device`);
continue;
}
// A package off the pin is reported above; its floor belongs to another pin.
if (offPin.length > 0 || releases === undefined) continue;
const floor = wideRangeFloor(range);
if (floor === undefined) continue;
const release = releases.release(floor);
const fix = `raise it to >=${expected}, the first ${ENGINE} release on agent-device ${version}`;
if (release.tagged && release.agentDevice !== version) {
problems.push(`${manifest.name} peers on ${ENGINE} ${JSON.stringify(range)}, but ${ENGINE} ${floor} pins agent-device ${JSON.stringify(release.agentDevice)}, not ${JSON.stringify(version)}: ${fix}`);
} else if (!release.tagged && compareVersions(floor, current) < 0) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
// An untagged floor at the current version is the release being cut: `version-packages` moves the engine before publish tags it.
problems.push(`${manifest.name} peers on ${ENGINE} ${JSON.stringify(range)}, but no ${ENGINE} ${floor} release is tagged, and ${current} is the engine's current version: ${fix}`);
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
}
}
if (releases === undefined) notes.push(`${ENGINE} floors not checked: this checkout has no ${ENGINE}@ tags (git fetch --tags)`);
return { version, pinned: pinned.toSorted(), problems, notes };
}
43 changes: 43 additions & 0 deletions scripts/check-peer-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
* (ERESOLVE). `restore-peer-ranges.ts` puts the ranges back after a publish;
* this check is what notices when it did not run.
*
* It also holds every package that drives agent-device to the engine's pin
* (`agent-device-pins.ts`): the version check between them runs only once a
* device is leased, so a pin a bump missed would install and then fail.
*
* Usage: `node scripts/check-peer-ranges.ts`, part of `pnpm check`. Exits 1
* listing every offending peer.
*/

import { execFileSync } from 'node:child_process';
import { relative } from 'node:path';
import { agentDevicePins, compareVersions, type EngineReleases } from './agent-device-pins.ts';
import { REPO_ROOT, isWidePeerRange, publicPackages, siblingPeers } from './public-packages.ts';

const packages = publicPackages();
Expand All @@ -33,9 +39,46 @@ for (const { path, manifest } of packages) {
}
}

/** Runs git in the repository; undefined when it fails (no git, a path the tag does not have). */
function git(...args: string[]): string | undefined {
try {
return execFileSync('git', args, { cwd: REPO_ROOT, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
} catch {
return undefined;
}
}

/** The engine's releases from its `@e2e-dev/mobile@<version>` tags; undefined in a checkout without them. */
function engineReleases(): EngineReleases | undefined {
Comment thread
madhusudanpatnaik marked this conversation as resolved.
Resolved
const prefix = '@e2e-dev/mobile@';
const listed = git('tag', '--list', `${prefix}*`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lists every @e2e-dev/mobile@* tag in the clone, including releases the branch does not contain. Once a newer engine release on another pin is fetched, pnpm check fails on branches behind main and suggests the floor they already have. git tag --merged HEAD --list '@e2e-dev/mobile@*' reads only reachable tags.

// A git that cannot run is not a checkout without tags: the floors would go unchecked with only a note.
if (listed === undefined) throw new Error(`check-peer-ranges: git could not list the ${prefix} tags, so the agent-device floors cannot be checked`);
const versions = listed.split('\n').map((tag) => tag.trim()).filter((tag) => tag.startsWith(prefix)).map((tag) => tag.slice(prefix.length));
if (versions.length === 0) return undefined;
const pins = new Map<string, string | undefined>();
const pinOf = (version: string): string | undefined => {
if (!pins.has(version)) {
const manifest = git('show', `${prefix}${version}:packages/mobile/package.json`);
pins.set(version, manifest === undefined ? undefined : (JSON.parse(manifest) as { dependencies?: Record<string, string> }).dependencies?.['agent-device']);
}
return pins.get(version);
};
const tagged = new Set(versions);
return {
release: (version) => (tagged.has(version) ? { tagged: true, agentDevice: pinOf(version) } : { tagged: false }),
releasedWith: (pin) => versions.filter((version) => !version.includes('-') && pinOf(version) === pin).toSorted(compareVersions),
};
}

const pins = agentDevicePins(packages.map(({ manifest }) => manifest), engineReleases());
problems.push(...pins.problems);
for (const note of pins.notes) process.stderr.write(`${note}\n`);

if (problems.length > 0) {
process.stderr.write(`${problems.join('\n')}\n`);
process.exitCode = 1;
} else {
process.stdout.write(`peer ranges: ${checked.toSorted().join(', ')} as wide ranges\n`);
process.stdout.write(`agent-device: ${pins.pinned.join(', ')} on ${String(pins.version)}\n`);
}
12 changes: 10 additions & 2 deletions scripts/public-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export interface PackageManifest {
name: string;
version?: string;
private?: boolean;
dependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
}

const WIDE_RANGE = /^>=(\d+)\.\d+\.\d+ <(\d+)$/;
const WIDE_RANGE = /^>=(\d+)\.(\d+)\.(\d+) <(\d+)$/;

/**
* Every publishable package under `packages/`: a named manifest without
Expand Down Expand Up @@ -68,7 +70,13 @@ export function widePeerRange(version: string): string {
/** Whether `range` has the shape `widePeerRange` produces, whatever its floor. */
export function isWidePeerRange(range: string): boolean {
const match = WIDE_RANGE.exec(range);
return match !== null && Number(match[2]) === Number(match[1]) + 1;
return match !== null && Number(match[4]) === Number(match[1]) + 1;
}

/** The floor of a range `widePeerRange` produces, as `major.minor.patch`; undefined for any other shape. */
export function wideRangeFloor(range: string): string | undefined {
const match = WIDE_RANGE.exec(range);
return match === null || Number(match[4]) !== Number(match[1]) + 1 ? undefined : `${match[1]}.${match[2]}.${match[3]}`;
}

/** The peers in `peers` that name another public package, with the range each declares. */
Expand Down
Loading