@serfab/cadre-host 1.6.0 → 1.8.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.
Files changed (91) hide show
  1. package/README.md +31 -16
  2. package/dist/bin/host.js +41 -20
  3. package/dist/bin/host.js.map +1 -1
  4. package/dist/donation/donation-service.d.ts +27 -4
  5. package/dist/donation/donation-service.js +57 -6
  6. package/dist/donation/donation-service.js.map +1 -1
  7. package/dist/donation/donation-store.d.ts +3 -1
  8. package/dist/donation/donation-store.js +5 -1
  9. package/dist/donation/donation-store.js.map +1 -1
  10. package/dist/donation/grant-service.d.ts +16 -7
  11. package/dist/donation/grant-service.js +57 -10
  12. package/dist/donation/grant-service.js.map +1 -1
  13. package/dist/donation/index.d.ts +2 -2
  14. package/dist/donation/index.js +1 -1
  15. package/dist/donation/index.js.map +1 -1
  16. package/dist/donation/types.d.ts +29 -7
  17. package/dist/donation/types.js.map +1 -1
  18. package/dist/index.d.ts +1 -1
  19. package/dist/nat/address-resolver.d.ts +3 -2
  20. package/dist/nat/address-resolver.js.map +1 -1
  21. package/dist/orchestrator/host-process-orchestrator.d.ts +5 -4
  22. package/dist/orchestrator/host-process-orchestrator.js +25 -14
  23. package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
  24. package/dist/orchestrator/node-identity.d.ts +3 -2
  25. package/dist/orchestrator/node-identity.js +3 -2
  26. package/dist/orchestrator/node-identity.js.map +1 -1
  27. package/dist/orchestrator/port-allocator.d.ts +6 -39
  28. package/dist/orchestrator/port-allocator.js +9 -89
  29. package/dist/orchestrator/port-allocator.js.map +1 -1
  30. package/dist/server/events/types.d.ts +3 -0
  31. package/dist/server/index.d.ts +19 -18
  32. package/dist/server/index.js +13 -15
  33. package/dist/server/index.js.map +1 -1
  34. package/dist/server/routes/grants-admin.d.ts +15 -3
  35. package/dist/server/routes/grants-admin.js +33 -7
  36. package/dist/server/routes/grants-admin.js.map +1 -1
  37. package/dist/server/routes/nodes.d.ts +13 -5
  38. package/dist/server/routes/nodes.js +58 -39
  39. package/dist/server/routes/nodes.js.map +1 -1
  40. package/dist/server/routes/status.d.ts +13 -0
  41. package/dist/server/routes/status.js +2 -0
  42. package/dist/server/routes/status.js.map +1 -1
  43. package/dist/ui/assets/index-C6nELNfC.js +23 -0
  44. package/dist/ui/assets/index-C6nELNfC.js.map +1 -0
  45. package/dist/ui/assets/index-TtIYSeZO.css +1 -0
  46. package/dist/ui/index.html +2 -2
  47. package/package.json +4 -4
  48. package/src/__tests__/global-setup.ts +58 -56
  49. package/src/__tests__/orchestrator-env-scrub.test.ts +12 -11
  50. package/src/__tests__/orchestrator-handle-contract.ts +134 -0
  51. package/src/__tests__/orchestrator-node-identity.test.ts +6 -7
  52. package/src/__tests__/orchestrator-owner.test.ts +6 -13
  53. package/src/__tests__/orchestrator-pin-keys.test.ts +6 -7
  54. package/src/__tests__/orchestrator-ports.test.ts +5 -34
  55. package/src/__tests__/orchestrator-push.test.ts +6 -5
  56. package/src/__tests__/orchestrator-teardown.ts +65 -0
  57. package/src/__tests__/orchestrator.test.ts +23 -22
  58. package/src/auth/__tests__/trust-circle-integration.test.ts +1 -1
  59. package/src/bin/host.ts +56 -33
  60. package/src/donation/__tests__/donation-service.test.ts +38 -0
  61. package/src/donation/__tests__/fake-orchestrator.test.ts +19 -14
  62. package/src/donation/__tests__/fake-orchestrator.ts +21 -3
  63. package/src/donation/__tests__/grant-service.test.ts +2 -2
  64. package/src/donation/donation-service.ts +58 -6
  65. package/src/donation/donation-store.ts +176 -171
  66. package/src/donation/grant-service.ts +252 -200
  67. package/src/donation/index.ts +65 -64
  68. package/src/donation/types.ts +260 -244
  69. package/src/index.ts +2 -0
  70. package/src/nat/address-resolver.ts +3 -2
  71. package/src/orchestrator/host-process-orchestrator.ts +1260 -1250
  72. package/src/orchestrator/node-identity.ts +3 -2
  73. package/src/orchestrator/port-allocator.ts +16 -91
  74. package/src/server/__tests__/fakes.ts +44 -0
  75. package/src/server/__tests__/grants-admin-route.test.ts +171 -73
  76. package/src/server/__tests__/nodes-route.test.ts +49 -25
  77. package/src/server/__tests__/publishers.test.ts +5 -41
  78. package/src/server/__tests__/server.smoke.test.ts +7 -37
  79. package/src/server/__tests__/status-route.test.ts +8 -21
  80. package/src/server/__tests__/strands-route.test.ts +3 -2
  81. package/src/server/events/types.ts +24 -23
  82. package/src/server/index.ts +267 -267
  83. package/src/server/routes/grants-admin.ts +74 -41
  84. package/src/server/routes/nodes.ts +61 -43
  85. package/src/server/routes/status.ts +16 -0
  86. package/dist/donation/__tests__/fake-orchestrator.d.ts +0 -103
  87. package/dist/donation/__tests__/fake-orchestrator.js +0 -180
  88. package/dist/donation/__tests__/fake-orchestrator.js.map +0 -1
  89. package/dist/ui/assets/index-BVoqLjyX.css +0 -1
  90. package/dist/ui/assets/index-CEoNRwM_.js +0 -17
  91. package/dist/ui/assets/index-CEoNRwM_.js.map +0 -1
