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
323 changes: 204 additions & 119 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
"@agent-relay/utils": "12.2.5",
"@modelcontextprotocol/sdk": "^1.23.0",
"@relayfile/client": "^0.10.27",
"@relayfile/sdk": "^0.10.63",
"@relayfile/sdk": "^0.10.64",
"@relayflows/cli": "1.0.1",
"@relayflows/sdk": "^2.0.16",
"@relayflows/sdk": "^2.0.19",
"@xterm/headless": "^6.0.0",
"ai-hist": "^0.18.0",
"ai-hist": "^0.18.1",
"commander": "^12.1.0",
"dotenv": "^17.2.3",
"jiti": "^2.6.1",
Expand Down
41 changes: 38 additions & 3 deletions packages/cli/src/cli/ci-standalone-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,21 @@ exit 0
set -euo pipefail
printf 'cli %s %s\\n' "\${1:-}" "\${2:-}" >> "$INVOCATION_LOG"

if [ "\${1:-}" != "node" ]; then
exit 64
fi
case "\${1:-}" in
node) ;;
file|flows|sessions)
# The mounted product groups. In the real binary these install their SDK on
# first use; here they only have to render the usage line the smoke greps
# for, or fail the way an unreachable SDK fails.
if [ "\${FAKE_MOUNT_BROKEN:-}" = "\${1:-}" ]; then
echo "\\\`agent-relay \${1}\\\` needs @relayfile/sdk, which is not installed." >&2
exit 1
fi
echo "Usage: agent-relay \${1} [options]"
exit 0
;;
*) exit 64 ;;
esac

case "\${2:-}" in
status)
Expand Down Expand Up @@ -287,6 +299,29 @@ describe('ci-standalone-smoke workspace reuse', () => {
expect(result.stderr).not.toContain('rk_live_fake_smoke_key');
});

it('verifies every mounted product group and fails when one cannot mount', () => {
// The check whose absence let #1795 ship: the broker lifecycle passed while
// `agent-relay file` could not load its SDK at all. It has to bite for each
// group, not just the first.
for (const group of ['file', 'flows', 'sessions']) {
const { cli, broker, invocationLog, toolsPath } = createFakeBinaries();
const result = spawnSync('bash', [smokeScript, cli, broker], {
encoding: 'utf8',
env: {
...process.env,
PATH: `${toolsPath}:${process.env.PATH ?? ''}`,
INVOCATION_LOG: invocationLog,
FAKE_MOUNT_BROKEN: group,
},
timeout: 10_000,
});

expect(result.status, `${group}: ${result.stderr}`).not.toBe(0);
expect(result.stderr, group).toContain(`cannot mount \`agent-relay ${group}\``);
expect(result.stdout, group).not.toContain('Standalone smoke passed');
}
});

