Skip to content

Commit b0db638

Browse files
committed
fix(cloud-connection): a declared rename still answers 501 when step 2c' has no installed record to compare
Read the renames the incoming version declares from that version alone on the unreadable / missing path, so the record's token never admits a rename this runtime cannot carry. Quote the ledger cause as describeLedgerCause does, widen installed_record_missing to an entry carrying no body, and note the one-row limit of the orphan comparison. Claude-Session: https://claude.ai/code/session_017ihL2dvnT51fbSebUyE3zW Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 02481a8 commit b0db638

3 files changed

Lines changed: 117 additions & 30 deletions

File tree

‎.changeset/30884-schema-upgrade-confirmation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Metadata-protocol: none
1616
Before any write, after the access upgrade check, the door compares the installed version (its ledger entry) with the incoming one through `comparePackageVersions` (`@objectstack/metadata-core`), the comparison the dry run and `os diff` read:
1717

1818
- **A suspected rename** (in one object, a stored field removed while a stored field is added, with no `renamedFrom` linking them) or **a type change** the drift categories call `needs_confirm` (both columns stored) or `destructive` (the new type stores no column) answers `409 SCHEMA_UPGRADE_CONFIRMATION_REQUIRED`. `error.details.findings` names each with a one-line `describe` (and, when the runtime's `manifest` service answers `planUpgrade`, the rows the installed column holds); `error.details.findingSet` is the token. The same request with `"confirmSchemaUpgrade": "<findingSet>"` installs, as the additive schema sync leaves the fields, and the ledger entry records `schemaUpgradeConfirmation` (the token, who, when). A token from another finding set is refused again with the fresh one. A type change riding on a declared rename is judged on its own.
19-
- **The installed side** is the ledger entry's body; an orphaned install (the registry still serves the package, the ledger holds no record) is compared against the body the registry serves. When there is no installed body to compare — the ledger file will not parse, or the orphan's registry row carries no body — the same `409` names that as its one finding (`installed_record_unreadable` with the file and the cause, or `installed_record_missing`), and its token admits the re-install, which rewrites the record.
19+
- **The installed side** is the ledger entry's body; an orphaned install (the registry still serves the package, the ledger holds no record) is compared against the body the registry serves. When there is no installed body to compare — the ledger file will not parse, or neither the ledger entry nor the orphan's registry row carries a body — the same `409` names that as its one finding (`installed_record_unreadable` with the file and the cause, or `installed_record_missing`), and its token admits the re-install, which rewrites the record; a rename the incoming version declares still answers the `501` below.
2020
- **A declared rename** (`renamedFrom` on a field of the new version) answers `501 NOT_IMPLEMENTED`, naming each rename: this runtime cannot yet move a renamed field's values to its new key, and no token answers it.
2121
- A `safe` type change (the installed field had no stored column), a removed field or object and a seed dataset change are refused by nothing; `dryRun: true` reports them.
2222

‎packages/cloud-connection/src/marketplace-install-local-corrupt-ledger.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,38 @@ describe('handleInstall() — step 2c′ over an unreadable record asks, never i
384384
expect(entry?.version).toBe('1.0.1');
385385
expect(entry?.schemaUpgradeConfirmation).toMatchObject({ findingSet: details.findingSet });
386386
});
387+
388+
it('a declared rename over an unreadable record still answers 501, with or without the token, and writes nothing', async () => {
389+
writeCorruptEntryFor('app.test.crm');
390+
const { rawApp, ctx } = await boot();
391+
const install = rawApp.routes.get('POST /api/v1/marketplace/install-local')!;
392+
const renamed = {
393+
...GOOD_MANIFEST,
394+
version: '1.0.1',
395+
objects: [{ name: 'crm_x', fields: { name2: { type: 'text', renamedFrom: ['name'] } } }],
396+
};
397+
const register = ctx.getService('manifest').register;
398+
register.mockClear(); // isolate from the boot's own registrations
399+
400+
const bare = await install(makeInstallC({ manifest: renamed }));
401+
402+
expect(bare.status).toBe(501);
403+
expect(bare.payload.error.code).toBe('NOT_IMPLEMENTED');
404+
expect(bare.payload.error.details.declaredRenames).toEqual([{ object: 'crm_x', from: 'name', to: 'name2', toType: 'text' }]);
405+
expect(bare.payload.error.details.findings).toEqual([expect.objectContaining({
406+
kind: 'installed_record_unreadable', file: 'app.test.crm.json',
407+
})]);
408+
409+
// The token the unreadable record's 409 would print — the same finding
410+
// set the plain upgrade above is asked to confirm — admits no rename.
411+
const plain = await install(makeInstallC({ manifest: { ...GOOD_MANIFEST, version: '1.0.1' } }));
412+
expect(plain.status).toBe(409);
413+
const token = plain.payload.error.details.findingSet;
414+
const withToken = await install(makeInstallC({ manifest: renamed, confirmSchemaUpgrade: token }));
415+
416+
expect(withToken.status).toBe(501);
417+
expect(withToken.payload.error.details.declaredRenames).toEqual([{ object: 'crm_x', from: 'name', to: 'name2', toType: 'text' }]);
418+
expect(register).not.toHaveBeenCalled();
419+
expect(new LocalManifestSource(dir).read('app.test.crm').failure).toBeDefined();
420+
});
387421
});

