@serfab/cadre-host 0.11.0 → 0.12.0

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.
@@ -1,112 +1,134 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
- import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3
- import { tmpdir } from 'node:os';
4
- import { join } from 'node:path';
5
-
6
- import { readHostConfig, writeHostConfig, hostOwnsCadre, type HostConfigFile } from '../config.js';
7
-
8
- describe('host.config.json round-trip', () => {
9
- let tmp: string;
10
-
11
- beforeEach(() => {
12
- tmp = mkdtempSync(join(tmpdir(), 'cadre-host-cfg-'));
13
- });
14
-
15
- afterEach(() => {
16
- rmSync(tmp, { recursive: true, force: true });
17
- });
18
-
19
- function makeCfg(overrides: Partial<HostConfigFile> = {}): HostConfigFile {
20
- return {
21
- version: 2,
22
- installId: 'abc123',
23
- uiPort: 8765,
24
- libp2pPort: 4001,
25
- dataDir: tmp,
26
- identityPath: join(tmp, 'identity.key'),
27
- upnpEnabled: true,
28
- installedAt: '2026-01-01T00:00:00.000Z',
29
- installerVersion: '0.6.0',
30
- updates: { autoApply: false },
31
- ...overrides,
32
- };
33
- }
34
-
35
- it('writes and reads the same shape', () => {
36
- const cfg = makeCfg();
37
- const path = join(tmp, 'host.config.json');
38
- writeHostConfig(path, cfg);
39
- expect(readHostConfig(path)).toEqual(cfg);
40
- });
41
-
42
- it('upgrades v1 files in place on read', () => {
43
- const path = join(tmp, 'host.config.json');
44
- const v1 = {
45
- version: 1,
46
- installId: 'abc123',
47
- uiPort: 8765,
48
- libp2pPort: 4001,
49
- dataDir: tmp,
50
- identityPath: join(tmp, 'identity.key'),
51
- upnpEnabled: true,
52
- installedAt: '2026-01-01T00:00:00.000Z',
53
- installerVersion: '0.6.0',
54
- };
55
- writeFileSync(path, JSON.stringify(v1));
56
- const upgraded = readHostConfig(path);
57
- expect(upgraded.version).toBe(2);
58
- expect(upgraded.updates).toEqual({ autoApply: false });
59
- // Reading again should be idempotent and stay v2.
60
- const reRead = readHostConfig(path);
61
- expect(reRead.version).toBe(2);
62
- });
63
-
64
- it('rejects unknown versions', () => {
65
- const path = join(tmp, 'host.config.json');
66
- writeFileSync(path, JSON.stringify({ ...makeCfg(), version: 99 }));
67
- expect(() => readHostConfig(path)).toThrow(/unsupported version=99/);
68
- });
69
-
70
- it('rejects malformed JSON', () => {
71
- const path = join(tmp, 'host.config.json');
72
- writeFileSync(path, 'not json');
73
- expect(() => readHostConfig(path)).toThrow(/Failed to parse/);
74
- });
75
-
76
- it('rejects missing required fields', () => {
77
- const path = join(tmp, 'host.config.json');
78
- writeFileSync(path, JSON.stringify({ version: 2, uiPort: 1234 }));
79
- expect(() => readHostConfig(path)).toThrow(/missing required fields/);
80
- });
81
-
82
- it('refuses to write the wrong version', () => {
83
- const path = join(tmp, 'host.config.json');
84
- expect(() => writeHostConfig(path, { ...makeCfg(), version: 3 as never })).toThrow(/version=3/);
85
- });
86
-
87
- it('round-trips ownCadre and hostOwnsCadre reflects it', () => {
88
- const path = join(tmp, 'host.config.json');
89
- const cfg = makeCfg({ ownCadre: { enabled: true } });
90
- writeHostConfig(path, cfg);
91
- const read = readHostConfig(path);
92
- expect(read.ownCadre).toEqual({ enabled: true });
93
- expect(hostOwnsCadre(read)).toBe(true);
94
- });
95
-
96
- it('treats an absent ownCadre as donor-only (hostOwnsCadre false)', () => {
97
- // A pre-ownCadre v2 config (field absent) must read back cleanly and be
98
- // donor-only — no migration, field stays absent.
99
- const path = join(tmp, 'host.config.json');
100
- const cfg = makeCfg();
101
- writeHostConfig(path, cfg);
102
- const read = readHostConfig(path);
103
- expect(read.ownCadre).toBeUndefined();
104
- expect(hostOwnsCadre(read)).toBe(false);
105
- });
106
-
107
- it('rejects a malformed ownCadre shape', () => {
108
- const path = join(tmp, 'host.config.json');
109
- writeFileSync(path, JSON.stringify({ ...makeCfg(), ownCadre: { enabled: 'yes' } }));
110
- expect(() => readHostConfig(path)).toThrow(/missing required fields/);
111
- });
112
- });
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+
6
+ import { readHostConfig, updateHostConfig, writeHostConfig, hostOwnsCadre, type HostConfigFile } from '../config.js';
7
+
8
+ describe('host.config.json round-trip', () => {
9
+ let tmp: string;
10
+
11
+ beforeEach(() => {
12
+ tmp = mkdtempSync(join(tmpdir(), 'cadre-host-cfg-'));
13
+ });
14
+
15
+ afterEach(() => {
16
+ rmSync(tmp, { recursive: true, force: true });
17
+ });
18
+
19
+ function makeCfg(overrides: Partial<HostConfigFile> = {}): HostConfigFile {
20
+ return {
21
+ version: 2,
22
+ installId: 'abc123',
23
+ uiPort: 8765,
24
+ libp2pPort: 4001,
25
+ dataDir: tmp,
26
+ identityPath: join(tmp, 'identity.key'),
27
+ upnpEnabled: true,
28
+ installedAt: '2026-01-01T00:00:00.000Z',
29
+ installerVersion: '0.6.0',
30
+ updates: { autoApply: false },
31
+ ...overrides,
32
+ };
33
+ }
34
+
35
+ it('writes and reads the same shape', () => {
36
+ const cfg = makeCfg();
37
+ const path = join(tmp, 'host.config.json');
38
+ writeHostConfig(path, cfg);
39
+ expect(readHostConfig(path)).toEqual(cfg);
40
+ });
41
+
42
+ it('rejects v1 files without rewriting them', () => {
43
+ const path = join(tmp, 'host.config.json');
44
+ const v1 = {
45
+ version: 1,
46
+ installId: 'abc123',
47
+ uiPort: 8765,
48
+ libp2pPort: 4001,
49
+ dataDir: tmp,
50
+ identityPath: join(tmp, 'identity.key'),
51
+ upnpEnabled: true,
52
+ installedAt: '2026-01-01T00:00:00.000Z',
53
+ installerVersion: '0.6.0',
54
+ };
55
+ const raw = JSON.stringify(v1);
56
+ writeFileSync(path, raw);
57
+ expect(() => readHostConfig(path)).toThrow(/unsupported version=1/);
58
+ expect(readFileSync(path, 'utf8')).toBe(raw);
59
+ });
60
+
61
+ it('rejects unknown versions', () => {
62
+ const path = join(tmp, 'host.config.json');
63
+ writeFileSync(path, JSON.stringify({ ...makeCfg(), version: 99 }));
64
+ expect(() => readHostConfig(path)).toThrow(/unsupported version=99/);
65
+ });
66
+
67
+ it('rejects a file with no version field', () => {
68
+ const { version: _version, ...noVersion } = makeCfg();
69
+ const path = join(tmp, 'host.config.json');
70
+ writeFileSync(path, JSON.stringify(noVersion));
71
+ expect(() => readHostConfig(path)).toThrow(/unsupported version=undefined/);
72
+ });
73
+
74
+ it('rejects malformed JSON', () => {
75
+ const path = join(tmp, 'host.config.json');
76
+ writeFileSync(path, 'not json');
77
+ expect(() => readHostConfig(path)).toThrow(/Failed to parse/);
78
+ });
79
+
80
+ it('rejects missing required fields', () => {
81
+ const path = join(tmp, 'host.config.json');
82
+ writeFileSync(path, JSON.stringify({ version: 2, uiPort: 1234 }));
83
+ expect(() => readHostConfig(path)).toThrow(/missing required fields/);
84
+ });
85
+
86
+ it('refuses to write the wrong version', () => {
87
+ const path = join(tmp, 'host.config.json');
88
+ expect(() => writeHostConfig(path, { ...makeCfg(), version: 3 as never })).toThrow(/version=3/);
89
+ });
90
+
91
+ it('update patches fields and re-stamps the current version', () => {
92
+ const path = join(tmp, 'host.config.json');
93
+ writeHostConfig(path, makeCfg());
94
+ const next = updateHostConfig(path, { updates: { autoApply: true } });
95
+ expect(next.version).toBe(2);
96
+ expect(next.updates).toEqual({ autoApply: true });
97
+ expect(next.installId).toBe('abc123');
98
+ expect(readHostConfig(path)).toEqual(next);
99
+ });
100
+
101
+ it('update never lets a patch downgrade the version', () => {
102
+ const path = join(tmp, 'host.config.json');
103
+ writeHostConfig(path, makeCfg());
104
+ const patch = { version: 1 } as unknown as Partial<Omit<HostConfigFile, 'version'>>;
105
+ expect(updateHostConfig(path, patch).version).toBe(2);
106
+ expect(readHostConfig(path).version).toBe(2);
107
+ });
108
+
109
+ it('round-trips ownCadre and hostOwnsCadre reflects it', () => {
110
+ const path = join(tmp, 'host.config.json');
111
+ const cfg = makeCfg({ ownCadre: { enabled: true } });
112
+ writeHostConfig(path, cfg);
113
+ const read = readHostConfig(path);
114
+ expect(read.ownCadre).toEqual({ enabled: true });
115
+ expect(hostOwnsCadre(read)).toBe(true);
116
+ });
117
+
118
+ it('treats an absent ownCadre as donor-only (hostOwnsCadre false)', () => {
119
+ // A pre-ownCadre v2 config (field absent) must read back cleanly and be
120
+ // donor-only — no migration, field stays absent.
121
+ const path = join(tmp, 'host.config.json');
122
+ const cfg = makeCfg();
123
+ writeHostConfig(path, cfg);
124
+ const read = readHostConfig(path);
125
+ expect(read.ownCadre).toBeUndefined();
126
+ expect(hostOwnsCadre(read)).toBe(false);
127
+ });
128
+
129
+ it('rejects a malformed ownCadre shape', () => {
130
+ const path = join(tmp, 'host.config.json');
131
+ writeFileSync(path, JSON.stringify({ ...makeCfg(), ownCadre: { enabled: 'yes' } }));
132
+ expect(() => readHostConfig(path)).toThrow(/missing required fields/);
133
+ });
134
+ });
@@ -5,10 +5,10 @@
5
5
  * orchestrator) each own their own files under `<dataDir>/`; this is NOT
