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
14 changes: 14 additions & 0 deletions .changeset/34574-artifact-cycle-refusal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@objectstack/core': patch
'@objectstack/spec': patch
'@objectstack/cloud-connection': patch
---

fix(core): a composed artifact whose packages depend on each other in a cycle is refused as `422 CYCLIC_ARTIFACT_PACKAGES` naming the cycle, and install-local answers every artifact-resolver refusal instead of a 500

Clause-②: yes (widening)
Metadata-protocol: none

- `@objectstack/core`: `resolveArtifactPackageEntries` / `resolveArtifactPackageOrder` refuse packages that depend on each other in a cycle with the ADR-0112 envelope their other refusals carry: `code: 'CYCLIC_ARTIFACT_PACKAGES'`, `status: 422`, and `details.cycle` listing the package ids along the cycle (each depends on the next, the first repeated last). Before, the cycle was a bare `Error` with no `code` or `status`. A caller that matched on the old message still finds `Circular dependency detected` in it. `resolvePluginOrder` now throws its cycle as `PluginOrderCycleError`, a new exported `Error` subclass that carries `cycle`. Its message is unchanged and it carries no `code`, so a kernel boot that hits a plugin cycle reads exactly as before.
- `@objectstack/spec`: `CYCLIC_ARTIFACT_PACKAGES` is registered in `ERROR_CODE_LEDGER` under `@objectstack/core`, so `ErrorCode` accepts it.
- `@objectstack/cloud-connection`: `POST /api/v1/marketplace/install-local` answered `500 INTERNAL_ERROR` with no reason for a composed artifact the resolver refuses: a `packages` that is not a list, a malformed entry, a duplicate package id, or a dependency cycle. On a real runtime, the step that judges the package's jobs and hooks reads the packages first and let the refusal escape. That step now answers the refusal itself, with its own status, code and `details`, and nothing is written. Every other step that reads the packages already did.
3 changes: 2 additions & 1 deletion content/docs/references/api/automation-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ const result = AutomationApiErrorCode.parse(data);

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +368 more>` | ✅ | The inner refusal's code in its WIRE spelling - the ADR-0112 member a record save of the same write answers in `error.code` (e.g. `UNIQUE_VIOLATION`); the closed set `ApiErrorSchema.code` parses against |
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +369 more>` | ✅ | The inner refusal's code in its WIRE spelling - the ADR-0112 member a record save of the same write answers in `error.code` (e.g. `UNIQUE_VIOLATION`); the closed set `ApiErrorSchema.code` parses against |
| **message** | `string` | ✅ | The inner refusal's own diagnostic text, verbatim, without the node and operation prefixes the run's failure text adds |

