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
26 changes: 12 additions & 14 deletions docs/agents/web-backend.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Web Backend

Web automation uses a managed `agent-browser` backend as an implementation detail.
The `@agent-device/web` plugin uses managed `agent-browser`.

- Runtime web commands resolve the backend only from the state-dir managed install at `tools/agent-browser/<version>`.
- Normal `--platform web` commands do not install the managed backend on first use.
If the backend is missing, they fail with a setup hint.
- Before first web automation or in CI/sandbox bootstrap steps, use `agent-device web setup`.
- To run the backend health check, use `agent-device web doctor`.
- Each backend call spawns the package's declared `bin` entry with the current Node runtime.
It never spawns the `node_modules/.bin` console shim.
A shell would reintroduce hazards from argument quoting.
Only `runManagedAgentBrowser` executes the backend.
- Before web automation, run `agent-device plugins add @agent-device/web`.
- To install the backend in the state directory, run `agent-device web setup`.
- To check the backend, run `agent-device web doctor`.
- Runtime web commands use `tools/agent-browser/<version>` in the state directory.
- Only `web setup` installs the backend.
- If the backend is absent, return the setup hint.
- Use `runManagedAgentBrowser` to run the package's declared JavaScript entry with the current Node runtime.
- Do not use the `node_modules/.bin` shim.

Do not document direct `agent-browser` commands as agent-device features.
Web `network dump` is the narrow exception.
It adapts `agent-browser network requests` to the existing agent-device network evidence shape.
Until an explicit agent-device command design exists for them, exclude these features from the minimal web surface:
Do not expose direct `agent-browser` commands.
Use `web network dump` to adapt `agent-browser network requests` to agent-device network evidence.
Keep these operations out of the web surface:

- Browser-specific network routing/interception/HAR.
- CDP.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"depgraph:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/depgraph/model.test.ts scripts/depgraph/entries.test.ts scripts/depgraph/import-graph.test.ts scripts/depgraph/affected.test.ts scripts/depgraph/structure-summary.test.ts tools/depgraph-view/*.test.mjs",
"check:production-exports": "fallow dead-code --config fallow-production-exports.json --production --unused-exports --fail-on-issues",
"check:bundle-owner-files": "node --experimental-strip-types scripts/check-bundle-owner-files.ts",
"check:package": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test packages/maestro/test/package-artifact.test.mjs packages/maestro/test/package-smoke.mjs packages/replay/test/package-smoke.mjs && node --experimental-strip-types scripts/check-package.ts",
"check:package": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test packages/maestro/test/package-artifact.test.mjs packages/maestro/test/package-smoke.mjs packages/replay/test/package-smoke.mjs packages/platform-web/test/package-smoke.mjs && node --experimental-strip-types scripts/check-package.ts",
"check:provider-plugin": "node scripts/check-provider-plugin.mjs",
"check:command-docs": "vitest run --project unit-core test/repo-guards/command-doc-coverage.test.ts",
"check:adr-citations": "vitest run --project unit-core test/repo-guards/adr-citations.test.ts",
Expand Down Expand Up @@ -326,7 +326,6 @@
"@agent-device/platform-harmonyos": "workspace:*",
"@agent-device/platform-linux": "workspace:*",
"@agent-device/platform-vega": "workspace:*",
"@agent-device/platform-web": "workspace:*",
"@agent-device/plugins": "workspace:*",
"@agent-device/provider-limrun": "workspace:*",
"@agent-device/provider-webdriver": "workspace:*",
Expand Down
8 changes: 8 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
"types": "./src/lease-scope.ts",
"default": "./src/lease-scope.ts"
},
"./plugin-installation": {
"types": "./src/plugin-installation.ts",
"default": "./src/plugin-installation.ts"
},
"./plugin-inventory": {
"types": "./src/plugin-inventory.ts",
"default": "./src/plugin-inventory.ts"
Expand All @@ -312,6 +316,10 @@
"types": "./src/logs-runtime-plan.ts",
"default": "./src/logs-runtime-plan.ts"
},
"./web-provider": {
"types": "./src/web-provider.ts",
"default": "./src/web-provider.ts"
},
"./managed-web-backend": {
"types": "./src/managed-web-backend.ts",
"default": "./src/managed-web-backend.ts"
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/src/managed-web-backend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** Deadline that web session teardown must leave for the managed browser. */
export const AGENT_BROWSER_TIMEOUT_MS = 30_000;

export type ManagedWebBackendStatus = Readonly<{
version: string;
stateDir: string;
Expand Down
25 changes: 14 additions & 11 deletions packages/contracts/src/platform-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import { PLATFORMS, type Platform } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import {
createPlatformModuleRegistry,
getOptionalPlatformPluginDeclaration,
isOptionalPlatform,
missingOptionalPlatformError,
OPTIONAL_PLATFORMS,
pluginInstallCommand,
pluginPackageSelector,
pluginRepairCommand,
REQUIRED_PLATFORMS,
type PlatformModuleMetadata,
type PlatformPartitionIsCanonical,
} from './platform-module.ts';
import {
getOptionalPlatformPluginDeclaration,
missingOptionalPlatformError,
pluginInstallCommand,
pluginPackageSelector,
pluginRepairCommand,
} from './plugin-installation.ts';

const CORE_VERSION = '0.22.0';
const reportAbsent = (family: Platform) => missingOptionalPlatformError(family, CORE_VERSION);

test('platform metadata registry is canonical, ordered, and immutable', () => {
const registrations = [...PLATFORMS]
.reverse()
.map((family) => ({ family, marker: family.toUpperCase() }));
const registry = createPlatformModuleRegistry(registrations, CORE_VERSION);
const registry = createPlatformModuleRegistry(registrations, reportAbsent);

assert.deepEqual(registry.families, PLATFORMS);
assert.deepEqual(
Expand All @@ -40,13 +43,13 @@ test('platform metadata registry rejects missing required and duplicate family o
// Only the required families are completeness-checked, so dropping one optional family is not
// a missing registration — this drops `apple`.
const missing = PLATFORMS.filter((family) => family !== 'apple').map((family) => ({ family }));
assert.throws(() => createPlatformModuleRegistry(missing, CORE_VERSION), /Missing.*apple/);
assert.throws(() => createPlatformModuleRegistry(missing, reportAbsent), /Missing.*apple/);

const duplicate: PlatformModuleMetadata[] = [
...PLATFORMS.map((family) => ({ family })),
{ family: 'apple' },
];
assert.throws(() => createPlatformModuleRegistry(duplicate, CORE_VERSION), /Duplicate.*apple/);
assert.throws(() => createPlatformModuleRegistry(duplicate, reportAbsent), /Duplicate.*apple/);
});

test('platform metadata registry rejects an unknown runtime family value', () => {
Expand All @@ -55,7 +58,7 @@ test('platform metadata registry rejects an unknown runtime family value', () =>
{ family: 'unknown' as Platform },
];
assert.throws(
() => createPlatformModuleRegistry(registrations, CORE_VERSION),
() => createPlatformModuleRegistry(registrations, reportAbsent),
/Unknown.*unknown/,
);
});
Expand Down Expand Up @@ -141,7 +144,7 @@ test('missingOptionalPlatformError returns plugin-not-installed with the family

test('optional families may be absent: the registry keeps only what is registered', () => {
const registrations = REQUIRED_PLATFORMS.map((family) => ({ family, marker: family }));
const registry = createPlatformModuleRegistry(registrations, CORE_VERSION);
const registry = createPlatformModuleRegistry(registrations, reportAbsent);

assert.deepEqual(registry.families, [...REQUIRED_PLATFORMS]);
assert.deepEqual(
Expand All @@ -154,7 +157,7 @@ test('optional families may be absent: the registry keeps only what is registere
test('get on an absent optional family throws the typed plugin-not-installed outcome', () => {
const registry = createPlatformModuleRegistry(
REQUIRED_PLATFORMS.map((family) => ({ family })),
CORE_VERSION,
reportAbsent,
);

for (const family of OPTIONAL_PLATFORMS) {
Expand Down
151 changes: 11 additions & 140 deletions packages/contracts/src/platform-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,141 +36,6 @@ export type PlatformPartitionIsCanonical = AssertTrue<
: false
>;

/**
* Which release of a plugin package the running core can install (#4694). The release workflow
* stamps the core and its public plugins with the same nightly version, so an installation hint must
* name that release: the bare package name on a nightly core resolves to the staged `latest`
* placeholder rather than to the build that core works with. Every catalog that tells a user to
* install a plugin — the provider catalog and the optional platform families ADR 0034 makes
* installable — builds its selector here, so one channel rule cannot be fixed in one catalog and
* missed in another. The caller supplies `coreVersion`: contracts owns vocabulary, and reading the
* installed version is host mechanics.
*/
export type PluginReleaseDeclaration = Readonly<{
package: string;
// A 0.22.x-dev version carries no source branch, so the 0.22 branch's nightly tag is the shared
// default for development builds, including main checkouts.
nightlyTag?: 'nightly-0.22';
}>;

// The release workflow stamps the core and its public plugins with the same nightly version.
const NIGHTLY_0_22_VERSION = /^0\.22\.\d+-nightly(?:-0-22)?\.\d{8}\.[1-9]\d*$/;
const DEVELOPMENT_0_22_VERSION = /^0\.22\.\d+-dev$/;

/** The package selector a core of `coreVersion` installs: its own release, else the branch tag. */
export function pluginPackageSelector(
declaration: PluginReleaseDeclaration,
coreVersion: string,
): string {
if (declaration.nightlyTag === undefined) return declaration.package;
const version = coreVersion.trim();
if (NIGHTLY_0_22_VERSION.test(version)) return `${declaration.package}@${version}`;
if (DEVELOPMENT_0_22_VERSION.test(version)) {
return `${declaration.package}@${declaration.nightlyTag}`;
}
return declaration.package;
}

/** The `plugins add` line that installs `declaration` for a core of `coreVersion`. */
export function pluginInstallCommand(
declaration: PluginReleaseDeclaration,
coreVersion: string,
): string {
return `agent-device plugins add ${pluginPackageSelector(declaration, coreVersion)}`;
}

/**
* The `plugins` line that repairs an installed `declaration`: `update` keeps a selector the user
* chose, while a pinned nightly release must be added again.
*/
export function pluginRepairCommand(
declaration: PluginReleaseDeclaration,
coreVersion: string,
): string {
const selector = pluginPackageSelector(declaration, coreVersion);
const action = selector === declaration.package ? 'update' : 'add';
return `agent-device plugins ${action} ${selector}`;
}

/**
* Engine-free install-hint catalog for the optional platform families (ADR 0034): each family
* maps to its package in the maintainer-controlled `@agent-device/*` scope. It is vetted
* declaration data only — it never probes whether an engine is installed or working, and an
* unknown family has no row and yields no inferred package. The record is total over
* `OptionalPlatform`, so a newly optional family cannot ship without its installation row, and each
* row uses the release-channel rule above, as the provider catalog does.
*/
export type OptionalPlatformPluginDeclaration = PluginReleaseDeclaration &
Readonly<{
family: OptionalPlatform;
label: string;
}>;

const OPTIONAL_PLATFORM_PLUGIN_CATALOG: Readonly<
Record<OptionalPlatform, OptionalPlatformPluginDeclaration>
> = Object.freeze({
harmonyos: Object.freeze({
family: 'harmonyos',
package: '@agent-device/harmonyos',
label: 'HarmonyOS',
nightlyTag: 'nightly-0.22',
}),
vega: Object.freeze({
family: 'vega',
package: '@agent-device/vega',
label: 'Vega',
nightlyTag: 'nightly-0.22',
}),
linux: Object.freeze({
family: 'linux',
package: '@agent-device/linux',
label: 'Linux desktop',
nightlyTag: 'nightly-0.22',
}),
web: Object.freeze({
family: 'web',
package: '@agent-device/web',
label: 'Web',
nightlyTag: 'nightly-0.22',
}),
} as const satisfies Record<OptionalPlatform, OptionalPlatformPluginDeclaration>);

/**
* The vetted declaration for an optional family, or `undefined` for any other family: the
* required families and unknown names produce no inferred package.
*/
export function getOptionalPlatformPluginDeclaration(
family: Platform,
): OptionalPlatformPluginDeclaration | undefined {
return isOptionalPlatform(family) ? OPTIONAL_PLATFORM_PLUGIN_CATALOG[family] : undefined;
}

/**
* The typed outcome for a platform family a composition does not have (ADR 0034). An optional family
* reports `plugin-not-installed` with the package that provides it and its installation command,
* before any device binding. The required families and unknown names have no catalog row, so they
* keep the plain unsupported-platform outcome with no package inferred: absence of a required family
* is a broken composition, not something a user can install.
*/
export function missingOptionalPlatformError(family: Platform, coreVersion: string): AppError {
const declaration = getOptionalPlatformPluginDeclaration(family);
if (!declaration) {
return new AppError('UNSUPPORTED_PLATFORM', `Unsupported platform: ${family}`);
}
const installCommand = pluginInstallCommand(declaration, coreVersion);
return new AppError(
'UNSUPPORTED_PLATFORM',
`${declaration.label} platform requires the ${declaration.package} plugin.`,
{
reason: 'plugin-not-installed',
family,
package: declaration.package,
installCommand,
hint: `Install:\n ${installCommand}\nThen retry the original command. If a local daemon is already running, close its sessions and run agent-device daemon stop with the same --state-dir before retrying.`,
},
);
}

/** Cheap family identity which may be evaluated during root composition. */
export type PlatformModuleMetadata = Readonly<{
family: Platform;
Expand All @@ -192,18 +57,18 @@ export type PlatformModuleRegistry<Module extends PlatformModuleMetadata> = Read
families: readonly Platform[];
modules: readonly Module[];
get(family: Platform): Module;
tryGet(family: Platform): Module | undefined;
}>;

/**
* Composes `registrations` into the presence authority for platform families.
*
* `coreVersion` is the running core's version, which the typed absence outcome needs to name the
* plugin release that core can install. The caller supplies it: reading the installed version is
* host mechanics, which contracts does not own.
* The caller supplies the absence reporter for this composition.
*/
export function createPlatformModuleRegistry<Module extends PlatformModuleMetadata>(
registrations: readonly Module[],
coreVersion: string,
reportAbsent = unsupportedPlatform,
assertSelectionCurrent?: (family: Platform) => void,
): PlatformModuleRegistry<Module> {
const byFamily = new Map<Platform, Module>();
for (const registration of registrations) {
Expand All @@ -227,17 +92,23 @@ export function createPlatformModuleRegistry<Module extends PlatformModuleMetada
return Object.freeze({
families,
modules,
tryGet: (family: Platform) => byFamily.get(family),
get: (family: Platform) => {
assertSelectionCurrent?.(family);
const registration = byFamily.get(family);
if (registration) return registration;
// An explicitly requested family with no registration is a missing plugin, reported as the
// typed outcome rather than a contract violation. Absence of a required family cannot reach
// here: the completeness check above rejects that composition.
throw missingOptionalPlatformError(family, coreVersion);
throw isOptionalPlatform(family) ? reportAbsent(family) : unsupportedPlatform(family);
},
});
}

function unsupportedPlatform(family: Platform): AppError {
return new AppError('UNSUPPORTED_PLATFORM', `Unsupported platform: ${family}`);
}

function freezeRegistration<Module extends PlatformModuleMetadata>(registration: Module): Module {
return Object.freeze({ ...registration }) as Module;
}
Expand Down
40 changes: 40 additions & 0 deletions packages/contracts/src/platform-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type { ManagedWebBackend } from './managed-web-backend.ts';
import type { DoctorCheck } from './doctor.ts';
import type { InventoryPlatformModule, PlatformModuleRegistry } from './platform-module.ts';
import type { PlatformRuntimeModule } from './platform-runtime-operations.ts';
import type { WebProvider } from './web-provider.ts';
import type { AppError } from '@agent-device/kernel/errors';
import type { DeviceInfo, Platform, PlatformSelector } from '@agent-device/kernel/device';
import type { HostEnvironment } from '@agent-device/kernel/source-value';
import type { PlatformGatedProviderResolverKey } from './platform-providers.ts';
Expand Down Expand Up @@ -63,3 +69,37 @@ export type PlatformPlugin = {
readonly platformGatedResolvers: readonly PlatformGatedProviderResolverKey[];
};
};

export type WebPlatformOperations = ManagedWebBackend &
Readonly<{
cleanupOrphans(openWebSessionNames: readonly string[]): Promise<void>;
browserLifecycleCheck(stateDir: string): Promise<DoctorCheck>;
createProvider(session: string): Promise<WebProvider>;
resolveRequestProvider(): Promise<WebProvider | undefined>;
}>;

export type WebPlatformRegistration = Readonly<{
family: 'web';
inventoryModule: InventoryPlatformModule<'web'>;
runtimeModule: PlatformRuntimeModule & { readonly family: 'web' };
createInteractor: PlatformPlugin['createInteractor'];
withRequestProvider<T>(provider: WebProvider, task: () => Promise<T>): Promise<T>;
operations: WebPlatformOperations;
}>;

export type WebPlatformSelection = Readonly<{
active?: WebPlatformRegistration;
get(): WebPlatformRegistration;
}>;

export type PlatformPluginSet<Plugin extends PlatformPlugin = PlatformPlugin> =
PlatformModuleRegistry<{ family: Platform; plugin: Plugin }>;

export type PlatformModuleSelection = Readonly<{
interactors: PlatformPluginSet;
web: WebPlatformSelection;
inventoryModules: readonly InventoryPlatformModule[];
runtimeModules: readonly PlatformRuntimeModule[];
reportAbsent(family: Platform): AppError;
assertSelectionCurrent(family: Platform): void;
}>;
Loading
Loading