Skip to content

Commit 2c293d5

Browse files
committed
fix(cloud-connection): state that a passing package-store error fails closed; the store double answers it null
`service-package`'s `get` logs an ordinary driver error and answers null, throwing only its seam-unreadable refusal, so a passing store error is a 404 at purge and an unrecorded copy at install (`copyRecord`), not a 503. The door's test double now meets faults the same way, with one pin for each. Claude-Session: https://claude.ai/code/session_01FEkQ5pQJCx8STEq28nr3Ej Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5d8c404 commit 2c293d5

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

‎.changeset/30808-unmanaged-copy-sample-data.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Metadata-protocol: none
1010

1111
- **`POST /marketplace/install-local` accepts `seedSampleData`** beside `installMode: 'unmanaged'`. It is off by default: without it an unmanaged copy loads no row, as before, and answers the same `seeded: { mode: 'skipped' }`. With `true`, the copy loads the package's `data` datasets once, inline, and answers `seeded: { mode: 'inline', inserted, updated, skipped, errors }` beside `copied`. Every row is owned by the person who installed it (`owner_id` and `created_by`). Rows the package wrote under different personas all collapse to that one owner, so per-role answers in the customer's environment differ from the gallery's per-persona transcripts. Lookups between the copied rows resolve in load order, exactly as on a managed install. `seedSampleData` that is not a boolean, or one sent with a managed install (which always loads its data), answers 400 `INVALID_REQUEST` before anything is written.
1212
- **The seed receipt.** The copy writes each dataset as a `seed` item of the new base (`<object>_seed`, then `<object>_seed_2` for a second dataset of the same object), in the environment's own metadata. Under `targetNamespace` only the dataset's `object` follows the rename; its records and `externalId` are copied verbatim, so the values a lookup or a purge matches never change. A dataset's `files` map is kept in the receipt verbatim and handed to the same loader a managed install uses; loading those bytes on a copy is not exercised by a test here. A dataset the copy cannot write (no `object`, no `records` array) is refused with 400 before any write, and a `seed` item the save door refuses rolls the whole copy back, as every other copied item does. A re-run is still refused with 409.
13-
- **The door's record of its copies.** The install-local door records each unmanaged copy it makes (with or without sample data) in its own storage directory, under `unmanaged-copies/` beside the install ledger, in the same act as the copy. The ledger never reads it as an install entry. The record is removed when the copy's base is uninstalled (through the protocol's uninstall cleanups), and it is lost with the ledger directory, as ledger entries are. A copy made before this release has no record. The record is tied to the base instance, not only to its id: it keeps the `version` and `created_at` of the base's row in the package store (`sys_packages`), and the purge answers 404 when that row is gone or carries another `created_at`, so a later base under the same id (a Studio duplicate also defaults to `<id>-copy`) never inherits the purge, even when the record outlived the copy. Two conditions stay open: a runtime with no `package` service persists no base row, so its record holds no instance and the purge's reach there is by id alone; and `created_at` has one-second resolution, so a base uninstalled and re-created under the same id within the same second shares it. When the record cannot be kept (its file cannot be written, or the package store does not answer with the new base's row), the install still answers 200 and says so in `copyRecord: { written: false, message }`; that copy is then out of the purge's reach. The answer carries no `copyRecord` when the record was kept.
13+
- **The door's record of its copies.** The install-local door records each unmanaged copy it makes (with or without sample data) in its own storage directory, under `unmanaged-copies/` beside the install ledger, in the same act as the copy. The ledger never reads it as an install entry. The record is removed when the copy's base is uninstalled (through the protocol's uninstall cleanups), and it is lost with the ledger directory, as ledger entries are. A copy made before this release has no record. The record is tied to the base instance, not only to its id: it keeps the `version` and `created_at` of the base's row in the package store (`sys_packages`), and the purge answers 404 when that row is gone or carries another `created_at`, so a later base under the same id (a Studio duplicate also defaults to `<id>-copy`) never inherits the purge, even when the record outlived the copy. Two conditions stay open: a runtime with no `package` service persists no base row, so its record holds no instance and the purge's reach there is by id alone; and `created_at` has one-second resolution, so a base uninstalled and re-created under the same id within the same second shares it. When the record cannot be kept (its file cannot be written, or the package store does not answer with the new base's row), the install still answers 200 and says so in `copyRecord: { written: false, message }`; that copy is then out of the purge's reach, and no door records it later (re-recording is follow-up #31425). The answer carries no `copyRecord` when the record was kept. The package store's read answers an ordinary database error (a lock timeout, a dropped connection) with "no row", logging it; it throws only when its query seam ran no read at all. So a passing store error leaves the copy unrecorded at install, and answers 404 at purge. Both fail closed: no row is deleted and no base is reached by mistake.
1414
- **The uninstall answers carry one more cleanup outcome.** The door registers the uninstall cleanup `install-local:unmanaged-copy-record`, so every uninstall answer's `cleanups` list now includes its outcome (`{ name: 'install-local:unmanaged-copy-record', success: true, removed: 0 }` for a base that is no recorded copy): the install-local `DELETE`, the protocol's `deletePackage`, and `DELETE /packages/:id` (`persisted.cleanups`, and the partial-failure 400). The shape of those answers is unchanged.
15-
- **`POST /marketplace/install-local/:id/purge-sample-data` on a copy** reaches only a base in that record; every other writable base, a Studio-authored one included, keeps the 404. For a recorded copy it reads the receipt and deletes exactly the rows it names, matched by `externalId` (a row the person wrote survives). A copy made without the sample data answers 400 `NOTHING_TO_PURGE` saying no sample data was copied. A base that is no longer installed answers 404 before its record is read. A record that cannot be read answers 503 naming the local record file to repair; a receipt or a package-store row that cannot be read answers 503 asking for a retry. Neither deletes anything. Purge before uninstalling the copy: an uninstall removes the receipt and the record with the base, and leaves the rows.
15+
- **`POST /marketplace/install-local/:id/purge-sample-data` on a copy** reaches only a base in that record; every other writable base, a Studio-authored one included, keeps the 404. For a recorded copy it reads the receipt and deletes exactly the rows it names, matched by `externalId` (a row the person wrote survives). A copy made without the sample data answers 400 `NOTHING_TO_PURGE` saying no sample data was copied. A base that is no longer installed answers 404 before its record is read. A record that cannot be read answers 503 naming the local record file to repair; a receipt that cannot be read, or a package store whose query seam ran no read, answers 503 asking for a retry. An ordinary package-store error answers 404, as above. None of these deletes anything. Purge before uninstalling the copy: an uninstall removes the receipt and the record with the base, and leaves the rows.
1616
- **`importUnmanagedPackage`** (metadata-protocol) takes the door-stated `seedData: true`, read off before the request is parsed, like `actor`. The published `ImportUnmanagedPackageRequestSchema` is unchanged.
1717

