@overcy/hive 0.0.0-stage → 0.2.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.
- package/README.md +213 -2
- package/dist/api.d.ts +31 -0
- package/dist/api.js +83 -0
- package/dist/atomic.d.ts +1 -0
- package/dist/atomic.js +24 -0
- package/dist/binding.d.ts +5 -0
- package/dist/binding.js +16 -0
- package/dist/bootstrap/deps.d.ts +3 -0
- package/dist/bootstrap/deps.js +35 -0
- package/dist/bootstrap/doctor.d.ts +29 -0
- package/dist/bootstrap/doctor.js +80 -0
- package/dist/bootstrap/engine.d.ts +29 -0
- package/dist/bootstrap/engine.js +160 -0
- package/dist/bootstrap/forwarder.d.ts +11 -0
- package/dist/bootstrap/forwarder.js +30 -0
- package/dist/bootstrap/identity.d.ts +7 -0
- package/dist/bootstrap/identity.js +52 -0
- package/dist/bootstrap/index.d.ts +36 -0
- package/dist/bootstrap/index.js +40 -0
- package/dist/bootstrap/kit.d.ts +9 -0
- package/dist/bootstrap/kit.js +56 -0
- package/dist/bootstrap/manifest.d.ts +73 -0
- package/dist/bootstrap/manifest.js +309 -0
- package/dist/bootstrap/model-access.d.ts +43 -0
- package/dist/bootstrap/model-access.js +74 -0
- package/dist/bootstrap/pin.d.ts +2 -0
- package/dist/bootstrap/pin.js +16 -0
- package/dist/bootstrap/plan.d.ts +49 -0
- package/dist/bootstrap/plan.js +69 -0
- package/dist/bootstrap/prompt.d.ts +3 -0
- package/dist/bootstrap/prompt.js +35 -0
- package/dist/bootstrap/registry.d.ts +85 -0
- package/dist/bootstrap/registry.js +38 -0
- package/dist/bootstrap/secrets.d.ts +11 -0
- package/dist/bootstrap/secrets.js +105 -0
- package/dist/bootstrap/steps/agents.d.ts +9 -0
- package/dist/bootstrap/steps/agents.js +75 -0
- package/dist/bootstrap/steps/auth.d.ts +8 -0
- package/dist/bootstrap/steps/auth.js +138 -0
- package/dist/bootstrap/steps/cron.d.ts +7 -0
- package/dist/bootstrap/steps/cron.js +74 -0
- package/dist/bootstrap/steps/drift.d.ts +5 -0
- package/dist/bootstrap/steps/drift.js +67 -0
- package/dist/bootstrap/steps/env.d.ts +6 -0
- package/dist/bootstrap/steps/env.js +108 -0
- package/dist/bootstrap/steps/files.d.ts +7 -0
- package/dist/bootstrap/steps/files.js +28 -0
- package/dist/bootstrap/steps/lanes.d.ts +14 -0
- package/dist/bootstrap/steps/lanes.js +153 -0
- package/dist/bootstrap/steps/model-access.d.ts +10 -0
- package/dist/bootstrap/steps/model-access.js +122 -0
- package/dist/bootstrap/steps/preflight.d.ts +3 -0
- package/dist/bootstrap/steps/preflight.js +49 -0
- package/dist/bootstrap/steps/reporter.d.ts +12 -0
- package/dist/bootstrap/steps/reporter.js +155 -0
- package/dist/bootstrap/system.d.ts +5 -0
- package/dist/bootstrap/system.js +69 -0
- package/dist/bootstrap/types.d.ts +157 -0
- package/dist/bootstrap/types.js +14 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +23 -0
- package/dist/collect.d.ts +28 -0
- package/dist/collect.js +63 -0
- package/dist/commands/connect.d.ts +33 -0
- package/dist/commands/connect.js +306 -0
- package/dist/commands/doctor.d.ts +7 -0
- package/dist/commands/doctor.js +13 -0
- package/dist/commands/login.d.ts +3 -0
- package/dist/commands/login.js +118 -0
- package/dist/commands/logout.d.ts +2 -0
- package/dist/commands/logout.js +37 -0
- package/dist/commands/node-add.d.ts +7 -0
- package/dist/commands/node-add.js +56 -0
- package/dist/commands/node.d.ts +19 -0
- package/dist/commands/node.js +43 -0
- package/dist/commands/prEvents.d.ts +7 -0
- package/dist/commands/prEvents.js +32 -0
- package/dist/commands/setup.d.ts +15 -0
- package/dist/commands/setup.js +35 -0
- package/dist/commands/status.d.ts +18 -0
- package/dist/commands/status.js +71 -0
- package/dist/commands/threads.d.ts +6 -0
- package/dist/commands/threads.js +36 -0
- package/dist/config.d.ts +71 -0
- package/dist/config.js +121 -0
- package/dist/context.d.ts +26 -0
- package/dist/context.js +31 -0
- package/dist/crypto.d.ts +7 -0
- package/dist/crypto.js +50 -0
- package/dist/machine.d.ts +66 -0
- package/dist/machine.js +103 -0
- package/dist/node/cli.d.ts +25 -0
- package/dist/node/cli.js +130 -0
- package/dist/node/engine.d.ts +47 -0
- package/dist/node/engine.js +463 -0
- package/dist/node/ingress.d.ts +102 -0
- package/dist/node/ingress.js +473 -0
- package/dist/node/layout.d.ts +56 -0
- package/dist/node/layout.js +55 -0
- package/dist/node/leave.d.ts +8 -0
- package/dist/node/leave.js +32 -0
- package/dist/node/lock.d.ts +19 -0
- package/dist/node/lock.js +99 -0
- package/dist/node/modelAccess.d.ts +2 -0
- package/dist/node/modelAccess.js +44 -0
- package/dist/node/repair.d.ts +28 -0
- package/dist/node/repair.js +78 -0
- package/dist/node/services.d.ts +58 -0
- package/dist/node/services.js +171 -0
- package/dist/node/state.d.ts +127 -0
- package/dist/node/state.js +95 -0
- package/dist/node/steps.d.ts +74 -0
- package/dist/node/steps.js +158 -0
- package/dist/node/supervisor.d.ts +22 -0
- package/dist/node/supervisor.js +95 -0
- package/dist/node/tick.d.ts +47 -0
- package/dist/node/tick.js +278 -0
- package/dist/node/toolchain.d.ts +50 -0
- package/dist/node/toolchain.js +230 -0
- package/dist/nodeContract.d.ts +101 -0
- package/dist/nodeContract.js +192 -0
- package/dist/package.d.ts +3 -0
- package/dist/package.js +5 -0
- package/dist/prEvents.d.ts +109 -0
- package/dist/prEvents.js +374 -0
- package/dist/run.d.ts +19 -0
- package/dist/run.js +487 -0
- package/dist/specs/device-client.json +920 -0
- package/dist/threads.d.ts +88 -0
- package/dist/threads.js +268 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/package.json +35 -4
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { lstat, mkdir, readFile, rename, rm } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
import { PENDING_KEY, SHARED_CONFIG } from "../config.js";
|
|
4
|
+
import { reporterService } from "./steps/reporter.js";
|
|
5
|
+
import { writeAtomic } from "./steps/files.js";
|
|
6
|
+
import { SetupError } from "./types.js";
|
|
7
|
+
export const INSTALL_MANIFEST_FILE = 'install-manifest.json';
|
|
8
|
+
const NO_SERVICE = { kind: 'none', name: null, file: null, status: 'done' };
|
|
9
|
+
const isString = (value) => typeof value === 'string';
|
|
10
|
+
const isNullableString = (value) => value === null || isString(value);
|
|
11
|
+
const isRecord = (value) => value !== null && typeof value === 'object';
|
|
12
|
+
function statusOf(value, version) {
|
|
13
|
+
if (version === 1)
|
|
14
|
+
return 'done';
|
|
15
|
+
return value === 'pending' || value === 'done' ? value : null;
|
|
16
|
+
}
|
|
17
|
+
function parseEntry(value, version) {
|
|
18
|
+
if (version === 1)
|
|
19
|
+
return isString(value) ? { path: value, status: 'done' } : null;
|
|
20
|
+
if (!isRecord(value))
|
|
21
|
+
return null;
|
|
22
|
+
const status = statusOf(value.status, version);
|
|
23
|
+
return isString(value.path) && status ? { path: value.path, status } : null;
|
|
24
|
+
}
|
|
25
|
+
function parseNullableEntry(value, version) {
|
|
26
|
+
return value === null ? null : (parseEntry(value, version) ?? undefined);
|
|
27
|
+
}
|
|
28
|
+
function parseService(value, version) {
|
|
29
|
+
if (!isRecord(value))
|
|
30
|
+
return null;
|
|
31
|
+
const { kind, name, file } = value;
|
|
32
|
+
const status = statusOf(value.status, version);
|
|
33
|
+
if (kind === 'none')
|
|
34
|
+
return name === null && file === null ? NO_SERVICE : null;
|
|
35
|
+
return (kind === 'systemd' || kind === 'launchd') && isString(name) && isString(file) && status ? { kind, name, file, status } : null;
|
|
36
|
+
}
|
|
37
|
+
function parseCrontabEntry(value, version) {
|
|
38
|
+
if (!isRecord(value))
|
|
39
|
+
return null;
|
|
40
|
+
const status = statusOf(value.status, version);
|
|
41
|
+
return isString(value.marker) && value.marker !== '' && isString(value.user) && value.user !== '' && status ? { marker: value.marker, user: value.user, status } : null;
|
|
42
|
+
}
|
|
43
|
+
function parseCrontab(value, version) {
|
|
44
|
+
if (value === null)
|
|
45
|
+
return [];
|
|
46
|
+
const entries = (Array.isArray(value) ? value : [value]).map((entry) => parseCrontabEntry(entry, version));
|
|
47
|
+
return entries.every((entry) => entry !== null) ? entries : undefined;
|
|
48
|
+
}
|
|
49
|
+
function parseManifest(value) {
|
|
50
|
+
if (!isRecord(value) || (value.version !== 1 && value.version !== 2))
|
|
51
|
+
return null;
|
|
52
|
+
const version = value.version;
|
|
53
|
+
const created = value.created;
|
|
54
|
+
if (!isNullableString(value.node_id) || !isString(value.api_url) || !isNullableString(value.kit_release) || !isString(value.written_at) || !isRecord(created) || !Array.isArray(created.paths))
|
|
55
|
+
return null;
|
|
56
|
+
const paths = created.paths.map((entry) => parseEntry(entry, version));
|
|
57
|
+
const worktree = parseNullableEntry(created.worktree_root, version);
|
|
58
|
+
const key = parseNullableEntry(created.key_path, version);
|
|
59
|
+
const token = parseNullableEntry(created.token_path, version);
|
|
60
|
+
const service = parseService(created.service, version);
|
|
61
|
+
const crontab = parseCrontab(created.crontab, version);
|
|
62
|
+
if (paths.some((entry) => entry === null) || worktree === undefined || key === undefined || token === undefined || service === null || crontab === undefined)
|
|
63
|
+
return null;
|
|
64
|
+
return {
|
|
65
|
+
version: 2,
|
|
66
|
+
node_id: value.node_id,
|
|
67
|
+
api_url: value.api_url,
|
|
68
|
+
created: { paths: paths, worktree_root: worktree, service, key_path: key, token_path: token, crontab },
|
|
69
|
+
kit_release: value.kit_release,
|
|
70
|
+
written_at: value.written_at,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function readManifestFile(file) {
|
|
74
|
+
let text;
|
|
75
|
+
try {
|
|
76
|
+
text = await readFile(file, 'utf8');
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error.code === 'ENOENT')
|
|
80
|
+
return null;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
let parsed;
|
|
84
|
+
try {
|
|
85
|
+
parsed = JSON.parse(text);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new SetupError(`${file} is not valid JSON. It records what bootstrap created, so move it aside only after you have removed those things by hand.`);
|
|
89
|
+
}
|
|
90
|
+
const manifest = parseManifest(parsed);
|
|
91
|
+
if (!manifest)
|
|
92
|
+
throw new SetupError(`${file} is not an install manifest this version understands.`);
|
|
93
|
+
return manifest;
|
|
94
|
+
}
|
|
95
|
+
export const journalSibling = (home) => join(dirname(home), `.${basename(home)}.${INSTALL_MANIFEST_FILE}`);
|
|
96
|
+
export async function readInstallManifest(file) {
|
|
97
|
+
const sibling = basename(file) === INSTALL_MANIFEST_FILE ? journalSibling(dirname(file)) : null;
|
|
98
|
+
const inHome = await readManifestFile(file);
|
|
99
|
+
if (!sibling)
|
|
100
|
+
return inHome;
|
|
101
|
+
if (inHome) {
|
|
102
|
+
await rm(sibling, { force: true });
|
|
103
|
+
return inHome;
|
|
104
|
+
}
|
|
105
|
+
return readManifestFile(sibling);
|
|
106
|
+
}
|
|
107
|
+
const ABSENT = new Set(['ENOENT', 'ENOTDIR']);
|
|
108
|
+
export const exists = (path) => lstat(path).then(() => true, (error) => {
|
|
109
|
+
if (error.code !== undefined && ABSENT.has(error.code))
|
|
110
|
+
return false;
|
|
111
|
+
throw error;
|
|
112
|
+
});
|
|
113
|
+
const manifestFile = (run) => join(run.paths.hiveHome, INSTALL_MANIFEST_FILE);
|
|
114
|
+
const emptyCreated = () => ({ paths: [], worktree_root: null, service: NO_SERVICE, key_path: null, token_path: null, crontab: [] });
|
|
115
|
+
export function intentsFor(step, run) {
|
|
116
|
+
const { configDir, dataDir, lanesDir, envFile } = run.paths;
|
|
117
|
+
const { host, pin } = run.deps;
|
|
118
|
+
const config = (name) => join(configDir, name);
|
|
119
|
+
const files = (...paths) => paths.map((path) => ({ slot: 'path', path }));
|
|
120
|
+
switch (step) {
|
|
121
|
+
case 'init':
|
|
122
|
+
return files(run.paths.hiveHome, configDir, config('setup.json'), ...(run.opts.saveApiUrl ? [config(SHARED_CONFIG)] : []));
|
|
123
|
+
case 'login':
|
|
124
|
+
return files(config('user.json'), config('state.json'));
|
|
125
|
+
case 'enroll':
|
|
126
|
+
return [...files(config('state.json'), config('registration.json'), config(PENDING_KEY)), { slot: 'key_path', path: config('key.json') }, { slot: 'token_path', path: config('node.json') }];
|
|
127
|
+
case 'model-access':
|
|
128
|
+
return files(config('model-access.json'), config('codex'), config('codex/config.toml'));
|
|
129
|
+
case 'lanes':
|
|
130
|
+
return run.role === 'proxy-only' ? [] : [...files(dataDir, ...(pin.release ? [run.paths.releaseDir(pin.release.version)] : [])), { slot: 'worktree_root', path: lanesDir }];
|
|
131
|
+
case 'env':
|
|
132
|
+
return files(envFile);
|
|
133
|
+
case 'reporter':
|
|
134
|
+
return [{ slot: 'service', service: { ...reporterService(host), status: 'pending' } }];
|
|
135
|
+
case 'preflight':
|
|
136
|
+
case 'role':
|
|
137
|
+
case 'cron':
|
|
138
|
+
case 'drift':
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export function cronIntent(run) {
|
|
143
|
+
const role = run.role ?? 'worker';
|
|
144
|
+
return role === 'primary' || role === 'worker' || role === 'lite' ? [{ slot: 'crontab', marker: `# BEGIN hive ${role}`, user: run.deps.host.username }] : [];
|
|
145
|
+
}
|
|
146
|
+
async function enrolledNodeId(run) {
|
|
147
|
+
const node = await run.ctx.store.read('node.json').catch(() => null);
|
|
148
|
+
return node ? String(node.node_id) : null;
|
|
149
|
+
}
|
|
150
|
+
const byPath = (a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
151
|
+
async function reconcile(created, settle) {
|
|
152
|
+
const settleEntry = async (entry) => {
|
|
153
|
+
if (entry.status === 'done')
|
|
154
|
+
return entry;
|
|
155
|
+
if (await exists(entry.path))
|
|
156
|
+
return { path: entry.path, status: 'done' };
|
|
157
|
+
return settle.prune ? null : entry;
|
|
158
|
+
};
|
|
159
|
+
const paths = [];
|
|
160
|
+
for (const entry of created.paths) {
|
|
161
|
+
const next = await settleEntry(entry);
|
|
162
|
+
if (next)
|
|
163
|
+
paths.push(next);
|
|
164
|
+
}
|
|
165
|
+
const service = created.service;
|
|
166
|
+
const settledService = service.kind === 'none' || service.status === 'done'
|
|
167
|
+
? service
|
|
168
|
+
: (await exists(service.file))
|
|
169
|
+
? { ...service, status: 'done' }
|
|
170
|
+
: settle.prune
|
|
171
|
+
? NO_SERVICE
|
|
172
|
+
: service;
|
|
173
|
+
const present = new Set(settle.blocks);
|
|
174
|
+
const crontab = settle.blocks === undefined ? created.crontab : created.crontab.flatMap((entry) => (present.has(entry.marker) ? [{ ...entry, status: 'done' }] : settle.prune ? [] : [entry]));
|
|
175
|
+
return {
|
|
176
|
+
paths: paths.sort(byPath),
|
|
177
|
+
worktree_root: created.worktree_root && (await settleEntry(created.worktree_root)),
|
|
178
|
+
service: settledService,
|
|
179
|
+
key_path: created.key_path && (await settleEntry(created.key_path)),
|
|
180
|
+
token_path: created.token_path && (await settleEntry(created.token_path)),
|
|
181
|
+
crontab,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
export class OwnershipRefusal extends SetupError {
|
|
185
|
+
}
|
|
186
|
+
const refuseUnowned = (path) => new OwnershipRefusal(`${path} already exists and this install did not create it, so nothing was changed. Move it aside or remove it by hand, then run the same command again.`);
|
|
187
|
+
const refuseRelocated = (recorded, wanted) => new OwnershipRefusal(`This install recorded ${recorded}, but this run wants ${wanted}, so nothing was changed. Point back at the recorded location, or remove what the manifest lists by hand and move the manifest aside.`);
|
|
188
|
+
async function occupied(path) {
|
|
189
|
+
try {
|
|
190
|
+
if ((await lstat(path)).isSymbolicLink())
|
|
191
|
+
throw new OwnershipRefusal(`${path} is a symbolic link, which this install never creates, so nothing was changed. Replace it with a plain file or remove it by hand, then run the same command again.`);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (error instanceof OwnershipRefusal)
|
|
196
|
+
throw error;
|
|
197
|
+
const code = error.code ?? 'unknown error';
|
|
198
|
+
if (ABSENT.has(code))
|
|
199
|
+
return false;
|
|
200
|
+
throw new OwnershipRefusal(`${path} cannot be inspected (${code}), so nothing was changed. Fix its permissions, then run the same command again.`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async function claimPath(path, recorded) {
|
|
204
|
+
if (recorded && recorded.path !== path)
|
|
205
|
+
throw refuseRelocated(recorded.path, path);
|
|
206
|
+
const present = await occupied(path);
|
|
207
|
+
if (recorded)
|
|
208
|
+
return null;
|
|
209
|
+
if (present)
|
|
210
|
+
throw refuseUnowned(path);
|
|
211
|
+
return { path, status: 'pending' };
|
|
212
|
+
}
|
|
213
|
+
async function admit(created, intent, blocks) {
|
|
214
|
+
switch (intent.slot) {
|
|
215
|
+
case 'crontab': {
|
|
216
|
+
if (blocks === undefined)
|
|
217
|
+
throw new CrontabBaselineError();
|
|
218
|
+
const unjournaled = blocks.filter((marker) => !created.crontab.some((entry) => entry.marker === marker));
|
|
219
|
+
if (unjournaled.length > 0)
|
|
220
|
+
throw new UnjournaledBlockError(unjournaled);
|
|
221
|
+
if (created.crontab.some((entry) => entry.marker === intent.marker))
|
|
222
|
+
return created;
|
|
223
|
+
return { ...created, crontab: [...created.crontab, { marker: intent.marker, user: intent.user, status: 'pending' }] };
|
|
224
|
+
}
|
|
225
|
+
case 'service': {
|
|
226
|
+
const { service } = created;
|
|
227
|
+
if (service.kind !== 'none') {
|
|
228
|
+
if (service.file !== intent.service.file)
|
|
229
|
+
throw refuseRelocated(service.file, intent.service.file);
|
|
230
|
+
await occupied(service.file);
|
|
231
|
+
return created;
|
|
232
|
+
}
|
|
233
|
+
if (await occupied(intent.service.file))
|
|
234
|
+
throw refuseUnowned(intent.service.file);
|
|
235
|
+
return { ...created, service: intent.service };
|
|
236
|
+
}
|
|
237
|
+
case 'path': {
|
|
238
|
+
const entry = await claimPath(intent.path, created.paths.find((recorded) => recorded.path === intent.path));
|
|
239
|
+
return entry ? { ...created, paths: [...created.paths, entry].sort(byPath) } : created;
|
|
240
|
+
}
|
|
241
|
+
default: {
|
|
242
|
+
const entry = await claimPath(intent.path, created[intent.slot]);
|
|
243
|
+
return entry ? { ...created, [intent.slot]: entry } : created;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export class CrontabBaselineError extends OwnershipRefusal {
|
|
248
|
+
constructor() {
|
|
249
|
+
super('The current crontab could not be read, so no scheduled-job block was applied or recorded. Fix `crontab -l` and run the same command again.');
|
|
250
|
+
this.name = 'CrontabBaselineError';
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export class UnjournaledBlockError extends OwnershipRefusal {
|
|
254
|
+
constructor(markers) {
|
|
255
|
+
super(`The current crontab holds a hive block this install did not create (${markers.join(', ')}), so replacing it would leave the change unrecorded and no scheduled-job block was applied. Remove that block by hand, then run the same command again.`);
|
|
256
|
+
this.name = 'UnjournaledBlockError';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const baselines = new WeakMap();
|
|
260
|
+
async function persistManifest(run, previous, created) {
|
|
261
|
+
if (!previous && created.paths.length === 0 && created.worktree_root === null && created.service.kind === 'none' && created.key_path === null && created.token_path === null && created.crontab.length === 0)
|
|
262
|
+
return;
|
|
263
|
+
const body = {
|
|
264
|
+
version: 2,
|
|
265
|
+
node_id: (await enrolledNodeId(run)) ?? previous?.node_id ?? null,
|
|
266
|
+
api_url: run.ctx.url,
|
|
267
|
+
created,
|
|
268
|
+
kit_release: run.saved.applied.release ?? null,
|
|
269
|
+
};
|
|
270
|
+
const content = JSON.stringify({ ...body, written_at: '' });
|
|
271
|
+
if (previous && JSON.stringify({ ...previous, written_at: '' }) === content && (await exists(manifestFile(run))))
|
|
272
|
+
return;
|
|
273
|
+
const baseline = baselines.get(run);
|
|
274
|
+
const unchangedSinceStart = baseline && JSON.stringify({ ...baseline, written_at: '' }) === content;
|
|
275
|
+
const manifest = { ...body, written_at: unchangedSinceStart ? baseline.written_at : run.ctx.now().toISOString() };
|
|
276
|
+
const text = `${JSON.stringify(manifest, null, 2)}\n`;
|
|
277
|
+
const { hiveHome } = run.paths;
|
|
278
|
+
if (await exists(hiveHome)) {
|
|
279
|
+
await writeAtomic(manifestFile(run), text, 0o600);
|
|
280
|
+
await rm(journalSibling(hiveHome), { force: true });
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!(await exists(dirname(hiveHome))))
|
|
284
|
+
throw new SetupError(`${dirname(hiveHome)} does not exist, so ${hiveHome} cannot be created. Create that directory or point HIVE_HOME at one that exists.`);
|
|
285
|
+
await writeAtomic(journalSibling(hiveHome), text, 0o600);
|
|
286
|
+
await run.deps.journalFault?.('after-sibling', 'home');
|
|
287
|
+
await mkdir(hiveHome);
|
|
288
|
+
await run.deps.journalFault?.('after-mkdir', 'home');
|
|
289
|
+
await rename(journalSibling(hiveHome), manifestFile(run));
|
|
290
|
+
await persistManifest(run, manifest, await reconcile(created, { prune: false, blocks: undefined }));
|
|
291
|
+
}
|
|
292
|
+
export async function journalAhead(run, intents, probe) {
|
|
293
|
+
const previous = await readInstallManifest(manifestFile(run));
|
|
294
|
+
if (!baselines.has(run))
|
|
295
|
+
baselines.set(run, previous);
|
|
296
|
+
const wantsCrontab = intents.some((intent) => intent.slot === 'crontab');
|
|
297
|
+
const blocks = (probe && (previous?.created.crontab.length || wantsCrontab) ? await probe() : null) ?? undefined;
|
|
298
|
+
let created = await reconcile(previous?.created ?? emptyCreated(), { prune: false, blocks });
|
|
299
|
+
for (const intent of intents)
|
|
300
|
+
created = await admit(created, intent, blocks);
|
|
301
|
+
await persistManifest(run, previous, created);
|
|
302
|
+
}
|
|
303
|
+
export async function journalSettle(run, probe) {
|
|
304
|
+
const previous = await readInstallManifest(manifestFile(run));
|
|
305
|
+
if (!previous)
|
|
306
|
+
return;
|
|
307
|
+
const blocks = (probe && previous.created.crontab.length ? await probe() : null) ?? undefined;
|
|
308
|
+
await persistManifest(run, previous, await reconcile(previous.created, { prune: true, blocks }));
|
|
309
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Context } from '../context.ts';
|
|
2
|
+
export declare const MODEL_ACCESS_METHODS: readonly ["own_proxy", "byok", "hive_credits", "overcy_pool"];
|
|
3
|
+
export type ModelAccessMethod = (typeof MODEL_ACCESS_METHODS)[number];
|
|
4
|
+
export type ModelAccess = {
|
|
5
|
+
version: number;
|
|
6
|
+
method: null;
|
|
7
|
+
server?: string;
|
|
8
|
+
} | {
|
|
9
|
+
version: number;
|
|
10
|
+
method: ModelAccessMethod;
|
|
11
|
+
server?: string;
|
|
12
|
+
upstream: {
|
|
13
|
+
anthropic: string;
|
|
14
|
+
openai: string;
|
|
15
|
+
};
|
|
16
|
+
credential: {
|
|
17
|
+
kind: 'bearer';
|
|
18
|
+
value: string;
|
|
19
|
+
expires_at: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export type ModelAccessSource = {
|
|
23
|
+
get: (signal: AbortSignal) => Promise<ModelAccess>;
|
|
24
|
+
};
|
|
25
|
+
export type ModelAccessStore = {
|
|
26
|
+
kind: 'keychain' | 'file';
|
|
27
|
+
read: () => Promise<ModelAccess | null>;
|
|
28
|
+
write: (access: ModelAccess) => Promise<void>;
|
|
29
|
+
};
|
|
30
|
+
export type ForwarderSettings = {
|
|
31
|
+
host: '127.0.0.1';
|
|
32
|
+
port: number;
|
|
33
|
+
localKey: string;
|
|
34
|
+
anthropicPath: '/anthropic';
|
|
35
|
+
openaiPath: '/openai/v1';
|
|
36
|
+
};
|
|
37
|
+
export declare class ModelAccessContractError extends Error {
|
|
38
|
+
constructor(message: string);
|
|
39
|
+
}
|
|
40
|
+
export declare const isModelAccessMethod: (value: unknown) => value is ModelAccessMethod;
|
|
41
|
+
export declare function parseModelAccess(raw: unknown): ModelAccess;
|
|
42
|
+
export declare function httpModelAccessSource(ctx: Context): ModelAccessSource;
|
|
43
|
+
export declare function sameAccess(saved: ModelAccess | null, fresh: ModelAccess, now: Date): boolean;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ApiError } from "../api.js";
|
|
2
|
+
export const MODEL_ACCESS_METHODS = ['own_proxy', 'byok', 'hive_credits', 'overcy_pool'];
|
|
3
|
+
export class ModelAccessContractError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'ModelAccessContractError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export const isModelAccessMethod = (value) => typeof value === 'string' && MODEL_ACCESS_METHODS.includes(value);
|
|
10
|
+
const isString = (value) => typeof value === 'string' && value !== '';
|
|
11
|
+
function httpUrl(value, field) {
|
|
12
|
+
if (!isString(value))
|
|
13
|
+
throw new ModelAccessContractError(`The model-access answer has no ${field}.`);
|
|
14
|
+
let url;
|
|
15
|
+
try {
|
|
16
|
+
url = new URL(value);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
throw new ModelAccessContractError(`The model-access answer's ${field} is not a URL.`);
|
|
20
|
+
}
|
|
21
|
+
if (url.protocol !== 'https:' && url.protocol !== 'http:')
|
|
22
|
+
throw new ModelAccessContractError(`The model-access answer's ${field} must be http or https.`);
|
|
23
|
+
if (url.username || url.password)
|
|
24
|
+
throw new ModelAccessContractError(`The model-access answer's ${field} must not carry a password.`);
|
|
25
|
+
return url.toString().replace(/\/+$/, '');
|
|
26
|
+
}
|
|
27
|
+
export function parseModelAccess(raw) {
|
|
28
|
+
const body = (raw ?? {});
|
|
29
|
+
if (typeof body.version !== 'number' || !Number.isInteger(body.version) || body.version < 0)
|
|
30
|
+
throw new ModelAccessContractError('The model-access answer has no version.');
|
|
31
|
+
if (body.method === null || body.method === undefined)
|
|
32
|
+
return { version: body.version, method: null };
|
|
33
|
+
if (!isModelAccessMethod(body.method))
|
|
34
|
+
throw new ModelAccessContractError(`The model-access answer names an unknown method (${JSON.stringify(body.method)}).`);
|
|
35
|
+
const upstream = (body.upstream ?? {});
|
|
36
|
+
const credential = (body.credential ?? {});
|
|
37
|
+
if (credential.kind !== 'bearer' || !isString(credential.value))
|
|
38
|
+
throw new ModelAccessContractError('The model-access answer has no bearer credential.');
|
|
39
|
+
if (!isString(credential.expires_at) || Number.isNaN(Date.parse(credential.expires_at)))
|
|
40
|
+
throw new ModelAccessContractError('The model-access credential has a bad expires_at.');
|
|
41
|
+
return {
|
|
42
|
+
version: body.version,
|
|
43
|
+
method: body.method,
|
|
44
|
+
upstream: { anthropic: httpUrl(upstream.anthropic, 'upstream.anthropic'), openai: httpUrl(upstream.openai, 'upstream.openai') },
|
|
45
|
+
credential: { kind: 'bearer', value: credential.value, expires_at: credential.expires_at },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function httpModelAccessSource(ctx) {
|
|
49
|
+
return {
|
|
50
|
+
async get(signal) {
|
|
51
|
+
const node = await ctx.store.read('node.json');
|
|
52
|
+
const key = await ctx.store.read('key.json');
|
|
53
|
+
if (!node || !key)
|
|
54
|
+
throw new ModelAccessContractError('This node is not enrolled, so it cannot ask Hive for its model access.');
|
|
55
|
+
try {
|
|
56
|
+
return parseModelAccess(await ctx.api.json('GET', '/api/v0/nodes/me/model-access', { token: node.token, key, signal }));
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error instanceof ApiError && error.status === 404)
|
|
60
|
+
throw new ModelAccessContractError('Hive does not serve model access yet (404 from /api/v0/nodes/me/model-access).');
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function sameAccess(saved, fresh, now) {
|
|
67
|
+
if (!saved || saved.version !== fresh.version || saved.method !== fresh.method)
|
|
68
|
+
return false;
|
|
69
|
+
if (saved.method === null || fresh.method === null)
|
|
70
|
+
return true;
|
|
71
|
+
if (saved.upstream.anthropic !== fresh.upstream.anthropic || saved.upstream.openai !== fresh.upstream.openai)
|
|
72
|
+
return false;
|
|
73
|
+
return Date.parse(saved.credential.expires_at) - now.getTime() > 30 * 60_000;
|
|
74
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const PIN = {
|
|
2
|
+
release: {
|
|
3
|
+
version: 'v0.2.1',
|
|
4
|
+
url: 'https://github.com/OvercyAI/hive-kit/releases/download/v0.2.1/overcy-hive-v0.2.1.tgz',
|
|
5
|
+
sha256: 'bed00f92f7e33a3d759e323db75a2072e1d8199050d3e976c3473503fc5d6108',
|
|
6
|
+
files: {
|
|
7
|
+
'install/install-local.sh': '84e5bcf27d454115d665b16e7f84c63d9f6eee21445fa8d66e28b8ab85ba341f',
|
|
8
|
+
'install/manifest.sh': '1d7215e4893283784491630554e88e74355147bb7922087019bda2efe80f82c7',
|
|
9
|
+
'install/lib.sh': '3a2bb9fb1829cf279e13ce716fe3d4fdaacb24c4e4e63bcda5ea83f738028b49',
|
|
10
|
+
'lanes/drift-check.sh': 'c234540161d395fb37c68e6e21f3f8c823bc2f82a661bdbb08fecafe37c3aa34',
|
|
11
|
+
'lanes/drift-lib.sh': 'dfa38cd8c905d78859d33c023de45a2da3589cc189df6734a16a8c5dd06d783e',
|
|
12
|
+
'lanes/hive-env.sh': 'fd306fe262202953ec12ec856d79dbdf4b6d241e31e41324caa9f0c72832b8a5',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
proxy: { version: '0.1.0' },
|
|
16
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure part of the step registry. Nothing here touches the machine, the
|
|
3
|
+
* network or the clock, so a table test can pin every case.
|
|
4
|
+
*
|
|
5
|
+
* Health says what a step's observation found:
|
|
6
|
+
* - `ok`: nothing to do.
|
|
7
|
+
* - `skipped`: the step does not apply to this node.
|
|
8
|
+
* - `stale`: out of date, but what later steps need still holds. The repair is
|
|
9
|
+
* queued and dependents are not blocked.
|
|
10
|
+
* - `failing`: the step does not work. The repair is queued and every
|
|
11
|
+
* dependent is blocked until a later tick finds it healthy.
|
|
12
|
+
* - `needs_action`: only a person can fix it. No repair is queued and every
|
|
13
|
+
* dependent is blocked.
|
|
14
|
+
*/
|
|
15
|
+
export type Health = 'ok' | 'skipped' | 'stale' | 'failing' | 'needs_action';
|
|
16
|
+
export type Observed = {
|
|
17
|
+
id: string;
|
|
18
|
+
status: Health;
|
|
19
|
+
};
|
|
20
|
+
export type Plannable = {
|
|
21
|
+
id: string;
|
|
22
|
+
needs: readonly string[];
|
|
23
|
+
};
|
|
24
|
+
export type Blocked = {
|
|
25
|
+
id: string;
|
|
26
|
+
by: string[];
|
|
27
|
+
};
|
|
28
|
+
export type Plan = {
|
|
29
|
+
/** Step ids to repair, in step order. */
|
|
30
|
+
repairs: string[];
|
|
31
|
+
/** Steps held back, each with the root causes that hold it. */
|
|
32
|
+
blocked: Blocked[];
|
|
33
|
+
};
|
|
34
|
+
export declare class PlanError extends Error {
|
|
35
|
+
constructor(message: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Queue repairs in step order and block the dependents of a broken step.
|
|
39
|
+
*
|
|
40
|
+
* A step is blocked when a step it needs is `failing` or `needs_action`, or is
|
|
41
|
+
* itself blocked. `by` names the root causes, not the chain between them. A
|
|
42
|
+
* blocked step queues no repair, whatever its own observation says, because its
|
|
43
|
+
* repair assumes what it needs works.
|
|
44
|
+
*
|
|
45
|
+
* Throws `PlanError` when a step needs an id that is unknown, is itself, or
|
|
46
|
+
* comes later in the list, and when the observations do not cover each step
|
|
47
|
+
* exactly once. A silent miss here would hide a step from every tick.
|
|
48
|
+
*/
|
|
49
|
+
export declare function planTick(observations: readonly Observed[], steps: readonly Plannable[]): Plan;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export class PlanError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'PlanError';
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const BREAKS_DEPENDENTS = new Set(['failing', 'needs_action']);
|
|
8
|
+
const QUEUES_REPAIR = new Set(['stale', 'failing']);
|
|
9
|
+
/**
|
|
10
|
+
* Queue repairs in step order and block the dependents of a broken step.
|
|
11
|
+
*
|
|
12
|
+
* A step is blocked when a step it needs is `failing` or `needs_action`, or is
|
|
13
|
+
* itself blocked. `by` names the root causes, not the chain between them. A
|
|
14
|
+
* blocked step queues no repair, whatever its own observation says, because its
|
|
15
|
+
* repair assumes what it needs works.
|
|
16
|
+
*
|
|
17
|
+
* Throws `PlanError` when a step needs an id that is unknown, is itself, or
|
|
18
|
+
* comes later in the list, and when the observations do not cover each step
|
|
19
|
+
* exactly once. A silent miss here would hide a step from every tick.
|
|
20
|
+
*/
|
|
21
|
+
export function planTick(observations, steps) {
|
|
22
|
+
const position = new Map();
|
|
23
|
+
steps.forEach((step, index) => {
|
|
24
|
+
if (position.has(step.id))
|
|
25
|
+
throw new PlanError(`Step ${step.id} is listed twice.`);
|
|
26
|
+
position.set(step.id, index);
|
|
27
|
+
});
|
|
28
|
+
for (const step of steps) {
|
|
29
|
+
for (const need of step.needs) {
|
|
30
|
+
const at = position.get(need);
|
|
31
|
+
if (at === undefined)
|
|
32
|
+
throw new PlanError(`Step ${step.id} needs ${need}, which is not a step.`);
|
|
33
|
+
if (at >= position.get(step.id))
|
|
34
|
+
throw new PlanError(`Step ${step.id} needs ${need}, which does not come before it.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const status = new Map();
|
|
38
|
+
for (const observed of observations) {
|
|
39
|
+
if (!position.has(observed.id))
|
|
40
|
+
throw new PlanError(`Observed ${observed.id}, which is not a step.`);
|
|
41
|
+
if (status.has(observed.id))
|
|
42
|
+
throw new PlanError(`Observed ${observed.id} twice.`);
|
|
43
|
+
status.set(observed.id, observed.status);
|
|
44
|
+
}
|
|
45
|
+
for (const step of steps)
|
|
46
|
+
if (!status.has(step.id))
|
|
47
|
+
throw new PlanError(`No observation for ${step.id}.`);
|
|
48
|
+
const rootsOf = new Map();
|
|
49
|
+
const plan = { repairs: [], blocked: [] };
|
|
50
|
+
for (const step of steps) {
|
|
51
|
+
const roots = [];
|
|
52
|
+
for (const need of step.needs) {
|
|
53
|
+
const held = rootsOf.get(need);
|
|
54
|
+
if (held)
|
|
55
|
+
roots.push(...held);
|
|
56
|
+
else if (BREAKS_DEPENDENTS.has(status.get(need)))
|
|
57
|
+
roots.push(need);
|
|
58
|
+
}
|
|
59
|
+
const by = [...new Set(roots)];
|
|
60
|
+
if (by.length > 0) {
|
|
61
|
+
rootsOf.set(step.id, by);
|
|
62
|
+
plan.blocked.push({ id: step.id, by });
|
|
63
|
+
}
|
|
64
|
+
else if (QUEUES_REPAIR.has(status.get(step.id))) {
|
|
65
|
+
plan.repairs.push(step.id);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return plan;
|
|
69
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline/promises';
|
|
2
|
+
const readlineAsk = async (question) => {
|
|
3
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
4
|
+
try {
|
|
5
|
+
return await rl.question(question);
|
|
6
|
+
}
|
|
7
|
+
finally {
|
|
8
|
+
rl.close();
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
export function makePrompter(interactive, ask = readlineAsk) {
|
|
12
|
+
return {
|
|
13
|
+
interactive,
|
|
14
|
+
async choose(question, choices, fallback) {
|
|
15
|
+
if (!interactive)
|
|
16
|
+
return fallback;
|
|
17
|
+
const canon = (text) => text.trim().toLowerCase().replace(/[\s_]+/g, '-');
|
|
18
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
19
|
+
const answer = (await ask(`${question} (${choices.join('/')}) [${fallback}] `)).trim();
|
|
20
|
+
if (answer === '')
|
|
21
|
+
return fallback;
|
|
22
|
+
const match = choices.find((choice) => canon(choice) === canon(answer));
|
|
23
|
+
if (match)
|
|
24
|
+
return match;
|
|
25
|
+
process.stderr.write(`"${answer}" is not a valid choice. Pick one of ${choices.join(', ')}.\n`);
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`That is not a valid choice. Expected one of ${choices.join(', ')}.`);
|
|
28
|
+
},
|
|
29
|
+
async confirm(question) {
|
|
30
|
+
if (!interactive)
|
|
31
|
+
return true;
|
|
32
|
+
return /^y/i.test((await ask(`${question} [y/N] `)).trim());
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|