Skip to content
Merged
23 changes: 23 additions & 0 deletions .changeset/33536-uninstall-unbinds-action-handlers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/cloud-connection': patch
'@objectstack/runtime': patch
'@objectstack/objectql': patch
---

fix(cloud-connection, runtime, objectql)!: an install-local uninstall withdraws the package's action handlers and body hooks from the running engine, so a call that names no package is served by the one remaining holder at once instead of after a restart

Clause-②: yes (narrowing) — `ObjectQL#removeActionsByPackage` now hands a key it empties in the slot a call that names no package reads back to the entry another owner still holds under it in its own slot. On `ObjectQL#removePackage`, on the binder's re-bind that drops a package body of a multi-package artifact, and on the authored-action resync's teardown (`removeActionsByPackage('metadata-service')`), the action doors (`POST /api/v1/actions/OBJECT/NAME/ID`, MCP `run_action`) now reach that handed-back entry. Before, they reached the next entry their handler-key rotation held (`OBJECT`, then `global`, then `*`, per handler-key candidate), and answered "not found" only when none was held. The owner gates of the reached entry move with it, so a call that was served can now be refused `403 PERMISSION_DENIED`, `409 ACTION_DISABLED` or `503`. The install-local uninstall the card reports is widening only: a bare call that answered `409 RESOURCE_CONFLICT`, naming the uninstalled package as one of two owners, now answers with the one remaining holder's handler. `@objectstack/runtime` gains the export `unbindAppArtifactHandlers` and the type `AppArtifactHandlerUnbinding`.
Metadata-protocol: none
<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed, renamed or re-shaped, and no stored row changes: what moves is which in-memory handler entry a call that names no package reaches after a live removal, which is the entry a restart already reaches, so there is nothing for `objectstack migrate meta` to rewrite and no tombstone. -->

**BREAKING** for a call that names no package, on a key that `removePackage`, a re-bind that drops a body, or the authored-action resync empties while another owner (host code, a plugin, a package or the environment) still holds a handler under it. The emptied key now resolves to that remaining holder's entry, so the call runs that handler, and that entry's owners' gates (403 / 409 `ACTION_DISABLED` / 503) can answer where the door's rotation used to fall through to a later entry and serve it. This is what a restart of the same process already answers. No data migration or heal is needed: nothing persisted changes. The install-local uninstall the card reports is unaffected, because the uninstalled package's handler stayed bound there and the key never emptied.

**The defect.** `DELETE /api/v1/marketplace/install-local/:manifestId` withdrew the package from the registry and left every handler its install had bound on the engine under `app:<manifestId>`: script-action handlers, body hooks and bundle functions. With two packages each holding an action of one name on one object, a call that names no package kept being refused as ambiguous until the runtime restarted, and the uninstalled package's body hooks kept firing.

**The fix.**