1818
A boot never applies the receipt, so a restart after the copy or after the purge inserts no row, and a plain save of a copied `seed` item loads nothing. Drafted and published, a copied `seed` item behaves as every environment `seed` item does: create-only, matched by `externalId`. Published after a purge, it loads its rows again under the publish rule, not the installer's: the publish names no person, so those rows carry no `owner_id` or `created_by`.

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,37 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
657657
* so a second mount is a restart over the same database. `packageRows`
658658
* stands in for the `package` service's `sys_packages` rows (`get`), the
659659
* import minting each new base's `created_at`; `packageStore: false`
660-
* mounts a runtime with no `package` service.
660+
* mounts a runtime with no `package` service. `storeFault(kind)` makes the
661+
* next `get` meet a fault the way `service-package`'s does: an ordinary
662+
* driver error (a lock timeout, a dropped connection) is caught, logged and
663+
* answered `null`; only the seam-unreadable refusal escapes, as a 503.
661664
*/
662665
function world(opts: { packageStore?: boolean } = {}) {
663666
const tables: Record<string, Row[]> = {};
664667
const objects = new Map<string, any>();
665668
const packages = new Map<string, { manifest: Record<string, unknown> }>();
666669
const packageRows = new Map<string, { version: string; created_at: string }>();
667670
let stamp = 0;
671+
let fault: 'ordinary' | 'seam' | undefined;
672+
const seamRefusal = Object.assign(
673+
new Error('The package store cannot answer: its query seam ran no SELECT.'), { code: 'SERVICE_UNAVAILABLE', status: 503 },
674+
);
668675
const packageGet = vi.fn(async (id: string, version = 'latest') => {
669-
const row = packageRows.get(id);
670-
if (!row || (version !== 'latest' && row.version !== version)) return null;
671-
return { id, ...row };
676+
try {
677+
if (fault !== undefined) {
678+
const kind = fault;
679+
fault = undefined;
680+
throw kind === 'seam' ? seamRefusal : new Error('SQLITE_BUSY: database is locked');
681+
}
682+
const row = packageRows.get(id);
683+
if (!row || (version !== 'latest' && row.version !== version)) return null;
684+
return { id, ...row };
685+
} catch (err) {
686+
if (err === seamRefusal) throw err;
687+
return null;
688+
}
672689
});
690+
const storeFault = (kind: 'ordinary' | 'seam') => { fault = kind; };
673691
const writes: string[] = [];
674692
let nextId = 0;
675693
const known = (object: string) => {
@@ -813,7 +831,7 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
813831
logger,
814832
};
815833
}
816-
return { tables, writes, seedRows, packages, packageRows, packageGet, importUnmanagedPackage, getMetaItem, cleanups, boot };
834+
return { tables, writes, seedRows, packages, packageRows, packageGet, storeFault, importUnmanagedPackage, getMetaItem, cleanups, boot };
817835
}
818836

819837
it('off by default: the import is not asked for the sample data, no row lands, and the answer says it was skipped', async () => {
@@ -1020,16 +1038,23 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
10201038
expect(w.packageGet).toHaveBeenLastCalledWith('com.example.crm-copy', '1.0.0');
10211039
});
10221040

