@north-light/api 0.3.738 → 0.3.740

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ import { piAgentDir } from '../../core/runtime/package-health.js';
5
5
  import { basename, dirname, join, relative, resolve } from 'node:path';
6
6
  import { isDeepStrictEqual } from 'node:util';
7
7
  import { BINDING_CATALOG, normalizeGesture, splitGestureExpression, inboxShortcut, resolveKeybindings, } from '../../core/keybindings/index.js';
8
- import { runtimeManifest } from './shared.js';
8
+ import { builtinPiPackageDir } from '../../core/scope.js';
9
9
  import { atomicWriteJson, readJsonIfExists, readJsonOrNull } from '../../core/fs-utils.js';
10
10
  import { resolveBundledPiCliPath } from '../../core/runtime/pi-cli.js';
11
11
  import { crtrVersion } from '../../shared/crtr-version.js';
@@ -272,7 +272,7 @@ const LOCAL_PACKAGE_NAME_ALIASES = {
272
272
  'dev-plugin': [],
273
273
  };
274
274
  function selectedPiPackageDir(name) {
275
- return join(runtimeManifest.runtimeHome(), 'selected', 'dist', 'builtin-pi-packages', name);
275
+ return builtinPiPackageDir(name);
276
276
  }
277
277
  export function buildSetupManifest(resolveBuiltinPackageDir = selectedPiPackageDir) {
278
278
  // Order matters at execution: the marketplace source registers before the
@@ -123,8 +123,9 @@ export const sysSigninLeaf = defineLeaf({
123
123
  ],
124
124
  },
125
125
  run: async (input) => {
126
- // A fresh home has no daemon yet, and the move reads the canvas through it: start it (a no-op when it already serves) before anything else.
127
- await spawnDaemon();
126
+ // A computer with local records needs its daemon (the move reads the canvas through it) and a machine's daemon is the machine role; a fresh computer starts none until it is registered.
127
+ if (readMachineConfig() !== undefined || hasLocalCanvas())
128
+ await spawnDaemon();
128
129
  const oldPackage = oldPackageInstalled();
129
130
  if (oldPackage !== undefined)
130
131
  process.stderr.write(`${oldPackage.message}\n`);
@@ -30,6 +30,12 @@ export declare function renameClashingProfiles(db: CanvasDb, schema: string, tar
30
30
  * different amounts. A staged one whose id the target holds with a different payer, units or usd is renamed `<id>:install-<install id>`; one the target holds unchanged keeps its
31
31
  * id and is skipped by the copy. A second load of the same bundle derives the same id. Runs before the pre-check. */
32
32
  export declare function renameClashingLegacyCharges(db: CanvasDb, schema: string, target: string, installId: string): Promise<number>;
33
+ /** A staged watch whose (watcher, object) the target already holds is already imported (a hosted person's nodes watching shared objects): it and its subscriptions are
34
+ * dropped, the target's rows stand. Only this key is skipped; any other unique conflict still fails the pre-check. Returns the dropped watches and subscriptions. Runs before the pre-check. */
35
+ export declare function dropExistingWatches(db: CanvasDb, schema: string, target: string): Promise<{
36
+ watches: number;
37
+ watch_subscriptions: number;
38
+ }>;
33
39
  /** Fill: a staged profile with no environment gets the source's machine environment (a Mac) or the person's Blaxel environment (hosted, created if
34
40
  * absent). Runs in the copy transaction: it writes target objects. */
35
41
  export declare function fillEnvironments(tx: CanvasDb, schema: string, target: string, source: SourceJson, person: string): Promise<number>;
@@ -112,6 +112,14 @@ export async function renameClashingLegacyCharges(db, schema, target, installId)
112
112
  WHERE s.id LIKE 'legacy:%' AND t.id = s.id AND (s.payer IS DISTINCT FROM t.payer OR s.units IS DISTINCT FROM t.units OR s.usd IS DISTINCT FROM t.usd)`).run(installId);
113
113
  return Number(done.changes);
114
114
  }
115
+ /** A staged watch whose (watcher, object) the target already holds is already imported (a hosted person's nodes watching shared objects): it and its subscriptions are
116
+ * dropped, the target's rows stand. Only this key is skipped; any other unique conflict still fails the pre-check. Returns the dropped watches and subscriptions. Runs before the pre-check. */
117
+ export async function dropExistingWatches(db, schema, target) {
118
+ const same = `FROM ${sq(target, 'watches')} t WHERE t.watcher = s.watcher AND t.object = s.object`;
119
+ const subs = await db.prepareUncached(`DELETE FROM ${sq(schema, 'watch_subscriptions')} u USING ${sq(schema, 'watches')} s WHERE u.watch = s.watch_id AND EXISTS (SELECT 1 ${same})`).run();
120
+ const watches = await db.prepareUncached(`DELETE FROM ${sq(schema, 'watches')} s WHERE EXISTS (SELECT 1 ${same})`).run();
121
+ return { watches: Number(watches.changes), watch_subscriptions: Number(subs.changes) };
122
+ }
115
123
  /** Fill: a staged profile with no environment gets the source's machine environment (a Mac) or the person's Blaxel environment (hosted, created if
116
124
  * absent). Runs in the copy transaction: it writes target objects. */
117
125
  export async function fillEnvironments(tx, schema, target, source, person) {
@@ -237,9 +237,12 @@ export async function ensureRootProfile() {
237
237
  }
238
238
  return await createProfile(ROOT_PROFILE_NAME, [], { grantee: 'app:terminal' });
239
239
  }
240
- const existing = getStoredProfile(ROOT_PROFILE_ID);
240
+ // A computer signed in to an account that already has a root profile (made on another computer, so with another id) uses that one.
241
+ const existing = getStoredProfile(ROOT_PROFILE_ID)
242
+ ?? listProfiles().find((p) => p.manifest.name === ROOT_PROFILE_NAME && profileApp(p.manifest) === 'app:terminal')
243
+ ?? null;
241
244
  if (existing !== null) {
242
- scaffoldProfile(ROOT_PROFILE_ID, existing.manifest);
245
+ scaffoldProfile(existing.profileId, existing.manifest);
243
246
  return existing;
244
247
  }
245
248
  const next = {
@@ -48,3 +48,5 @@ export declare const installIdPath: () => string;
48
48
  export declare function readOrCreateInstallId(): string;
49
49
  /** Whether this computer still holds a local canvas from before crtr was fleet-only (a supervised Postgres or a SQLite file). */
50
50
  export declare const hasLocalCanvas: () => boolean;
51
+ /** A computer with no `machine.json`, no local records and no join registration has not been set up: it has no control plane to reach until `crtr sys signin`. */
52
+ export declare const computerIsUnset: () => boolean;
@@ -7,6 +7,7 @@ import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync }
7
7
  import { randomBytes } from 'node:crypto';
8
8
  import { dirname, join } from 'node:path';
9
9
  import { runtimeRoot, secretsDir } from '../../core/layout.js';
10
+ import { joinedIssuer } from '../../core/directory/join.js';
10
11
  import { CrtrError } from '../../core/errors.js';
11
12
  import { atomicWriteJson } from '../../core/fs-utils.js';
12
13
  import { ExitCode } from '../../types.js';
@@ -75,3 +76,5 @@ export function readOrCreateInstallId() {
75
76
  }
76
77
  /** Whether this computer still holds a local canvas from before crtr was fleet-only (a supervised Postgres or a SQLite file). */
77
78
  export const hasLocalCanvas = () => ['pg', 'canvas.db', 'canvas.db.imported'].some((name) => existsSync(join(runtimeRoot(), name)));
79
+ /** A computer with no `machine.json`, no local records and no join registration has not been set up: it has no control plane to reach until `crtr sys signin`. */
80
+ export const computerIsUnset = () => readMachineConfig() === undefined && !hasLocalCanvas() && joinedIssuer() === null;
@@ -16,7 +16,7 @@ import { hostExecPath } from '../core/runtime/branded-host.js';
16
16
  // reaches openDb) so the CLI daemon front door stays canvas.db-free (plan B-0).
17
17
  import { isDaemonRunning, readPidfile, isPidAlive } from './pidfile.js';
18
18
  import { DAEMON_STARTUP_WINDOW_MS } from './startup-policy.js';
19
- import { readMachineConfig } from './machine-link/config.js';
19
+ import { computerIsUnset, readMachineConfig } from './machine-link/config.js';
20
20
  import { exclusiveLockOwnerPid } from '../core/exclusive-lock.js';
21
21
  import { onDiskMigrationLockPath } from '../core/canvas/paths.js';
22
22
  import { envNoDaemonAutostart } from '../shared/env.js';
@@ -694,6 +694,9 @@ export function ensureDaemon(deps = {}) {
694
694
  const now = deps.now ?? Date.now;
695
695
  if (autostartDisabled())
696
696
  return;
697
+ // A computer with no `machine.json` and no local records is not set up: no command starts a local control plane there (`crtr sys signin` comes first).
698
+ if (!deps.spawnDaemon && computerIsUnset())
699
+ return;
697
700
  if (autostartInFlight)
698
701
  return;
699
702
  if (now() < autostartNextAttemptAt)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/api",
3
- "version": "0.3.738",
3
+ "version": "0.3.740",
4
4
  "description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/api/index.js",