@ours.network/install 1.2.1-nightly.3 → 1.2.1-nightly.30

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.
Files changed (45) hide show
  1. package/GATEWAY_SETUP.md +178 -0
  2. package/README.md +320 -9
  3. package/ROOTLESS_PODMAN_VALIDATION.md +210 -0
  4. package/assets/Dockerfile +17 -13
  5. package/assets/Dockerfile.gateway +5 -0
  6. package/assets/docker-compose.yaml +1 -1
  7. package/assets/fleet-settings.example.json +48 -0
  8. package/assets/gateway-entrypoint.sh +37 -0
  9. package/assets/release-lock.json +1122 -4471
  10. package/assets/release.json +33 -19
  11. package/assets/scripts/build/build-common.mjs +1 -1
  12. package/assets/scripts/build/build-sdk.mjs +6 -1
  13. package/assets/scripts/maintenance/build-context.mjs +1 -1
  14. package/assets/scripts/maintenance/daemon-owner.mjs +15 -0
  15. package/assets/scripts/maintenance/docker-layout-conversion.mjs +7 -3
  16. package/assets/scripts/maintenance/release-graph.mjs +18 -1
  17. package/assets/scripts/maintenance/state-operation.mjs +4 -2
  18. package/assets/scripts/runtime/client-setup.mjs +23 -5
  19. package/assets/scripts/runtime/diagnostics.mjs +72 -0
  20. package/assets/scripts/runtime/entrypoint.sh +1 -1
  21. package/assets/sources.json +46 -28
  22. package/install.mjs +7 -2
  23. package/lib/build-transition.mjs +1 -0
  24. package/lib/client-cli.mjs +127 -0
  25. package/lib/container-engine.mjs +127 -0
  26. package/lib/diagnostics.mjs +1 -0
  27. package/lib/docker-conversion-runtime.mjs +3 -2
  28. package/lib/docker-layout-installation.mjs +2 -1
  29. package/lib/docker-runtime-repair.mjs +160 -0
  30. package/lib/effects.mjs +278 -130
  31. package/lib/gateway-transition.mjs +87 -0
  32. package/lib/gateway.mjs +142 -0
  33. package/lib/legacy-migration.mjs +13 -2
  34. package/lib/orchestrate.mjs +129 -43
  35. package/lib/plan.mjs +22 -9
  36. package/lib/podman-preflight.mjs +40 -0
  37. package/lib/server-onboarding.mjs +154 -16
  38. package/lib/server-ports.mjs +17 -0
  39. package/lib/setup-options.mjs +88 -27
  40. package/lib/setup.mjs +35 -18
  41. package/lib/target.mjs +24 -8
  42. package/lib/uninstall.mjs +2 -2
  43. package/lib/usage.mjs +19 -6
  44. package/lib/workspace-setup.mjs +83 -0
  45. package/package.json +4 -2
package/lib/effects.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ import { selectServerPorts } from './server-ports.mjs';
2
+ import { qualifyPodman } from './podman-preflight.mjs';
3
+ import { engineName, bindPodman, runContainer, nativeBuildCommands } from './container-engine.mjs';
1
4
  // ours-install v3 — the real side effects.
2
5
  //
3
6
  // Every mutation the installer can perform lives here and nowhere else, behind
@@ -11,21 +14,25 @@
11
14
  // state-directory guard and the enable/reload. The installer never touches a
12
15
  // unit file or the service manager directly.
13
16
 
17
+ import { publishClientCli } from './client-cli.mjs';
18
+ import { commandFailure } from './diagnostics.mjs';
14
19
  import { spawnSync, execFileSync } from 'node:child_process';
