@agentage/extension 0.14.0 → 0.15.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 +3 -1
- package/dist/health/health.d.ts +2 -0
- package/dist/health/health.js +4 -2
- package/dist/health/health.schema.d.ts +9 -0
- package/dist/health/health.schema.js +3 -0
- package/dist/host/clock-rate.d.ts +1 -0
- package/dist/host/clock-rate.js +12 -0
- package/dist/host/daemon/add.js +24 -3
- package/dist/host/daemon/child-files.d.ts +20 -0
- package/dist/host/daemon/child-files.js +45 -0
- package/dist/host/daemon/child.d.ts +10 -4
- package/dist/host/daemon/child.js +63 -37
- package/dist/host/daemon/daemon-files.d.ts +3 -0
- package/dist/host/daemon/daemon-files.js +5 -1
- package/dist/host/daemon/daemon.d.ts +1 -0
- package/dist/host/daemon/daemon.js +21 -6
- package/dist/host/daemon/gate.d.ts +3 -0
- package/dist/host/daemon/gate.js +19 -0
- package/dist/host/daemon/guard.d.ts +2 -1
- package/dist/host/daemon/guard.js +16 -4
- package/dist/host/daemon/health-routes.d.ts +17 -0
- package/dist/host/daemon/health-routes.js +61 -0
- package/dist/host/daemon/host-token.d.ts +6 -0
- package/dist/host/daemon/host-token.js +28 -0
- package/dist/host/daemon/last-words.d.ts +2 -0
- package/dist/host/daemon/last-words.js +28 -0
- package/dist/host/daemon/limits.d.ts +4 -0
- package/dist/host/daemon/limits.js +7 -0
- package/dist/host/daemon/ops.js +4 -1
- package/dist/host/daemon/pending.d.ts +1 -1
- package/dist/host/daemon/proxy.d.ts +1 -1
- package/dist/host/daemon/proxy.js +16 -8
- package/dist/host/daemon/reload-marker.d.ts +8 -0
- package/dist/host/daemon/reload-marker.js +56 -0
- package/dist/host/daemon/reload-route.d.ts +4 -0
- package/dist/host/daemon/reload-route.js +21 -0
- package/dist/host/daemon/reload.d.ts +10 -0
- package/dist/host/daemon/reload.js +73 -0
- package/dist/host/daemon/restart.d.ts +14 -0
- package/dist/host/daemon/restart.js +59 -0
- package/dist/host/daemon/routes.d.ts +2 -10
- package/dist/host/daemon/routes.js +13 -31
- package/dist/host/daemon/run-lock.d.ts +1 -0
- package/dist/host/daemon/run-lock.js +15 -0
- package/dist/host/daemon/seal-reach.d.ts +2 -0
- package/dist/host/daemon/seal-reach.js +38 -0
- package/dist/host/daemon/seal.d.ts +16 -0
- package/dist/host/daemon/seal.js +71 -0
- package/dist/host/daemon/start-checks.d.ts +6 -0
- package/dist/host/daemon/start-checks.js +28 -0
- package/dist/host/daemon/supervisor.d.ts +9 -3
- package/dist/host/daemon/supervisor.js +33 -40
- package/dist/host/data-lock.d.ts +2 -1
- package/dist/host/data-lock.js +25 -6
- package/dist/host/host-env.d.ts +1 -0
- package/dist/host/host-env.js +2 -0
- package/dist/host/host-extension.d.ts +4 -0
- package/dist/host/host-extension.js +23 -43
- package/dist/host/host-parts.d.ts +25 -0
- package/dist/host/host-parts.js +75 -0
- package/dist/host/host-store.d.ts +3 -0
- package/dist/host/host-store.js +21 -2
- package/dist/host/main.js +2 -0
- package/dist/host/part-checks.d.ts +2 -0
- package/dist/host/part-checks.js +19 -0
- package/dist/host/serve-host.js +6 -3
- package/dist/manifest/parts.d.ts +3 -0
- package/dist/manifest/parts.js +3 -0
- package/dist/mcp/mcp-app.d.ts +4 -0
- package/dist/mcp/mcp-app.js +11 -11
- package/dist/runtime/part-mounts.d.ts +29 -0
- package/dist/runtime/part-mounts.js +52 -0
- package/dist/runtime/part-runners.d.ts +2 -15
- package/dist/runtime/part-runners.js +13 -58
- package/dist/server/app.d.ts +3 -0
- package/dist/server/app.js +16 -16
- package/dist/tool/check.d.ts +2 -0
- package/dist/tool/check.js +23 -8
- package/dist/tool/container-parts.d.ts +1 -1
- package/dist/tool/container-parts.js +6 -6
- package/dist/tool/format.js +15 -8
- package/dist/tool/kit-version.d.ts +2 -0
- package/dist/tool/kit-version.js +12 -0
- package/dist/worker/clock.d.ts +6 -0
- package/dist/worker/clock.js +21 -0
- package/dist/worker/scheduler.d.ts +1 -0
- package/dist/worker/scheduler.js +6 -2
- package/package.json +1 -1
- package/schemas/health.schema.json +17 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { dirname, join, parse, sep } from 'node:path';
|
|
4
|
+
import { assertReach, real } from './seal-reach.js';
|
|
5
|
+
// C2: the permission model with the bypass fixes of D2; the same rule as e2e sealFloorMet.
|
|
6
|
+
export const SEAL_FLOOR = 'Node 22.22.2 or 24.14.1';
|
|
7
|
+
const versionOf = (version) => version.split('.').map(Number);
|
|
8
|
+
const atLeast = (version, major, minor, patch) => {
|
|
9
|
+
const [a = 0, b = 0, c = 0] = versionOf(version);
|
|
10
|
+
return a === major && (b > minor || (b === minor && c >= patch));
|
|
11
|
+
};
|
|
12
|
+
// An odd major has no known fix release: off; an even major above 24 is on.
|
|
13
|
+
export const sealOf = (version) => {
|
|
14
|
+
const [major = 0] = versionOf(version);
|
|
15
|
+
if (major === 22)
|
|
16
|
+
return atLeast(version, 22, 22, 2) ? 'on' : 'off';
|
|
17
|
+
if (major === 24)
|
|
18
|
+
return atLeast(version, 24, 14, 1) ? 'on' : 'off';
|
|
19
|
+
return major > 24 && major % 2 === 0 ? 'on' : 'off';
|
|
20
|
+
};
|
|
21
|
+
// --permission is a stable flag from 22.13.0 and 23.5.0: below the floor it still limits, seal off.
|
|
22
|
+
export const permissionFlag = (version) => {
|
|
23
|
+
const [major = 0] = versionOf(version);
|
|
24
|
+
return major >= 24 || atLeast(version, 22, 13, 0) || atLeast(version, 23, 5, 0);
|
|
25
|
+
};
|
|
26
|
+
export const sealOffReason = (version) => `seal off: ${SEAL_FLOOR} needed, this is Node ${version}`;
|
|
27
|
+
const NODE_MODULES = `${sep}node_modules${sep}`;
|
|
28
|
+
// The folder that holds the node_modules a path sits in (a tree of the cli); else the path itself.
|
|
29
|
+
export const treeOf = (dir) => {
|
|
30
|
+
const at = dir.lastIndexOf(NODE_MODULES);
|
|
31
|
+
return at < 0 ? dir : dir.slice(0, at);
|
|
32
|
+
};
|
|
33
|
+
const hasWorkspaces = (dir) => {
|
|
34
|
+
try {
|
|
35
|
+
return 'workspaces' in JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// The folders from start up, never above the user's home nor the root.
|
|
42
|
+
const upward = (start) => {
|
|
43
|
+
const found = [start];
|
|
44
|
+
for (let dir = start; dir !== homedir() && dir !== parse(dir).root;)
|
|
45
|
+
found.push((dir = dirname(dir)));
|
|
46
|
+
return found;
|
|
47
|
+
};
|
|
48
|
+
// The nearest folder with a package.json; none: the folder of the file.
|
|
49
|
+
const packageRoot = (file) => upward(dirname(file)).find((dir) => existsSync(join(dir, 'package.json'))) ?? dirname(file);
|
|
50
|
+
// Installed: the node_modules that holds the kit. A checkout: its package and its workspace root.
|
|
51
|
+
export const kitReadDirs = (main) => {
|
|
52
|
+
const at = main.lastIndexOf(NODE_MODULES);
|
|
53
|
+
if (at >= 0)
|
|
54
|
+
return [main.slice(0, at + NODE_MODULES.length - 1)];
|
|
55
|
+
const root = packageRoot(main);
|
|
56
|
+
return [...new Set([root, upward(root).find(hasWorkspaces) ?? root])];
|
|
57
|
+
};
|
|
58
|
+
// C2: read kit, tree, run/{id}, data/{id}; write the last two; no child process, worker, addon, WASI, sqlite.
|
|
59
|
+
export const sealFlags = ({ id, main, dir, configDir, runDir, dataDir }) => {
|
|
60
|
+
const both = (paths) => [...new Set(paths.flatMap((path) => [path, real(path)]))];
|
|
61
|
+
const writes = both([runDir, dataDir]);
|
|
62
|
+
const kit = both([...kitReadDirs(main), treeOf(dir)]);
|
|
63
|
+
assertReach(kit, writes, configDir, id);
|
|
64
|
+
const reads = [...new Set([...kit, ...writes])];
|
|
65
|
+
return [
|
|
66
|
+
'--permission',
|
|
67
|
+
'--no-experimental-sqlite',
|
|
68
|
+
...reads.map((path) => `--allow-fs-read=${path}`),
|
|
69
|
+
...writes.map((path) => `--allow-fs-write=${path}`),
|
|
70
|
+
];
|
|
71
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../../manifest/manifest.schema.js';
|
|
2
|
+
export declare const NEEDS_SECRET = "NEEDS_SECRET";
|
|
3
|
+
export declare const assertSecrets: (pkg: ExtensionPackage, secrets: Record<string, string>) => void;
|
|
4
|
+
export declare const needsSecret: (err: unknown) => boolean;
|
|
5
|
+
export declare const healthy: (port: number, ms: number) => Promise<void>;
|
|
6
|
+
export declare const redact: (text: string, secrets: Record<string, string>) => string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {} from '../../manifest/manifest.schema.js';
|
|
2
|
+
import { hostParts, mountedIn } from '../../manifest/parts.js';
|
|
3
|
+
export const NEEDS_SECRET = 'NEEDS_SECRET';
|
|
4
|
+
// C6: a declared secret of a server part with no value; the daemon checks it before the fork.
|
|
5
|
+
export const assertSecrets = (pkg, secrets) => {
|
|
6
|
+
const names = new Set(hostParts(pkg).flatMap((part) => mountedIn(pkg, part)));
|
|
7
|
+
const missing = [...names].find((name) => !secrets[name]);
|
|
8
|
+
if (missing)
|
|
9
|
+
throw Object.assign(new Error(`needs secret ${missing}`), { code: NEEDS_SECRET });
|
|
10
|
+
};
|
|
11
|
+
export const needsSecret = (err) => err?.code === NEEDS_SECRET;
|
|
12
|
+
// Healthy before it gets the route: the child's own /health answers 200.
|
|
13
|
+
export const healthy = async (port, ms) => {
|
|
14
|
+
const signal = AbortSignal.timeout(ms);
|
|
15
|
+
const res = await fetch(`http://127.0.0.1:${port}/health`, { signal }).catch((err) => {
|
|
16
|
+
if (err.name === 'TimeoutError')
|
|
17
|
+
throw new Error(`health did not answer in ${ms} ms`);
|
|
18
|
+
const cause = err.cause?.code ?? err.message;
|
|
19
|
+
throw new Error(`health could not be reached (${cause})`);
|
|
20
|
+
});
|
|
21
|
+
if (res.status !== 200)
|
|
22
|
+
throw new Error(`health answered ${res.status}`);
|
|
23
|
+
};
|
|
24
|
+
// A secret value this child was given never reaches error, /health, the list or the log.
|
|
25
|
+
export const redact = (text, secrets) => Object.values(secrets)
|
|
26
|
+
.filter((value) => value.length >= 4)
|
|
27
|
+
.sort((a, b) => b.length - a.length)
|
|
28
|
+
.reduce((out, value) => out.split(value).join('***'), text);
|
|
@@ -5,7 +5,9 @@ import { type Lane } from './lane.js';
|
|
|
5
5
|
import { type Limits } from './limits.js';
|
|
6
6
|
import { type PendingChanges } from './pending.js';
|
|
7
7
|
import { type Queue } from './queue.js';
|
|
8
|
+
import { type Retry } from './restart.js';
|
|
8
9
|
import { type Stopping } from './stopping.js';
|
|
10
|
+
export { halt, hold } from './gate.js';
|
|
9
11
|
export interface InstallSpec {
|
|
10
12
|
id: string;
|
|
11
13
|
version?: string;
|
|
@@ -26,6 +28,9 @@ export interface Entry {
|
|
|
26
28
|
error?: string;
|
|
27
29
|
lane?: Lane;
|
|
28
30
|
gate?: Promise<void>;
|
|
31
|
+
exits?: number[];
|
|
32
|
+
restarts?: number;
|
|
33
|
+
retry?: Retry;
|
|
29
34
|
}
|
|
30
35
|
export interface Values {
|
|
31
36
|
settings: Record<string, unknown>;
|
|
@@ -48,7 +53,8 @@ export interface SupervisorDeps extends Limits {
|
|
|
48
53
|
unsynced: Set<string>;
|
|
49
54
|
}
|
|
50
55
|
export declare const valuesOf: (configDir: string, id: string) => Promise<Values>;
|
|
51
|
-
export declare const
|
|
52
|
-
export declare const
|
|
56
|
+
export declare const launchFor: (deps: SupervisorDeps, entry: Entry, install: InstallEntry, values: Values, given?: string) => Promise<Lane>;
|
|
57
|
+
export declare const startEntry: (deps: SupervisorDeps, entry: Entry) => Promise<Error | undefined>;
|
|
58
|
+
export declare const restore: (deps: SupervisorDeps, entry: Entry) => Promise<void>;
|
|
59
|
+
export declare const startFailed: (entry: Entry, err: unknown) => import("../../envelope/envelope.js").Failure;
|
|
53
60
|
export declare const swap: (deps: SupervisorDeps, entry: Entry, to: Next, undo?: () => Promise<unknown>) => Promise<void>;
|
|
54
|
-
export declare const halt: (entry: Entry, drainMs: number) => Promise<void>;
|
|
@@ -4,12 +4,16 @@ import { readManifest } from '../../manifest/read-manifest.js';
|
|
|
4
4
|
import {} from './child.js';
|
|
5
5
|
import { dataDirOf, readValues } from './daemon-files.js';
|
|
6
6
|
import {} from './installation.js';
|
|
7
|
-
import {
|
|
7
|
+
import { halt } from './gate.js';
|
|
8
|
+
import { laneOf } from './lane.js';
|
|
8
9
|
import {} from './limits.js';
|
|
9
10
|
import { runInstaller } from './installer-call.js';
|
|
10
11
|
import {} from './pending.js';
|
|
11
12
|
import {} from './queue.js';
|
|
13
|
+
import { crashed, healthyFor, reasonOf, resetRestarts } from './restart.js';
|
|
14
|
+
import { assertSecrets, healthy, redact } from './start-checks.js';
|
|
12
15
|
import {} from './stopping.js';
|
|
16
|
+
export { halt, hold } from './gate.js';
|
|
13
17
|
export const valuesOf = async (configDir, id) => ({
|
|
14
18
|
settings: await readValues(configDir, 'settings', id),
|
|
15
19
|
secrets: (await readValues(configDir, 'secrets', id)),
|
|
@@ -24,82 +28,77 @@ const dirOf = async (deps, entry, install, given) => {
|
|
|
24
28
|
return entry.dir;
|
|
25
29
|
return (await runInstaller(deps, { id: entry.id, version: install.version })).dir;
|
|
26
30
|
};
|
|
27
|
-
// Healthy before it gets the route: the child's own /health answers 200.
|
|
28
|
-
const healthy = async (port, ms) => {
|
|
29
|
-
const signal = AbortSignal.timeout(ms);
|
|
30
|
-
const res = await fetch(`http://127.0.0.1:${port}/health`, { signal }).catch((err) => {
|
|
31
|
-
if (err.name === 'TimeoutError')
|
|
32
|
-
throw new Error(`health did not answer in ${ms} ms`);
|
|
33
|
-
const cause = err.cause?.code ?? err.message;
|
|
34
|
-
throw new Error(`health could not be reached (${cause})`);
|
|
35
|
-
});
|
|
36
|
-
if (res.status !== 200)
|
|
37
|
-
throw new Error(`health answered ${res.status}`);
|
|
38
|
-
};
|
|
39
31
|
// A new child for install with values, healthy; the entry learns the version the package has.
|
|
40
|
-
const launchFor = async (deps, entry, install, values, given) => {
|
|
32
|
+
export const launchFor = async (deps, entry, install, values, given) => {
|
|
41
33
|
const dir = await dirOf(deps, entry, install, given);
|
|
42
34
|
const pkg = await readManifest(dir);
|
|
35
|
+
assertSecrets(pkg, values.secrets);
|
|
43
36
|
const spec = { id: entry.id, dir, dataDir: dataDirOf(deps.configDir, entry.id), ...values };
|
|
44
|
-
const child = await deps
|
|
37
|
+
const child = await deps
|
|
38
|
+
.launch({ ...spec, origin: deps.origin(), storeOpenMs: deps.healthMs })
|
|
39
|
+
.catch((err) => Promise.reject(Object.assign(err, { message: redact(err.message, values.secrets) })));
|
|
45
40
|
await healthy(child.port, deps.healthMs).catch(async (err) => {
|
|
46
41
|
await child.stop();
|
|
47
42
|
throw err;
|
|
48
43
|
});
|
|
49
44
|
Object.assign(entry, { dir, version: pkg.version });
|
|
50
45
|
const lane = laneOf(child);
|
|
51
|
-
|
|
46
|
+
const words = (how) => redact(reasonOf(how), values.secrets);
|
|
47
|
+
void child.exited.then((how) => exitedEarly(deps, entry, lane, words(how)));
|
|
48
|
+
healthyFor(deps, entry, lane);
|
|
52
49
|
return lane;
|
|
53
50
|
};
|
|
54
|
-
// A child that exits while it is the route
|
|
55
|
-
const exitedEarly = (deps, entry, lane,
|
|
51
|
+
// A child that exits while it is the route restarts (C6); a retired one was detached first.
|
|
52
|
+
const exitedEarly = (deps, entry, lane, why) => {
|
|
56
53
|
if (entry.lane !== lane)
|
|
57
54
|
return;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
deps.log.warn('extension process exited', { id: entry.id });
|
|
55
|
+
entry.lane = undefined;
|
|
56
|
+
crashed(deps, entry, why, () => restartEntry(deps, entry));
|
|
61
57
|
};
|
|
62
58
|
const failed = (deps, entry, err) => {
|
|
63
59
|
Object.assign(entry, { state: 'failed', error: err.message });
|
|
64
60
|
deps.log.warn('extension failed to start', { id: entry.id, reason: entry.error });
|
|
65
61
|
};
|
|
66
|
-
// The install and values of installation.json, as they are on disk.
|
|
62
|
+
// The install and values of installation.json, as they are on disk; the error when it failed.
|
|
67
63
|
export const startEntry = async (deps, entry) => {
|
|
68
64
|
entry.state = 'starting';
|
|
69
65
|
try {
|
|
70
66
|
const values = await valuesOf(deps.configDir, entry.id);
|
|
71
67
|
entry.lane = await launchFor(deps, entry, entry.install, values);
|
|
72
68
|
Object.assign(entry, { state: 'running', error: undefined });
|
|
69
|
+
return undefined;
|
|
73
70
|
}
|
|
74
71
|
catch (err) {
|
|
75
72
|
failed(deps, entry, err);
|
|
73
|
+
return err;
|
|
76
74
|
}
|
|
77
75
|
};
|
|
78
|
-
//
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
entry.gate = undefined;
|
|
84
|
-
release();
|
|
85
|
-
};
|
|
76
|
+
// A restart that does not start counts as one more exit.
|
|
77
|
+
const restartEntry = async (deps, entry) => {
|
|
78
|
+
const err = await startEntry(deps, entry);
|
|
79
|
+
if (err)
|
|
80
|
+
crashed(deps, entry, err.message, () => restartEntry(deps, entry));
|
|
86
81
|
};
|
|
87
82
|
// The old install again after a refused change; a disabled one stays stopped.
|
|
88
|
-
const restore = async (deps, entry) => {
|
|
83
|
+
export const restore = async (deps, entry) => {
|
|
89
84
|
if (entry.install.enabled === false)
|
|
90
85
|
entry.state = 'disabled';
|
|
91
86
|
else
|
|
92
87
|
await startEntry(deps, entry);
|
|
93
88
|
};
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
89
|
+
// C6: the envelope of a refused start; reason is the child's own words.
|
|
90
|
+
export const startFailed = (entry, err) => {
|
|
91
|
+
const reason = err.message;
|
|
92
|
+
const details = { code: 'START_FAILED', reason };
|
|
93
|
+
return fail('VALIDATION_ERROR', `${entry.id} failed to start: ${reason}`, { details });
|
|
94
|
+
};
|
|
97
95
|
// One writer per data folder (the caller holds the gate): stop the old child, start the new one.
|
|
98
96
|
export const swap = async (deps, entry, to, undo = async () => undefined) => {
|
|
99
97
|
try {
|
|
100
98
|
await halt(entry, deps.drainMs);
|
|
101
99
|
entry.lane = await launchFor(deps, entry, to.install, to.values, to.dir);
|
|
102
100
|
Object.assign(entry, { state: 'running', error: undefined });
|
|
101
|
+
resetRestarts(entry);
|
|
103
102
|
}
|
|
104
103
|
catch (err) {
|
|
105
104
|
await undo();
|
|
@@ -107,9 +106,3 @@ export const swap = async (deps, entry, to, undo = async () => undefined) => {
|
|
|
107
106
|
throw startFailed(entry, err);
|
|
108
107
|
}
|
|
109
108
|
};
|
|
110
|
-
// Detached first, so its exit is not a failure; then drained and stopped.
|
|
111
|
-
export const halt = async (entry, drainMs) => {
|
|
112
|
-
const lane = entry.lane;
|
|
113
|
-
entry.lane = undefined;
|
|
114
|
-
await retire(lane, drainMs);
|
|
115
|
-
};
|
package/dist/host/data-lock.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const DATA_DIR_LOCKED = "DATA_DIR_LOCKED";
|
|
2
2
|
export declare const EMPTY_LOCK_MS = 1000;
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const clearOldLock: (lock: string) => Promise<void>;
|
|
4
|
+
export declare const lockDataDir: (dataDir: string, lock?: string) => Promise<() => Promise<void>>;
|
package/dist/host/data-lock.js
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import { rmSync } from 'node:fs';
|
|
2
|
+
import { readFileSync, rmSync } from 'node:fs';
|
|
3
3
|
import { link, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
5
5
|
export const DATA_DIR_LOCKED = 'DATA_DIR_LOCKED';
|
|
6
6
|
// The locks of this process: our own pid in a lock not in here is stale (a container is pid 1 again).
|
|
7
7
|
const held = new Set();
|
|
8
|
+
// Linux: an exited child not reaped yet (state Z) answers kill(0) but holds nothing.
|
|
9
|
+
const zombie = (pid) => {
|
|
10
|
+
if (process.platform !== 'linux')
|
|
11
|
+
return false;
|
|
12
|
+
try {
|
|
13
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
14
|
+
return stat.slice(stat.lastIndexOf(')') + 2).startsWith('Z');
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
8
20
|
const alive = (pid) => {
|
|
9
21
|
try {
|
|
10
|
-
return process.kill(pid, 0);
|
|
22
|
+
return process.kill(pid, 0) && !zombie(pid);
|
|
11
23
|
}
|
|
12
24
|
catch (err) {
|
|
13
25
|
return err.code === 'EPERM';
|
|
@@ -27,7 +39,7 @@ const holder = async (file) => {
|
|
|
27
39
|
return Number(pid);
|
|
28
40
|
};
|
|
29
41
|
const live = (lock, pid) => pid === process.pid ? held.has(lock) : pid > 0 && alive(pid);
|
|
30
|
-
const locked = (lock, pid) => Object.assign(new Error(`${lock.
|
|
42
|
+
const locked = (lock, pid) => Object.assign(new Error(`${lock.replace(/[/\\]?(store)?\.lock$/, '')} is open in process ${pid}`), {
|
|
31
43
|
code: DATA_DIR_LOCKED,
|
|
32
44
|
pid,
|
|
33
45
|
});
|
|
@@ -99,6 +111,14 @@ const inTurn = (lock, fn) => {
|
|
|
99
111
|
void tail.then(() => turns.get(lock) === tail && turns.delete(lock));
|
|
100
112
|
return run;
|
|
101
113
|
};
|
|
114
|
+
// A lock of an older kit (beside the folder, out of a sealed child's reach): dead is removed, live refuses.
|
|
115
|
+
export const clearOldLock = async (lock) => {
|
|
116
|
+
const pid = await holder(lock);
|
|
117
|
+
if (pid > 0 && live(lock, pid))
|
|
118
|
+
throw locked(lock, pid);
|
|
119
|
+
if (pid > 0)
|
|
120
|
+
await unlink(lock).catch(() => undefined);
|
|
121
|
+
};
|
|
102
122
|
const acquire = async (lock) => {
|
|
103
123
|
if (await take(lock))
|
|
104
124
|
return;
|
|
@@ -109,9 +129,8 @@ const acquire = async (lock) => {
|
|
|
109
129
|
if (!(await take(lock)))
|
|
110
130
|
throw locked(lock, await holder(lock));
|
|
111
131
|
};
|
|
112
|
-
// PGlite has one writer per folder: <dataDir>.lock holds the pid; a dead holder's lock is taken over.
|
|
113
|
-
export const lockDataDir = async (dataDir) => {
|
|
114
|
-
const lock = `${dataDir}.lock`;
|
|
132
|
+
// PGlite has one writer per folder: the lock (<dataDir>.lock unless given) holds the pid; a dead holder's lock is taken over.
|
|
133
|
+
export const lockDataDir = async (dataDir, lock = `${dataDir}.lock`) => {
|
|
115
134
|
await inTurn(lock, () => acquire(lock));
|
|
116
135
|
// Sync, so no other taker in this process sees the file without its entry in held.
|
|
117
136
|
return async () => {
|
package/dist/host/host-env.d.ts
CHANGED
package/dist/host/host-env.js
CHANGED
|
@@ -21,6 +21,8 @@ export const hostEnvOf = (env) => {
|
|
|
21
21
|
port,
|
|
22
22
|
bind: env['AGENTAGE_HOST_BIND'] === 'all' ? '0.0.0.0' : '127.0.0.1',
|
|
23
23
|
dataDir: env['AGENTAGE_DATA_DIR'] ?? '',
|
|
24
|
+
// C2: the daemon's run/{id}; the store lock goes there (a sealed child cannot write beside data/{id}).
|
|
25
|
+
runDir: env['AGENTAGE_RUN_DIR'] ?? '',
|
|
24
26
|
settings: env['AGENTAGE_SETTINGS'] ? parseObject(env['AGENTAGE_SETTINGS']) : {},
|
|
25
27
|
secretsDir: env['AGENTAGE_SECRETS_DIR'] || SECRETS_DIR,
|
|
26
28
|
origin: env['AGENTAGE_ORIGIN'] || `http://127.0.0.1${port ? `:${port}` : ''}`,
|
|
@@ -3,19 +3,23 @@ import { type BuildInfo } from '../discovery/discovery.js';
|
|
|
3
3
|
import { type HealthBody } from '../health/health.schema.js';
|
|
4
4
|
import { type Log } from '../log/log.js';
|
|
5
5
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
6
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
6
7
|
import { type PartAppBase } from '../server/app.js';
|
|
7
8
|
export interface HostOptions {
|
|
8
9
|
dataDir: string;
|
|
10
|
+
runDir?: string;
|
|
9
11
|
settings?: Record<string, unknown>;
|
|
10
12
|
secrets?: Record<string, string>;
|
|
11
13
|
onExit?: () => void;
|
|
12
14
|
origin?: string;
|
|
13
15
|
localOnly?: boolean;
|
|
16
|
+
localPorts?: number[];
|
|
14
17
|
storeOpenMs?: number;
|
|
15
18
|
}
|
|
16
19
|
export interface HostedExtension {
|
|
17
20
|
id: string;
|
|
18
21
|
version: string;
|
|
22
|
+
parts: HostPartKind[];
|
|
19
23
|
app: Express;
|
|
20
24
|
stop(): Promise<void>;
|
|
21
25
|
health(): Promise<HealthBody>;
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
2
|
import {} from 'express';
|
|
3
|
-
import { createContext } from '../context/context.js';
|
|
4
|
-
import { readSecrets, secretsFrom } from '../context/secrets.js';
|
|
5
|
-
import { settingsOf } from '../context/settings.js';
|
|
6
3
|
import {} from '../discovery/discovery.js';
|
|
7
|
-
import { readOpenapi } from '../discovery/openapi.js';
|
|
8
4
|
import { healthBody } from '../health/health.js';
|
|
9
5
|
import {} from '../health/health.schema.js';
|
|
10
6
|
import { createLog } from '../log/log.js';
|
|
11
7
|
import {} from '../manifest/manifest.schema.js';
|
|
12
|
-
import {
|
|
8
|
+
import {} from '../manifest/parts.js';
|
|
13
9
|
import { readManifest } from '../manifest/read-manifest.js';
|
|
14
10
|
import { buildInfoFromEnv } from '../runtime/build-info.js';
|
|
15
|
-
import {
|
|
16
|
-
import { createPartApp } from '../server/app.js';
|
|
11
|
+
import {} from '../server/app.js';
|
|
17
12
|
import { storeCheck } from '../store/store.js';
|
|
18
|
-
import {} from '../
|
|
13
|
+
import { hostClockRate } from '../worker/clock.js';
|
|
19
14
|
import { localOnly } from './daemon/guard.js';
|
|
15
|
+
import { hostPartsOf } from './host-parts.js';
|
|
20
16
|
import { openHostStore, STORE_OPEN_MS } from './host-store.js';
|
|
21
17
|
const once = (fn) => {
|
|
22
18
|
let done = false;
|
|
@@ -27,65 +23,49 @@ const once = (fn) => {
|
|
|
27
23
|
fn();
|
|
28
24
|
};
|
|
29
25
|
};
|
|
30
|
-
const secretsOf = ({ pkg, opts, extras }) => extras.secretsDir
|
|
31
|
-
? readSecrets(pkg, 'api', extras.secretsDir)
|
|
32
|
-
: secretsFrom(pkg, 'api', async (name) => opts.secrets?.[name] ?? '');
|
|
33
|
-
// createContext with the host's values: settings and secrets as given, no platform.
|
|
34
|
-
const contextOf = async (h, checks) => createContext({
|
|
35
|
-
pkg: h.pkg,
|
|
36
|
-
part: 'api',
|
|
37
|
-
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
38
|
-
commit: h.build.commit,
|
|
39
|
-
log: h.log,
|
|
40
|
-
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
41
|
-
store: h.store,
|
|
42
|
-
secrets: await secretsOf(h),
|
|
43
|
-
checks,
|
|
44
|
-
onExit: h.onExit,
|
|
45
|
-
});
|
|
46
|
-
// The api part of the package; a package without one serves /health only.
|
|
47
|
-
const appOf = async (h, base) => {
|
|
48
|
-
if (!normalizeParts(h.pkg.agentage.parts).code.api)
|
|
49
|
-
return createPartApp({ ...base, part: 'api', mount: () => undefined });
|
|
50
|
-
const ctx = await contextOf(h, base.checks ?? {});
|
|
51
|
-
const openapi = await readOpenapi(h.dir);
|
|
52
|
-
return (await RUNNERS.api({ dir: h.dir, pkg: h.pkg, env: {}, ctx: () => ctx, base, openapi }))
|
|
53
|
-
.app;
|
|
54
|
-
};
|
|
55
26
|
const baseOf = async (h) => {
|
|
56
27
|
const guard = (await h.extras.guard?.(h.pkg, h.log)) ?? {};
|
|
57
28
|
const checks = { ...(h.store && { store: storeCheck(h.store) }), ...guard.checks };
|
|
58
29
|
const resolved = { instance: randomBytes(4).toString('hex'), startedAt: Date.now() };
|
|
59
|
-
const
|
|
30
|
+
const clockRate = hostClockRate();
|
|
31
|
+
const guards = h.opts.localOnly ? [localOnly(undefined, h.opts.localPorts)] : undefined;
|
|
60
32
|
const { pkg, log, build, onExit } = h;
|
|
61
|
-
return { pkg, log, build, onExit, guards, ...guard, checks, ...resolved };
|
|
33
|
+
return { pkg, log, build, onExit, guards, ...guard, checks, clockRate, ...resolved };
|
|
62
34
|
};
|
|
63
|
-
|
|
35
|
+
// The jobs stop before the store closes: a tick never writes to a closed store.
|
|
36
|
+
const hostedOf = (h, hosted, base) => {
|
|
64
37
|
const { id } = h.pkg.agentage;
|
|
65
38
|
const { version } = h.pkg;
|
|
66
|
-
const {
|
|
67
|
-
const
|
|
39
|
+
const { app, parts } = hosted;
|
|
40
|
+
const { instance = '', startedAt = 0, build, checks, clockRate } = base;
|
|
41
|
+
const at = { instance, startedAt, clockRate };
|
|
42
|
+
const health = () => healthBody({ extension: id, part: 'api', parts, version, build, checks, ...at });
|
|
68
43
|
let stopped;
|
|
69
|
-
const
|
|
70
|
-
|
|
44
|
+
const close = () => (hosted.stop(), h.store ? h.store.close() : Promise.resolve());
|
|
45
|
+
const stop = () => (stopped ??= close());
|
|
46
|
+
return { id, version, parts, app, health, stop };
|
|
71
47
|
};
|
|
72
48
|
// hostExtension with the extras of the entry (dist/host/main.js).
|
|
73
49
|
export const hostWith = async (dir, opts, extras) => {
|
|
74
50
|
const pkg = await readManifest(dir);
|
|
75
51
|
const log = createLog({ extension: pkg.agentage.id, part: 'api', version: pkg.version });
|
|
76
52
|
const openMs = opts.storeOpenMs ?? STORE_OPEN_MS;
|
|
77
|
-
const
|
|
53
|
+
const at = { dir, dataDir: opts.dataDir, runDir: opts.runDir, openMs };
|
|
54
|
+
const store = await openHostStore(pkg, at, log);
|
|
78
55
|
const onExit = once(opts.onExit ?? (() => undefined));
|
|
79
56
|
const build = extras.build ?? buildInfoFromEnv({});
|
|
80
57
|
const h = { dir, pkg, log, opts, extras, store, onExit, build };
|
|
81
58
|
try {
|
|
82
59
|
const base = await baseOf(h);
|
|
83
|
-
|
|
60
|
+
const hosted = await hostPartsOf(h, base);
|
|
61
|
+
// After every create and the store open: the migrations ran or a job's first query waits on them.
|
|
62
|
+
hosted.start();
|
|
63
|
+
return hostedOf(h, hosted, base);
|
|
84
64
|
}
|
|
85
65
|
catch (err) {
|
|
86
66
|
await store?.close();
|
|
87
67
|
throw err;
|
|
88
68
|
}
|
|
89
69
|
};
|
|
90
|
-
// One extension in this process:
|
|
70
|
+
// One extension in this process: api at /api/{id}/*, mcp at /mcp/{id}, the worker jobs, one store, no token check.
|
|
91
71
|
export const hostExtension = (dir, opts) => hostWith(dir, opts, {});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
import { type BuildInfo } from '../discovery/discovery.js';
|
|
3
|
+
import { type Log } from '../log/log.js';
|
|
4
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
5
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
6
|
+
import { type PartAppBase } from '../server/app.js';
|
|
7
|
+
import { type PgliteStore } from '../store/pglite-store.js';
|
|
8
|
+
import { type HostExtras, type HostOptions } from './host-extension.js';
|
|
9
|
+
export interface Hosting {
|
|
10
|
+
dir: string;
|
|
11
|
+
pkg: ExtensionPackage;
|
|
12
|
+
log: Log;
|
|
13
|
+
opts: HostOptions;
|
|
14
|
+
extras: HostExtras;
|
|
15
|
+
store: PgliteStore | undefined;
|
|
16
|
+
onExit: () => void;
|
|
17
|
+
build: BuildInfo;
|
|
18
|
+
}
|
|
19
|
+
export interface HostedParts {
|
|
20
|
+
app: Express;
|
|
21
|
+
parts: HostPartKind[];
|
|
22
|
+
start(): void;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const hostPartsOf: (h: Hosting, base: PartAppBase) => Promise<HostedParts>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {} from 'express';
|
|
2
|
+
import { createContext } from '../context/context.js';
|
|
3
|
+
import { readSecrets, secretsFrom } from '../context/secrets.js';
|
|
4
|
+
import { settingsOf } from '../context/settings.js';
|
|
5
|
+
import {} from '../discovery/discovery.js';
|
|
6
|
+
import { readOpenapi } from '../discovery/openapi.js';
|
|
7
|
+
import { createLog } from '../log/log.js';
|
|
8
|
+
import {} from '../manifest/manifest.schema.js';
|
|
9
|
+
import { hostParts } from '../manifest/parts.js';
|
|
10
|
+
import { mcpPathOf } from '../mcp/mcp-app.js';
|
|
11
|
+
import { MOUNTS } from '../runtime/part-mounts.js';
|
|
12
|
+
import { createPartApp } from '../server/app.js';
|
|
13
|
+
import {} from '../store/pglite-store.js';
|
|
14
|
+
import {} from '../health/health.js';
|
|
15
|
+
import {} from './host-extension.js';
|
|
16
|
+
import { partChecks } from './part-checks.js';
|
|
17
|
+
const secretsOf = ({ pkg, opts, extras }, part) => extras.secretsDir
|
|
18
|
+
? readSecrets(pkg, part, extras.secretsDir)
|
|
19
|
+
: secretsFrom(pkg, part, async (name) => opts.secrets?.[name] ?? '');
|
|
20
|
+
// A ctx for each part: its own log and mounted secrets, the one store and the one map of checks.
|
|
21
|
+
const contextOf = async (h, part, checks) => createContext({
|
|
22
|
+
pkg: h.pkg,
|
|
23
|
+
part,
|
|
24
|
+
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
25
|
+
commit: h.build.commit,
|
|
26
|
+
log: part === 'api'
|
|
27
|
+
? h.log
|
|
28
|
+
: createLog({ extension: h.pkg.agentage.id, part, version: h.pkg.version }),
|
|
29
|
+
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
30
|
+
store: h.store,
|
|
31
|
+
secrets: await secretsOf(h, part),
|
|
32
|
+
checks,
|
|
33
|
+
onExit: h.onExit,
|
|
34
|
+
});
|
|
35
|
+
const inputOf = async (h, part, base, reserved) => {
|
|
36
|
+
const ctx = await contextOf(h, part, partChecks(base.checks ?? {}, reserved));
|
|
37
|
+
const openapi = await readOpenapi(h.dir);
|
|
38
|
+
const mcpPaths = [mcpPathOf(h.pkg.agentage.id)];
|
|
39
|
+
return {
|
|
40
|
+
dir: h.dir,
|
|
41
|
+
pkg: h.pkg,
|
|
42
|
+
env: {},
|
|
43
|
+
ctx: () => ctx,
|
|
44
|
+
base,
|
|
45
|
+
openapi,
|
|
46
|
+
mcpPaths,
|
|
47
|
+
openListing: !base.tokenCheck,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
// In order api, mcp, worker; a create that throws stops what the earlier ones started.
|
|
51
|
+
const mountsOf = async (h, parts, base) => {
|
|
52
|
+
const mounts = [];
|
|
53
|
+
// The host's own checks (store, token) stay refused names, as in a part image.
|
|
54
|
+
const reserved = new Set(Object.keys(base.checks ?? {}));
|
|
55
|
+
try {
|
|
56
|
+
for (const part of parts)
|
|
57
|
+
mounts.push(await MOUNTS[part](await inputOf(h, part, base, reserved)));
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
for (const m of mounts)
|
|
61
|
+
m.stop?.();
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
return mounts;
|
|
65
|
+
};
|
|
66
|
+
// The api at /api/{id}/*, the mcp at /mcp/{id}, the worker jobs; one /health with every check.
|
|
67
|
+
export const hostPartsOf = async (h, base) => {
|
|
68
|
+
const parts = hostParts(h.pkg);
|
|
69
|
+
const mounts = await mountsOf(h, parts, base);
|
|
70
|
+
const mount = (app) => mounts.forEach((m) => m.mount(app));
|
|
71
|
+
const app = createPartApp({ ...base, parts, part: 'api', mount });
|
|
72
|
+
const start = () => mounts.forEach((m) => m.start?.());
|
|
73
|
+
const stop = () => mounts.forEach((m) => m.stop?.());
|
|
74
|
+
return { app, parts, start, stop };
|
|
75
|
+
};
|
|
@@ -2,9 +2,12 @@ import { type Log } from '../log/log.js';
|
|
|
2
2
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
3
3
|
import { type PgliteStore } from '../store/pglite-store.js';
|
|
4
4
|
export declare const STORE_OPEN_MS = 120000;
|
|
5
|
+
export declare const lockWithin: (dataDir: string, openMs: number, lock?: string) => Promise<() => Promise<void>>;
|
|
6
|
+
export declare const lockOf: ({ dataDir, runDir }: Pick<StoreAt, "dataDir" | "runDir">) => string;
|
|
5
7
|
interface StoreAt {
|
|
6
8
|
dir: string;
|
|
7
9
|
dataDir: string;
|
|
10
|
+
runDir?: string;
|
|
8
11
|
openMs: number;
|
|
9
12
|
}
|
|
10
13
|
export declare const openHostStore: (pkg: ExtensionPackage, at: StoreAt, log: Log) => Promise<PgliteStore | undefined>;
|