mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-09 07:10:34 +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>
74 lines
3.6 KiB
TypeScript
74 lines
3.6 KiB
TypeScript
/**
|
|
* The managed instance default: one locked "fill if unset" write.
|
|
*
|
|
* On a managed instance, boot points the instance default at the managed
|
|
* sandbox when nothing deliberate is set: the default is unset, names the
|
|
* local environment, or names an environment that no longer exists. A set
|
|
* default (any other environment, whoever chose it) is never changed.
|
|
*
|
|
* The write locks the instance default row and rechecks it inside the same
|
|
* transaction, so two overlapping boots, or a boot overlapping an admin's
|
|
* default write, can never overwrite a value that another transaction just
|
|
* committed: whichever commits first wins, and the other sees a set default.
|
|
*
|
|
* The fill obeys the same readiness rule as an admin's default write
|
|
* (`assertAgentsReadyForEnvironmentChange`): it declines a target that would
|
|
* give active agents following the default a plugin that has not prepared
|
|
* them. A managed sandbox target never needs agent setup, so this only ever
|
|
* declines a plugin environment.
|
|
*
|
|
* Once filled, the default is an ordinary set value. Nothing reverts it
|
|
* automatically; an admin can change it in instance settings.
|
|
*/
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { environments, instanceSettings } from "@paperclipai/db";
|
|
import { instanceSettingsService } from "./instance-settings.js";
|
|
import { agentsNeedSetupForEnvironmentChange } from "./plugin-environment-driver.js";
|
|
|
|
/** Singleton key of the instance settings row that holds `defaultEnvironmentId`. */
|
|
const INSTANCE_DEFAULT_SETTINGS_KEY = "default";
|
|
|
|
export interface FillInstanceDefaultResult {
|
|
filled: boolean;
|
|
previousDefaultEnvironmentId: string | null;
|
|
/** Set when the default was fillable but active agents following it need setup for the target first. */
|
|
declined?: "agents_need_setup";
|
|
}
|
|
|
|
/**
|
|
* Point the instance default at `environmentId` only if the current default
|
|
* is unset, the local environment, or an environment that no longer exists,
|
|
* and no active agent following the default would need setup for it first.
|
|
*/
|
|
export async function fillUnsetInstanceDefault(db: Db, environmentId: string): Promise<FillInstanceDefaultResult> {
|
|
return await db.transaction(async (tx) => {
|
|
const settings = instanceSettingsService(tx as unknown as Db);
|
|
// Creates the singleton row on a fresh instance, so the lock below always
|
|
// has a row to hold.
|
|
await settings.get();
|
|
const [defaultRow] = await tx
|
|
.select({ defaultEnvironmentId: instanceSettings.defaultEnvironmentId })
|
|
.from(instanceSettings)
|
|
.where(eq(instanceSettings.singletonKey, INSTANCE_DEFAULT_SETTINGS_KEY))
|
|
.for("update");
|
|
const current = defaultRow?.defaultEnvironmentId ?? null;
|
|
if (current === environmentId) return { filled: false, previousDefaultEnvironmentId: current };
|
|
const environmentRef = async (id: string) => (await tx
|
|
.select({ driver: environments.driver, config: environments.config })
|
|
.from(environments)
|
|
.where(eq(environments.id, id)))[0] ?? null;
|
|
const currentEnvironment = current === null ? null : await environmentRef(current);
|
|
if (currentEnvironment && currentEnvironment.driver !== "local") {
|
|
return { filled: false, previousDefaultEnvironmentId: current };
|
|
}
|
|
if (await agentsNeedSetupForEnvironmentChange(tx as unknown as Db, currentEnvironment, await environmentRef(environmentId),
|
|
{ inheritsDefault: true })) {
|
|
return { filled: false, previousDefaultEnvironmentId: current, declined: "agents_need_setup" };
|
|
}
|
|
await settings.update({ defaultEnvironmentId: environmentId }, { db: tx });
|
|
return { filled: true, previousDefaultEnvironmentId: current };
|
|
});
|
|
}
|