Skip to content

Commit 30eba36

Browse files
committed
fix(cloud-connection,cli): install-local ledger follows OS_HOME, adopting the cwd ledger once
The ledger resolved from the working directory alone, so os dev --fresh and a second instance under another OS_HOME rehydrated an earlier run's installs. With OS_HOME set it now lives at <OS_HOME>/installed-packages; unset, it stays in the project. The cwd ledger is copied once into a new directory (never under a --fresh home), and os doctor reads the same resolver. Claude-Session: https://claude.ai/code/session_01Utp6CY4LWqfjLCYqpZhNCs Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5db77f7 commit 30eba36

11 files changed

Lines changed: 628 additions & 65 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/cloud-connection': patch
3+
'@objectstack/cli': patch
4+
---
5+
6+
fix(cloud-connection,cli): the install-local ledger follows `OS_HOME`, so `os dev --fresh` and a second instance under another `OS_HOME` start without an earlier run's installs
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: none
10+
11+
- **Now.** With no `storageDir`, the install-local ledger (`LocalManifestSource`, and so `MarketplaceInstallLocalPlugin`) lives at `<OS_HOME>/installed-packages` when `OS_HOME` is set, and at `<cwd>/.objectstack/installed-packages` when it is not. Before, it was always the working directory, so `os dev --fresh` (a fresh `OS_HOME` and database) rehydrated a package an earlier run installed, and so did a second instance under another `OS_HOME`.
12+
- **Unchanged.** `os dev` without `OS_HOME` keeps the ledger in the project, beside its database. `os start` in project mode sets `OS_HOME` to `<cwd>/.objectstack`, which is the same directory as before. An explicit `storageDir` is used as given.
13+
- **Existing installs.** When the ledger moved (an `OS_HOME` other than `<cwd>/.objectstack`) and its new directory does not exist yet, the boot copies the `.json` files of the working-directory ledger into it once, and logs a warning naming both directories. The old files are left in place and are not read again; remove them. A copy that fails is logged as an error, and the next boot tries again. Under `os dev --fresh` nothing is copied.
14+
- **`os doctor`** reads the same directory the runtime does, `OS_HOME` included.
15+
- **The widening.** `@objectstack/cloud-connection` exports `resolveInstalledPackagesDir({ env?, cwd? })` (returns `{ dir, legacyDir }`), `INSTALLED_PACKAGES_HOME_SUBDIR`, and `LocalManifestSource.legacyDir` / `adoptLegacyEntries()`. `os doctor` takes the ledger directory from `resolveInstalledPackagesDir`; a `@objectstack/cloud-connection` without it is reported as a reader that does not declare the ledger directory. Align the two packages' versions.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { describe, it, expect } from 'vitest';
4+
import { FRESH_HOME_CHANNEL_ENV as CONSUMER_SPELLING } from '@objectstack/cloud-connection';
5+
import { applyFreshHomeChannel, FRESH_HOME_CHANNEL_ENV } from './dev.js';
6+
7+
/**
8+
* #30217 — `os dev --fresh` tells its `serve` child which `OS_HOME` is the
9+
* run's ephemeral one, so the install ledger there adopts nothing from the
10+
* project's working-directory ledger and the run starts without an earlier
11+
* run's installs. The parent owns the variable: without `--fresh` it is
12+
* removed, so an inherited copy never speaks for this run.
13+
*/
14+
describe('#30217 `os dev` owns the fresh-home channel on its serve child', () => {
15+
it('is spelled exactly as @objectstack/cloud-connection reads it', () => {
16+
expect(FRESH_HOME_CHANNEL_ENV).toBe(CONSUMER_SPELLING);
17+
});
18+
19+
it('names the ephemeral home under --fresh', () => {
20+
const env: NodeJS.ProcessEnv = { OS_HOME: '/tmp/objectstack-dev-abc' };
21+
applyFreshHomeChannel(env, '/tmp/objectstack-dev-abc');
22+
expect(env[FRESH_HOME_CHANNEL_ENV]).toBe('/tmp/objectstack-dev-abc');
23+
});
24+
25+
it('removes an inherited copy when the run is not --fresh', () => {
26+
const env: NodeJS.ProcessEnv = { [FRESH_HOME_CHANNEL_ENV]: '/tmp/objectstack-dev-old' };
27+
applyFreshHomeChannel(env, undefined);
28+
expect(FRESH_HOME_CHANNEL_ENV in env).toBe(false);
29+
});
30+
});