### Allowed Values: `FlowInnerRefusal.code`
Expand Down Expand Up @@ -523,6 +523,7 @@ const result = AutomationApiErrorCode.parse(data);
* `CREATE_FAILED`
* `CREDENTIALS_NAME_DIFFERENT_ACCOUNTS`
* `CUBE_NOT_FOUND`
* `CYCLIC_ARTIFACT_PACKAGES`
* `DATASET_INVALID`
* `DATASOURCE_ADMIN_ERROR`
* `DEFAULT_RECORD_PAGE_CONFLICT`
Expand Down
3 changes: 2 additions & 1 deletion content/docs/references/api/contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const result = ApiErrorSchema.parse(data);

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +368 more>` | ✅ | Error code (e.g. VALIDATION_ERROR; StandardErrorCode ∪ the ledger the serving side registers — ERROR_CODE_LEDGER for framework packages) |
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +369 more>` | ✅ | Error code (e.g. VALIDATION_ERROR; StandardErrorCode ∪ the ledger the serving side registers — ERROR_CODE_LEDGER for framework packages) |
| **declaredCode** | `string` | optional | The producer-declared code, verbatim, when it is not a member of the closed `code` vocabulary — the open, author-authored channel (app-specific spellings; ADR-0112) |
| **message** | `string` | ✅ | Readable error message |
| **userMessage** | `string` | optional | Producer-marked user-facing refusal text, verbatim, in the request's locale: platform-composed text falls back to the workspace default locale, then `en`; an app-authored refusal falls back to its declared wording. Present exactly when the producer opted in at throw time; consumers render it to end users as received and keep their generic substitution for anything unmarked. Status-agnostic; never replaces `message`. |
Expand Down Expand Up @@ -141,6 +141,7 @@ const result = ApiErrorSchema.parse(data);
* `CREATE_FAILED`
* `CREDENTIALS_NAME_DIFFERENT_ACCOUNTS`
* `CUBE_NOT_FOUND`
* `CYCLIC_ARTIFACT_PACKAGES`
* `DATASET_INVALID`
* `DATASOURCE_ADMIN_ERROR`
* `DEFAULT_RECORD_PAGE_CONFLICT`
Expand Down
1 change: 1 addition & 0 deletions content/docs/references/api/error-code-ledger.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ const result = ErrorCode.parse(data);
* `CREATE_FAILED`
* `CREDENTIALS_NAME_DIFFERENT_ACCOUNTS`
* `CUBE_NOT_FOUND`
* `CYCLIC_ARTIFACT_PACKAGES`
* `DATASET_INVALID`
* `DATASOURCE_ADMIN_ERROR`
* `DEFAULT_RECORD_PAGE_CONFLICT`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ describe('install-local parses the inline manifest\'s `namespace` through its de

it('UNMOVED — an artifact MEMBER\'s refused namespace keeps the artifact-entry parse\'s refusal, nothing written', async () => {
// Each `packages[]` entry is parsed whole through `ArtifactPackageSchema`
// before any write; this door adds no second judge of it.
// before any write; this door adds no second judge of it. [#34574] The
// refusal is ANSWERED with its own 422 and code (step 1c reads the
// packages first over this real runtime), where it used to leave the
// door as a throw the transport answered `500`.
const h = await mount(dir);
const thrown = await h.install({
const res = await h.install({
manifest: {
id: 'com.qa.suite',
version: '1.0.0',
Expand All @@ -204,10 +207,12 @@ describe('install-local parses the inline manifest\'s `namespace` through its de
{ manifest: { id: 'com.qa.two', name: 'qa_two', version: '1.0.0', type: 'module', namespace: 'QA_N', objects: [] } },
],
},
}).then(() => undefined, (e: unknown) => e as { code?: string; status?: number; message?: string });
expect(thrown?.code).toBe('INVALID_ARTIFACT_PACKAGE_ENTRY');
expect(thrown?.status).toBe(422);
expect(thrown?.message).toContain(declaredSentence('QA_N'));
});
expect(res.status, JSON.stringify(res.payload)).toBe(422);
expect(envelopeViolations(res.payload)).toEqual([]);
expect(res.payload.success).toBe(false);
expect(res.payload.error.code).toBe('INVALID_ARTIFACT_PACKAGE_ENTRY');
expect(res.payload.error.message).toContain(declaredSentence('QA_N'));
expectNothingWritten(h);
});

Expand Down
48 changes: 36 additions & 12 deletions packages/cloud-connection/src/marketplace-install-local-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,33 +475,40 @@ function isDeclaredRegisterRefusal(err: unknown): boolean {
}

/**
* [#34407, #34432] The answer to a package body the artifact resolver refuses
* before any write (`resolveStackCollection` → `resolveArtifactPackageEntries`:
* [#34407, #34432, #34574] The answer to a package body the artifact resolver
* refuses before any write (`resolveArtifactPackageEntries`, reached through
* `resolveStackCollection` or the runtime binder's judgements:
* `INVALID_ARTIFACT_PACKAGES`, `INVALID_ARTIFACT_PACKAGE_ENTRY`,
* `DUPLICATE_ARTIFACT_PACKAGE`, each an ADR-0112 envelope with its declared
* `status: 422`): that refusal's own declared status and code, the resolver's
* sentence, and that nothing was written. The one reader of such a refusal on
* this door's pre-write steps — the D5e gate (2b) and the declared renames
* (2c″) both read the bodies through the resolver, so the same bytes are
* answered the same way whichever step reads them first.
* `DUPLICATE_ARTIFACT_PACKAGE`, `CYCLIC_ARTIFACT_PACKAGES`, each an ADR-0112
* envelope with its declared `status: 422`): that refusal's own declared
* status and code, the resolver's sentence, its `details` when it states any
* (a cycle's `details.cycle`), and that nothing was written. The one reader of
* such a refusal on this door's pre-write steps — the unrunnable-code
* judgements (1c), the datasource holder (2a), the D5e gate (2b) and the
* declared renames (2c″) all read the bodies through the resolver, so the same
* bytes are answered the same way whichever step reads them first.
*
* `undefined` for a throw that declares no status below 500 — an undeclared
* throw (the resolver's own dependency-cycle error among them) or a declared
* 5xx: the caller rethrows it, never relabels it.
* throw or a declared 5xx: the caller rethrows it, never relabels it.
*/
function declaredRefusalAnswer(
manifestId: string,
err: unknown,
): { status: number; body: { success: false; error: { code: string; message: string } } } | undefined {
): { status: number; body: { success: false; error: { code: string; message: string; details?: Record<string, unknown> } } } | undefined {
const refusal = resolveThrownHttpError(err);
if (refusal.declaredStatus === undefined || refusal.status >= 500) return undefined;
const stated = (err as { details?: unknown } | null | undefined)?.details;
const details = stated !== null && typeof stated === 'object' && !Array.isArray(stated)
? stated as Record<string, unknown>
: undefined;
return {
status: refusal.status,
body: {
success: false,
error: {
code: refusal.code,
message: `Package '${manifestId}' cannot be installed: ${String((err as any)?.message ?? err)} Nothing was installed or written.`,
...(details !== undefined ? { details } : {}),
},
},
};
Expand Down Expand Up @@ -2956,7 +2963,24 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
// unbindable `pull` is warned by the binder and NOT scheduled), and
// its hook with no `body` is warned and NOT bound
// ({@link bindArtifactHandlers}).
const unrunnable = await this.unrunnableCode(ctx, manifest, manifestId);
// [#34574] The binder's judgements read every package body of a
// composed bundle through the artifact resolver, so over a runtime
// that carries them THIS is the first step to resolve the
// packages. A `packages` the resolver refuses — not a list, a
// malformed entry, a duplicate id, packages that depend on each
// other in a cycle — is answered here with that refusal
// ({@link declaredRefusalAnswer}), as 2a, 2b and 2c″ answer it.
// Before, the refusal left the door as a throw, and the transport,
// which renders no `422`, answered `500 INTERNAL_ERROR`. An
// undeclared throw stays a throw.
let unrunnable: UnrunnableCode;
try {
unrunnable = await this.unrunnableCode(ctx, manifest, manifestId);
} catch (err: unknown) {
const refused = declaredRefusalAnswer(manifestId, err);
if (!refused) throw err;
return c.json(refused.body, refused.status);
}
if (unrunnable.jobs.length > 0 || unrunnable.sharedJobNames.length > 0 || unrunnable.hooks.length > 0) {
return c.json({
success: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* `marketplace-install-local-composed-top-level-reads.test.ts`), so 2c″ is the
* first step to resolve the packages. (A runtime that carries those
* judgements resolves the packages at step 1c first, and the resolver's
* refusal leaves the door as a throw.) The controls pin the rest of the
* contract: a table that really could not be read still answers 503, and an
* undeclared throw out of the resolver (its own dependency-cycle error) is
* rethrown, never relabelled.
* refusal is answered there, at step 1c — pinned over the real runtime in
* `marketplace-install-local-resolver-refusal-real-runtime.test.ts`.) The
* controls pin the rest of the contract: a table that really could not be read
* still answers 503, and a dependency cycle between two packages — an
* undeclared throw out of the resolver until #34574 — is now the resolver's
* declared `422 CYCLIC_ARTIFACT_PACKAGES`, answered like its siblings, never
* the 503.
*/

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
Expand Down Expand Up @@ -188,7 +191,7 @@ describe('#34432 — step 2c″ answers a body the artifact resolver refuses wit
});
});