15
20
  import { chmodSync, closeSync, constants, cpSync, existsSync, fstatSync, lstatSync, openSync, readFileSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
16
21
  import { homedir, userInfo, platform as osPlatform, release as osRelease, arch as osArch } from 'node:os';
17
22
  import { fileURLToPath } from 'node:url';
18
23
  import { randomUUID, createHash } from 'node:crypto';
19
24
  import { dirname, join, resolve } from 'node:path';
20
- import { maintenanceServices, installationPaths, validateInstallation, consumerServiceState, unitNameForStateDir, launchdLabelForStateDir, messengerServicePlan, selectSourcePackages, resolveSourcePolicy, SERVER_SERVICES } from './plan.mjs';
21
- import { validateHostProfile } from './target.mjs';
25
+ import { clientPackageNames, maintenanceServices, installationPaths, validateInstallation, consumerServiceState, unitNameForStateDir, launchdLabelForStateDir, messengerServicePlan, selectSourcePackages, resolveSourcePolicy, SERVER_SERVICES } from './plan.mjs';
26
+ import { validateHostProfile, validateGatewayClientProfile } from './target.mjs';
27
+ import { serverBase, validateGatewayDiscovery, gatewayCompose, gatewayNginx, gatewayAddress } from './gateway.mjs';
22
28
  import { createServerOnboarding } from './server-onboarding.mjs';
23
29
  import { atomicWriteConfig, snapshotConfig, restoreConfig } from './config.mjs';
24
30
  import { select as selectOnTty, multiselect as multiselectOnTty, askLine as askLineOnTty } from './prompt.mjs';
25
31
  import { classifyHarnessProbe } from './logic.mjs';
32
+ import { qualifyDockerRuntime, refreshDockerPolicyCopy } from './docker-runtime-repair.mjs';
26
33
  import { classifyStateDir } from './detect.mjs';
27
34
  import { BASE_RECORDS, CONTEXT, readBuildRecords, equalBuildRecords, initializeBuildMarker } from '../assets/scripts/maintenance/build-context.mjs';
28
- import { releaseBinding, verifyReleaseGraph, verifyRuntimeRelease } from '../assets/scripts/maintenance/release-graph.mjs';
35
+ import { hostCliPolicy, releaseBinding, verifyReleaseGraph, verifyRuntimeRelease } from '../assets/scripts/maintenance/release-graph.mjs';
29
36
 
30
37
  /** GET http://127.0.0.1:<port>/state-dir — the unauthenticated identity probe. */
31
38
  async function probePort(port, { timeoutMs = 1500 } = {}) {
@@ -399,7 +406,7 @@ export function realEffects({ write, ttyFd, env = process.env, home = homedir(),
399
406
  // invocation only and never to the installer's own process: a state
400
407
  // directory selected by one run must not leak into anything the operator
401
408
  // starts afterwards.
402
- run: async (cmd, args, { env: extraEnv = null, stream = false, cwd, sensitive = false, allowCodes = [] } = {}) => {
409
+ run: async (cmd, args, { env: extraEnv = null, stream = false, cwd, sensitive = false, allowCodes = [], timeout } = {}) => {
403
410
  // Always built from this layer's OWN env rather than left to spawnSync's
404
411
  // implicit inheritance, so what a child receives is a property of the
405
412
  // effects object a caller constructed and not of whatever ambient shell
@@ -410,18 +417,18 @@ export function realEffects({ write, ttyFd, env = process.env, home = homedir(),
410
417
  const executable = cmd === 'npm' ? npmBin : cmd;
411
418
  const r = spawnSync(executable, args, {
412
419
  cwd,
420
+ timeout,
413
421
  encoding: 'utf8',
414
422
  stdio: [...(stream ? ['ignore', 'inherit', 'inherit'] : ['ignore', 'pipe', 'pipe']), ...(installationLockFd === null ? [] : [installationLockFd])],
415
423
  env: childEnv,
416
424
  });
417
425
  if (r.error) {
418
- const error = new Error(`${executable} could not start (${r.error.code ?? 'launch error'})`, { cause: r.error });
426
+ const error = commandFailure(executable, args, r, { sensitive, cwd, env: childEnv });
419
427
  error.code = r.error.code;
420
428
  throw error;
421
429
  }
422
430
  if (r.status !== 0 && !allowCodes.includes(r.status)) {
423
- const detail = sensitive ? '' : (r.stderr || r.stdout || '').trim().split('\n').slice(-3).join('; ');
424
- throw new Error(`${executable} ${args.join(' ')} exited ${r.status}${detail ? `: ${detail}` : ''}`);
431
+ throw commandFailure(executable, args, r, { sensitive, cwd, env: childEnv });
425
432
  }
426
433
  return { ok: true, code: r.status, stdout: r.stdout ?? '' };
427
434
  },
@@ -435,7 +442,8 @@ export function realEffects({ write, ttyFd, env = process.env, home = homedir(),
435
442
  delete childEnv.OURS_INSTALLER_LOCK_FD;
436
443
  if (installationLockFd !== null) childEnv.OURS_INSTALLER_LOCK_FD = '3';
437
444
  const r = spawnSync(cmd, args, { stdio: ['inherit', 'inherit', 'inherit', ...(installationLockFd === null ? [] : [installationLockFd])], env: childEnv });
438
- return { ok: !r.error && r.status === 0, code: r.status ?? -1 };
445
+ if (r.error || r.status !== 0) throw commandFailure(cmd, args, r, { env: childEnv });
446
+ return { ok: true, code: r.status };
439
447
  },
440
448
  installedVersion: (pkg) => installedVersionOf(pkg, npmBin),
441
449
  packageDependencies: (spec) => packageDependenciesOf(spec, npmBin),
@@ -534,6 +542,7 @@ export function networkEffects(effects) {
534
542
  const baseEnv = (record) => ({
535
543
  OURS_DAEMON_ID: record.instanceId,
536
544
  OURS_IMAGE: `${record.project}:runtime`,
545
+ OURS_GATEWAY_IMAGE: `${record.project}:gateway`,
537
546
  OURS_MAINTENANCE_IMAGE: `${record.project}:maintenance`,
538
547
  OURS_UID: String(record.uid ?? 1000), OURS_GID: String(record.gid ?? 1000),
539
548
  OURS_HOST_PORT: String(record.port ?? 3050),
@@ -545,14 +554,55 @@ export function networkEffects(effects) {
545
554
  'compose', '--project-directory', record.workDir, '--file', join(record.workDir,
546
555
  record.schema === 1 && existsSync(join(record.workDir, 'docker-compose.legacy.yaml'))
547
556
  ? 'docker-compose.legacy.yaml' : 'docker-compose.yaml'),
557
+ ...(record.gateway ? ['--file', join(record.workDir, 'docker-compose.gateway.yaml')] : []),
558
+ ...(engineName(record) === 'podman' ? ['--file', join(record.workDir, 'compose.podman.json')] : []),
548
559
  '--project-name', record.project, ...args,
549
560
  ];
550
- const compose = (record, args, options = {}) => effects.run('docker', composeArgs(record, args),
551
- { ...options, env: { ...baseEnv(record), ...options.env } });
561
+ const container = (record, args, options = {}) => runContainer(effects, record, args, options);
562
+ const compose = async (record, args, options = {}) => {
563
+ const selected = { ...options, env: { ...baseEnv(record), ...options.env } };
564
+ if (engineName(record) === 'podman' && args[0] === 'build') {
565
+ const result = await container(record, composeArgs(record, ['--profile', '*', 'config', '--format', 'json']), { env: selected.env });
566
+ for (const command of nativeBuildCommands(JSON.parse(result.stdout), args.slice(1), { ...env, ...selected.env })) await container(record, command, selected);
567
+ return { code: 0, stdout: '' };
568
+ }
569
+ if (engineName(record) === 'podman' && ['run', 'up'].includes(args[0])) {
570
+ const resolved = JSON.parse((await container(record, composeArgs(record, ['--profile', '*', 'config', '--format', 'json']), { env: selected.env })).stdout);
571
+ for (const [key, volume] of Object.entries(resolved.volumes ?? {})) {
572
+ if (volume.external) throw new Error('Unexpected external installation volume');
573
+ const name = volume.name ?? `${record.project}_${key}`;
574
+ const found = await container(record, ['volume', 'inspect', '--format', '{{json .}}', name], { allowCodes: [1] });
575
+ if (found.code === 1) continue;
576
+ const value = JSON.parse(found.stdout);
577
+ if (value.Name !== name || value.Labels?.['com.docker.compose.project'] !== record.project || value.Labels?.['com.docker.compose.volume'] !== key) throw new Error(`Refusing foreign volume: ${name}`);
578
+ }
579
+ }
580
+ // Compose run can implicitly build an absent administration image. Build
581
+ // its resolved target natively first, just like explicit build operations.
582
+ if (engineName(record) === 'podman' && args[0] === 'run') {
583
+ const resolved = JSON.parse((await container(record, composeArgs(record, ['--profile', '*', 'config', '--format', 'json']), { env: selected.env })).stdout);
584
+ const takesValue = new Set(['--name', '--entrypoint', '--env', '-e', '--user', '-u', '--workdir', '-w', '--volume', '-v', '--publish', '-p', '--label', '-l', '--pull']);
585
+ let name;
586
+ for (let i = 1; i < args.length; i++) {
587
+ if (takesValue.has(args[i])) { i++; continue; }
588
+ if (args[i].startsWith('-')) continue;
589
+ name = args[i]; break;
590
+ }
591
+ const service = resolved.services?.[name];
592
+ if (!service) throw new Error(`Unresolved Podman administration service: ${name}`);
593
+ if (service.build) {
594
+ const image = await container(record, ['image', 'inspect', service.image], { allowCodes: [1] });
595
+ if (image.code === 1) for (const command of nativeBuildCommands(resolved, [name], { ...env, ...selected.env })) await container(record, command, selected);
596
+ }
597
+ }
598
+ return container(record, composeArgs(record, args), selected);
599
+ };
552
600
  const dockerStartupError = async (record, service, cause) => {
553
601
  const args = ['logs', '--no-color', '--tail', '50', '--timestamps', service];
554
602
  const quote = value => `'${String(value).replaceAll("'", "'\\''")}'`;
555
- const command = `OURS_DAEMON_ID=${quote(record.instanceId)} docker ${composeArgs(record, args).map(quote).join(' ')}`;
603
+ const engine = engineName(record);
604
+ const prefix = engine === 'podman' ? ['--remote', '--url', `unix://${record.containerBinding.socket}`] : [];
605
+ const command = `OURS_DAEMON_ID=${quote(record.instanceId)} ${engine} ${[...prefix, ...composeArgs(record, args)].map(quote).join(' ')}`;
556
606
  let detail;
557
607
  try {
558
608
  const logs = await compose(record, args);
@@ -563,7 +613,7 @@ export function networkEffects(effects) {
563
613
  } catch {
564
614
  detail = 'Container logs could not be read.';
565
615
  }
566
- return new Error(`Docker service "${service}" failed to start or become healthy.\n${detail}\nStartup error: ${cause.message}\nInspect logs: ${command}`, { cause });
616
+ return new Error(`${engine === 'podman' ? 'Podman' : 'Docker'} service "${service}" failed to start or become healthy.\n${detail}\nStartup error: ${cause.message}\nInspect logs: ${command}`, { cause });
567
617
  };
568
618
  const bin = (record, name) => join(record.workDir, 'node_modules', '.bin', name);
569
619
  const localEnv = (record, service = 'daemon') => {
@@ -577,14 +627,15 @@ export function networkEffects(effects) {
577
627
  return { ...common, OURS_MESSENGER_STATE_DIR: state, ...(record.messengerIdentity ? { OURS_MESSENGER_IDENTITY: record.messengerIdentity } : {}), OURS_MESSENGER_PORT: String(record.messengerPort), OURS_MESSENGER_HOST: '127.0.0.1', OURS_MESSENGER_PUBLIC_ORIGIN: `http://127.0.0.1:${record.messengerPort}` };
578
628
  };
579
629
  const ownerCommand = (record, service, op, options = {}) => {
580
- const name = { daemon: 'ours', telegram: 'ours-tg-connector', cowork: 'ours-cowork' }[service];
581
- const args = service === 'daemon' ? ['daemon', op, ...(['install-service', 'uninstall-service'].includes(op) ? ['--yes'] : []), '--config', record.configPath, '--state-dir', installationPaths(record).daemon, '--json'] : [op];
630
+ const daemonBinary = existsSync(bin(record, 'ours-daemon')) ? 'ours-daemon' : 'ours';
631
+ const name = { daemon: daemonBinary, telegram: 'ours-tg-connector', cowork: 'ours-cowork' }[service];
632
+ const args = service === 'daemon' ? [...(daemonBinary === 'ours' ? ['daemon'] : []), op, ...(['install-service', 'uninstall-service'].includes(op) ? ['--yes'] : []), '--config', record.configPath, '--state-dir', installationPaths(record).daemon, '--json'] : [op];
582
633
  return effects.run(bin(record, name), args, { ...options, env: localEnv(record, service) });
583
634
  };
584
635
  const requireCleanContainerExit = async (record, selected) => {
585
636
  const containers = await compose(record, ['ps', '-aq', ...selected]);
586
637
  for (const id of containers.stdout.split(/\s+/).filter(Boolean)) {
587
- const result = await effects.run('docker', ['inspect', '--format', '{{json .State}}', id]);
638
+ const result = await container(record, ['inspect', '--format', '{{json .State}}', id]);
588
639
  const state = JSON.parse(result.stdout);
589
640
  if (state.Status !== 'exited' || state.ExitCode !== 0 || state.OOMKilled !== false || state.Dead !== false) {
590
641
  throw new Error('Selected source container did not stop cleanly; state operation refused');
@@ -618,7 +669,7 @@ export function networkEffects(effects) {
618
669
  return version;
619
670
  });
620
671
  },
621
- newInstallation(root, mode) {
672
+ newInstallation(root, mode, { containerEngine } = {}) {
622
673
  if (existsSync(root)) {
623
674
  privateDirectory(root);
624
675
  if (readdirSync(root).some(name => name !== '.operation.lock')) throw new Error('Installation root is not empty and has no selection record');
@@ -626,9 +677,10 @@ export function networkEffects(effects) {
626
677
  const instanceId = env.OURS_DAEMON_ID || randomUUID();
627
678
  if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(instanceId)) throw new Error('OURS_DAEMON_ID must be a lowercase UUID');
628
679
  const project = `ours-${createHash('sha256').update(root).digest('hex').slice(0, 16)}`;
629
- return { schema: 2, root, mode, instanceId, project, workDir: join(root, 'runtime'), configPath: installationPaths({ schema: 2, root }).config, sourcesPath: join(root, 'sources.json'), services: [...SERVER_SERVICES], port: 3050, coworkPort: 3052, messengerPort: 8420, messengerIdentity: env.OURS_MESSENGER_IDENTITY || null, uid: 1000, gid: 1000 };
680
+ return { schema: 2, root, mode, ...(containerEngine ? { containerEngine } : {}), instanceId, project, workDir: join(root, 'runtime'), configPath: installationPaths({ schema: 2, root }).config, sourcesPath: join(root, 'sources.json'), services: [...SERVER_SERVICES, ...(mode === 'docker' ? ['gateway'] : [])], ...(mode === 'docker' ? { gateway: { version: 1 } } : {}), port: 3050, coworkPort: 3052, messengerPort: 8420, messengerIdentity: env.OURS_MESSENGER_IDENTITY || null, uid: 1000, gid: 1000 };
630
681
  },
631
- async serverPreflight(record, operation, { existing, sourcePath = record.sourcesPath, sourceManifest, identityName } = {}) {
682
+ async serverPreflight(record, operation, { existing, sourcePath = record.sourcesPath, sourceManifest, identityName, explicitPorts } = {}) {
683
+ if (!existing && operation === 'install' && Number.isInteger(record.port)) selectServerPorts(record, portTakenSync, explicitPorts, effects.out);
632
684
  if (existing) {
633
685
  privateDirectory(record.root);
634
686
  assertPrivateRegularFile(join(record.root, 'installation.json'), 'selection');
@@ -636,37 +688,55 @@ export function networkEffects(effects) {
636
688
  if (env.OURS_DAEMON_ID && env.OURS_DAEMON_ID !== record.instanceId) throw new Error('Conflicting instance ID');
637
689
  }
638
690
  if (record.mode === 'docker') {
639
- const nativeRoot = existing ? '/path/to/new-empty-directory' : record.root;
640
- const quotedRoot = `'${String(nativeRoot).replaceAll("'", "'\\''")}'`;
641
- const quotedName = `'${String(identityName ?? record.messengerIdentity ?? 'Your Name').replaceAll("'", "'\\''")}'`;
642
- const recovery = [
643
- 'Please install Docker Desktop on macOS/Windows, or Docker Engine with the Compose plugin on Linux, and start Docker before retrying.',
644
- 'Docker is recommended for macOS and Windows.',
645
- `Alternatively, use native installation: ours-install server install --mode packages --state-dir ${quotedRoot} --identity-name ${quotedName}`,
646
- 'Native mode requires systemd user services on Linux/WSL or a launchd GUI session on macOS.',
647
- ...(existing ? ['Keep this existing Docker installation in Docker mode; use a separate empty directory for a new native installation.'] : []),
648
- ].join('\n');
649
- try {
650
- await effects.run('docker', ['info', '--format', '{{.ServerVersion}}']);
651
- } catch (cause) {
652
- const problem = cause.code === 'ENOENT'
653
- ? 'Docker command was not found in PATH.'
654
- : `Docker Engine is not reachable. Start Docker and check that your user can access it.\nDetails: ${cause.message}`;
655
- throw new Error(`${problem}\n${recovery}`, { cause });
656
- }
657
- let version;
658
- try {
659
- version = await effects.run('docker', ['compose', 'version', '--short']);
660
- } catch (cause) {
661
- throw new Error(`Docker Compose 2.35 or newer is required, but the Compose plugin could not run. Update Docker Desktop or install the Docker Compose plugin.\n${recovery}`, { cause });
662
- }
663
- const match = /^v?(\d+)\.(\d+)/.exec(version.stdout.trim());
664
- if (!match || Number(match[1]) < 2 || (Number(match[1]) === 2 && Number(match[2]) < 35)) {
665
- throw new Error(`Docker Compose 2.35 or newer is required. Update Docker Desktop or the Docker Compose plugin.\n${recovery}`);
691
+ if (engineName(record) === 'podman') {
692
+ await bindPodman(effects, record);
693
+ if (['install', 'start', 'restart', 'update', 'rebuild', 'gateway-enable'].includes(operation)) {
694
+ const userHome = userInfo().homedir;
695
+ for (const [key, expected] of Object.entries({ HOME: userHome, XDG_CONFIG_HOME: join(userHome, '.config'), XDG_DATA_HOME: join(userHome, '.local/share'), XDG_RUNTIME_DIR: `/run/user/${process.getuid()}` })) {
696
+ if (env[key] && env[key] !== expected) throw new Error(`${key} selects a custom Podman environment that the standard boot service does not retain`);
697
+ }
698
+ if (record.containerBinding.socket !== `/run/user/${process.getuid()}/podman/podman.sock`) throw new Error('The standard user boot service requires the default local Podman socket');
699
+ await effects.run('systemctl', ['--user', 'is-enabled', 'podman.socket']);
700
+ await effects.run('systemctl', ['--user', 'is-enabled', 'podman-restart.service']);
701
+ const restart = await effects.run('systemctl', ['--user', 'show', 'podman-restart.service', '--property=ExecStart', '--value']);
702
+ if (!/--filter[ =]+should-start-on-boot=true(?:[ ;}]|$)/.test(restart.stdout)) throw new Error('Installed podman-restart.service cannot recover unless-stopped workloads; use a Podman release with should-start-on-boot support');
703
+ const linger = await effects.run('loginctl', ['show-user', String(process.getuid()), '--property=Linger', '--value']);
704
+ if (linger.stdout.trim() !== 'yes') throw new Error('Rootless server requires user linger for logout/reboot recovery; ask your administrator to enable it');
705
+ await qualifyPodman(effects, record);
706
+ }
707
+ } else {
708
+ const nativeRoot = existing ? '/path/to/new-empty-directory' : record.root;
709
+ const quotedRoot = `'${String(nativeRoot).replaceAll("'", "'\\''")}'`;
710
+ const quotedName = `'${String(identityName ?? record.messengerIdentity ?? 'Your Name').replaceAll("'", "'\\''")}'`;
711
+ const recovery = [
712
+ 'Please install Docker Desktop on macOS/Windows, or Docker Engine with the Compose plugin on Linux, and start Docker before retrying.',
713
+ 'Docker is recommended for macOS and Windows.',
714
+ `Alternatively, use native installation: ours-install server install --mode packages --state-dir ${quotedRoot} --identity-name ${quotedName}`,
715
+ 'Native mode requires systemd user services on Linux/WSL or a launchd GUI session on macOS.',
716
+ ...(existing ? ['Keep this existing Docker installation in Docker mode; use a separate empty directory for a new native installation.'] : []),
717
+ ].join('\n');
718
+ try {
719
+ await container(record, ['info', '--format', '{{.ServerVersion}}']);
720
+ } catch (cause) {
721
+ const problem = cause.code === 'ENOENT'
722
+ ? 'Docker command was not found in PATH.'
723
+ : `Docker Engine is not reachable. Start Docker and check that your user can access it.\nDetails: ${cause.message}`;
724
+ throw new Error(`${problem}\n${recovery}`, { cause });
725
+ }
726
+ let version;
727
+ try {
728
+ version = await container(record, ['compose', 'version', '--short']);
729
+ } catch (cause) {
730
+ throw new Error(`Docker Compose 2.35 or newer is required, but the Compose plugin could not run. Update Docker Desktop or install the Docker Compose plugin.\n${recovery}`, { cause });
731
+ }
732
+ const match = /^v?(\d+)\.(\d+)/.exec(version.stdout.trim());
733
+ if (!match || Number(match[1]) < 2 || (Number(match[1]) === 2 && Number(match[2]) < 35)) {
734
+ throw new Error(`Docker Compose 2.35 or newer is required. Update Docker Desktop or the Docker Compose plugin.\n${recovery}`);
735
+ }
666
736
  }
667
737
  if (operation !== 'status') {
668
738
  // Compose clients can disappear while their Engine-owned command continues.
669
- const active = await effects.run('docker', ['ps', '--filter', `label=com.docker.compose.project=${record.project}`, '--filter', 'label=com.docker.compose.oneoff=True', '--format', '{{.ID}}']);
739
+ const active = await container(record, ['ps', '--filter', `label=com.docker.compose.project=${record.project}`, '--filter', 'label=com.docker.compose.oneoff=True', '--format', '{{.ID}}']);
670
740
  if (active.stdout.trim()) throw new Error('Another server installation operation is still running in Docker');
671
741
  }
672
742
  } else {
@@ -704,6 +774,55 @@ export function networkEffects(effects) {
704
774
  else if (!readFileSync(record.sourcesPath).equals(bytes)) throw new Error('Migration source selection changed');
705
775
  if (!retainConfig) writePrivateNew(record.configPath, JSON.stringify({ stateDir: record.mode === 'docker' ? '/var/lib/ours' : installationPaths(record).daemon, port: record.port, apiVisibility: 'owner' }, null, 2) + '\n');
706
776
  },
777
+ async qualifyDockerRuntime(record) { return qualifyDockerRuntime(record, effects); },
778
+ async prepareGateway(record) {
779
+ atomicWriteConfig(join(record.workDir, 'docker-compose.gateway.yaml'), gatewayCompose(record));
780
+ atomicWriteConfig(join(record.workDir, 'nginx.conf'), gatewayNginx(record));
781
+ atomicWriteConfig(join(record.workDir, 'gateway-entrypoint.sh'), readFileSync(join(INSTALLER_ASSETS, 'gateway-entrypoint.sh'), 'utf8'));
782
+ atomicWriteConfig(join(record.workDir, 'Dockerfile.gateway'), readFileSync(join(INSTALLER_ASSETS, 'Dockerfile.gateway'), 'utf8'));
783
+ await compose(record, ['build', 'gateway'], { stream: true, env: { BUILDKIT_PROGRESS: 'plain' } });
784
+ },
785
+ async removeGatewayContainer(record) { await compose(record, ['rm', '-f', 'gateway']); },
786
+ async verifyGateway(record) {
787
+ const { prefix } = gatewayAddress(record);
788
+ const script = `const fs=require('node:fs');let stage='selection';(async()=>{
789
+ const base=${JSON.stringify('http://gateway:8080' + prefix)};
790
+ const get=async(path,options={})=>{const r=await fetch(base+path,{...options,redirect:'error',signal:AbortSignal.timeout(5000)});if(!r.ok)throw Error('Gateway readiness HTTP '+r.status);return r.json();};
791
+ const selection=await get('/daemon/selection');
792
+ if(selection.instanceId!==${JSON.stringify(record.instanceId)})throw Error('Gateway daemon instance mismatch');
793
+ stage='unauthenticated-management';
794
+ const denied=await fetch(base+'/cowork/management/rpc',{method:'POST',redirect:'error',signal:AbortSignal.timeout(5000),headers:{'content-type':'application/json'},body:'{}'});
795
+ await denied.body?.cancel();if(denied.status!==401)throw Error('Gateway management does not reject unauthenticated requests');
796
+ stage='credential';
797
+ const token=fs.readFileSync('/var/lib/ours/daemon-token','utf8').trim();
798
+ const headers={'x-ours-api-token':token,'content-type':'application/json'};
799
+ stage='authenticated-daemon';
800
+ const identities=await get('/daemon/identities',{headers});
801
+ if(!Array.isArray(identities.identities))throw Error('Gateway daemon API invalid');
802
+ stage='authenticated-management';
803
+ const result=await get('/cowork/management/rpc',{method:'POST',headers,body:JSON.stringify({version:1,id:'installer-readiness',method:'room.list',params:{}})});
804
+ if(result.version!==1||result.id!=='installer-readiness'||!Array.isArray(result.result)||result.error)throw Error('Gateway Cowork management unavailable');
805
+ })().catch(error=>{const code=error.cause?.code;process.stdout.write('gateway-readiness:'+stage+':'+(['EAI_AGAIN','ENOTFOUND','ECONNREFUSED','ETIMEDOUT'].includes(code)?code:'CHECK_FAILED'));process.exitCode=1;});`;
806
+ const result = await compose(record, ['exec', '-T', 'daemon', 'node', '-e', script], { sensitive: true, allowCodes: [1] });
807
+ if (result.code === 1) {
808
+ const match = /^gateway-readiness:(selection|unauthenticated-management|credential|authenticated-daemon|authenticated-management):(EAI_AGAIN|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|CHECK_FAILED)$/.exec(result.stdout.trim());
809
+ const detail = match ? ` (${match[1]}, ${match[2]})` : '';
810
+ throw new Error(`Gateway readiness failed${detail}. Keep installation state intact; check the managed gateway startup and network connection, then retry setup.`);
811
+ }
812
+ },
813
+ async qualifyGatewayRuntime(record) {
814
+ for (const [name, args, capability] of [
815
+ ['cowork', ['--json', 'capabilities'], 'cowork.http-management-v1'],
816
+ ['messenger-server', ['capabilities'], 'messenger.gateway-prefix-v1'],
817
+ ['tg-connector', ['capabilities'], 'telegram.gateway-listener-v1'],
818
+ ]) {
819
+ const probe = await container(record, ['run', '--rm', '--network', 'none', '--read-only', '--entrypoint', 'node', `${record.project}:runtime`, `/opt/ours/node_modules/@ours.network/${name}/dist/cli.js`, ...args]);
820
+ let value; try { value = JSON.parse(probe.stdout); } catch {}
821
+ const capabilities = value?.result?.capabilities ?? value?.capabilities;
822
+ if (!Array.isArray(capabilities) || !capabilities.includes(capability))
823
+ throw new Error(`Selected ${name} artifact lacks ${capability}; select a compatible release before enabling the gateway`);
824
+ }
825
+ },
707
826
  async prepareInstallation(record, { runtimeOnly = false } = {}) {
708
827
  let copied = false;
709
828
  if (!existsSync(record.workDir)) {
@@ -717,11 +836,24 @@ export function networkEffects(effects) {
717
836
  if (!existsSync(materialized)) writePrivateNew(materialized, retained);
718
837
  else if (!readFileSync(materialized).equals(retained)) throw new Error('Materialized sources differ from retained selection');
719
838
  if (record.mode === 'docker') {
839
+ if (engineName(record) === 'podman') atomicWriteConfig(join(record.workDir, 'compose.podman.json'), JSON.stringify({ services: Object.fromEntries(record.services.map(name => [name, { restart: 'unless-stopped' }])) }));
840
+ const rebuildPrivateScripts = refreshDockerPolicyCopy(record);
841
+ if (record.gateway) {
842
+ atomicWriteConfig(join(record.workDir, 'docker-compose.gateway.yaml'), gatewayCompose(record));
843
+ atomicWriteConfig(join(record.workDir, 'nginx.conf'), gatewayNginx(record));
844
+ for (const file of ['Dockerfile.gateway', 'gateway-entrypoint.sh']) atomicWriteConfig(join(record.workDir, file), readFileSync(join(INSTALLER_ASSETS, file), 'utf8'));
845
+ }
720
846
  // The installer owns these dependencies in both installation modes.
721
847
  const { dependencies } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
722
848
  writeFileSync(join(record.workDir, 'scripts/maintenance/package.json'), JSON.stringify({ private: true, type: 'module', dependencies }, null, 2) + '\n', { mode: 0o600 });
723
- const image = await effects.run('docker', ['image', 'inspect', `${record.project}:runtime`], { allowCodes: [1] });
724
- if (image.code !== 0) await compose(record, ['build', 'daemon'], { stream: true, env: { BUILDKIT_PROGRESS: 'plain' } });
849
+ const image = await container(record, ['image', 'inspect', `${record.project}:runtime`], { allowCodes: [1] });
850
+ if (image.code !== 0 || rebuildPrivateScripts) await compose(record, ['build', 'daemon'], { stream: true, env: { BUILDKIT_PROGRESS: 'plain' } });
851
+ await effects.qualifyDockerRuntime(record);
852
+ if (rebuildPrivateScripts) rmSync(join(record.workDir, '.script-permissions-rebuild'));
853
+ if (record.gateway) {
854
+ await effects.qualifyGatewayRuntime(record);
855
+ await compose(record, ['build', 'gateway'], { stream: true, env: { BUILDKIT_PROGRESS: 'plain' } });
856
+ }
725
857
  if (runtimeOnly) return;
726
858
  await compose(record, ['run', '--rm', '--no-deps', '-T', 'prepare', 'prepare']);
727
859
  } else {
@@ -744,7 +876,6 @@ export function networkEffects(effects) {
744
876
  const profilePath = join(installationPaths(record).mcp, 'profile.json');
745
877
  if (!existsSync(profilePath)) writePrivateNew(profilePath, JSON.stringify({ endpoint: `http://127.0.0.1:${record.port}`, expectedInstanceId: record.instanceId, credentialPath: join(installationPaths(record).daemon, 'daemon-token') }));
746
878
  const config = JSON.parse(readFileSync(record.configPath, 'utf8'));
747
- config.networkMcp ??= { profile: { endpoint: `http://127.0.0.1:${record.port}`, expectedInstanceId: record.instanceId, credentialPath: join(installationPaths(record).daemon, 'daemon-token') }, applicationConfigPath: join(installationPaths(record).mcp, 'config.json') };
748
879
  effects.writeJson(record.configPath, JSON.stringify(config, null, 2) + '\n');
749
880
  const cowork = join(installationPaths(record).cowork, 'config.json');
750
881
  if (!existsSync(cowork)) writePrivateNew(cowork, JSON.stringify({ version: 1, stateDir: installationPaths(record).cowork, rest: { enabled: true, host: '127.0.0.1', port: record.coworkPort } }));
@@ -792,17 +923,17 @@ export function networkEffects(effects) {
792
923
  },
793
924
  async copyDockerBuildRecords(record, directory) {
794
925
  // Inspect immutable image metadata; never reinterpret a failed context copy as legacy.
795
- const label = (await effects.run('docker', ['image', 'inspect', '--format', '{{ index .Config.Labels "network.ours.build-context" }}', `${record.project}:runtime`])).stdout.trim();
926
+ const label = (await container(record, ['image', 'inspect', '--format', '{{ index .Config.Labels "network.ours.build-context" }}', `${record.project}:runtime`])).stdout.trim();
796
927
  if (!['', '<no value>', '1'].includes(label)) throw new Error('Unsupported image build-context schema');
797
928
  const names = [...BASE_RECORDS, ...(label === '1' ? [CONTEXT] : [])];
798
929
  const stage = mkdtempSync(join(directory, '.records-'));
799
930
  const name = `${record.project}-records`;
800
931
  let created = false;
801
932
  try {
802
- await effects.run('docker', ['create', '--name', name, '--entrypoint', '/bin/true', `${record.project}:runtime`]);
933
+ await container(record, ['create', '--name', name, '--entrypoint', '/bin/true', `${record.project}:runtime`]);
803
934
  created = true;
804
935
  for (const file of names) {
805
- await effects.run('docker', ['cp', `${name}:/opt/ours/${file}`, join(stage, file)]);
936
+ await container(record, ['cp', `${name}:/opt/ours/${file}`, join(stage, file)]);
806
937
  const st = lstatSync(join(stage, file));
807
938
  if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o7002)) throw new Error('Unsafe copied build record');
808
939
  chmodSync(join(stage, file), 0o600);
@@ -812,7 +943,7 @@ export function networkEffects(effects) {
812
943
  for (const file of names) renameSync(join(stage, file), join(directory, file));
813
944
  if (label !== '1' && existsSync(join(directory, CONTEXT))) throw new Error('Legacy image conflicts with retained build context');
814
945
  } finally {
815
- try { if (created) await effects.run('docker', ['rm', name]); }
946
+ try { if (created) await container(record, ['rm', name]); }
816
947
  finally { rmSync(stage, { recursive: true, force: true }); }
817
948
  }
818
949
  },
@@ -863,12 +994,12 @@ export function networkEffects(effects) {
863
994
  async publishServerBuild(record, candidate) {
864
995
  const previous = join(candidate.root, 'previous-runtime');
865
996
  if (record.mode === 'docker') {
866
- for (const target of ['runtime', 'maintenance']) {
997
+ for (const target of ['runtime', 'maintenance', ...(record.gateway ? ['gateway'] : [])]) {
867
998
  const retained = `${candidate.project}:previous-${target}`;
868
- const found = await effects.run('docker', ['image', 'inspect', retained], { allowCodes: [1] });
999
+ const found = await container(record, ['image', 'inspect', retained], { allowCodes: [1] });
869
1000
  if (found.code !== 0) {
870
- const current = await effects.run('docker', ['image', 'inspect', `${record.project}:${target}`], { allowCodes: [1] });
871
- if (current.code === 0) await effects.run('docker', ['tag', `${record.project}:${target}`, retained]);
1001
+ const current = await container(record, ['image', 'inspect', `${record.project}:${target}`], { allowCodes: [1] });
1002
+ if (current.code === 0) await container(record, ['tag', `${record.project}:${target}`, retained]);
872
1003
  }
873
1004
  }
874
1005
  }
@@ -884,8 +1015,8 @@ export function networkEffects(effects) {
884
1015
  renameSync(candidate.workDir, record.workDir);
885
1016
  } else privateDirectory(record.workDir);
886
1017
  if (record.mode === 'docker') {
887
- for (const target of ['runtime', 'maintenance']) {
888
- await effects.run('docker', ['tag', `${candidate.project}:${target}`, `${record.project}:${target}`]);
1018
+ for (const target of ['runtime', 'maintenance', ...(record.gateway ? ['gateway'] : [])]) {
1019
+ await container(candidate, ['tag', `${candidate.project}:${target}`, `${record.project}:${target}`]);
889
1020
  }
890
1021
  }
891
1022
  atomicWriteConfig(record.sourcesPath, readFileSync(candidate.sourcesPath));
@@ -899,10 +1030,10 @@ export function networkEffects(effects) {
899
1030
  },
900
1031
  async discardServerBuild(candidate) {
901
1032
  if (candidate.mode === 'docker') {
902
- for (const target of ['runtime', 'maintenance', 'previous-runtime', 'previous-maintenance']) {
1033
+ for (const target of ['runtime', 'maintenance', 'previous-runtime', 'previous-maintenance', ...(candidate.gateway ? ['gateway', 'previous-gateway'] : [])]) {
903
1034
  const image = `${candidate.project}:${target}`;
904
- const found = await effects.run('docker', ['image', 'inspect', image], { allowCodes: [1] });
905
- if (found.code === 0) await effects.run('docker', ['image', 'rm', image]);
1035
+ const found = await container(candidate, ['image', 'inspect', image], { allowCodes: [1] });
1036
+ if (found.code === 0) await container(candidate, ['image', 'rm', image]);
906
1037
  }
907
1038
  }
908
1039
  privateDirectory(candidate.root);
@@ -910,7 +1041,15 @@ export function networkEffects(effects) {
910
1041
  },
911
1042
  async serverAccess(record, operation, { output, migrate = false } = {}) {
912
1043
  if (record.mode === 'docker') {
913
- if (!output) return compose(record, ['run', '--rm', '--no-deps', '-T', 'access', operation], { sensitive: true, env: { OURS_ACCESS_MIGRATE: migrate ? '1' : '0' } });
1044
+ // Retained images contain the old helper that discarded CLI errors.
1045
+ // Execute the shipped installer helper against the SAME selected image
1046
+ // and Compose volume/user/environment contract, without retag/rebuild.
1047
+ const diagnosticsUrl = `data:text/javascript;base64,${readFileSync(join(INSTALLER_ASSETS, 'scripts/runtime/diagnostics.mjs')).toString('base64')}`;
1048
+ const helper = `process.argv.splice(1, 0, '/opt/ours/docker/client-setup.mjs');\n` + readFileSync(join(INSTALLER_ASSETS, 'scripts/runtime/client-setup.mjs'), 'utf8')
1049
+ .replace("'../maintenance/build-context.mjs'", "'/opt/ours/maintenance/build-context.mjs'")
1050
+ .replace("'./diagnostics.mjs'", JSON.stringify(diagnosticsUrl));
1051
+ const accessArgs = (...args) => ['--entrypoint', 'node', 'access', '--input-type=module', '-e', helper, '--', ...args];
1052
+ if (!output) return compose(record, ['run', '--rm', '--no-deps', '-T', ...accessArgs(operation)], { sensitive: true, env: { OURS_ACCESS_MIGRATE: migrate ? '1' : '0' } });
914
1053
  privateDirectory(dirname(output));
915
1054
  if (existsSync(output)) throw new Error('Credential output already exists');
916
1055
  const name = `${record.project}-issue-${randomUUID()}`;
@@ -918,23 +1057,23 @@ export function networkEffects(effects) {
918
1057
  const staging = join(dirname(output), `.ours-issued-${randomUUID()}`);
919
1058
  ensurePrivateDirectory(staging);
920
1059
  try {
921
- await compose(record, ['run', '--name', name, '--no-deps', '-T', 'access', 'access-issue', issuedPath], { sensitive: true });
1060
+ await compose(record, ['run', '--name', name, '--no-deps', '-T', ...accessArgs('access-issue', issuedPath)], { sensitive: true });
922
1061
  const file = join(staging, 'credential');
923
- await effects.run('docker', ['cp', `${name}:${issuedPath}`, file], { sensitive: true });
1062
+ await container(record, ['cp', `${name}:${issuedPath}`, file], { sensitive: true });
924
1063
  assertPrivateRegularFile(file, 'issued credential');
925
1064
  // Exclusive publication never overwrites an unrelated credential.
926
1065
  writePrivateNew(output, readFileSync(file));
927
1066
  } finally {
928
1067
  await compose(record, ['run', '--rm', '--no-deps', '-T', '--entrypoint', 'rm', 'access', '-f', issuedPath], { sensitive: true });
929
- await effects.run('docker', ['rm', '-f', name], { sensitive: true });
1068
+ await container(record, ['rm', '-f', name], { sensitive: true });
930
1069
  rmSync(staging, { recursive: true, force: true });
931
1070
  }
932
1071
  return;
933
1072
  }
934
1073
  const options = { env: localEnv(record), sensitive: true };
935
- if (operation !== 'access-issue') return effects.run(bin(record, 'ours'), ['config', operation, '--config', record.configPath, ...(operation === 'access-replace' ? ['--confirm'] : migrate ? ['--migrate'] : []), '--json'], options);
1074
+ if (operation !== 'access-issue') return effects.run(bin(record, existsSync(bin(record, 'ours-daemon')) ? 'ours-daemon' : 'ours'), ['config', operation, '--config', record.configPath, ...(operation === 'access-replace' ? ['--confirm'] : migrate ? ['--migrate'] : []), '--json'], options);
936
1075
  const outputs = output ? [output] : [join(installationPaths(record).daemon, 'daemon-token'), ...['telegram', 'cowork', 'messenger'].map(s => installationPaths(record).credentials[s])];
937
- for (const path of outputs) await effects.run(bin(record, 'ours'), ['config', operation, '--config', record.configPath, '--output', path, ...(output ? [] : ['--replace']), '--json'], options);
1076
+ for (const path of outputs) await effects.run(bin(record, existsSync(bin(record, 'ours-daemon')) ? 'ours-daemon' : 'ours'), ['config', operation, '--config', record.configPath, '--output', path, ...(output ? [] : ['--replace']), '--json'], options);
938
1077
  },
939
1078
  async recordRuntimeBuild(record) {
940
1079
  if (record.mode === 'docker') return; // Image preparation records its build.
@@ -978,7 +1117,7 @@ export function networkEffects(effects) {
978
1117
  await effects.run(process.execPath, [join(INSTALLER_ASSETS, 'scripts/maintenance/state-operation.mjs'), ...command], {
979
1118
  env: { ...localEnv(record), OURS_STATE_DOMAIN: args.domain,
980
1119
  OURS_STATE_ROOT: join(record.root, 'storage'), OURS_LIVE_ROOT: ['server', 'daemon'].includes(args.domain) ? paths.state : paths[args.domain],
981
- OURS_BUILD_ROOT: record.workDir, OURS_CLI_PATH: bin(record, 'ours'),
1120
+ OURS_BUILD_ROOT: record.workDir, OURS_CLI_PATH: bin(record, existsSync(bin(record, 'ours-daemon')) ? 'ours-daemon' : 'ours'),
982
1121
  OURS_DAEMON_CONFIG: record.configPath, OURS_COWORK_CLI_PATH: bin(record, 'ours-cowork'),
983
1122
  OURS_COWORK_CONFIG: join(paths.cowork, 'config.json'), OURS_COWORK_STATE_DIR: paths.cowork },
984
1123
  });
@@ -996,12 +1135,12 @@ export function networkEffects(effects) {
996
1135
  if (typeof name !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(name)) {
997
1136
  throw new Error('Invalid conversion volume name');
998
1137
  }
999
- const result = await effects.run('docker', ['volume', 'inspect', '--format', '{{json .}}', name], {
1138
+ const result = await container(record, ['volume', 'inspect', '--format', '{{json .}}', name], {
1000
1139
  ...(optional ? { allowCodes: [1] } : {}),
1001
1140
  });
1002
1141
  if (result.code !== 0) {
1003
1142
  if (optional) {
1004
- const inventory = await effects.run('docker', ['volume', 'ls', '--format', '{{.Name}}']);
1143
+ const inventory = await container(record, ['volume', 'ls', '--format', '{{.Name}}']);
1005
1144
  if (inventory.code === 0 && !inventory.stdout.split(/\s+/).includes(name)) return false;
1006
1145
  }
1007
1146
  throw new Error(`Source volume is missing or unavailable: ${name}`);
@@ -1063,7 +1202,7 @@ export function networkEffects(effects) {
1063
1202
  await ownerCommand(record, 'cowork', 'prepare-backup');
1064
1203
  },
1065
1204
  async retainConvertedPackageAuthority(record, daemon) {
1066
- await effects.run(bin(record, 'ours'), [
1205
+ await effects.run(bin(record, existsSync(bin(record, 'ours-daemon')) ? 'ours-daemon' : 'ours'), [
1067
1206
  'config', 'access-retain', '--config', record.configPath,
1068
1207
  '--target-state-dir', daemon, '--json',
1069
1208
  ], { env: localEnv(record), sensitive: true });
@@ -1125,7 +1264,9 @@ export function networkEffects(effects) {
1125
1264
  }
1126
1265
  const start = async service => {
1127
1266
  effects.out(`Starting ${service}; waiting for readiness...`);
1128
- try { await compose(record, ['up', '-d', '--no-build', '--no-deps', '--wait', service]); }
1267
+ // Gateway has no persistent state. Recreate its network endpoint on each
1268
+ // managed start, including retries after an interrupted/failed bind.
1269
+ try { await compose(record, ['up', '-d', '--no-build', '--no-deps', ...(service === 'gateway' ? ['--force-recreate'] : []), '--wait', service]); }
1129
1270
  catch (cause) { throw await dockerStartupError(record, service, cause); }
1130
1271
  effects.out(`${service} is ready.`);
1131
1272
  };
@@ -1135,6 +1276,7 @@ export function networkEffects(effects) {
1135
1276
  try { await start(service); }
1136
1277
  catch (error) { failures.push({ service, error }); }
1137
1278
  }
1279
+ if (!failures.length && record.gateway && ['daemon', 'cowork', 'gateway'].every(service => selected.includes(service))) await effects.verifyGateway(record);
1138
1280
  if (failures.length) throw new Error(`Application readiness failed: ${failures.map(f => f.service).join(', ')}. Check the selected application prerequisites.\n${failures.map(f => f.error.message).join('\n\n')}`);
1139
1281
  return;
1140
1282
  }
@@ -1149,21 +1291,19 @@ export function networkEffects(effects) {
1149
1291
  return JSON.parse(readFileSync(path, 'utf8'));
1150
1292
  },
1151
1293
  async discoverClientProfile(endpoint, credentialPath) {
1152
- const url = new URL(endpoint);
1153
- if (url.protocol !== 'http:' || url.username || url.password || url.pathname !== '/' || url.search || url.hash)
1154
- throw new Error('Client endpoint must be an HTTP origin');
1155
- const response = await fetch(`${url.origin}/selection`, { redirect: 'error', signal: AbortSignal.timeout(5000) });
1156
- if (!response.ok) throw new Error(`Daemon selection answered HTTP ${response.status}`);
1157
- const selection = await response.json();
1158
- // Full metadata and authenticated capability validation follows before publication.
1159
- return validateHostProfile({ endpoint: url.origin, expectedInstanceId: selection.instanceId, credentialPath: resolve(credentialPath) });
1294
+ const base = serverBase(endpoint);
1295
+ const request = path => fetch(`${base}${path}`, { redirect: 'error', signal: AbortSignal.timeout(5000) });
1296
+ const discovery = await request('/.well-known/ours');
1297
+ if (discovery.ok) return validateGatewayDiscovery(base, await discovery.json(), resolve(credentialPath));
1298
+ throw new Error(`Gateway discovery answered HTTP ${discovery.status}; enable the gateway. Direct daemon fallback is not supported.`);
1160
1299
  },
1161
- importClientProfile({ profile, sourcesPath, sources: resolvedSources, integrations, fleetSettingsPath, refresh = false }) {
1300
+ importClientProfile({ profile, sourcesPath, sources: resolvedSources, integrations, fleetSettingsPath, disableFleetAgentsSetup = false, refresh = false }) {
1301
+ profile = validateGatewayClientProfile(profile);
1162
1302
  const root = join(home, '.ours-client');
1163
1303
  const configPath = join(root, 'profile.json');
1164
1304
  const credentialPath = join(root, 'credential');
1165
1305
  const current = effects.readManagedClientProfile();
1166
- if (current && (current.endpoint !== profile.endpoint || current.expectedInstanceId !== profile.expectedInstanceId))
1306
+ if (current && current.expectedInstanceId !== profile.expectedInstanceId)
1167
1307
  throw new Error('Managed client already selects another server; existing default was not changed');
1168
1308
  assertPrivateRegularFile(profile.credentialPath, 'credential');
1169
1309
  const credential = readFileSync(profile.credentialPath, 'utf8');
@@ -1178,9 +1318,12 @@ export function networkEffects(effects) {
1178
1318
  if (current && !refresh) {
1179
1319
  assertPrivateRegularFile(credentialPath, 'managed credential');
1180
1320
  if (readFileSync(credentialPath, 'utf8') !== credential) atomicWriteConfig(credentialPath, credential);
1181
- return { configPath, profile: validateHostProfile(current), settings: current.installer };
1321
+ const saved = { ...current, ...profile, credentialPath };
1322
+ if (current.endpoint !== saved.endpoint || current.serverUrl !== saved.serverUrl)
1323
+ atomicWriteConfig(configPath, JSON.stringify(saved, null, 2) + '\n');
1324
+ return { configPath, profile: validateGatewayClientProfile(saved), settings: current.installer };
1182
1325
  }
1183
- const settings = { sourcesPath: join(root, 'sources.json'), integrations };
1326
+ const settings = { sourcesPath: join(root, 'sources.json'), integrations, ...(disableFleetAgentsSetup ? { disableFleetAgentsSetup: true } : {}) };
1184
1327
  if (fleetSettings) settings.fleetSettingsPath = join(root, 'fleet-settings.json');
1185
1328
  // Publish the profile last: clients cannot select incomplete imported inputs.
1186
1329
  atomicWriteConfig(settings.sourcesPath, sources);
@@ -1190,18 +1333,39 @@ export function networkEffects(effects) {
1190
1333
  atomicWriteConfig(configPath, JSON.stringify(saved, null, 2) + '\n');
1191
1334
  return { configPath, profile: validateHostProfile(saved), settings };
1192
1335
  },
1193
- async acquireClientPackages(configPath, sourcesPath, integrations, { refresh = false } = {}) {
1336
+ async qualifyInstalledGatewayClient(profile) {
1337
+ if (!profile.installer?.integrations?.includes('fleet')) return;
1338
+ let info;
1339
+ try { info = JSON.parse((await effects.run('ours-fleet', ['version', '--json'])).stdout); } catch {}
1340
+ if (!Array.isArray(info?.capabilities) || !info.capabilities.includes('cowork.http-management-v1'))
1341
+ throw new Error('Installed Fleet does not support gateway HTTP management; upgrade Fleet before gateway-enable');
1342
+ },
1343
+ async qualifyGatewayClient({ profile, sourcesPath, sources, integrations, refresh = false }) {
1344
+ if (!profile.serverUrl || !integrations.includes('fleet')) return;
1345
+ // Acquire into the retained package cache without publishing a profile,
1346
+ // credential, native command, or integration. Normal acquisition rechecks it.
1347
+ const staging = mkdtempSync(join(home, '.ours-gateway-client-'));
1348
+ try {
1349
+ const stagedSources = join(staging, 'sources.json');
1350
+ writeFileSync(stagedSources, sources ? JSON.stringify(sources, null, 2) + '\n' : readFileSync(sourcesPath), { mode: 0o600 });
1351
+ await effects.acquireClientPackages(join(home, '.ours-client/profile.json'), stagedSources, integrations,
1352
+ { refresh, gatewayServerUrl: profile.serverUrl, qualifyOnly: true });
1353
+ } finally { rmSync(staging, { recursive: true, force: true }); }
1354
+ },
1355
+ async acquireClientPackages(configPath, sourcesPath, integrations, { refresh = false, hostCliOnly = false, gatewayServerUrl, qualifyOnly = false } = {}) {
1194
1356
  const manifest = JSON.parse(readFileSync(sourcesPath, 'utf8'));
1195
- // Public SDK client APIs are actual integration dependencies; Fleet also owns CLI usage.
1196
- const selected = [...new Set(['sdk', ...(integrations.includes('fleet') ? ['cli'] : []), ...integrations])];
1357
+ // Every client installation includes the native CLI and its SDK dependency.
1358
+ const selected = clientPackageNames(integrations);
1197
1359
  const packages = selectSourcePackages(manifest, 'client', selected);
1198
- const selectionKey = refresh ? JSON.stringify([configPath, manifest, integrations]) : configPath;
1360
+ const selectionKey = JSON.stringify([hostCliOnly ? 'host-cli-private-v1' : 'host-cli-v1', configPath, ...(hostCliOnly ? [manifest] : []), ...(refresh ? [manifest, integrations] : [])]);
1199
1361
  const root = join(home, '.ours-client-install', createHash('sha256').update(selectionKey).digest('hex').slice(0, 16));
1200
1362
  const hasGit = Object.values(packages).some(selection => selection.source);
1201
1363
  await effects.run('npm', ['--version']);
1202
1364
  if (hasGit) {
1203
1365
  for (const command of ['python3', 'git', 'make', 'cc']) await effects.run(command, ['--version']);
1204
1366
  }
1367
+ // This directory retains active executables, not disposable download cache.
1368
+ ensurePrivateDirectory(join(home, '.ours-client-install'));
1205
1369
  ensurePrivateDirectory(root);
1206
1370
  const retained = join(root, 'sources.json');
1207
1371
  const bytes = readFileSync(sourcesPath);
@@ -1226,13 +1390,33 @@ export function networkEffects(effects) {
1226
1390
  writePrivateNew(join(root, '.packages-ready'), 'ready\n');
1227
1391
  }
1228
1392
  verifyReleaseGraph(root, manifest, { requiredPackages: Object.keys(packages) });
1393
+ if (integrations.includes('fleet') && (gatewayServerUrl || readHostProfileFile(configPath)?.serverUrl)) {
1394
+ let info;
1395
+ try { info = JSON.parse(readFileSync(join(root, 'node_modules/@ours.network/fleet/dist/build-info.json'), 'utf8')); } catch {}
1396
+ if (!Array.isArray(info?.capabilities) || !info.capabilities.includes('cowork.http-management-v1'))
1397
+ throw new Error('Selected Fleet artifact does not support gateway HTTP management; select a compatible client release');
1398
+ }
1399
+ if (qualifyOnly) return;
1400
+ const cliPolicy = hostCliOnly ? null : hostCliPolicy(manifest);
1229
1401
  // Local acquisition alone does not publish native commands. Use the user's
1230
1402
  // configured npm prefix and retained dependency closure, including on retry.
1231
1403
  for (const name of integrations.filter(name => name === 'fleet' || name === 'codex')) {
1232
1404
  await effects.run('npm', ['install', '--global', '--install-links=false', '--offline', '--ignore-scripts', '--no-audit', '--no-fund', join(root, 'node_modules', '@ours.network', name)]);
1233
1405
  }
1406
+ let cliBin = join(root, 'node_modules/.bin/ours');
1407
+ if (cliPolicy) {
1408
+ const cliSources = join(root, 'host-cli-sources.json');
1409
+ const cliBytes = JSON.stringify(cliPolicy, null, 2) + '\n';
1410
+ if (!existsSync(cliSources)) writePrivateNew(cliSources, cliBytes);
1411
+ else if (readFileSync(cliSources, 'utf8') !== cliBytes) throw new Error('Retained host CLI source selection differs');
1412
+ const acquired = await effects.acquireClientPackages(configPath, cliSources, [], { refresh, hostCliOnly: true });
1413
+ cliBin = acquired.cliBin;
1414
+ } else {
1415
+ // Publish last so optional integration installation cannot downgrade ours.
1416
+ await publishClientCli(effects, join(root, 'node_modules', '@ours.network/cli'), { policy: manifest, isolated: hostCliOnly });
1417
+ }
1234
1418
  const localPackages = Object.fromEntries(integrations.filter(n => n !== 'fleet').map(name => [name, join(root, 'node_modules', '@ours.network', name)]));
1235
- return { localPackages, packages: {}, fleetBin: integrations.includes('fleet') ? join(root, 'node_modules/.bin/ours-fleet') : null };
1419
+ return { localPackages, packages: {}, cliBin, fleetBin: integrations.includes('fleet') ? join(root, 'node_modules/.bin/ours-fleet') : null };
1236
1420
  },
1237
1421
  async prepareClientMarketplace(name, packagePath) {
1238
1422
  const acquisitionRoot = dirname(dirname(dirname(packagePath)));
@@ -1241,7 +1425,7 @@ export function networkEffects(effects) {
1241
1425
  const release = releaseBinding(policy);
1242
1426
  const integrationsPath = join(acquisitionRoot, 'integrations.json');
1243
1427
  const integrations = existsSync(integrationsPath) ? JSON.parse(readFileSync(integrationsPath, 'utf8')) : null;
1244
- const requiredPackages = integrations ? [...new Set(['sdk', ...(integrations.includes('fleet') ? ['cli'] : []), ...integrations])].map(name => '@ours.network/' + name) : Object.keys(policy.packages ?? {});
1428
+ const requiredPackages = integrations ? clientPackageNames(integrations).map(name => '@ours.network/' + name) : Object.keys(policy.packages ?? {});
1245
1429
  verifyReleaseGraph(acquisitionRoot, policy, { requiredPackages });
1246
1430
  const root = join(acquisitionRoot, 'marketplaces', name);
1247
1431
  const plugin = join(root, 'plugins', 'ours');
@@ -1250,7 +1434,7 @@ export function networkEffects(effects) {
1250
1434
  cpSync(packagePath, plugin, { recursive: true });
1251
1435
  const manifestPath = join(plugin, 'package.json');
1252
1436
  const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
1253
- for (const dependency of release ? [] : ['sdk', 'cli']) {
1437
+ for (const dependency of release ? [] : ['sdk', 'cli', 'mcp']) {
1254
1438
  const name = `@ours.network/${dependency}`;
1255
1439
  if (manifest.dependencies?.[name]) manifest.dependencies[name] = `file:${join(dirname(packagePath), dependency)}`;
1256
1440
  }
@@ -1270,42 +1454,7 @@ export function networkEffects(effects) {
1270
1454
  effects.writeJson(manifestPath, JSON.stringify(value, null, 2) + '\n');
1271
1455
  return root;
1272
1456
  },
1273
- async verifyPackagedMcp(configPath) {
1274
- const profile = typeof configPath === 'string' ? readHostProfileFile(configPath) : validateHostProfile(configPath);
1275
- assertPrivateRegularFile(profile.credentialPath, 'credential');
1276
- const token = readFileSync(profile.credentialPath, 'utf8').trim();
1277
- const headers = { 'content-type': 'application/json', accept: 'application/json, text/event-stream', 'x-ours-api-token': token, 'x-ours-session-mode': 'external', 'x-ours-lease-token': randomUUID() };
1278
- const request = async (method, params, id) => {
1279
- const response = await fetch(`${profile.endpoint}/mcp`, { method: 'POST', redirect: 'error', signal: AbortSignal.timeout(5000), headers, body: JSON.stringify({ jsonrpc: '2.0', ...(id === undefined ? {} : { id }), method, params }) });
1280
- const session = response.headers.get('mcp-session-id');
1281
- if (session) headers['mcp-session-id'] = session;
1282
- if (!response.ok) throw new Error(`Packaged MCP verification failed: HTTP ${response.status}`);
1283
- if (id === undefined) { await response.body?.cancel(); return; }
1284
- const text = await response.text();
1285
- const messages = response.headers.get('content-type')?.includes('text/event-stream')
1286
- ? text.split('\n').filter(line => line.startsWith('data:')).map(line => JSON.parse(line.slice(5)))
1287
- : [JSON.parse(text)];
1288
- const message = messages.find(value => value.id === id);
1289
- if (!message || message.error || !message.result) throw new Error('Packaged MCP returned no successful protocol result');
1290
- return message.result;
1291
- };
1292
- try {
1293
- const initialized = await request('initialize', { protocolVersion: '2025-03-26', capabilities: {}, clientInfo: { name: 'ours-install', version: '1' } }, 1);
1294
- if (initialized.serverInfo?.name !== 'ours' || !initialized.protocolVersion) throw new Error('Selected MCP is not the packaged OURS server');
1295
- headers['mcp-protocol-version'] = initialized.protocolVersion;
1296
- await request('notifications/initialized', {}, undefined);
1297
- const resources = await request('resources/list', {}, 2);
1298
- if (!resources.resources?.some(resource => resource.uri === 'ours://application-identities')) throw new Error('Packaged network MCP identity resource is absent');
1299
- const toolList = await request('tools/list', {}, 3);
1300
- if (!toolList.tools?.some(tool => tool.name === 'list_identities')) throw new Error('Packaged OURS identity tools are absent');
1301
- } finally {
1302
- if (headers['mcp-session-id']) {
1303
- const response = await fetch(`${profile.endpoint}/mcp`, { method: 'DELETE', redirect: 'error', signal: AbortSignal.timeout(5000), headers });
1304
- await response.body?.cancel();
1305
- if (!response.ok) throw new Error('MCP verification session could not be closed');
1306
- }
1307
- }
1308
- },
1457
+
1309
1458
  };
1310
1459
  }
1311
1460
 
@@ -1352,7 +1501,6 @@ async function nativeLifecycle(record, operation, selected, { effects, localEnv,
1352
1501
  if (service === 'daemon') {
1353
1502
  const profilePath = join(installationPaths(record).mcp, 'profile.json');
1354
1503
  await effects.verifyHostProfile(profilePath);
1355
- await effects.verifyPackagedMcp(profilePath);
1356
1504
  return;
1357
1505
  }
1358
1506
  if (service === 'cowork') { await ownerCommand(record, service, 'status'); return; }
@@ -1430,7 +1578,7 @@ async function nativeLifecycle(record, operation, selected, { effects, localEnv,
1430
1578
  }
1431
1579
  if (!ready) throw new Error('Application is not ready');
1432
1580
  } catch {
1433
- if (service === 'daemon') throw new Error('Daemon and packaged MCP are not ready; consumers were not started');
1581
+ if (service === 'daemon') throw new Error('Daemon API is not ready; consumers were not started');
1434
1582
  failures.push(service);
1435
1583
  }
1436
1584
  }