6
6
  * an umbrella config.
7
7
  *
8
- * Schema versions:
9
- * v1 — initial layout from 6.4.1.
10
- * v2 — adds `updates: { autoApply, manifestUrl? }` for the update-flow
11
- * (6.4.2). v1 files are silently upgraded on read with autoApply=false.
8
+ * Schema version: 2. `version` is a forward guard — a file whose version
9
+ * does not match `CURRENT_VERSION` is rejected on read rather than
10
+ * reinterpreted, so a future incompatible layout fails loudly instead of
11
+ * being silently misread.
12
12
  */
13
13
 
14
14
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
@@ -119,7 +119,7 @@ export function updateHostConfig(
119
119
  ...current,
120
120
  ...patch,
121
121
  updates: { ...current.updates, ...(patch.updates ?? {}) },
122
- version: 2,
122
+ version: CURRENT_VERSION,
123
123
  };
124
124
  writeHostConfig(filePath, next);
125
125
  return next;
@@ -137,12 +137,6 @@ export function readHostConfig(filePath: string): HostConfigFile {
137
137
  throw new Error(`host.config.json at ${filePath} is not a JSON object`);
138
138
  }
139
139
  const obj = parsed as Record<string, unknown>;
140
- if (obj.version === 1) {
141
- // Silent in-place upgrade. v1 lacks `updates`; default to notify-only.
142
- const upgraded = upgradeV1(obj, filePath);
143
- writeHostConfig(filePath, upgraded);
144
- return upgraded;
145
- }
146
140
  if (obj.version !== CURRENT_VERSION) {
147
141
  throw new Error(
148
142
  `host.config.json at ${filePath} has unsupported version=${obj.version} (this build expects ${CURRENT_VERSION})`,
@@ -154,38 +148,6 @@ export function readHostConfig(filePath: string): HostConfigFile {
154
148
  return obj;
155
149
  }
156
150
 
157
- /** v1 → v2 migration: stamp `updates: { autoApply: false }` and bump version. */
158
- function upgradeV1(obj: Record<string, unknown>, filePath: string): HostConfigFile {
159
- if (!isV1Shape(obj)) {
160
- throw new Error(`host.config.json at ${filePath} is missing required v1 fields`);
161
- }
162
- return {
163
- version: 2,
164
- installId: obj.installId as string,
165
- uiPort: obj.uiPort as number,
166
- libp2pPort: obj.libp2pPort as number,
167
- dataDir: obj.dataDir as string,
168
- identityPath: obj.identityPath as string,
169
- upnpEnabled: obj.upnpEnabled as boolean,
170
- installedAt: obj.installedAt as string,
171
- installerVersion: obj.installerVersion as string,
172
- updates: { autoApply: false },
173
- };
174
- }
175
-
176
- function isV1Shape(v: Record<string, unknown>): boolean {
177
- return (
178
- typeof v.installId === 'string' &&
179
- typeof v.uiPort === 'number' &&
180
- typeof v.libp2pPort === 'number' &&
181
- typeof v.dataDir === 'string' &&
182
- typeof v.identityPath === 'string' &&
183
- typeof v.upnpEnabled === 'boolean' &&
184
- typeof v.installedAt === 'string' &&
185
- typeof v.installerVersion === 'string'
186
- );
187
- }
188
-
189
151
  function isHostConfigShape(v: unknown): v is HostConfigFile {
190
152
  if (!v || typeof v !== 'object') return false;
191
153
  const o = v as Record<string, unknown>;
@@ -9,7 +9,14 @@
9
9
  * We persist the protobuf form (not raw bytes) because that's the format
10
10
  * libp2p's `privateKeyFromProtobuf` consumes directly and it preserves the
11
11
  * key type tag — future migrations to a different key algorithm don't
12
- * require a schema change here.
12
+ * require a schema change here. Raw bytes carry no type tag AND no way to tell
13
+ * a damaged file from a valid one: `privateKeyFromRaw` accepts any 64 bytes as
14
+ * an Ed25519 key without checking that the public half matches the private, so
15
+ * a truncated file would load as a *different* working identity.
16
+ *
17
+ * This is the repo-wide identity file format, not a cadre-host-local choice:
18
+ * `cadre enroll create` writes exactly these bytes, and `cadre-cli`'s
19
+ * `identity.keyFile` accepts nothing else (`config/loader.ts` → `loadIdentityKey`).
13
20
  */
14
21
 
15
22
  import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';