1023-
it('a base whose package-store row is gone is not reached (404); a package store that does not answer is a 503 naming no file', async () => {
1041+
it('at purge: a gone row, or an ordinary store error the store answers null, fails closed (404); only the seam refusal is a 503', async () => {
10241042
const w = world();
10251043
const h = await w.boot();
10261044
await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
10271045
const before = w.writes.length;
1028-
w.packageGet.mockRejectedValueOnce(new Error('sys_packages is unreachable'));
1046+
// An ordinary driver error: the store logs it and answers null, so the
1047+
// door cannot tell it from a gone row — 404, nothing deleted.
1048+
w.storeFault('ordinary');
1049+
const passing = await h.purge('com.example.crm-copy');
1050+
expect(passing.status, JSON.stringify(passing.payload)).toBe(404);
1051+
expect(passing.payload.error.code).toBe('RESOURCE_NOT_FOUND');
1052+
// The seam-unreadable refusal is the one fault that escapes the store: 503, no file named, no store text.
1053+
w.storeFault('seam');
10291054
const unread = await h.purge('com.example.crm-copy');
10301055
expect(unread.status, JSON.stringify(unread.payload)).toBe(503);
10311056
expect(unread.payload.error.message).not.toContain('Repair');
1032-
expect(unread.payload.error.message).not.toContain('sys_packages is unreachable');
1057+
expect(unread.payload.error.message).not.toContain('query seam');
10331058
const row = w.packageRows.get('com.example.crm-copy')!;
10341059
w.packageRows.delete('com.example.crm-copy');
10351060
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
@@ -1066,16 +1091,19 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
10661091
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
10671092
rmSync(join(dir, 'unmanaged-copies'), { force: true });
10681093
}
1069-
// The package store holds no row of the new base: no record is written.
1070-
{
1094+
// The package store meets a fault at install — an ordinary error it
1095+
// answers null, or the seam refusal it throws: no record is written,
1096+
// and the copy stays unrecorded (re-recording is #31425).
1097+
for (const kind of ['ordinary', 'seam'] as const) {
10711098
const w = world();
10721099
const h = await w.boot();
1073-
w.packageGet.mockResolvedValueOnce(null);
1100+
w.storeFault(kind);
10741101
const res = await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1075-
expect(res.status, JSON.stringify(res.payload)).toBe(200);
1102+
expect(res.status, `${kind}: ${JSON.stringify(res.payload)}`).toBe(200);
10761103
expect(res.payload.data.copyRecord).toEqual({
10771104
written: false, message: expect.stringContaining('the package store did not answer with the new base\'s row'),
10781105
});
1106+
expect(res.payload.data.copyRecord.message).not.toContain('SQLITE_BUSY');
10791107
expect(existsSync(COPY_RECORD())).toBe(false);
10801108
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
10811109
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,7 +5606,10 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
56065606
* or is a later base under the copy's id (the package store's row is gone
56075607
* or carries another `created_at`); `notSeeded` for a copy made without
56085608
* the sample data; `failure` when the record (`recordFile` set), the
5609-
* package store, or the receipt could not be read.
5609+
* package store, or the receipt could not be read. The package store's
5610+
* `get` answers `null` for an ordinary driver error (it logs it and throws
5611+
* only its seam-unreadable refusal), so a passing store error is a 404
5612+
* here, not a 503: the purge fails closed and deletes nothing.
56105613
*/
56115614
private unmanagedCopyPurgeDatasets = async (
56125615
ctx: PluginContext,
@@ -5643,7 +5646,10 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
56435646
* store's row of `baseId` ({@link readUnmanagedCopyInstance}). No
56445647
* `instance` on a runtime with no `package` service (the record's reach is
56455648
* then by id alone — the residual its module docblock states); a
5646-
* `failure`, and so no record, when the store holds no readable row.
5649+
* `failure`, and so no record, when the store holds no readable row —
5650+
* which is also what a passing store error reads as, since the store
5651+
* answers `null` for it. That copy stays unrecorded (`copyRecord` says
5652+
* so); no door records it later (#31425).
56475653
*/
56485654
private unmanagedCopyInstance = async (
56495655
ctx: PluginContext,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@
4949
* condition above stays open there (such a base does not survive a
5050
* restart either);
5151
* - `created_at` is the store's `CURRENT_TIMESTAMP`, so a base uninstalled
52-
* and re-created under the same id within the same second shares it.
52+
* and re-created under the same id within the same second shares it;
53+
* - the store's `get` catches an ordinary driver error (a lock timeout, a
54+
* dropped connection), logs it and answers `null`; only its
55+
* seam-unreadable refusal is thrown. So a passing store error reads as
56+
* "no row": at purge the copy answers 404 (the purge refuses, nothing is
57+
* deleted), and at install no record is written and `copyRecord` says so;
58+
* no door writes that record later (re-recording is #31425). Both fail
59+
* closed. Only the seam refusal reaches the purge as a 503.
5360
*/
5461

5562
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';

0 commit comments

Comments
 (0)