mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 06:10:44 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A managed instance gets its instance-level environments from the `PAPERCLIP_MANAGED_CONFIG` document, which can declare plugin environments next to the managed sandbox > - A platform that runs managed instances wants a new instance to start with one of those plugin environments as its default, without changing the default of any instance that already has one > - #19632 made boot fill the instance default only when it is unset, local or deleted, in one locked transaction, and removed the revert, so every existing managed instance keeps a set default > - This pull request adds an `instanceDefault` flag to a plugin entry. That entry fills an unset default through the same locked fill, before the managed sandbox does > - The fill follows the same readiness rule as an admin's default write, so it never moves ready agents onto a plugin that has not prepared them > - The platform also needs a way to know whether a release understands plugin entries, so the release now ships a small capabilities file > - The benefit: new instances get the plugin environment as their default, and existing instances and admin choices are never changed ## Linked Issues or Issue Description Refs #19632 (the locked fill-if-unset rule this builds on), #19508 (plugin entries in the managed config) **Subsystem affected** Server environments: the managed config parser and the managed environment bootstrap. **Problem or motivation** The managed config can declare plugin environments, but it can't make one the instance default on a new instance. An external platform also has no way to tell whether a given release supports plugin entries before it sends one. **Proposed solution** - Add `instanceDefault: true` to at most one plugin entry. Right after that entry's boot ensure succeeds, it fills the instance default through `fillUnsetInstanceDefault`. The managed sandbox fill runs after it. - On a new instance the plugin environment wins. On an existing instance the default is already set, so neither fill changes it. - The fill declines while an active agent that follows the default hasn't been prepared by that plugin. This is the rule the instance-settings route enforces. When the plugin fill declines, the managed sandbox fills as usual. - Ship `server/src/services/managed-config-capabilities.json`, which lists `pluginEnvironments`. **Alternatives considered** - A recorded one-time decision with a retry worker. It needed pending states and extra checks for many writer and trigger combinations. Filling only an unset default, under the existing lock, needs none of that. - Rejecting the fill whenever agents exist. That is stricter than the admin route and blocks harmless cases, such as agents that are paused or already prepared. **Roadmap alignment** This extends existing managed-environment behaviour. It adds no UI. ## What Changed - `server/src/services/managed-config.ts`: - `ManagedPluginEnvironmentSpec.instanceDefault` (boolean, default `false`). - The parser refuses `instanceDefault` on more than one plugin entry, on the sandbox entry (even `false`), or as a non-boolean. - `MANAGED_CONFIG_CAPABILITIES` and `MANAGED_CONFIG_CAPABILITIES_SCHEMA_VERSION` constants. A plugin entry is refused unless `pluginEnvironments` is listed. - `server/src/services/managed-config-capabilities.json`: `{ "schemaVersion": 1, "capabilities": ["pluginEnvironments"] }`. A test pins it to the constants and to this literal contract. - `server/src/services/managed-environments.ts`: - New `fillPluginInstanceDefault`. It runs right after a successful boot ensure of the flagged entry and logs a fill or a decline. Recovery after boot never fills. - The managed sandbox fill now runs once, after the plugin entries. - `server/src/services/managed-instance-default.ts`: `fillUnsetInstanceDefault` runs `agentsNeedSetupForEnvironmentChange` inside its locked transaction and returns `declined: "agents_need_setup"`. A sandbox target never needs agent setup, so the managed sandbox fill behaves as before. - `server/src/services/plugin-environment-driver.ts`: the readiness query is factored into `agentsNeedSetupForEnvironmentChange`. `assertAgentsReadyForEnvironmentChange` wraps it and still throws the same conflict. - `doc/DEVELOPING.md`: documents `instanceDefault`, the fill order, the readiness decline, and the capabilities file. ## Verification - `cd server && npx vitest run src/__tests__/managed-plugin-environments.test.ts` (embedded Postgres), under `instanceDefault (fill only an unset default)`: - A new instance gets the plugin row as its default. - An existing sandbox default is kept, and so is an admin's choice. - If the plugin was down at the first boot and the sandbox filled, a later recovery and boot keep the sandbox default. - An admin-cleared or local default is declined while a ready or verifying agent follows the default without the plugin's setup. The managed sandbox then fills it. - It fills when the agents that follow the default are paused or already prepared, or are pinned to another environment. - Without the flag, nothing happens. - A new agent that follows the default gets the plugin in `requiredPluginIds`. - `npx vitest run src/__tests__/managed-instance-default.test.ts` (embedded Postgres): the earlier fill, lock and overlap cases pass. A sandbox fill also succeeds while active agents follow the default. - `npx vitest run src/services/managed-environments-plugin-entries.test.ts`: - The fill order is plugin then sandbox. - When the plugin is down there is no fill, and recovery never fills. - A failed ensure gives only the sandbox fill, and a failed fill keeps the ensure counted. - When the plugin fill declines, the sandbox still fills. - `npx vitest run src/__tests__/managed-config.test.ts src/services/managed-config-capabilities.test.ts`: parser refusals and the capabilities contract. - `npx vitest run src/services/managed-environments.test.ts src/__tests__/instance-settings-routes.test.ts src/__tests__/environment-routes.test.ts`: pass. The route still throws the same readiness conflict. - `pnpm --filter @paperclipai/server typecheck` passes. - I did not run the full `pnpm test:run`, `pnpm -r typecheck` or `pnpm build`. CI covers them. ## Risks - An instance whose default is unset at the first boot after this release gets the plugin environment, if its agents are ready for it. That covers a new instance, and an instance whose admin cleared the default or chose the local environment. This is the intended behaviour. - The readiness check doesn't lock agent rows. If an agent becomes ready during the boot fill, it can miss the check, which is the same race the admin route has. Agents created after the fill are prepared for the plugin, because setup reads the current default. - If the plugin is down at a new instance's first boot, the instance keeps the managed sandbox default from then on. - No schema change. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, through Claude Code with tool use (file edits, shell, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
532 lines
22 KiB
TypeScript
532 lines
22 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { unprocessable } from "../errors.js";
|
|
import { applyManagedEnvironments, createManagedEnvironmentRecovery } from "./managed-environments.js";
|
|
import { idleWorkSnapshot } from "./task-admission.js";
|
|
import {
|
|
PLUGIN_KEY,
|
|
environmentsSeam,
|
|
noDb,
|
|
parsedConfig,
|
|
pluginEnvironmentRow,
|
|
readyDriverResolver,
|
|
reconciliationResult,
|
|
recoveryHandle,
|
|
tick,
|
|
type RecoveryHandle,
|
|
} from "./managed-environments.test-support.js";
|
|
|
|
describe("applyManagedEnvironments plugin entries", () => {
|
|
const pluginEntry = (overrides: Record<string, unknown> = {}) => ({
|
|
name: "Remote Host",
|
|
description: "Runs the agent on a remote host",
|
|
driver: "plugin",
|
|
pluginKey: PLUGIN_KEY,
|
|
driverKey: "tasks",
|
|
driverConfig: {},
|
|
...overrides,
|
|
});
|
|
const configWith = (environments: unknown[], features: Record<string, unknown> = {}) => parsedConfig({
|
|
plugins: { autoInstall: ["daytona", "remote-host"] },
|
|
features,
|
|
environments,
|
|
});
|
|
const readyPlugin = (status = "ready", declaresDriver = true) => vi.fn(async () => ({
|
|
plugin: { id: "remote-host-plugin", pluginKey: PLUGIN_KEY, status },
|
|
declaresDriver,
|
|
}));
|
|
const workerManager = (running = true, handle?: RecoveryHandle) => ({
|
|
isRunning: vi.fn(() => running),
|
|
call: vi.fn(),
|
|
getWorker: vi.fn(() => handle),
|
|
});
|
|
|
|
it("ensures a plugin entry with the plugin validator's worker manager", async () => {
|
|
const environments = environmentsSeam();
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
|
|
expect(environments.ensureManagedPluginEnvironment).toHaveBeenCalledWith({
|
|
name: "Remote Host",
|
|
description: "Runs the agent on a remote host",
|
|
pluginKey: PLUGIN_KEY,
|
|
driverKey: "tasks",
|
|
driverConfig: {},
|
|
workerManager: { isRunning: expect.any(Function), call: expect.any(Function) },
|
|
});
|
|
expect(result).toMatchObject({ ensured: 1, added: 1, failed: 0 });
|
|
expect(result?.pluginOutcomes).toEqual([{
|
|
environmentId: "plugin-env",
|
|
name: "Remote Host",
|
|
pluginKey: PLUGIN_KEY,
|
|
driverKey: "tasks",
|
|
action: "added",
|
|
}]);
|
|
expect(environments.ensureManagedSandboxEnvironment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("runs the sandbox entry first", async () => {
|
|
const order: string[] = [];
|
|
const environments = environmentsSeam({
|
|
ensureManagedSandboxEnvironment: vi.fn(async () => {
|
|
order.push("sandbox");
|
|
return reconciliationResult() as never;
|
|
}),
|
|
ensureManagedPluginEnvironment: vi.fn(async () => {
|
|
order.push("plugin");
|
|
return { environment: pluginEnvironmentRow(), action: "added" as const };
|
|
}),
|
|
});
|
|
await applyManagedEnvironments(noDb, configWith([{ name: "Daytona", provider: "daytona" }, pluginEntry()]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
environments,
|
|
resolveSandboxProviderDriver: readyDriverResolver(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(order).toEqual(["sandbox", "plugin"]);
|
|
});
|
|
|
|
it("refuses startup when no auto-installed catalog entry installs the entry's plugin", async () => {
|
|
const environments = environmentsSeam();
|
|
await expect(applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: ["paperclip.daytona-sandbox-provider"],
|
|
})).rejects.toThrow(/uses plugin "example.remote-host-plugin", which no "plugins.autoInstall" catalog entry installs/);
|
|
expect(environments.ensureManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("resolves the auto-installed manifest ids from the catalog when the caller passes none", async () => {
|
|
// "remote-host" is not in this build's bundled catalog and the test root
|
|
// has no distribution catalog, so the catalog lookup refuses.
|
|
await expect(applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: { PAPERCLIP_BUNDLED_PLUGIN_ROOT: "/nonexistent-paperclip-catalog" },
|
|
workerManager: workerManager(),
|
|
environments: environmentsSeam(),
|
|
})).rejects.toThrow(/auto-install key "remote-host" is not in the bundled catalog/);
|
|
});
|
|
|
|
it("does not claim the sandbox slot, so a forced execution mode can coexist with plugin-only entries", async () => {
|
|
await expect(applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: { PAPERCLIP_EXECUTION_MODE: "kubernetes" },
|
|
workerManager: workerManager(),
|
|
environments: environmentsSeam(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
})).resolves.toMatchObject({ ensured: 1 });
|
|
});
|
|
|
|
it("archives the row of a plugin that is down and reactivates it on worker recovery", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const environments = environmentsSeam({
|
|
archiveManagedPluginEnvironment: vi.fn().mockResolvedValue(pluginEnvironmentRow({ status: "archived" })),
|
|
});
|
|
const manager = workerManager(false, handle);
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
expect(environments.archiveManagedPluginEnvironment).toHaveBeenCalledWith({ pluginKey: PLUGIN_KEY, driverKey: "tasks" });
|
|
expect(environments.ensureManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
|
|
manager.isRunning.mockReturnValue(true);
|
|
fireReady();
|
|
fireReady();
|
|
await tick();
|
|
|
|
expect(environments.ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("keeps recovery armed after a worker failure and stops after one success", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const ensureManagedPluginEnvironment = vi.fn()
|
|
.mockRejectedValueOnce(new Error("worker crashed during environmentValidateConfig"))
|
|
.mockResolvedValue({ environment: pluginEnvironmentRow(), action: "updated" });
|
|
const environments = environmentsSeam({ ensureManagedPluginEnvironment });
|
|
const manager = workerManager(false, handle);
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
manager.isRunning.mockReturnValue(true);
|
|
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(handle.off).not.toHaveBeenCalled();
|
|
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(2);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("queues one more attempt when the worker becomes ready during an attempt", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
let failFirst!: (err: Error) => void;
|
|
const ensureManagedPluginEnvironment = vi.fn()
|
|
.mockImplementationOnce(() => new Promise((_resolve, reject) => { failFirst = reject; }))
|
|
.mockResolvedValue({ environment: pluginEnvironmentRow(), action: "updated" });
|
|
const manager = workerManager(false, handle);
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments: environmentsSeam({ ensureManagedPluginEnvironment }),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
manager.isRunning.mockReturnValue(true);
|
|
|
|
fireReady();
|
|
fireReady();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
failFirst(new Error("worker crashed"));
|
|
await tick();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(2);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("stops recovery when the plugin permanently rejects the config", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const ensureManagedPluginEnvironment = vi.fn().mockRejectedValue(unprocessable("driverConfig.region is not supported"));
|
|
const manager = workerManager(false, handle);
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments: environmentsSeam({ ensureManagedPluginEnvironment }),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
manager.isRunning.mockReturnValue(true);
|
|
|
|
fireReady();
|
|
await tick();
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("archives and arms recovery when the worker dies during the boot ensure", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const manager = workerManager(true, handle);
|
|
const ensureManagedPluginEnvironment = vi.fn()
|
|
.mockImplementationOnce(async () => {
|
|
manager.isRunning.mockReturnValue(false);
|
|
throw new Error("worker crashed during environmentValidateConfig");
|
|
})
|
|
.mockResolvedValue({ environment: pluginEnvironmentRow(), action: "updated" });
|
|
const environments = environmentsSeam({ ensureManagedPluginEnvironment });
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
await tick();
|
|
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
expect(environments.archiveManagedPluginEnvironment).toHaveBeenCalledWith({ pluginKey: PLUGIN_KEY, driverKey: "tasks" });
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
|
|
manager.isRunning.mockReturnValue(true);
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(2);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("keeps the row and retries when the ensure fails while the worker keeps running", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const ensureManagedPluginEnvironment = vi.fn()
|
|
.mockRejectedValueOnce(new Error("environmentValidateConfig timed out"))
|
|
.mockRejectedValueOnce(new Error("environmentValidateConfig timed out"))
|
|
.mockResolvedValue({ environment: pluginEnvironmentRow(), action: "unchanged" });
|
|
const environments = environmentsSeam({ ensureManagedPluginEnvironment });
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: workerManager(true, handle),
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
await tick();
|
|
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
expect(environments.archiveManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
// The immediate retry also failed; the next `ready` succeeds.
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(2);
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(3);
|
|
expect(handle.off).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("neither archives nor retries when the boot ensure gets a permanent config rejection", async () => {
|
|
const { handle } = recoveryHandle();
|
|
const environments = environmentsSeam({
|
|
ensureManagedPluginEnvironment: vi.fn().mockRejectedValue(unprocessable("driverConfig.region is not supported")),
|
|
});
|
|
const manager = workerManager(true, handle);
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
expect(environments.archiveManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
expect(manager.getWorker).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps waiting through a restart that replaces the worker handle", async () => {
|
|
// Manager-wide `ready` subscription: the worker handle is never consulted,
|
|
// so a replacement handle's `ready` still reaches the listener.
|
|
const readyListeners = new Set<(pluginId: string) => void>();
|
|
const manager = {
|
|
isRunning: vi.fn(() => false),
|
|
call: vi.fn(),
|
|
getWorker: vi.fn(() => undefined),
|
|
onWorkerReady: vi.fn((listener: (pluginId: string) => void) => {
|
|
readyListeners.add(listener);
|
|
return () => { readyListeners.delete(listener); };
|
|
}),
|
|
};
|
|
const environments = environmentsSeam();
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(manager.getWorker).not.toHaveBeenCalled();
|
|
|
|
manager.isRunning.mockReturnValue(true);
|
|
for (const listener of [...readyListeners]) listener("another-plugin");
|
|
await tick();
|
|
expect(environments.ensureManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
for (const listener of [...readyListeners]) listener("remote-host-plugin");
|
|
await tick();
|
|
expect(environments.ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(readyListeners.size).toBe(0);
|
|
});
|
|
|
|
it("tracks recovery attempts as idle work and lets shutdown await and unsubscribe them", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
let finishEnsure!: () => void;
|
|
const ensureManagedPluginEnvironment = vi.fn(() => new Promise((resolve) => {
|
|
finishEnsure = () => resolve({ environment: pluginEnvironmentRow(), action: "updated" });
|
|
}));
|
|
const recovery = createManagedEnvironmentRecovery();
|
|
const manager = workerManager(false, handle);
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments: environmentsSeam({ ensureManagedPluginEnvironment: ensureManagedPluginEnvironment as never }),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
recovery,
|
|
});
|
|
const idleBefore = idleWorkSnapshot().active;
|
|
manager.isRunning.mockReturnValue(true);
|
|
fireReady();
|
|
expect(idleWorkSnapshot().active).toBe(idleBefore + 1);
|
|
|
|
let stopped = false;
|
|
const stopping = recovery.stop().then(() => { stopped = true; });
|
|
await tick();
|
|
expect(stopped).toBe(false);
|
|
expect(handle.off).toHaveBeenCalled();
|
|
finishEnsure();
|
|
await stopping;
|
|
expect(stopped).toBe(true);
|
|
expect(idleWorkSnapshot().active).toBe(idleBefore);
|
|
|
|
fireReady();
|
|
await tick();
|
|
expect(ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("treats a missing plugin, a plugin without the driver, or a worker manager without call as unavailable", async () => {
|
|
for (const [resolveEnvironmentPlugin, manager] of [
|
|
[vi.fn(async () => null), workerManager()],
|
|
[readyPlugin("ready", false), workerManager()],
|
|
[readyPlugin("error"), workerManager()],
|
|
[readyPlugin(), { isRunning: vi.fn(() => true), getWorker: vi.fn(() => undefined) }],
|
|
] as const) {
|
|
const environments = environmentsSeam();
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
environments,
|
|
resolveEnvironmentPlugin,
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
expect(environments.archiveManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(environments.ensureManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
expect(manager.getWorker).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
it("never archives a row whose entry was removed from the document", async () => {
|
|
const environments = environmentsSeam();
|
|
expect(await applyManagedEnvironments(noDb, configWith([]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
environments,
|
|
})).toBeNull();
|
|
expect(environments.archiveManagedPluginEnvironment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("never fills the instance default with a plugin entry that does not declare instanceDefault", async () => {
|
|
// Without the flag, only the managed sandbox fills an unset default.
|
|
const fillInstanceDefault = vi.fn(async () => ({ filled: false, previousDefaultEnvironmentId: "plugin-env" }));
|
|
await applyManagedEnvironments(noDb, configWith(
|
|
[{ name: "Daytona", provider: "daytona" }, pluginEntry()],
|
|
{ enableManagedSandboxOnly: true },
|
|
), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
fillInstanceDefault,
|
|
environments: environmentsSeam(),
|
|
resolveSandboxProviderDriver: readyDriverResolver(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(fillInstanceDefault).toHaveBeenCalledTimes(1);
|
|
expect(fillInstanceDefault).toHaveBeenCalledWith("env-1");
|
|
});
|
|
|
|
describe("instanceDefault", () => {
|
|
const sandboxAndComputer = () => configWith(
|
|
[{ name: "Daytona", provider: "daytona" }, pluginEntry({ instanceDefault: true })],
|
|
{ enableManagedSandboxOnly: true },
|
|
);
|
|
|
|
it("fills an unset default with the plugin row first, then lets the sandbox fill", async () => {
|
|
// The locked fill decides; here it reports a fill each time it is asked.
|
|
const fillInstanceDefault = vi.fn(async (_environmentId: string) => ({ filled: true, previousDefaultEnvironmentId: null }));
|
|
await applyManagedEnvironments(noDb, sandboxAndComputer(), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
fillInstanceDefault,
|
|
environments: environmentsSeam(),
|
|
resolveSandboxProviderDriver: readyDriverResolver(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
// The plugin row goes first, even though the sandbox entry is ensured
|
|
// first; on a new instance the plugin fill wins and the sandbox fill
|
|
// then finds the default set.
|
|
expect(fillInstanceDefault.mock.calls.map(([environmentId]) => environmentId)).toEqual(["plugin-env", "env-1"]);
|
|
});
|
|
|
|
it("lets the sandbox fill when the plugin fill declines because agents need setup", async () => {
|
|
const fillInstanceDefault = vi.fn(async (environmentId: string) => environmentId === "plugin-env"
|
|
? { filled: false, previousDefaultEnvironmentId: null, declined: "agents_need_setup" as const }
|
|
: { filled: true, previousDefaultEnvironmentId: null });
|
|
await applyManagedEnvironments(noDb, sandboxAndComputer(), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
fillInstanceDefault,
|
|
environments: environmentsSeam(),
|
|
resolveSandboxProviderDriver: readyDriverResolver(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(fillInstanceDefault.mock.calls.map(([environmentId]) => environmentId)).toEqual(["plugin-env", "env-1"]);
|
|
expect(await fillInstanceDefault.mock.results[1]!.value).toMatchObject({ filled: true });
|
|
});
|
|
|
|
it("does not fill with the plugin row when its plugin is down, and recovery never fills", async () => {
|
|
const { handle, fireReady } = recoveryHandle();
|
|
const fillInstanceDefault = vi.fn(async () => ({ filled: true, previousDefaultEnvironmentId: null }));
|
|
const environments = environmentsSeam();
|
|
const manager = workerManager(false, handle);
|
|
await applyManagedEnvironments(noDb, configWith([pluginEntry({ instanceDefault: true })]), {
|
|
env: {},
|
|
workerManager: manager,
|
|
fillInstanceDefault,
|
|
environments,
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(fillInstanceDefault).not.toHaveBeenCalled();
|
|
|
|
manager.isRunning.mockReturnValue(true);
|
|
fireReady();
|
|
await tick();
|
|
expect(environments.ensureManagedPluginEnvironment).toHaveBeenCalledTimes(1);
|
|
expect(fillInstanceDefault).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not fill with the plugin row when its ensure fails", async () => {
|
|
const fillInstanceDefault = vi.fn(async () => ({ filled: true, previousDefaultEnvironmentId: null }));
|
|
await applyManagedEnvironments(noDb, sandboxAndComputer(), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
fillInstanceDefault,
|
|
environments: environmentsSeam({
|
|
ensureManagedPluginEnvironment: vi.fn().mockRejectedValue(unprocessable("driverConfig.region is not supported")),
|
|
}),
|
|
resolveSandboxProviderDriver: readyDriverResolver(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
// Only the sandbox fill runs.
|
|
expect(fillInstanceDefault.mock.calls).toEqual([["env-1"]]);
|
|
});
|
|
|
|
it("keeps the plugin ensure counted when its fill fails", async () => {
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry({ instanceDefault: true })]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
fillInstanceDefault: vi.fn().mockRejectedValue(new Error("db exploded")),
|
|
environments: environmentsSeam(),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(result).toMatchObject({ ensured: 1, failed: 0 });
|
|
});
|
|
});
|
|
|
|
it("is fail-safe: an ensure failure is logged and counted, not thrown", async () => {
|
|
const result = await applyManagedEnvironments(noDb, configWith([pluginEntry()]), {
|
|
env: {},
|
|
workerManager: workerManager(),
|
|
environments: environmentsSeam({
|
|
ensureManagedPluginEnvironment: vi.fn().mockRejectedValue(new Error("validator refused")),
|
|
}),
|
|
resolveEnvironmentPlugin: readyPlugin(),
|
|
autoInstalledPluginKeys: [PLUGIN_KEY],
|
|
});
|
|
expect(result).toMatchObject({ ensured: 0, failed: 1 });
|
|
});
|
|
});
|