‎packages/cloud-connection/src/marketplace-install-local-plugin.ts‎

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@
117117
* the confirmation in the ledger entry. An orphaned install (the
118118
* registry serves it, the ledger holds no record) is compared against
119119
* the body the registry serves. A record of the installed version
120-
* that will not parse, or an orphan whose registry row carries no
121-
* body, answers the same 409 with one finding naming it
122-
* (`installed_record_unreadable` / `installed_record_missing`): no
123-
* comparison ran, and its token admits the re-install. Removed fields,
124-
* `safe` type changes and seed changes are refused by nothing.
120+
* that will not parse, or no installed body anywhere (an entry or an
121+
* orphan's registry row carrying none), answers the same 409 with one
122+
* finding naming it (`installed_record_unreadable` /
123+
* `installed_record_missing`): no comparison ran, and its token admits
124+
* the re-install — unless the version declares a rename, still 501.
125+
* Removed fields, `safe` type changes and seed changes are refused by
126+
* nothing.
125127
*
126128
* [epic cloud#3652 Q3, #30884] `dryRun: true` answers `200` with the
127129
* package upgrade plan — the installed version (its ledger entry)
@@ -583,7 +585,11 @@ type SchemaUpgradeFinding =
583585
describe: string;
584586
}
585587
| {
586-
/** The registry serves the package but the ledger holds no record of it (an orphaned install). */
588+
/**
589+
* No installed body to compare: the ledger entry parses but carries no
590+
* package body, or the registry serves the package while the ledger
591+
* holds no record of it (an orphaned install) and its row carries none.
592+
*/
587593
kind: 'installed_record_missing';
588594
describe: string;
589595
};
@@ -674,34 +680,74 @@ function schemaUpgradeFindingSet(manifestId: string, findings: readonly SchemaUp
674680

675681
/**
676682
* [epic cloud#3652 Q3, #30884] The verdict when there is no installed side to
677-
* compare: the ledger file would not parse, or neither the ledger nor the
678-
* registry (an orphaned install's row) holds the installed body. The upgrade's
679-
* renames and type changes cannot be known, and a comparison that did not
680-
* run is never taken for "no finding" (2a, 2b and 2c answer the same way): the
681-
* one finding names what is missing, and its token admits the re-install —
682-
* the re-install that rewrites the record — confirmed and recorded, one round
683-
* trip, never silently.
683+
* compare: the ledger file would not parse, its entry carries no package body,
684+
* or neither the ledger nor the registry (an orphaned install's row) holds
685+
* one. The upgrade's renames and type changes against the installed version
686+
* cannot be known, and a comparison that did not run is never taken for "no
687+
* finding" (2a, 2b and 2c answer the same way): the one finding names what is
688+
* missing, and its token admits the re-install — the re-install that rewrites
689+
* the record — confirmed and recorded, one round trip, never silently.
690+
*
691+
* A rename the incoming version declares is read from that version alone
692+
* ({@link declaredRenamesOfIncoming}) and still answers 501 (ruling record
693+
* 6709061297, C2): the token admits an unchecked re-install, never a rename
694+
* this runtime cannot carry.
684695
*/
685696
function installedRecordUnavailableVerdict(
686697
manifestId: string,
687698
failure: { file: string; cause: unknown } | undefined,
699+
incoming: PackageUpgradeSide,
688700
): { declaredRenames: UpgradePlanDeclaredRename[]; findings: SchemaUpgradeFinding[]; findingSet: string } {
689701
const consequence = 'so this upgrade cannot be checked for fields it renames or retypes: confirming installs it '
690702
+ 'unchecked, and the install rewrites the record.';
691703
const finding: SchemaUpgradeFinding = failure
692704
? {
693705
kind: 'installed_record_unreadable',
694706
file: failure.file,
695-
cause: String((failure.cause as { message?: unknown } | null)?.message ?? failure.cause),
707+
cause: describeLedgerCause(failure.cause),
696708
describe: `The record of the installed "${manifestId}" (${failure.file}) could not be read `
697-
+ `(${String((failure.cause as { message?: unknown } | null)?.message ?? failure.cause)}), ${consequence}`,
709+
+ `(${describeLedgerCause(failure.cause)}), ${consequence}`,
698710
}
699711
: {
700712
kind: 'installed_record_missing',
701-
describe: `This runtime serves "${manifestId}" but holds no record of the installed version's package body, `
702-
+ consequence,
713+
describe: `This runtime holds no package body of the installed "${manifestId}" (neither its install record `
714+
+ `nor the registry carries one), ${consequence}`,
703715
};
704-
return { declaredRenames: [], findings: [finding], findingSet: schemaUpgradeFindingSet(manifestId, [finding]) };
716+
return {
717+
declaredRenames: declaredRenamesOfIncoming(incoming),
718+
findings: [finding],
719+
findingSet: schemaUpgradeFindingSet(manifestId, [finding]),
720+
};
721+
}
722+
723+
/**
724+
* [epic cloud#3652 Q3, #30884] The renames the incoming version declares,
725+
* read from it alone, for the verdict without an installed side: each field
726+
* whose `renamedFrom` names a key the same object no longer declares, as its
727+
* first such key ({@link comparePackageVersions} takes the first that the
728+
* installed version has; with no installed version, the first is the claim).
729+
* There is no `fromType`: the installed type is the unknown.
730+
*/
731+
function declaredRenamesOfIncoming(incoming: PackageUpgradeSide): UpgradePlanDeclaredRename[] {
732+
const renames: UpgradePlanDeclaredRename[] = [];
733+
for (const object of incoming.objects ?? []) {
734+
const o = object as { name?: unknown; fields?: unknown } | null;
735+
if (!o || typeof o.name !== 'string' || o.name === '' || !o.fields || typeof o.fields !== 'object') continue;
736+
const fields = o.fields as Record<string, unknown>;
737+
for (const [key, def] of Object.entries(fields)) {
738+
const d = def as { renamedFrom?: unknown; type?: unknown } | null;
739+
if (!d || !Array.isArray(d.renamedFrom)) continue;
740+
const from = d.renamedFrom.find((k): k is string => typeof k === 'string' && k !== '' && !(k in fields));
741+
if (from === undefined) continue;
742+
renames.push({
743+
object: o.name,
744+
from,
745+
to: key,
746+
...(typeof d.type === 'string' && d.type !== '' ? { toType: d.type } : {}),
747+
});
748+
}
749+
}
750+
return renames;
705751
}
706752

707753
/** One side of the comparison, a multi-package artifact folded as `planUpgrade` folds it. */
@@ -3823,12 +3869,13 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
38233869
* - **The installed side** is the ledger entry's body; for an orphaned
38243870
* install (no ledger file, the registry still serves the package) it is
38253871
* the body the registry serves ({@link registeredPackageBody}).
3826-
* - **No installed body** (the ledger file would not parse, or neither the
3827-
* ledger nor the registry holds one): no comparison can run, and one
3828-
* that did not run is never taken for "no finding" — the 409 below with
3829-
* one finding, `installed_record_unreadable` (its `file` and `cause`) or
3830-
* `installed_record_missing`. Its token admits the re-install, which
3831-
* rewrites the record, confirmed and recorded.
3872+
* - **No installed body** (the ledger file would not parse, its entry
3873+
* carries no body, or neither the ledger nor the registry holds one): no
3874+
* comparison can run, and one that did not run is never taken for "no
3875+
* finding" — the 409 below with one finding, `installed_record_unreadable`
3876+
* (its `file` and `cause`) or `installed_record_missing`. Its token admits
3877+
* the re-install, which rewrites the record, confirmed and recorded — but
3878+
* a rename the incoming version declares still answers the 501 below.
38323879
* - **A declared rename**: `501 NOT_IMPLEMENTED`, naming each — no token
38333880
* answers it while this runtime cannot carry a renamed field's column to
38343881
* its new key (ruling record 6709061297, C2). Card 01b (#30885), which
@@ -3858,14 +3905,15 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
38583905
// The installed side: the ledger entry's body; for an orphaned install
38593906
// (no ledger file, the registry still serves the package — findConflict
38603907
// step 3) the body the registry serves. With neither — a ledger file
3861-
// that will not parse, or a registry row carrying no body — the verdict
3862-
// is the one finding that says so, confirmable like any other, never
3863-
// "no finding".
3908+
// that will not parse, an entry or a registry row carrying no body —
3909+
// the verdict is the one finding that says so, confirmable like any
3910+
// other, never "no finding"; a rename the incoming version declares
3911+
// still answers 501.
38643912
const installedBody = previous.entry?.manifest
38653913
?? (previous.entry === null && previous.failure === undefined ? this.registeredPackageBody(ctx, manifestId) : undefined);
38663914
const installed = installedBody ? this.convertPackageBodyForward(ctx, installedBody) : undefined;
38673915
const verdict = installed === undefined
3868-
? installedRecordUnavailableVerdict(manifestId, previous.failure)
3916+
? installedRecordUnavailableVerdict(manifestId, previous.failure, packageUpgradeSideOf(converted))
38693917
: schemaUpgradeVerdict(manifestId, comparePackageVersions(packageUpgradeSideOf(installed), packageUpgradeSideOf(converted)));
38703918
const { declaredRenames, findingSet } = verdict;
38713919
if (declaredRenames.length === 0 && verdict.findings.length === 0) return {};
@@ -3920,7 +3968,8 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
39203968
+ 'the new field would start empty while the values stay in the old column. Nothing was '
39213969
+ 'installed or written. Install this version once the runtime carries declared renames; or, '
39223970
+ 'to install it now without moving the values, remove the `renamedFrom` declaration, '
3923-
+ 'rebuild, and confirm the suspected rename the next refusal lists.'
3971+
+ 'rebuild, and confirm what the next refusal lists (a suspected rename, or the '
3972+
+ 'installed record it could not read).'
39243973
+ (findings.length > 0
39253974
? ` That refusal will also ask to confirm ${findings.length} other finding(s), listed here.`
39263975
: ''),
@@ -4945,6 +4994,10 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
49454994
* under `manifestId` — the row {@link findConflict} step 3 finds for an
49464995
* orphaned install — or `undefined` when there is none, or it carries no
49474996
* body. Step 2c′ compares against it when the ledger holds no record.
4997+
*
4998+
* One row, the one {@link findConflict} matches: for an artifact that
4999+
* bundles several packages, the other packages' rows are not folded in,
5000+
* so their objects read as added (never refused) rather than compared.
49485001
*/
49495002
private registeredPackageBody = (ctx: PluginContext, manifestId: string): unknown => {
49505003
try {

0 commit comments

Comments
 (0)