it('accepts a successful delete without adding a follow-up read dependency', () => {
const { cli, broker, invocationLog, toolsPath } = createFakeBinaries();
const result = spawnSync('bash', [smokeScript, cli, broker], {
Expand Down
229 changes: 229 additions & 0 deletions packages/cli/src/cli/commands/product-surfaces.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { readFileSync } from 'node:fs';

import { Command } from 'commander';
import { describe, expect, it, vi } from 'vitest';

import type { RelayCliIo, RelayCliSurface } from '@agent-relay/cli-surface';

import { SurfaceProvisionError } from '../lib/product-surface-store.js';

import {
PRODUCT_SURFACES,
SURFACE_PACKAGES,
importProductSurface,
loadProductSurface,
registerProductSurfaceCommands,
type ProductSurfaceDefinition,
Expand Down Expand Up @@ -108,6 +114,82 @@ describe('loadProductSurface', () => {
).rejects.toThrow(/needs @relayfile\/sdk, which is not installed/);
});

it.each([
['Windows drive letter', 'C:\\Users\\dev\\node_modules\\@relayfile\\sdk\\dist\\index.js'],
['UNC share', '\\\\build\\share\\node_modules\\@relayfile\\sdk\\dist\\index.js'],
])('treats an incomplete install as incomplete on a %s path', async (_label, modulePath) => {
// The first version of this check required a leading "/", so every Windows
// and UNC incomplete install was reported as "not installed" — sending the
// operator to reinstall a package already on disk.
const partial = Object.assign(new Error(`Cannot find module '${modulePath}'`), {
code: 'ERR_MODULE_NOT_FOUND',
});
await expect(
loadProductSurface(
DEFINITION,
makeDeps(async () => {
throw partial;
})
)
).rejects.toThrow(/installed but incomplete/);
});

it('blames the dependency, not the product, for a CommonJS module error', async () => {
// A CommonJS failure names a bare specifier — `Cannot find module 'yaml'` —
// with no path and no "package" wording, so neither of the other two
// matchers sees it. Left alone it falls through and tells the operator to
// reinstall @relayfile/sdk, which is present and not the problem.
const transitive = Object.assign(new Error("Cannot find module 'yaml'"), {
code: 'MODULE_NOT_FOUND',
});
const message = await loadProductSurface(
DEFINITION,
makeDeps(async () => {
throw transitive;
})
).catch((error: Error) => error.message);

expect(message).toMatch(/yaml/);
expect(message).not.toMatch(/needs @relayfile\/sdk, which is not installed/);
});

it('separates an incomplete install from a missing package', async () => {
// ERR_MODULE_NOT_FOUND covers both, and reporting the second as "not
// installed" sends the operator to reinstall a package already on disk.
// Reported against a global install mid-upgrade to 12.2.5.
const partial = Object.assign(
new Error(
"Cannot find module '/usr/lib/node_modules/agent-relay/node_modules/@relayfile/sdk/dist/relay-cli/index.js'"
),
{ code: 'ERR_MODULE_NOT_FOUND' }
);
await expect(
loadProductSurface(
DEFINITION,
makeDeps(async () => {
throw partial;
})
)
).rejects.toThrow(/installed but incomplete/);
});

it('names the dependency when the product itself is present', async () => {
// Reinstalling @relayfile/sdk would not fix a missing transitive dep, so
// the message must not name @relayfile/sdk as the thing to install.
const transitive = Object.assign(
new Error("Cannot find package 'some-transitive-dep' imported from /x/y.js"),
{ code: 'ERR_MODULE_NOT_FOUND' }
);
await expect(
loadProductSurface(
DEFINITION,
makeDeps(async () => {
throw transitive;
})
)
).rejects.toThrow(/depends on some-transitive-dep, which is not installed/);
});

it('says to upgrade when the package predates the subpath export', async () => {
// The raw ERR_PACKAGE_PATH_NOT_EXPORTED text never names the fix.
await expect(
Expand Down Expand Up @@ -142,6 +224,28 @@ describe('loadProductSurface', () => {
).rejects.toThrow(/Could not load `agent-relay file`[\s\S]*disk on fire/);
});

it('prints a provisioning failure as written, with no stack', async () => {
// The store already composed the actionable text — which package, which
// directory, what to do. Re-framing it as "Could not load … : <message>"
// would bury the instruction under a second layer of narration.
const failure = new SurfaceProvisionError(
'@relayfile/sdk could not be installed into /home/x/.agentworkforce/relay/surfaces.\n' +
'npm error code ENOTFOUND\n' +
'Retry when online, or install agent-relay from npm.'
);
const message = await loadProductSurface(
DEFINITION,
makeDeps(async () => {
throw failure;
})
).catch((error: unknown) => (error as Error).message);

expect(message).toContain('`agent-relay file` could not be prepared.');
expect(message).toContain('npm error code ENOTFOUND');
expect(message).toContain('Retry when online');
expect(message).not.toContain('at ');
});

it('rejects a module that exports no factory', async () => {
await expect(
loadProductSurface(
Expand All @@ -152,6 +256,131 @@ describe('loadProductSurface', () => {
});
});

describe('SURFACE_PACKAGES', () => {
const manifest = JSON.parse(readFileSync(new URL('../../../package.json', import.meta.url), 'utf8')) as {
dependencies: Record<string, string>;
};

it('pins the same version the npm distribution installs', () => {
// The standalone binary installs these itself and has no manifest to read,
// so the ranges live in source. A pin bumped in package.json alone is
// invisible until someone runs the compiled binary and gets the old SDK.
for (const definition of PRODUCT_SURFACES) {
const pkg = SURFACE_PACKAGES[definition.specifier];
expect(pkg, `${definition.as} has no entry in SURFACE_PACKAGES`).toBeDefined();
expect(pkg!.range, `${pkg!.name} disagrees with packages/cli/package.json`).toBe(
manifest.dependencies[pkg!.name]
);
}
});

it('names the package the specifier belongs to', () => {
for (const [specifier, pkg] of Object.entries(SURFACE_PACKAGES)) {
expect(specifier.startsWith(`${pkg.name}/`)).toBe(true);
}
});
});

describe('importProductSurface', () => {
/** Seams whose defaults would provision; every test starts from "never". */
function fallbackDeps(overrides: Partial<Parameters<typeof importProductSurface>[1]> = {}) {
return {
isStandalone: () => false,
provision: vi.fn(async () => '/unused'),
importFromStore: vi.fn(async () => ({ createRelayCliSurface: () => fakeSurface() })),
...overrides,
};
}

function notFound(specifier: string): Error {
return Object.assign(new Error(`Cannot find package '${specifier}' imported from /x/y.js`), {
code: 'ERR_MODULE_NOT_FOUND',
});
}

it('returns the resolved module without provisioning anything', async () => {
// The npm distribution's only path. Nothing may be installed, created, or
// even checked for when the specifier resolves.
const module = { createRelayCliSurface: () => fakeSurface() };
const deps = fallbackDeps({ isStandalone: () => true, importSpecifier: async () => module });

await expect(importProductSurface('@relayfile/sdk/relay-cli', deps)).resolves.toBe(module);
expect(deps.provision).not.toHaveBeenCalled();
expect(deps.importFromStore).not.toHaveBeenCalled();
});

it('provisions the package when it does not resolve in the compiled binary', async () => {
const stored = { createRelayCliSurface: () => fakeSurface() };
const deps = fallbackDeps({
isStandalone: () => true,
importSpecifier: async () => {
throw notFound('@relayfile/sdk');
},
provision: vi.fn(async () => '/store/relayfile-sdk-0.10.64-abcd1234'),
importFromStore: vi.fn(async () => stored),
});

await expect(importProductSurface('@relayfile/sdk/relay-cli', deps)).resolves.toBe(stored);
expect(deps.provision).toHaveBeenCalledWith(SURFACE_PACKAGES['@relayfile/sdk/relay-cli']);
expect(deps.importFromStore).toHaveBeenCalledWith(
'/store/relayfile-sdk-0.10.64-abcd1234',
'@relayfile/sdk/relay-cli'
);
});

it('leaves the npm distribution alone when the import fails there', async () => {
// An npm install that cannot resolve the SDK has a broken dependency tree,
// and `describeLoadFailure` already says so. Installing a second copy into
// the home directory would paper over it.
const error = notFound('@relayfile/sdk');
const deps = fallbackDeps({
isStandalone: () => false,
importSpecifier: async () => {
throw error;
},
});

await expect(importProductSurface('@relayfile/sdk/relay-cli', deps)).rejects.toBe(error);
expect(deps.provision).not.toHaveBeenCalled();
});

it('does not provision a specifier this CLI does not pin', async () => {
// The end-to-end test mounts product builds by absolute path; there is no
// package to install for those, and no version to install it at.
const error = notFound('/abs/path/dist/relay-cli.js');
const deps = fallbackDeps({
isStandalone: () => true,
importSpecifier: async () => {
throw error;
},
});

await expect(importProductSurface('/abs/path/dist/relay-cli.js', deps)).rejects.toBe(error);
expect(deps.provision).not.toHaveBeenCalled();
});

it('does not provision when the package resolved and then threw', async () => {
// The package is present; a second copy of it would throw identically, and
// installing one would hide the error that actually needs reading.
const error = new Error('surface module threw while evaluating');
const deps = fallbackDeps({
isStandalone: () => true,
importSpecifier: async () => {
throw error;
},
});

await expect(importProductSurface('@relayfile/sdk/relay-cli', deps)).rejects.toBe(error);
expect(deps.provision).not.toHaveBeenCalled();
});

it('still resolves a specifier outside the table', async () => {
// The end-to-end test mounts product builds by absolute path, so the
// dynamic import has to stay.
await expect(importProductSurface('node:path')).resolves.toHaveProperty('join');
});
});

describe('registerProductSurfaceCommands', () => {
function program(importModule: (specifier: string) => Promise<unknown>, io: RelayCliIo) {
const root = new Command('agent-relay');
Expand Down
Loading
Loading