- `@objectstack/runtime`: `unbindAppArtifactHandlers(ql, appId, { logger, source })` is the inverse of `bindAppArtifactHandlers`. It removes the actions, hooks and functions of the package's owner and of every owner the package's last bind on that engine recorded (a multi-package artifact's bodies), releases those owners' bare-name function claims, and forgets the bind record, so a later install binds as a first bind. Another package's handlers, host code and the environment's authored-action handlers are untouched. Jobs keep leaving through the `runtime.package-jobs` uninstall cleanup.
- `@objectstack/cloud-connection`: the uninstall door calls it right after the registry withdrawal, and not when the registry refuses the withdrawal (the package stays loaded then). A runtime that has no such export is warned with the remedy, as the install door already warns for a runtime with no binder.
- `@objectstack/objectql`: `removeActionsByPackage` hands a key back to the owner that still holds a handler for it. Each owner keeps its handler in its own package slot, but the slot a call that names no package runs held only the last registration. Removing that registration emptied the key, so the engine's bare `executeAction` answered "not found" while another owner still held a handler. The key now takes back the entry another coordinate's slot holds: the last coordinate holding the key in the order the slots were created, which is not always the last registration of this key. That choice matters where the bare-call rule runs one owner while others hold the key: host code or a plugin beside one package on an object-bound key, and two shared owners with no package (host code beside a plugin, or two plugins). When the earlier-created slot registered this key last, the hand-back runs the other holder's handler where a restart runs the last registration. While two or more packages still hold the key, the bare call is refused naming them as before.

**Not changed.** `PATCH /api/v1/packages/:id/disable` leaves a disabled package's handlers in place, and the bare-call rule already counts no disabled package as an owner. `DELETE /api/v1/packages/:id` (the protocol's `deletePackage`) does not call this unbind, so it is unchanged.
44 changes: 44 additions & 0 deletions packages/cloud-connection/src/marketplace-install-local-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,12 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
try { registry = ctx.getService<IObjectQLEngine>('objectql')?.registry; } catch { /* no data engine — nothing registered */ }
const declared = captureUninstallDeclared(registry, manifestId);
const withdrawal = this.withdrawFromRunningKernel(ctx, manifestId);
// The registry verb reaches no executable handler: the package's
// script-action handlers, body hooks and functions this door's install
// bound on the engine ({@link bindArtifactHandlers}) go right after the
// registry half withdrew, through the binder's own inverse — never on
// a refused withdrawal, which leaves the package loaded and running.
if (!withdrawal) await this.unbindArtifactHandlers(ctx, manifestId);
const copies = withdrawal ? undefined : await this.withdrawObjectCopies(ctx, manifestId);
// [#24182] …and the copies of OTHER packages' objects that this one's
// extensions rewrote: the bridge that wrote them restores the owner's
Expand Down Expand Up @@ -6646,6 +6652,44 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
await schedule(ctx, manifest, { appId: manifestId, ql, source: 'MarketplaceInstallLocal' });
};

/**
* Withdraw from the running engine the script-action handlers, body hooks
* and functions {@link bindArtifactHandlers} bound for `manifestId`,
* through `unbindAppArtifactHandlers` — the runtime binder's inverse, so
* the door unbinds exactly the owners the binder bound and nothing of
* another package, host code or the environment. The uninstall door runs
* it once the registry has withdrawn the package.
*
* Before this, a withdrawn package's handlers stayed under its owner
* `app:<manifestId>` until a restart: beside another package's action of
* the same object and name, a call that names no package
* (`POST /api/v1/actions/OBJECT/NAME/ID`) went on answering `409
* RESOURCE_CONFLICT`, naming the uninstalled package as one of two owners.
*
* Resolved lazily through `@objectstack/runtime`, like the binder. Never
* throws: a runtime with no inverse is warned with the remedy, the bind
* door's posture for a runtime with no binder.
*/
private unbindArtifactHandlers = async (ctx: PluginContext, manifestId: string): Promise<void> => {
let ql: IObjectQLEngine | undefined;
try { ql = ctx.getService<IObjectQLEngine>('objectql'); } catch { /* no data engine — nothing bound */ }
if (!ql) return;
let unbind: typeof import('@objectstack/runtime')['unbindAppArtifactHandlers'] | undefined;
try {
const mod: any = await import('@objectstack/runtime');
if (typeof mod?.unbindAppArtifactHandlers === 'function') unbind = mod.unbindAppArtifactHandlers;
} catch { /* reported below */ }
if (!unbind) {
ctx.logger?.warn?.(
`[MarketplaceInstallLocal] uninstalled ${manifestId}, but this runtime has no unbindAppArtifactHandlers — `
+ 'its script actions, body hooks and functions stay bound until the runtime restarts. '
+ 'Upgrade @objectstack/runtime alongside @objectstack/cloud-connection.',
);
return;
}
unbind(ql, manifestId, { logger: ctx.logger, source: 'MarketplaceInstallLocal' });
};

/**
* [#21489, #21585] The code in `manifest` this door cannot run, which the
* install route refuses:
Expand Down
Loading
Loading