describe('#34432 — controls: the 503 stays the unread table\'s, and an undeclared throw stays a throw', () => {
describe('#34432 — controls: the 503 stays the unread table\'s, and a cycle is the resolver\'s declared refusal', () => {
it('CONTROL: a well-formed composed artifact whose renamed field\'s table cannot be read still answers 503 SERVICE_UNAVAILABLE', async () => {
const driver = fakeDriver({ planThrows: true });
const rt = await boot(dir, driver);
Expand All @@ -212,21 +215,20 @@ describe('#34432 — controls: the 503 stays the unread table\'s, and an undecla
expect(driver.planDeclaredRenames).toHaveBeenCalledTimes(1);
});

it('an undeclared throw out of the resolver (a dependency cycle between two packages) is rethrown, never answered 503', async () => {
it('a dependency cycle between two packages answers 422 CYCLIC_ARTIFACT_PACKAGES naming the cycle, never 503, and nothing is written', async () => {
const driver = fakeDriver();
const rt = await boot(dir, driver);
const cyclic = [
entry(`${SUITE}.core`, [ACCOUNT], { [`${SUITE}.leads`]: '*' }),
entry(`${SUITE}.leads`, [LEAD], { [`${SUITE}.core`]: '*' }),
];

const outcome = await rt.install({ manifest: composed(cyclic) }).then(
(res: any) => ({ answered: res }),
(err: unknown) => ({ thrown: err as Error }),
);
const res = await rt.install({ manifest: composed(cyclic) });

expect('answered' in outcome ? JSON.stringify(outcome.answered.payload) : 'thrown').toBe('thrown');
expect((outcome as { thrown: Error }).thrown.message).toContain('Circular dependency');
expect(res.status, JSON.stringify(res.payload)).toBe(422);
expect(res.payload.success).toBe(false);
expect(res.payload.error.code).toBe('CYCLIC_ARTIFACT_PACKAGES');
expect(res.payload.error.details).toEqual({ cycle: [`${SUITE}.core`, `${SUITE}.leads`, `${SUITE}.core`] });
expectNothingWritten(rt, driver);
});
});
Loading
Loading