Fix managed environment reconciliation and qualify runtimes before deployment (#22734)

## Thinking Path

> - Paperclip manages AI agents and the environments where they run.
> - Deployments can supply managed environment rows and preinstalled
runtimes.
> - A new instance can have a managed row before it has a company
binding.
> - A later configuration update must update that row without taking
ownership of a tenant row.
> - Release builders also need to check sandbox compatibility before
deployment.
> - This pull request fixes reconciliation and exposes the same runtime
compatibility check as an offline command.
> - Deployment failures can then hold a candidate release while current
instances continue to start.

## Linked Issues or Issue Description

**What happened?**

A platform-managed sandbox row can become stale before the instance has
its first company. The row has an ownership marker but no company
binding, so the old drift check treats it as an operator edit.
Separately, release builders have no standalone way to run the
compatibility check used at task startup.

**Expected behavior**

Update a platform-owned row when managed configuration changes. Preserve
tenant-owned rows and deliberate archives. Allow a release builder to
check the exact candidate image and an observed sandbox installation
before deployment.

**Steps to reproduce**

1. Apply a fully managed environment to an instance without companies.
2. Change its snapshot and apply the configuration again.
3. Observe that the old implementation leaves the original snapshot in
place.

Refs #22835. That change refreshes stale lease qualification proofs at
runtime. This PR checks release compatibility before deployment and
fixes the managed row update.

## What Changed

- Reconcile explicitly platform-owned rows before a company binding
exists. Preserve row identity, tenant environment values, and archive
safeguards.
- Extract the existing pure provider-pack compatibility check. Task
startup and the offline command use this same function.
- Add `server/dist/qualify-sandbox-runtime.js`. It reads expected
artifacts from the candidate image and observed installation data from
standard input. It checks all packaged providers by default. Compatible
publication hashes may differ.
- Return exit status 0 for compatible, 2 for incompatible, and 1 for
invalid or unavailable evidence.
- Document the observation and integrity requirements. Keep existing
startup fallback and degraded startup behavior. No convergence flag or
new startup gate is added.

## Verification

- 128 tests passed across the command, runtime compatibility,
environment service, and managed environment suites.
- `pnpm exec tsc --noEmit -p server/tsconfig.json` passed.
- All current-head CI checks passed, including the full selected server,
chat, serialized, runner, build, typecheck, and e2e suites.
- `pnpm verify --base origin/master` selected 2,376 files
conservatively. The redundant local sweep was stopped after CI
completed; no full local-sweep pass is claimed.
- The initially failed comment-reload browser case passed 3 local
repetitions and the failed CI jobs passed on rerun without source
changes.
- The command test launches a real offline subprocess and checks
compatible, incompatible, and invalid input outcomes.
- Added-line secret and PII scan passed. No deployment or workspace
repair was performed.

## Risks

The command trusts remote observations supplied by its caller. The
caller must verify installed files and prove the installation is
protected before asserting `immutable: true`. This is release evidence,
not a signed authorization. The runner binary must match; provider-pack
compatibility uses the existing interface rules. No schema migration is
required. Startup continues with the existing fallback if later runtime
conditions change.

## Model Used

OpenAI Codex, `gpt-6.1-sol`, xhigh reasoning, with tool use and code
execution.

## Checklist

- [x] I searched for similar PRs and linked related work.
- [x] Thinking path and issue description included
- [x] Model specified
- [x] Focused tests and server typecheck passed
- [x] Tests and documentation updated
- [x] No internal instance links or identifiers included
- [x] All required CI gates are green on the current head
- [x] Greptile is 5/5 with no unresolved findings

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-11-24 17:41:51 -08:00
1 parent 021be2c56d
commit ff0f395579
7 files changed
+533 -134

No files matched your search

+73
View File
@@ -29,6 +29,7 @@ All environment variables that Paperclip uses for server configuration.
| `PAPERCLIP_HIDDEN_SETTINGS` | (unset) | Comma-separated settings surfaces to hide from the UI and floor at the API, for operators hosting Paperclip for others (managed cloud, internal shared server). See [Hiding settings surfaces](#hiding-settings-surfaces). |
| `PAPERCLIP_SETTING_DEFAULTS` | (unset) | JSON object replacing the schema default of selected instance settings, for hosting operators. See [Operator setting defaults](#operator-setting-defaults). |
| `PAPERCLIP_DEBUG_WORK_SIGNALS` | (unset) | Diagnostic only. Comma-separated allowlist of work-signal topics, each optionally followed by `:N` (for example `stranded-assignments:10,run-dispatch`). For each listed topic the server writes JSON lines to stderr: every work signal (topic and hint), with the caller stack on one signal in N (default 20); remote and reconnect notifications; direct settled publishes; and the recovery queues' loads, checks and direct wakes (direct wakes always carry the caller stack). Logged values are internal ids only (company, issue, run), never secrets or user identifiers. Stack frames keep their raw file and line positions, so bundled builds map them offline with that build's source map. Each topic and event pair is capped at 10 lines per second; a `suppressed` line names the dropped event and its count. The value is read once at startup, so enabling or changing it requires a restart. Unset or empty disables it completely. |
| `PAPERCLIP_MANAGED_CONFIG` | (unset) | Trusted deployment configuration for managed features, plugins and environments. See [Managed sandbox reconciliation](#managed-sandbox-reconciliation). |
Daytona connectivity for `paperclip_runner` uses authenticated provider
WebSocket ingress and follows the instance experimental setting
@@ -180,6 +181,78 @@ Unknown field names are logged and ignored (mixed-version fleet safe).
Malformed JSON or an invalid value for a known field refuses startup — policy
configuration fails closed.
### Managed sandbox reconciliation
Managed-config sandbox declarations reconcile the instance's Paperclip-marked
environment after its provider plugin becomes ready. Platform-owned stock can
advance before the first company exists; company-scoped stock bindings are
initialized when companies appear. This includes legacy marked rows that have
no stock baseline. Ordinary self-hosted reconciliation continues to preserve
operator modifications.
The internal `platformFullyManaged` assertion is reserved for deployment callers
that prevent operator edits. Other callers of `ensureManagedSandboxEnvironment`
leave it unset. Platform reconciliation requires the row's existing
`managedByPaperclip` ownership marker even when company bindings exist: a
tracking binding recorded for a skipped tenant-created row never authorizes
adopting it. Archived rows require a matching stock binding that records both
the archive status and Paperclip's archive token before automatic reactivation.
A token on the environment row alone is insufficient. An operator save removes
that token when reaffirming an archive. An archived row without a corroborating
binding stays preserved for individual operator review, including when it was
archived before the first company existed. Later tracking-only bindings do not
authorize its reactivation.
Re-running reconciliation with the desired declaration repairs stale managed
stock in place and is idempotent. It preserves environment identity, tenant
environment variables and unrelated metadata, and does not replace running
provider leases. Existing workspaces can therefore receive a separately
scheduled reconciliation rather than deleting their environments or leases.
An unmarked environment holding the declared name remains untouched. A
deliberately archived row remains archived; only Paperclip's own availability
archive can reactivate automatically.
Inspect the reconciliation's `action`, `stockStatus` and `updateAvailable`
outcomes when verifying an update. Writing the desired configuration or seeing
healthy HTTP responses alone does not prove that a database environment row
converged. Entry failures and skipped stock updates are logged while startup
continues degraded. Validate repeated empty-instance boots and the actual row
in deployment qualification; do not turn a performance check into a startup gate.
### Qualify a sandbox runtime before deployment
The image includes `/app/server/dist/qualify-sandbox-runtime.js`. Deployment
systems can run it without a database, network access or provider credentials:
```sh
node /app/server/dist/qualify-sandbox-runtime.js \
--manifest /opt/paperclip-runner/provider-pack/provider-pack.json \
--runner /app/packages/paperclip-runner/dist/bin/paperclip-runnerd < installed.json
```
The input has `installed` (`manifest`, hexadecimal `runnerSha256`, and `immutable`)
and an optional nonempty `providers` list (`{ "kind": "opencode" }` or
`{ "kind": "acpx", "agent": "claude" }`, for example). The deployment system
chooses its provider list. Omit the list to check OpenCode and every packaged
provider profile. The report records `allPackProviders: true` for that scope.
Core contains no hosting-provider policy.
The caller must collect the installed manifest and runner hash from the exact
sandbox snapshot under qualification, verify its files against that manifest,
and establish that the installation is protected against runtime writes before
asserting `immutable: true`. This command does not make those remote observations
itself. Its result is deployment evidence, not a signed grant or a replacement
for file-integrity verification.
The command uses the same compatibility check as task startup. Different pack
hashes remain compatible when their interface contract and selected provider
profiles agree. A different daemon binary, missing protected installation,
changed contract, or changed provider profile refuses qualification. Exit code
0 means compatible, 2 means incompatible, and 1 means invalid input or an
unavailable qualification. The JSON result records the manifests, daemon hash
and provider scope. Qualify the candidate before publishing or deploying it;
a refusal should leave the current deployment serving.
## Secrets
| Variable | Default | Description |