@@ -1,171 +1,176 @@
1
- import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
- import { dirname, join } from 'node:path';
3
- import debug from 'debug';
4
-
5
- import type { Donation, DonationFile, DonationStatus } from './types.js';
6
- import { DonationError } from './types.js';
7
-
8
- const log = debug('cadre:host:donation-store');
9
-
10
- const FILE_VERSION = 1;
11
-
12
- /**
13
- * Statuses that count a donation as "live" against a grant's `maxNodes` quota.
14
- * `error` and `terminated` donations are kept for audit but do NOT count.
15
- */
16
- const LIVE_STATUSES: ReadonlySet<DonationStatus> = new Set<DonationStatus>([
17
- 'provisioning',
18
- 'awaiting_seed',
19
- 'seeded',
20
- ]);
21
-
22
- /**
23
- * Atomic JSON store for `donations.json` — one row per donated node. Modelled on
24
- * `grant-store.ts`: a single file written to `<path>.tmp` then renamed, with an
25
- * in-memory cache keyed by donation id.
26
- *
27
- * **Persists `seedToken`.** Unlike the orchestrator handle (whose `toPersisted`
28
- * deliberately omits the freshly-minted-per-spawn seed token), this record keeps
29
- * it: if the host restarts in the request→seed gap, only the donation row can
30
- * reproduce the bearer needed to present the seed to the node. See
31
- * `tickets/complete/cadre-provider-seed-endpoint-never-populated.md`.
32
- *
33
- * Concurrency assumption: only one cadre-host process owns a given rootDir
34
- * (the orchestrator already enforces this). No file-locking primitives.
35
- *
36
- * NOTE: every method here is **synchronous**, and `DonationService` depends on
37
- * that for correctness, not just convenience — each of its long operations
38
- * re-reads a record and writes it back with no `await` in between, which is only
39
- * atomic against the event loop while `get`/`put` cannot yield. Giving this class
40
- * an async write path would silently reopen the resurrect-an-ended-loan races
41
- * those re-reads exist to close, and the tests that guard them would still pass.
42
- */
43
- export class DonationStore {
44
- private readonly path: string;
45
- private cache: DonationFile | null = null;
46
-
47
- constructor(rootDir: string) {
48
- mkdirSync(rootDir, { recursive: true });
49
- this.path = join(rootDir, 'donations.json');
50
- }
51
-
52
- filePath(): string {
53
- return this.path;
54
- }
55
-
56
- /**
57
- * Load (with caching). A missing file returns an empty store. A
58
- * present-but-malformed file throws rather than silently wiping — losing the
59
- * records would orphan live child processes (their seed tokens gone).
60
- */
61
- load(): DonationFile {
62
- if (this.cache) return this.cache;
63
- if (!existsSync(this.path)) {
64
- this.cache = emptyFile();
65
- return this.cache;
66
- }
67
- let raw: string;
68
- try {
69
- raw = readFileSync(this.path, 'utf8');
70
- } catch (err) {
71
- throw new DonationError(
72
- 'storage_error',
73
- `failed to read donations file at ${this.path}: ${(err as Error).message}`,
74
- );
75
- }
76
- let parsed: unknown;
77
- try {
78
- parsed = JSON.parse(raw);
79
- } catch (err) {
80
- throw new DonationError(
81
- 'storage_error',
82
- `donations file at ${this.path} is not valid JSON: ${(err as Error).message}`,
83
- );
84
- }
85
- if (!isDonationFile(parsed)) {
86
- throw new DonationError(
87
- 'storage_error',
88
- `donations file at ${this.path} has unexpected shape`,
89
- );
90
- }
91
- this.cache = parsed;
92
- return this.cache;
93
- }
94
-
95
- /** Persist the current state atomically (write then rename). */
96
- save(state: DonationFile): void {
97
- // NOTE: rewrites the whole donations.json on every mutation. Fine at
98
- // household scale (a handful of donated nodes); if counts ever grow large,
99
- // switch to an append/compact log or per-id files.
100
- mkdirSync(dirname(this.path), { recursive: true });
101
- const payload = JSON.stringify({ ...state, version: FILE_VERSION }, null, 2);
102
- const tmp = `${this.path}.tmp`;
103
- try {
104
- writeFileSync(tmp, payload, { encoding: 'utf8' });
105
- renameSync(tmp, this.path);
106
- } catch (err) {
107
- throw new DonationError(
108
- 'storage_error',
109
- `failed to write donations file at ${this.path}: ${(err as Error).message}`,
110
- );
111
- }
112
- this.cache = { ...state, version: FILE_VERSION };
113
- log('saved donations (%d) to %s', Object.keys(state.donations).length, this.path);
114
- }
115
-
116
- /** Insert or replace a donation row. */
117
- put(donation: Donation): void {
118
- const state = this.load();
119
- const { id, ...rest } = donation;
120
- state.donations[id] = rest;
121
- this.save(state);
122
- }
123
-
124
- get(id: string): Donation | undefined {
125
- const state = this.load();
126
- const row = state.donations[id];
127
- if (!row) return undefined;
128
- return { id, ...row };
129
- }
130
-
131
- list(): Donation[] {
132
- const state = this.load();
133
- return Object.entries(state.donations).map(([id, row]) => ({ id, ...row }));
134
- }
135
-
136
- /** All donations authorized by a given grant token. */
137
- listByGrant(grantToken: string): Donation[] {
138
- return this.list().filter((d) => d.grantToken === grantToken);
139
- }
140
-
141
- remove(id: string): boolean {
142
- const state = this.load();
143
- if (!(id in state.donations)) return false;
144
- delete state.donations[id];
145
- this.save(state);
146
- return true;
147
- }
148
-
149
- /**
150
- * The authoritative live-node tally the grant validator consumes: donations
151
- * under `grantToken` in a live status (`provisioning` / `awaiting_seed` /
152
- * `seeded`). `error` and `terminated` rows are excluded.
153
- */
154
- liveNodeCount(grantToken: string): number {
155
- return this.list().filter(
156
- (d) => d.grantToken === grantToken && LIVE_STATUSES.has(d.status),
157
- ).length;
158
- }
159
- }
160
-
161
- function emptyFile(): DonationFile {
162
- return { version: FILE_VERSION, donations: {} };
163
- }
164
-
165
- function isDonationFile(v: unknown): v is DonationFile {
166
- if (!v || typeof v !== 'object') return false;
167
- const obj = v as Record<string, unknown>;
168
- if (obj.version !== FILE_VERSION) return false;
169
- if (!obj.donations || typeof obj.donations !== 'object') return false;
170
- return true;
171
- }
1
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import debug from 'debug';
4
+
5
+ import type { Donation, DonationFile, DonationStatus } from './types.js';
6
+ import { DonationError } from './types.js';
7
+
8
+ const log = debug('cadre:host:donation-store');
9
+
10
+ const FILE_VERSION = 1;
11
+
12
+ /**
13
+ * Statuses that count a donation as "live" against a grant's `maxNodes` quota.
14
+ * `error` and `terminated` donations are kept for audit but do NOT count.
15
+ */
16
+ const LIVE_STATUSES: ReadonlySet<DonationStatus> = new Set<DonationStatus>([
17
+ 'provisioning',
18
+ 'awaiting_seed',
19
+ 'seeded',
20
+ ]);
21
+
22
+ /** Whether a donation in `status` counts against its grant's `maxNodes` quota. */
23
+ export function isLiveDonationStatus(status: DonationStatus): boolean {
24
+ return LIVE_STATUSES.has(status);
25
+ }
26
+
27
+ /**
28
+ * Atomic JSON store for `donations.json` — one row per donated node. Modelled on
29
+ * `grant-store.ts`: a single file written to `<path>.tmp` then renamed, with an
30
+ * in-memory cache keyed by donation id.
31
+ *
32
+ * **Persists `seedToken`.** Unlike the orchestrator handle (whose `toPersisted`
33
+ * deliberately omits the freshly-minted-per-spawn seed token), this record keeps
34
+ * it: if the host restarts in the request→seed gap, only the donation row can
35
+ * reproduce the bearer needed to present the seed to the node. See
36
+ * `tickets/complete/cadre-provider-seed-endpoint-never-populated.md`.
37
+ *
38
+ * Concurrency assumption: only one cadre-host process owns a given rootDir
39
+ * (the orchestrator already enforces this). No file-locking primitives.
40
+ *
41
+ * NOTE: every method here is **synchronous**, and `DonationService` depends on
42
+ * that for correctness, not just convenience — each of its long operations
43
+ * re-reads a record and writes it back with no `await` in between, which is only
44
+ * atomic against the event loop while `get`/`put` cannot yield. Giving this class
45
+ * an async write path would silently reopen the resurrect-an-ended-loan races
46
+ * those re-reads exist to close, and the tests that guard them would still pass.
47
+ */
48
+ export class DonationStore {
49
+ private readonly path: string;
50
+ private cache: DonationFile | null = null;
51
+
52
+ constructor(rootDir: string) {
53
+ mkdirSync(rootDir, { recursive: true });
54
+ this.path = join(rootDir, 'donations.json');
55
+ }
56
+
57
+ filePath(): string {
58
+ return this.path;
59
+ }
60
+
61
+ /**
62
+ * Load (with caching). A missing file returns an empty store. A
63
+ * present-but-malformed file throws rather than silently wiping — losing the
64
+ * records would orphan live child processes (their seed tokens gone).
65
+ */
66
+ load(): DonationFile {
67
+ if (this.cache) return this.cache;
68
+ if (!existsSync(this.path)) {
69
+ this.cache = emptyFile();
70
+ return this.cache;
71
+ }
72
+ let raw: string;
73
+ try {
74
+ raw = readFileSync(this.path, 'utf8');
75
+ } catch (err) {
76
+ throw new DonationError(
77
+ 'storage_error',
78
+ `failed to read donations file at ${this.path}: ${(err as Error).message}`,
79
+ );
80
+ }
81
+ let parsed: unknown;
82
+ try {
83
+ parsed = JSON.parse(raw);
84
+ } catch (err) {
85
+ throw new DonationError(
86
+ 'storage_error',
87
+ `donations file at ${this.path} is not valid JSON: ${(err as Error).message}`,
88
+ );
89
+ }
90
+ if (!isDonationFile(parsed)) {
91
+ throw new DonationError(
92
+ 'storage_error',
93
+ `donations file at ${this.path} has unexpected shape`,
94
+ );
95
+ }
96
+ this.cache = parsed;
97
+ return this.cache;
98
+ }
99
+
100
+ /** Persist the current state atomically (write then rename). */
101
+ save(state: DonationFile): void {
102
+ // NOTE: rewrites the whole donations.json on every mutation. Fine at
103
+ // household scale (a handful of donated nodes); if counts ever grow large,
104
+ // switch to an append/compact log or per-id files.
105
+ mkdirSync(dirname(this.path), { recursive: true });
106
+ const payload = JSON.stringify({ ...state, version: FILE_VERSION }, null, 2);
107
+ const tmp = `${this.path}.tmp`;
108
+ try {
109
+ writeFileSync(tmp, payload, { encoding: 'utf8' });
110
+ renameSync(tmp, this.path);
111
+ } catch (err) {
112
+ throw new DonationError(
113
+ 'storage_error',
114
+ `failed to write donations file at ${this.path}: ${(err as Error).message}`,
115
+ );
116
+ }
117
+ this.cache = { ...state, version: FILE_VERSION };
118
+ log('saved donations (%d) to %s', Object.keys(state.donations).length, this.path);
119
+ }
120
+
121
+ /** Insert or replace a donation row. */
122
+ put(donation: Donation): void {
123
+ const state = this.load();
124
+ const { id, ...rest } = donation;
125
+ state.donations[id] = rest;
126
+ this.save(state);
127
+ }
128
+
129
+ get(id: string): Donation | undefined {
130
+ const state = this.load();
131
+ const row = state.donations[id];
132
+ if (!row) return undefined;
133
+ return { id, ...row };
134
+ }
135
+
136
+ list(): Donation[] {
137
+ const state = this.load();
138
+ return Object.entries(state.donations).map(([id, row]) => ({ id, ...row }));
139
+ }
140
+
141
+ /** All donations authorized by a given grant token. */
142
+ listByGrant(grantToken: string): Donation[] {
143
+ return this.list().filter((d) => d.grantToken === grantToken);
144
+ }
145
+
146
+ remove(id: string): boolean {
147
+ const state = this.load();
148
+ if (!(id in state.donations)) return false;
149
+ delete state.donations[id];
150
+ this.save(state);
151
+ return true;
152
+ }
153
+
154
+ /**
155
+ * The authoritative live-node tally the grant validator consumes: donations
156
+ * under `grantToken` in a live status (`provisioning` / `awaiting_seed` /
157
+ * `seeded`). `error` and `terminated` rows are excluded.
158
+ */
159
+ liveNodeCount(grantToken: string): number {
160
+ return this.list().filter(
161
+ (d) => d.grantToken === grantToken && isLiveDonationStatus(d.status),
162
+ ).length;
163
+ }
164
+ }
165
+
166
+ function emptyFile(): DonationFile {
167
+ return { version: FILE_VERSION, donations: {} };
168
+ }
169
+
170
+ function isDonationFile(v: unknown): v is DonationFile {
171
+ if (!v || typeof v !== 'object') return false;
172
+ const obj = v as Record<string, unknown>;
173
+ if (obj.version !== FILE_VERSION) return false;
174
+ if (!obj.donations || typeof obj.donations !== 'object') return false;
175
+ return true;
176
+ }