‎packages/cli/src/commands/dev.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,28 @@ export function forwardSeedSettledToParent(msg: unknown): boolean {
251251
return true;
252252
}
253253

254+
/**
255+
* The internal channel `@objectstack/cloud-connection` reads as
256+
* `FRESH_HOME_CHANNEL_ENV` (#30217): the ephemeral `OS_HOME` a `--fresh` run
257+
* created. Spelled here rather than imported because oclif loads every command
258+
* module on every invocation; `dev-fresh-home-channel.test.ts` holds the two
259+
* spellings equal.
260+
*/
261+
export const FRESH_HOME_CHANNEL_ENV = 'OS_INTERNAL_FRESH_HOME';
262+
263+
/**
264+
* Set or clear {@link FRESH_HOME_CHANNEL_ENV} on the serve child's env
265+
* (#30217). Under `--fresh` it names the run's ephemeral `OS_HOME`, so the
266+
* install ledger there adopts nothing from the project's working-directory
267+
* ledger — a fresh run starts without an earlier run's installs. Otherwise it is
268+
* removed: the parent owns the variable, so an inherited copy never speaks for
269+
* a home this run did not create. Not an operator setting.
270+
*/
271+
export function applyFreshHomeChannel(childEnv: NodeJS.ProcessEnv, freshHome: string | undefined): void {
272+
if (freshHome) childEnv[FRESH_HOME_CHANNEL_ENV] = freshHome;
273+
else delete childEnv[FRESH_HOME_CHANNEL_ENV];
274+
}
275+
254276
/**
255277
* Whether this `os dev` boot runs the watch-recompile loop (#20681).
256278
*
@@ -676,6 +698,9 @@ export default class Dev extends Command {
676698
// (OS_HOME → <home>/data/objectstack.db, published as OS_DATABASE_URL),
677699
// - the storage-service uploads root, published on the settings
678700
// service's own env name OS_STORAGE_LOCAL_ROOT (#4968),
701+
// - the install-local ledger, which follows OS_HOME (#30217); the
702+
// child is told this home is ephemeral (applyFreshHomeChannel), so
703+
// it adopts nothing from the project's working-directory ledger,
679704
// - any other state a plugin keys off OS_HOME.
680705
// In one sentence: framework-owned, OS_HOME-keyed state plus the env
681706
// channels this block publishes below. Auto-deleted on exit.
@@ -810,6 +835,7 @@ export default class Dev extends Command {
810835
...(flags['database-auth-token'] ? { OS_DATABASE_AUTH_TOKEN: flags['database-auth-token'] } : {}),
811836
...(flags['auth-secret'] ? { OS_AUTH_SECRET: flags['auth-secret'] } : {}),
812837
};
838+
applyFreshHomeChannel(localEnv, freshHome);
813839
// The data key's file: the ONE answer `os start` takes too (#29934), so a
814840
// secret saved here reads back under `os start` over the same project —
815841
// and the file an earlier `os start` kept in the project is still read.

‎packages/cli/src/commands/doctor-ledger-dir-authority.test.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*
66
* ── What this file pins, honestly ────────────────────────────────────────
77
*
8-
* Not a live defect. `DEFAULT_INSTALLED_PACKAGES_DIR` is an export of
8+
* Not a live defect. The directory authority (`DEFAULT_INSTALLED_PACKAGES_DIR`,
9+
* `resolveInstalledPackagesDir` since #30217) is an export of
910
* `@objectstack/cloud-connection` in every version ever shipped, so the `??`
1011
* fallback this change deletes had never fired. What is being hardened is a
1112
* DIAGNOSIS BOUNDARY — the last cell of the edge #5644 carved. That issue
@@ -114,10 +115,10 @@ describe('installedPackageLedgerDirAuthorityMissingCheck — loaded but unrecogn
114115
expect(check.message).not.toContain('\n');
115116
});
116117

117-
it('names the received value when the export exists but is not a string', () => {
118+
it('names the received value when the export exists but is not a function', () => {
118119
const check = installedPackageLedgerDirAuthorityMissingCheck(42);
119120

120-
expect(check.message).toContain('not a string');
121+
expect(check.message).toContain('not a function');
121122
expect(check.message).toContain('got number: 42');
122123
});
123124

@@ -136,7 +137,7 @@ describe('installedPackageLedgerDirAuthorityMissingCheck — loaded but unrecogn
136137
// it from the #5644 row one cell over. Without both, the reader's first
137138
// move would be to rebuild a package that builds and loads fine.
138139
expect(fix).toContain('IS installed here and it DID load');
139-
expect(fix).toContain('authority on what the ledger directory is called');
140+
expect(fix).toContain('authority on where the ledger directory is');
140141
// And it names the remedy: the two ends of this contract disagree.
141142
expect(fix).toContain('Align their versions');
142143
});
@@ -145,7 +146,7 @@ describe('installedPackageLedgerDirAuthorityMissingCheck — loaded but unrecogn
145146
const check = installedPackageLedgerDirAuthorityMissingCheck(undefined);
146147

147148
// The one name doctor had for the ledger is the export that came back
148-
// non-string. A path in the row — the old `??` guess included — would be
149+
// as no function. A path in the row — the old `??` guess included — would be
149150
// doctor asserting what it cannot know (#5644's option-B rejection, one
150151
// cell over).
151152
expect(check.message).not.toContain('.objectstack');
@@ -182,7 +183,7 @@ describe('installedPackageLedgerSkippedEntriesCheck — names the directory it a
182183
*
183184
* Simulated through the loader seam, like the #5644 cause case next door: the
184185
* mock hands doctor a module that LOADED (`state: 'loaded'`) and carries no
185-
* `DEFAULT_INSTALLED_PACKAGES_DIR`. No preflight (`assertLedgerReaderIsBuilt`)
186+
* `resolveInstalledPackagesDir`. No preflight (`assertLedgerReaderIsBuilt`)
186187
* in this describe, and that is not a rollback of #5612: nothing here reads
187188
* the real package at all — the seam is mocked — so there is no accident for
188189
* a preflight to catch.
@@ -271,7 +272,7 @@ describe('os doctor, end to end, against a reader that declares no ledger direct
271272
it('reports the named row and reads NO directory — guessed or otherwise', async () => {
272273
const ctorCalls: string[] = [];
273274
const fakeModule = {
274-
// Deliberately NO `DEFAULT_INSTALLED_PACKAGES_DIR` — the state under test.
275+
// Deliberately NO `resolveInstalledPackagesDir` — the state under test.
275276
LocalManifestSource: class {
276277
constructor(dir: string) {
277278
ctorCalls.push(dir);
@@ -312,13 +313,13 @@ describe('os doctor, end to end, against a reader that declares no ledger direct
312313
expect(verboseRun.exitCode).toBeUndefined();
313314
}, 120_000);
314315

315-
it('a non-string export is the same fact — never a path.join over it', async () => {
316+
it('a non-function export is the same fact — never a call of it', async () => {
316317
const ctorCalls: string[] = [];
317318
const fakeModule = {
318-
// Present, and not a string: the shape whose `path.join` throw the old
319-
// code mis-reported through the config `catch` (see the function's
320-
// header in doctor.ts).
321-
DEFAULT_INSTALLED_PACKAGES_DIR: 42,
319+
// Present, and not a function: the shape a bare call would throw on, which
320+
// the old `path.join` over the string authority mis-reported through the
321+
// config `catch` (see the function's header in doctor.ts).
322+
resolveInstalledPackagesDir: 42,
322323
LocalManifestSource: class {
323324
constructor(dir: string) {
324325
ctorCalls.push(dir);
@@ -333,7 +334,7 @@ describe('os doctor, end to end, against a reader that declares no ledger direct
333334

334335
expect(ctorCalls).toHaveLength(0);
335336
expect(run.out).toContain(AUTHORITY_HEADLINE);
336-
expect(run.out).toContain('not a string');
337+
expect(run.out).toContain('not a function');
337338
expect(run.out).toContain('got number: 42');
338339
expect(run.out).not.toContain(CLEAN_BILL);
339340
expect(run.out).not.toContain('invoice.code');

‎packages/cli/src/commands/doctor-ledger-read-failure.test.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,10 @@ describe('os doctor, end to end, against an unreadable installed-package ledger'
479479
// installed-packages` here would move the literal into the test and pin
480480
// the row against the guess a second time — the defect, relocated.
481481
// Re-imported rather than read off `LEDGER_REL` for the same reason.
482-
const { DEFAULT_INSTALLED_PACKAGES_DIR } = await import('@objectstack/cloud-connection');
482+
const { resolveInstalledPackagesDir } = await import('@objectstack/cloud-connection');
483483
// `mkdtemp` makes this unique per run, which is what makes "the resolved
484484
// dir, not the literal" assertable at all: no literal can contain it.
485-
const resolved = path.join(tmp, DEFAULT_INSTALLED_PACKAGES_DIR);
485+
const resolved = resolveInstalledPackagesDir({ cwd: tmp }).dir;
486486

487487
const run = await runDoctor(['--verbose']);
488488

@@ -673,6 +673,34 @@ describe('os doctor, end to end, against an unreadable installed-package ledger'
673673
expect(verboseRun.out).toContain('Repair the JSON, or delete the file');
674674
expect(verboseRun.exitCode).toBeUndefined();
675675
}, 60_000);
676+
677+
it('reads the ledger under OS_HOME when OS_HOME is set — the directory the runtime reads (#30217)', async () => {
678+
// The runtime's ledger follows OS_HOME. Doctor reading `<cwd>/.objectstack/
679+
// installed-packages` here would report on a directory the runtime no
680+
// longer reads, and stay silent about the one it does.
681+
writeConfig();
682+
const home = path.join(tmp, 'home');
683+
const homeLedger = path.join(home, 'installed-packages');
684+
fs.mkdirSync(homeLedger, { recursive: true });
685+
fs.writeFileSync(path.join(homeLedger, 'billing.json'), JSON.stringify(globalUniqueEntry('billing')));
686+
// A stale working-directory ledger beside it: not what the runtime reads.
687+
fs.mkdirSync(ledgerPath(), { recursive: true });
688+
fs.writeFileSync(path.join(ledgerPath(), 'stale.json'), '{"manifestId":"stale"');
689+
690+
const savedHome = process.env.OS_HOME;
691+
process.env.OS_HOME = home;
692+
let run: { out: string; exitCode: number | undefined };
693+
try {
694+
run = await runDoctor(['--verbose']);
695+
} finally {
696+
if (savedHome === undefined) delete process.env.OS_HOME;
697+
else process.env.OS_HOME = savedHome;
698+
}
699+
700+
expect(run.out).toContain("installed package 'billing'");
701+
expect(run.out).not.toContain('stale.json');
702+
expect(run.out).not.toContain(SKIPPED_HEADLINE);
703+
}, 60_000);
676704
});
677705

678706
/**

0 commit comments

Comments
 (0)