@agentage/extension 0.15.1 → 0.16.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/dist/host/daemon/add.d.ts +2 -0
- package/dist/host/daemon/add.js +6 -1
- package/dist/host/daemon/child-files.d.ts +2 -2
- package/dist/host/daemon/child-files.js +16 -8
- package/dist/host/daemon/child.d.ts +1 -1
- package/dist/host/daemon/child.js +2 -4
- package/dist/host/daemon/daemon-files.js +18 -3
- package/dist/host/daemon/daemon.js +3 -3
- package/dist/host/daemon/health-routes.d.ts +0 -1
- package/dist/host/daemon/health-routes.js +2 -2
- package/dist/host/daemon/installation.js +2 -4
- package/dist/host/daemon/json-file.d.ts +2 -0
- package/dist/host/daemon/json-file.js +38 -10
- package/dist/host/daemon/ops.js +4 -2
- package/dist/host/daemon/proxy.d.ts +6 -1
- package/dist/host/daemon/proxy.js +13 -4
- package/dist/host/daemon/reload-marker.js +2 -1
- package/dist/host/daemon/reload.js +3 -4
- package/dist/host/daemon/routes.js +9 -5
- package/dist/host/daemon/seal.d.ts +2 -2
- package/dist/host/daemon/seal.js +1 -1
- package/dist/host/daemon/start-checks.d.ts +0 -1
- package/dist/host/daemon/start-checks.js +1 -1
- package/dist/host/daemon/supervisor.d.ts +1 -4
- package/dist/host/daemon/supervisor.js +15 -17
- package/dist/host/host-env.d.ts +1 -0
- package/dist/host/host-env.js +2 -0
- package/dist/host/host-extension.d.ts +2 -0
- package/dist/host/host-extension.js +11 -4
- package/dist/host/host-migrations.d.ts +8 -0
- package/dist/host/host-migrations.js +62 -0
- package/dist/host/host-parts.d.ts +1 -1
- package/dist/host/host-parts.js +2 -2
- package/dist/host/host-url-store.d.ts +21 -0
- package/dist/host/host-url-store.js +62 -0
- package/dist/host/main.js +9 -3
- package/dist/host/serve-host.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/manifest/parts.d.ts +2 -1
- package/dist/manifest/parts.js +1 -1
- package/dist/mcp/mcp-app.d.ts +0 -1
- package/dist/mcp/mcp-app.js +1 -1
- package/dist/runtime/part-mounts.d.ts +1 -1
- package/dist/store/clickhouse-http.d.ts +20 -0
- package/dist/store/clickhouse-http.js +90 -0
- package/dist/store/clickhouse-store.d.ts +22 -0
- package/dist/store/clickhouse-store.js +87 -0
- package/dist/store/migrate.js +1 -3
- package/dist/store/store-url.d.ts +4 -0
- package/dist/store/store-url.js +28 -0
- package/dist/store/store.d.ts +1 -0
- package/dist/store/store.js +2 -0
- package/dist/worker/clock.d.ts +0 -1
- package/dist/worker/clock.js +1 -1
- package/dist/worker/scheduler.d.ts +1 -1
- package/dist/worker/scheduler.js +12 -2
- package/package.json +1 -1
- package/dist/host/daemon/reload-route.d.ts +0 -4
- package/dist/host/daemon/reload-route.js +0 -21
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
1
2
|
import { type Entries } from './entries.js';
|
|
2
3
|
import { type SupervisorDeps } from './supervisor.js';
|
|
4
|
+
export declare const VersionSpecSchema: z.ZodString;
|
|
3
5
|
export declare const addExtension: (deps: SupervisorDeps, entries: Entries, body: unknown) => Promise<{
|
|
4
6
|
error?: string | undefined;
|
|
5
7
|
pid?: number | undefined;
|
package/dist/host/daemon/add.js
CHANGED
|
@@ -17,11 +17,16 @@ const RESERVED_ID = 'host';
|
|
|
17
17
|
// The cli's rule for what follows `<id>@`: a version, a range or a dist-tag; never a path, URL or flag.
|
|
18
18
|
const DIST_TAG = /^[A-Za-z][\w.-]*$/;
|
|
19
19
|
const isVersionSpec = (spec) => !/\s/.test(spec) && (validRange(spec) !== null || DIST_TAG.test(spec));
|
|
20
|
+
// POST and PATCH: the same rule, so no route hands the installer a path.
|
|
21
|
+
export const VersionSpecSchema = z
|
|
22
|
+
.string()
|
|
23
|
+
.min(1)
|
|
24
|
+
.refine(isVersionSpec, 'a version, a range or a dist-tag');
|
|
20
25
|
// enabled: false records the extension and never starts it (a profile "off" entry).
|
|
21
26
|
const AddSchema = z.union([
|
|
22
27
|
z.strictObject({
|
|
23
28
|
id: ExtensionIdSchema,
|
|
24
|
-
version:
|
|
29
|
+
version: VersionSpecSchema.optional(),
|
|
25
30
|
enabled: z.boolean().optional(),
|
|
26
31
|
}),
|
|
27
32
|
z.strictObject({
|
|
@@ -9,11 +9,11 @@ export interface RunFolders {
|
|
|
9
9
|
secretsDir: string;
|
|
10
10
|
tmpDir: string;
|
|
11
11
|
made: {
|
|
12
|
-
run
|
|
12
|
+
run?: string;
|
|
13
13
|
data: boolean;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
-
export declare const
|
|
16
|
+
export declare const configDirOf: (dataDir: string) => string;
|
|
17
17
|
export declare const makeRun: (spec: RunSpec) => Promise<RunFolders>;
|
|
18
18
|
export declare const fillRun: (run: RunFolders, spec: RunSpec) => Promise<void>;
|
|
19
19
|
export declare const undoRun: ({ runDir, made }: RunFolders, dataDir: string) => Promise<void>;
|
|
@@ -7,7 +7,7 @@ import { clearOldLock } from '../data-lock.js';
|
|
|
7
7
|
import { runDirOf } from './daemon-files.js';
|
|
8
8
|
import { clearRunLock } from './run-lock.js';
|
|
9
9
|
// The supervisor's dataDir is <configDir>/data/{id}, so run/{id} sits beside data/ (C2).
|
|
10
|
-
export const
|
|
10
|
+
export const configDirOf = (dataDir) => dirname(dirname(dataDir));
|
|
11
11
|
// Only the names the package declares now: a row can still hold one an older version declared.
|
|
12
12
|
const declared = async (spec) => {
|
|
13
13
|
const names = new Set(secretNames(await readManifest(spec.dir)));
|
|
@@ -15,15 +15,15 @@ const declared = async (spec) => {
|
|
|
15
15
|
};
|
|
16
16
|
// The folders first, so undoRun knows what this launch made; data/{id} before the fork (PGlite).
|
|
17
17
|
export const makeRun = async (spec) => {
|
|
18
|
-
const runDir =
|
|
19
|
-
const
|
|
18
|
+
const runDir = runDirOf(configDirOf(spec.dataDir), spec.id);
|
|
19
|
+
const made = { run: await mkdir(runDir, { recursive: true, mode: 0o700 }), data: false };
|
|
20
|
+
const run = { runDir, secretsDir: join(runDir, 'secrets'), tmpDir: join(runDir, 'tmp'), made };
|
|
20
21
|
const data = await mkdir(spec.dataDir, { recursive: true }).catch(async (err) => {
|
|
21
|
-
|
|
22
|
-
await rm(runDir, { recursive: true, force: true });
|
|
22
|
+
await undoRun(run, spec.dataDir);
|
|
23
23
|
throw err;
|
|
24
24
|
});
|
|
25
|
-
|
|
26
|
-
return
|
|
25
|
+
made.data = data !== undefined;
|
|
26
|
+
return run;
|
|
27
27
|
};
|
|
28
28
|
// Fresh secrets and tmp; the old lock beside data/{id} and a left-over run lock are cleared.
|
|
29
29
|
export const fillRun = async (run, spec) => {
|
|
@@ -36,10 +36,18 @@ export const fillRun = async (run, spec) => {
|
|
|
36
36
|
for (const [name, value] of await declared(spec))
|
|
37
37
|
await writeFile(join(run.secretsDir, SecretNameSchema.parse(name)), value, { mode: 0o600 });
|
|
38
38
|
};
|
|
39
|
-
//
|
|
39
|
+
// From dir up to top, each folder only while it is empty: rmdir refuses one that is not.
|
|
40
|
+
const emptyUpTo = async (dir, top) => {
|
|
41
|
+
for (; dir.startsWith(top); dir = dirname(dir))
|
|
42
|
+
if (await rmdir(dir).catch(() => 'kept'))
|
|
43
|
+
return;
|
|
44
|
+
};
|
|
45
|
+
// A child that never reported its port: what this launch made goes, run/ only while empty.
|
|
40
46
|
export const undoRun = async ({ runDir, made }, dataDir) => {
|
|
41
47
|
if (made.run)
|
|
42
48
|
await rm(runDir, { recursive: true, force: true });
|
|
49
|
+
if (made.run)
|
|
50
|
+
await emptyUpTo(dirname(runDir), made.run);
|
|
43
51
|
if (made.data)
|
|
44
52
|
await rmdir(dataDir).catch(() => undefined);
|
|
45
53
|
};
|
|
@@ -25,5 +25,5 @@ export type Launch = ((spec: LaunchSpec) => Promise<Child>) & {
|
|
|
25
25
|
node?: string;
|
|
26
26
|
};
|
|
27
27
|
export declare const childEnv: (spec: LaunchSpec, secretsDir: string, runDir?: string) => Record<string, string>;
|
|
28
|
-
export { lastWords } from './last-words.js';
|
|
29
28
|
export declare const forkLaunch: (main?: string, startMs?: number, rate?: number, node?: string) => Launch;
|
|
29
|
+
export {};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { fork } from 'node:child_process';
|
|
2
2
|
import { rm } from 'node:fs/promises';
|
|
3
|
-
import { dirname } from 'node:path';
|
|
4
3
|
import { fileURLToPath } from 'node:url';
|
|
5
4
|
import { CLOCK_RATE_ENV, parseClockRate } from '../../worker/clock.js';
|
|
6
|
-
import { fillRun, makeRun, undoRun } from './child-files.js';
|
|
5
|
+
import { configDirOf, fillRun, makeRun, undoRun } from './child-files.js';
|
|
7
6
|
import { lastWords } from './last-words.js';
|
|
8
7
|
import { LIMITS } from './limits.js';
|
|
9
8
|
import { pipeRedacted } from './redact-stream.js';
|
|
@@ -22,7 +21,6 @@ export const childEnv = (spec, secretsDir, runDir) => ({
|
|
|
22
21
|
...(runDir && { AGENTAGE_RUN_DIR: runDir }),
|
|
23
22
|
...(spec.storeOpenMs && { AGENTAGE_STORE_OPEN_MS: String(spec.storeOpenMs) }),
|
|
24
23
|
});
|
|
25
|
-
export { lastWords } from './last-words.js';
|
|
26
24
|
const passedEnv = () => Object.fromEntries(PASSED.flatMap((k) => (process.env[k] ? [[k, process.env[k]]] : [])));
|
|
27
25
|
const captured = (child, secrets) => {
|
|
28
26
|
let tail = '';
|
|
@@ -78,7 +76,7 @@ const stopOf = (child, exited) => async () => {
|
|
|
78
76
|
const execArgvOf = (node, main, spec, run) => {
|
|
79
77
|
if (!permissionFlag(node))
|
|
80
78
|
return [];
|
|
81
|
-
const configDir =
|
|
79
|
+
const configDir = configDirOf(spec.dataDir);
|
|
82
80
|
const { id, dir, dataDir } = spec;
|
|
83
81
|
return sealFlags({ id, main, dir, configDir, runDir: run.runDir, dataDir });
|
|
84
82
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { rm, unlink } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { fail } from '../../envelope/envelope.js';
|
|
4
|
+
import { readJsonFile, unreadableReason, withFileLock, writeJsonFile } from './json-file.js';
|
|
4
5
|
const DAEMON_FILE = 'daemon.json';
|
|
5
6
|
export const DAEMON_PROTOCOL = 1;
|
|
6
7
|
export const writeDaemonFile = (configDir, file) => writeJsonFile(join(configDir, DAEMON_FILE), file, 0o600);
|
|
@@ -12,12 +13,26 @@ export const runDirOf = (configDir, id) => join(configDir, 'run', id);
|
|
|
12
13
|
export const clearRunDir = (configDir, id) => rm(runDirOf(configDir, id), { recursive: true, force: true });
|
|
13
14
|
export const purgeData = (configDir, id) => rm(dataDirOf(configDir, id), { recursive: true, force: true });
|
|
14
15
|
const fileOf = (configDir, kind) => join(configDir, `${kind}.json`);
|
|
15
|
-
|
|
16
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
17
|
+
// A file that is not JSON, or not { id: { name: value } }, is refused by name and never written over.
|
|
18
|
+
const unreadable = (kind, reason) => fail('CONFLICT', `${kind}.json cannot be read (${reason}): fix it; no change is written`, {
|
|
19
|
+
code: `${kind.toUpperCase()}_UNREADABLE`,
|
|
20
|
+
details: { file: `${kind}.json`, reason },
|
|
21
|
+
});
|
|
22
|
+
const rowsOf = async (configDir, kind) => {
|
|
23
|
+
const all = await readJsonFile(fileOf(configDir, kind), {}).catch((err) => {
|
|
24
|
+
throw unreadable(kind, unreadableReason(err));
|
|
25
|
+
});
|
|
26
|
+
if (!isObject(all) || !Object.values(all).every(isObject))
|
|
27
|
+
throw unreadable(kind, 'not an object of rows by id');
|
|
28
|
+
return all;
|
|
29
|
+
};
|
|
30
|
+
export const readValues = async (configDir, kind, id) => (await rowsOf(configDir, kind))[id] ?? {};
|
|
16
31
|
// The rows of one file changed under its lock; the same object back writes nothing.
|
|
17
32
|
const changeValues = (configDir, kind, change, waitMs) => {
|
|
18
33
|
const file = fileOf(configDir, kind);
|
|
19
34
|
const write = async () => {
|
|
20
|
-
const all = await
|
|
35
|
+
const all = await rowsOf(configDir, kind);
|
|
21
36
|
const next = change(all);
|
|
22
37
|
if (next !== all)
|
|
23
38
|
await writeJsonFile(file, next, kind === 'secrets' ? 0o600 : 0o644);
|
|
@@ -17,7 +17,8 @@ import { launchUnlessStopping, stopAll, stoppingOf } from './stopping.js';
|
|
|
17
17
|
import { daemonApp } from './routes.js';
|
|
18
18
|
import { runInstaller } from './installer-call.js';
|
|
19
19
|
import { repairReload } from './reload-marker.js';
|
|
20
|
-
import { halt
|
|
20
|
+
import { halt } from './gate.js';
|
|
21
|
+
import { startEntry } from './supervisor.js';
|
|
21
22
|
export const DAEMON_PORT = 4243;
|
|
22
23
|
const warnOf = (deps) => (err) => deps.log.warn('a daemon file was not cleaned at boot', { reason: err.message });
|
|
23
24
|
// The valid rows of installation.json; a bad file is kept aside, replaced by them, and in /health.
|
|
@@ -116,6 +117,5 @@ export const startDaemon = async (opts, launch) => {
|
|
|
116
117
|
}
|
|
117
118
|
return { port, stop };
|
|
118
119
|
};
|
|
119
|
-
// One daemon on 127.0.0.1, one child process for each extension (D1).
|
|
120
|
-
// A bad clockRate rejects the promise, like every other refusal of this function.
|
|
120
|
+
// One daemon on 127.0.0.1, one child process for each extension (D1); a bad clockRate rejects.
|
|
121
121
|
export const createDaemon = async (opts) => startDaemon(opts, forkLaunch(undefined, opts.startMs, opts.clockRate));
|
|
@@ -13,5 +13,4 @@ export interface DaemonInfo {
|
|
|
13
13
|
seal?: Seal;
|
|
14
14
|
sealError?: string;
|
|
15
15
|
}
|
|
16
|
-
export declare const HOST_DISCOVERY_PATH = "/.well-known/agentage-host.json";
|
|
17
16
|
export declare const healthRoutes: (app: Express, deps: SupervisorDeps, entries: Entries, info: DaemonInfo) => void;
|
|
@@ -20,7 +20,7 @@ const installationOf = ({ invalid, unreadable, notJson }) => {
|
|
|
20
20
|
return { installation: 'unreadable', installationError: unreadable };
|
|
21
21
|
return invalid || notJson ? { installation: 'invalid' } : {};
|
|
22
22
|
};
|
|
23
|
-
const sealMark = ({ seal, sealError }) => seal === 'off' ? { seal, sealError
|
|
23
|
+
const sealMark = ({ seal, sealError }) => (seal === 'off' ? { seal, sealError } : {});
|
|
24
24
|
const statusOf = (deps, entries, info) => {
|
|
25
25
|
const settled = [...entries.values()].every((e) => ['running', 'disabled'].includes(e.state));
|
|
26
26
|
const clean = !info.invalid && !info.unreadable && !info.notJson && info.seal !== 'off';
|
|
@@ -50,7 +50,7 @@ const discoveryOf = async (entries) => {
|
|
|
50
50
|
}));
|
|
51
51
|
return { host: 'local', extensions: await Promise.all(listed) };
|
|
52
52
|
};
|
|
53
|
-
|
|
53
|
+
const HOST_DISCOVERY_PATH = '/.well-known/agentage-host.json';
|
|
54
54
|
export const healthRoutes = (app, deps, entries, info) => {
|
|
55
55
|
app.get('/health', async (_req, res) => {
|
|
56
56
|
res.set('Cache-Control', 'no-store').json(await healthOf(deps, entries, info));
|
|
@@ -4,7 +4,7 @@ import { z } from 'zod';
|
|
|
4
4
|
import { fail } from '../../envelope/envelope.js';
|
|
5
5
|
import { ExtensionIdSchema } from '../../manifest/primitives.schema.js';
|
|
6
6
|
import { assertNoOtherDaemon } from './daemon-owner.js';
|
|
7
|
-
import { readJsonFile, withFileLock, writeJsonFile } from './json-file.js';
|
|
7
|
+
import { readJsonFile, unreadableReason, withFileLock, writeJsonFile } from './json-file.js';
|
|
8
8
|
// One extension in installation.json (C3): a version, or a source folder; other keys round-trip.
|
|
9
9
|
const InstallEntrySchema = z.looseObject({
|
|
10
10
|
id: ExtensionIdSchema,
|
|
@@ -29,9 +29,7 @@ const checked = (raw) => {
|
|
|
29
29
|
};
|
|
30
30
|
// At boot the daemon has no rows from it, so a fix needs a restart; later a fix is enough.
|
|
31
31
|
const unreadable = (why, atBoot = false) => fail('CONFLICT', `${INSTALLATION_FILE} cannot be read (${why}): fix it${atBoot ? ', then restart the daemon' : ''}; no change is written`, { code: 'INSTALLATION_UNREADABLE', details: { reason: why } });
|
|
32
|
-
const reasonOf =
|
|
33
|
-
? 'not JSON'
|
|
34
|
-
: String(err.code ?? err.message);
|
|
32
|
+
const reasonOf = unreadableReason;
|
|
35
33
|
// Not readable (EACCES, EISDIR): thrown.
|
|
36
34
|
const rawOf = (configDir, notJson = 'empty') => readJsonFile(join(configDir, INSTALLATION_FILE), EMPTY).catch((err) => {
|
|
37
35
|
if (err instanceof SyntaxError && notJson === 'empty')
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const readJsonFile: <T>(file: string, fallback: T) => Promise<T>;
|
|
2
|
+
export declare const unreadableReason: (err: unknown) => string;
|
|
3
|
+
export declare const unwritable: (file: string, err: unknown) => unknown;
|
|
2
4
|
export declare const writeJsonFile: (file: string, value: unknown, given?: number) => Promise<void>;
|
|
3
5
|
export declare const LOCK_WAIT_MS = 5000;
|
|
4
6
|
export declare const withFileLock: <T>(file: string, fn: () => T | Promise<T>, waitMs?: number) => Promise<T>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import { chmod, open, readFile, rename, stat } from 'node:fs/promises';
|
|
2
|
+
import { chmod, open, readFile, rename, stat, unlink } from 'node:fs/promises';
|
|
3
3
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
4
|
-
import {
|
|
4
|
+
import { constants } from 'node:os';
|
|
5
|
+
import { basename, dirname } from 'node:path';
|
|
5
6
|
import { fail } from '../../envelope/envelope.js';
|
|
6
7
|
import { DATA_DIR_LOCKED, lockDataDir } from '../data-lock.js';
|
|
7
8
|
export const readJsonFile = async (file, fallback) => {
|
|
@@ -14,22 +15,47 @@ export const readJsonFile = async (file, fallback) => {
|
|
|
14
15
|
throw err;
|
|
15
16
|
}
|
|
16
17
|
};
|
|
18
|
+
// Fixed words only: a parser message quotes the file, and secrets.json holds secrets.
|
|
19
|
+
export const unreadableReason = (err) => err instanceof SyntaxError
|
|
20
|
+
? 'not JSON'
|
|
21
|
+
: String(err.code ?? err.message);
|
|
22
|
+
// The folder is full or not writable: the errno, the file and where to look, never a bare 500.
|
|
23
|
+
const NOT_WRITABLE = ['ENOSPC', 'EDQUOT', 'EROFS', 'EACCES', 'EPERM'];
|
|
24
|
+
// Node has no code name for EDQUOT on Linux ("Unknown system error -122"): read the errno too.
|
|
25
|
+
const errnoName = (err) => {
|
|
26
|
+
const { errno, code } = err;
|
|
27
|
+
return errno === -constants.errno.EDQUOT ? 'EDQUOT' : (code ?? '');
|
|
28
|
+
};
|
|
29
|
+
export const unwritable = (file, err) => {
|
|
30
|
+
const code = errnoName(err);
|
|
31
|
+
if (!NOT_WRITABLE.includes(code))
|
|
32
|
+
return err;
|
|
33
|
+
const message = `${basename(file)} cannot be written (${code}): check the free space and rights of ${dirname(file)}; no change was made`;
|
|
34
|
+
return fail('UNAVAILABLE', message, { retryAfter: 30 });
|
|
35
|
+
};
|
|
17
36
|
// The mode of a file already there, narrowed by the default: a 0600 file is never widened.
|
|
18
37
|
const modeOf = (file, mode) => stat(file).then((st) => st.mode & mode & 0o777, () => mode);
|
|
19
38
|
// A reader sees the old file or the new one, never half of one, also after a power loss.
|
|
20
39
|
export const writeJsonFile = async (file, value, given = 0o644) => {
|
|
21
40
|
const mode = await modeOf(file, given);
|
|
22
41
|
const tmp = `${file}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
|
|
23
|
-
const handle = await open(tmp, 'w', mode);
|
|
24
42
|
try {
|
|
25
|
-
await
|
|
26
|
-
|
|
43
|
+
const handle = await open(tmp, 'w', mode);
|
|
44
|
+
try {
|
|
45
|
+
await handle.writeFile(`${JSON.stringify(value, null, 2)}\n`);
|
|
46
|
+
await handle.sync();
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await handle.close();
|
|
50
|
+
}
|
|
51
|
+
await chmod(tmp, mode);
|
|
52
|
+
await rename(tmp, file);
|
|
27
53
|
}
|
|
28
|
-
|
|
29
|
-
|
|
54
|
+
catch (err) {
|
|
55
|
+
// A full disk leaves no half tmp file beside the real one.
|
|
56
|
+
await unlink(tmp).catch(() => undefined);
|
|
57
|
+
throw unwritable(file, err);
|
|
30
58
|
}
|
|
31
|
-
await chmod(tmp, mode);
|
|
32
|
-
await rename(tmp, file);
|
|
33
59
|
};
|
|
34
60
|
export const LOCK_WAIT_MS = 5_000;
|
|
35
61
|
// A live holder (or a reused pid of a dead one) named, so a person can check that process.
|
|
@@ -52,7 +78,9 @@ const acquire = async (file, waitMs) => {
|
|
|
52
78
|
};
|
|
53
79
|
// One writer at a time across processes: an exclusive lock file (<file>.lock) beside the file.
|
|
54
80
|
export const withFileLock = async (file, fn, waitMs = LOCK_WAIT_MS) => {
|
|
55
|
-
const release = await acquire(file, waitMs)
|
|
81
|
+
const release = await acquire(file, waitMs).catch((err) => {
|
|
82
|
+
throw unwritable(file, err);
|
|
83
|
+
});
|
|
56
84
|
try {
|
|
57
85
|
return await fn();
|
|
58
86
|
}
|
package/dist/host/daemon/ops.js
CHANGED
|
@@ -3,6 +3,7 @@ import { z } from 'zod';
|
|
|
3
3
|
import { fail } from '../../envelope/envelope.js';
|
|
4
4
|
import { parse } from '../../envelope/parse.js';
|
|
5
5
|
import { SecretNameSchema } from '../../manifest/primitives.schema.js';
|
|
6
|
+
import { VersionSpecSchema } from './add.js';
|
|
6
7
|
import { clearRunDir, purgeData } from './daemon-files.js';
|
|
7
8
|
import { unmarkReload } from './reload-marker.js';
|
|
8
9
|
import { entryOf, summary } from './entries.js';
|
|
@@ -10,9 +11,10 @@ import { unsynced, writeAll } from './write-all.js';
|
|
|
10
11
|
import { runInstaller } from './installer-call.js';
|
|
11
12
|
import { checkedSettings } from './patch-values.js';
|
|
12
13
|
import {} from './installation.js';
|
|
13
|
-
import { halt, hold
|
|
14
|
+
import { halt, hold } from './gate.js';
|
|
15
|
+
import { swap, valuesOf, } from './supervisor.js';
|
|
14
16
|
const PatchSchema = z.strictObject({
|
|
15
|
-
version:
|
|
17
|
+
version: VersionSpecSchema.optional(),
|
|
16
18
|
enabled: z.boolean().optional(),
|
|
17
19
|
settings: z.record(z.string(), z.unknown()).optional(),
|
|
18
20
|
secrets: z.record(SecretNameSchema, z.string()).optional(),
|
|
@@ -2,5 +2,10 @@ import { type IncomingHttpHeaders } from 'node:http';
|
|
|
2
2
|
import { type RequestHandler } from 'express';
|
|
3
3
|
import { type Entries } from './entries.js';
|
|
4
4
|
import { type Limits } from './limits.js';
|
|
5
|
+
import { type Stopping } from './stopping.js';
|
|
6
|
+
type ProxyLimits = Pick<Limits, 'gateMs' | 'answerMs'> & {
|
|
7
|
+
stopping?: Stopping;
|
|
8
|
+
};
|
|
5
9
|
export declare const endToEnd: (headers: IncomingHttpHeaders) => IncomingHttpHeaders;
|
|
6
|
-
export declare const proxyTo: (entries: Entries, limits:
|
|
10
|
+
export declare const proxyTo: (entries: Entries, limits: ProxyLimits, hostToken?: string) => RequestHandler;
|
|
11
|
+
export {};
|
|
@@ -6,18 +6,27 @@ import { carriesHostToken } from './host-token.js';
|
|
|
6
6
|
import { enter } from './lane.js';
|
|
7
7
|
import {} from './entries.js';
|
|
8
8
|
import {} from './limits.js';
|
|
9
|
+
import { stopsNow } from './stopping.js';
|
|
10
|
+
import {} from './supervisor.js';
|
|
11
|
+
// With no child: the daemon stops, a change still holds it (state still running), or its state.
|
|
12
|
+
const noLane = (entry, stopping) => {
|
|
13
|
+
if (stopping?.signal.aborted)
|
|
14
|
+
return stopsNow();
|
|
15
|
+
const now = entry.state === 'running' ? 'changing' : entry.state;
|
|
16
|
+
return fail('UNAVAILABLE', `${entry.id} is ${now}: try again`, { retryAfter: 5 });
|
|
17
|
+
};
|
|
9
18
|
// 404 not installed or disabled (chosen, not to retry); 503 starting, failed, or held past gateMs.
|
|
10
|
-
const laneFor = async (entries, id,
|
|
19
|
+
const laneFor = async (entries, id, limits) => {
|
|
11
20
|
const entry = entries.get(id);
|
|
12
21
|
if (!entry)
|
|
13
22
|
throw fail('NOT_FOUND', `${id} is not installed`);
|
|
14
23
|
if (entry.state === 'disabled')
|
|
15
24
|
throw fail('NOT_FOUND', `${id} is disabled`);
|
|
16
25
|
if (entry.gate)
|
|
17
|
-
await Promise.race([entry.gate, sleep(gateMs, undefined, { ref: false })]);
|
|
26
|
+
await Promise.race([entry.gate, sleep(limits.gateMs, undefined, { ref: false })]);
|
|
18
27
|
const lane = entry.lane;
|
|
19
28
|
if (!lane)
|
|
20
|
-
throw
|
|
29
|
+
throw noLane(entry, limits.stopping);
|
|
21
30
|
// Counted in the same tick it is read: a swap cannot stop this child before the request is open.
|
|
22
31
|
return { lane, leave: enter(lane) };
|
|
23
32
|
};
|
|
@@ -92,6 +101,6 @@ const forward = (req, res, next, { to, ms, hostToken }) => {
|
|
|
92
101
|
};
|
|
93
102
|
// ANY /api/{id}/* and /mcp/{id} (C1): the same lane, the same refusals (404 unknown, 503 not running).
|
|
94
103
|
export const proxyTo = (entries, limits, hostToken) => async (req, res, next) => {
|
|
95
|
-
const to = await laneFor(entries, String(req.params['id']), limits
|
|
104
|
+
const to = await laneFor(entries, String(req.params['id']), limits);
|
|
96
105
|
forward(req, res, next, { to, ms: limits.answerMs, hostToken });
|
|
97
106
|
};
|
|
@@ -2,12 +2,13 @@ import { existsSync } from 'node:fs';
|
|
|
2
2
|
import { cp, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import {} from '../../log/log.js';
|
|
5
|
+
import { runDirOf } from './daemon-files.js';
|
|
5
6
|
import {} from './supervisor.js';
|
|
6
7
|
// D14 (revised): the child runs dist/; dist.next/ is the watcher's tree and stays; dist.prev/ is the last good.
|
|
7
8
|
const DIST = 'dist';
|
|
8
9
|
const NEXT = 'dist.next';
|
|
9
10
|
const PREV = 'dist.prev';
|
|
10
|
-
const markerOf = (configDir, id) => join(configDir,
|
|
11
|
+
const markerOf = (configDir, id) => join(runDirOf(configDir, id), 'reload.json');
|
|
11
12
|
const gone = (path) => rm(path, { recursive: true, force: true });
|
|
12
13
|
// Written once a stale dist.prev is gone: marker and dist.prev together mean a swap not confirmed.
|
|
13
14
|
export const markReload = async (configDir, id, dir) => {
|
|
@@ -3,8 +3,8 @@ import { fail } from '../../envelope/envelope.js';
|
|
|
3
3
|
import { parse } from '../../envelope/parse.js';
|
|
4
4
|
import { entryOf } from './entries.js';
|
|
5
5
|
import { confirmReload, markReload, swapBack, swapIn, unmarkReload } from './reload-marker.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { halt, hold } from './gate.js';
|
|
7
|
+
import { startEntry } from './supervisor.js';
|
|
8
8
|
const ReloadSchema = z.strictObject({});
|
|
9
9
|
// Only a linked working copy has a build to reload; a disabled one stays stopped.
|
|
10
10
|
const folderOf = (entry) => {
|
|
@@ -34,13 +34,12 @@ const restoreLast = async (deps, entry, dir, swapped) => {
|
|
|
34
34
|
// The new child runs: what is left to clean is best-effort, never a second start.
|
|
35
35
|
const settle = async (deps, entry, dir) => {
|
|
36
36
|
await confirmReload(deps.configDir, entry.id, dir).catch((err) => deps.log.warn('a reload left dist.prev or its marker', { id: entry.id, reason: err.message }));
|
|
37
|
-
resetRestarts(entry);
|
|
38
37
|
return answer(entry);
|
|
39
38
|
};
|
|
40
39
|
const tryNew = async (deps, entry, dir) => {
|
|
41
40
|
await markReload(deps.configDir, entry.id, dir);
|
|
42
41
|
const swapped = await swapIn(dir);
|
|
43
|
-
const err = await startEntry(deps, entry);
|
|
42
|
+
const err = await startEntry(deps, entry, true);
|
|
44
43
|
if (!err)
|
|
45
44
|
return settle(deps, entry, dir);
|
|
46
45
|
await restoreLast(deps, entry, dir, swapped);
|
|
@@ -13,7 +13,7 @@ import { changeExtension, removeExtension } from './ops.js';
|
|
|
13
13
|
import {} from './pending.js';
|
|
14
14
|
import { waitAtMost } from './queue.js';
|
|
15
15
|
import { proxyTo } from './proxy.js';
|
|
16
|
-
import {
|
|
16
|
+
import { reloadExtension } from './reload.js';
|
|
17
17
|
import { stopsNow } from './stopping.js';
|
|
18
18
|
import {} from './supervisor.js';
|
|
19
19
|
const reasonOf = (err) => err.details?.reason;
|
|
@@ -36,6 +36,7 @@ const runner = (deps, info, p) => (what, fn) => p.track(what, () => waitAtMost(d
|
|
|
36
36
|
await checkReadable(deps, info);
|
|
37
37
|
return fn();
|
|
38
38
|
}));
|
|
39
|
+
const purge = (q) => q['purge'] === 'true';
|
|
39
40
|
const changes = (deps, entries, info, p) => {
|
|
40
41
|
const run = runner(deps, info, p);
|
|
41
42
|
const id = (params) => String(params['id']);
|
|
@@ -47,21 +48,24 @@ const changes = (deps, entries, info, p) => {
|
|
|
47
48
|
const what = { op: 'update', id: id(params) };
|
|
48
49
|
res.json(ok(await run(what, () => changeExtension(deps, entries, id(params), body))));
|
|
49
50
|
};
|
|
50
|
-
const purge = (q) => q['purge'] === 'true';
|
|
51
51
|
const remove = async ({ params, query }, res) => {
|
|
52
52
|
const what = { op: 'remove', id: id(params) };
|
|
53
53
|
res.json(ok(await run(what, () => removeExtension(deps, entries, what.id, purge(query)))));
|
|
54
54
|
};
|
|
55
|
-
|
|
55
|
+
const reload = async ({ params, body }, res) => {
|
|
56
|
+
const what = { op: 'reload', id: id(params) };
|
|
57
|
+
res.json(ok(await run(what, () => reloadExtension(deps, entries, what.id, body))));
|
|
58
|
+
};
|
|
59
|
+
return { add, change, remove, reload };
|
|
56
60
|
};
|
|
57
61
|
const hostRoutes = (app, deps, entries, info, p) => {
|
|
58
62
|
app.get('/host/extensions', (_req, res) => void res.json(ok([...entries.values()].map(listed))));
|
|
59
63
|
const json = express.json({ limit: BODY_LIMIT });
|
|
60
|
-
const { add, change, remove } = changes(deps, entries, info, p);
|
|
64
|
+
const { add, change, remove, reload } = changes(deps, entries, info, p);
|
|
61
65
|
app.post('/host/extensions', json, add);
|
|
62
66
|
app.patch('/host/extensions/:id', json, change);
|
|
63
67
|
app.delete('/host/extensions/:id', remove);
|
|
64
|
-
|
|
68
|
+
app.post('/host/extensions/:id/reload', json, reload);
|
|
65
69
|
};
|
|
66
70
|
// C5 and C1: /health, discovery, /host/* (host token), /api/{id}/* and /mcp/{id} to the child; Host and Origin first.
|
|
67
71
|
export const daemonApp = (deps, entries, port, info, hostToken) => {
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
export type Seal = 'on' | 'off';
|
|
2
|
-
export declare const SEAL_FLOOR = "Node 22.22.2 or 24.14.1";
|
|
3
2
|
export declare const sealOf: (version: string) => Seal;
|
|
4
3
|
export declare const permissionFlag: (version: string) => boolean;
|
|
5
4
|
export declare const sealOffReason: (version: string) => string;
|
|
6
5
|
export declare const treeOf: (dir: string) => string;
|
|
7
6
|
export declare const kitReadDirs: (main: string) => string[];
|
|
8
|
-
|
|
7
|
+
interface SealAt {
|
|
9
8
|
id: string;
|
|
10
9
|
main: string;
|
|
11
10
|
dir: string;
|
|
@@ -14,3 +13,4 @@ export interface SealAt {
|
|
|
14
13
|
dataDir: string;
|
|
15
14
|
}
|
|
16
15
|
export declare const sealFlags: ({ id, main, dir, configDir, runDir, dataDir }: SealAt) => string[];
|
|
16
|
+
export {};
|
package/dist/host/daemon/seal.js
CHANGED
|
@@ -3,7 +3,7 @@ import { homedir } from 'node:os';
|
|
|
3
3
|
import { dirname, join, parse, sep } from 'node:path';
|
|
4
4
|
import { assertReach, real } from './seal-reach.js';
|
|
5
5
|
// C2: the permission model with the bypass fixes of D2; the same rule as e2e sealFloorMet.
|
|
6
|
-
|
|
6
|
+
const SEAL_FLOOR = 'Node 22.22.2 or 24.14.1';
|
|
7
7
|
const versionOf = (version) => version.split('.').map(Number);
|
|
8
8
|
const atLeast = (version, major, minor, patch) => {
|
|
9
9
|
const [a = 0, b = 0, c = 0] = versionOf(version);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type ExtensionPackage } from '../../manifest/manifest.schema.js';
|
|
2
|
-
export declare const NEEDS_SECRET = "NEEDS_SECRET";
|
|
3
2
|
export declare const assertSecrets: (pkg: ExtensionPackage, secrets: Record<string, string>) => void;
|
|
4
3
|
export declare const needsSecret: (err: unknown) => boolean;
|
|
5
4
|
export declare const healthy: (port: number, ms: number) => Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {} from '../../manifest/manifest.schema.js';
|
|
2
2
|
import { hostParts, mountedIn } from '../../manifest/parts.js';
|
|
3
|
-
|
|
3
|
+
const NEEDS_SECRET = 'NEEDS_SECRET';
|
|
4
4
|
// C6: a declared secret of a server part with no value; the daemon checks it before the fork.
|
|
5
5
|
export const assertSecrets = (pkg, secrets) => {
|
|
6
6
|
const names = new Set(hostParts(pkg).flatMap((part) => mountedIn(pkg, part)));
|
|
@@ -7,7 +7,6 @@ import { type PendingChanges } from './pending.js';
|
|
|
7
7
|
import { type Queue } from './queue.js';
|
|
8
8
|
import { type Retry } from './restart.js';
|
|
9
9
|
import { type Stopping } from './stopping.js';
|
|
10
|
-
export { halt, hold } from './gate.js';
|
|
11
10
|
export interface InstallSpec {
|
|
12
11
|
id: string;
|
|
13
12
|
version?: string;
|
|
@@ -53,8 +52,6 @@ export interface SupervisorDeps extends Limits {
|
|
|
53
52
|
unsynced: Set<string>;
|
|
54
53
|
}
|
|
55
54
|
export declare const valuesOf: (configDir: string, id: string) => Promise<Values>;
|
|
56
|
-
export declare const
|
|
57
|
-
export declare const startEntry: (deps: SupervisorDeps, entry: Entry) => Promise<Error | undefined>;
|
|
58
|
-
export declare const restore: (deps: SupervisorDeps, entry: Entry) => Promise<void>;
|
|
55
|
+
export declare const startEntry: (deps: SupervisorDeps, entry: Entry, fresh?: boolean) => Promise<Error | undefined>;
|
|
59
56
|
export declare const startFailed: (entry: Entry, err: unknown) => import("../../envelope/envelope.js").Failure;
|
|
60
57
|
export declare const swap: (deps: SupervisorDeps, entry: Entry, to: Next, undo?: () => Promise<unknown>) => Promise<void>;
|
|
@@ -13,7 +13,6 @@ import {} from './queue.js';
|
|
|
13
13
|
import { crashed, healthyFor, reasonOf, resetRestarts } from './restart.js';
|
|
14
14
|
import { assertSecrets, healthy, redact } from './start-checks.js';
|
|
15
15
|
import {} from './stopping.js';
|
|
16
|
-
export { halt, hold } from './gate.js';
|
|
17
16
|
export const valuesOf = async (configDir, id) => ({
|
|
18
17
|
settings: await readValues(configDir, 'settings', id),
|
|
19
18
|
secrets: (await readValues(configDir, 'secrets', id)),
|
|
@@ -28,25 +27,27 @@ const dirOf = async (deps, entry, install, given) => {
|
|
|
28
27
|
return entry.dir;
|
|
29
28
|
return (await runInstaller(deps, { id: entry.id, version: install.version })).dir;
|
|
30
29
|
};
|
|
31
|
-
// A new child for
|
|
32
|
-
|
|
33
|
-
const dir = await dirOf(deps, entry, install,
|
|
30
|
+
// A new child for to, healthy, then the route; fresh: a start the owner asked for (restarts reset).
|
|
31
|
+
const launchFor = async (deps, entry, to, fresh) => {
|
|
32
|
+
const dir = await dirOf(deps, entry, to.install, to.dir);
|
|
34
33
|
const pkg = await readManifest(dir);
|
|
35
|
-
assertSecrets(pkg, values.secrets);
|
|
36
|
-
const spec = { id: entry.id, dir, dataDir: dataDirOf(deps.configDir, entry.id), ...values };
|
|
34
|
+
assertSecrets(pkg, to.values.secrets);
|
|
35
|
+
const spec = { id: entry.id, dir, dataDir: dataDirOf(deps.configDir, entry.id), ...to.values };
|
|
37
36
|
const child = await deps
|
|
38
37
|
.launch({ ...spec, origin: deps.origin(), storeOpenMs: deps.healthMs })
|
|
39
|
-
.catch((err) => Promise.reject(Object.assign(err, { message: redact(err.message, values.secrets) })));
|
|
38
|
+
.catch((err) => Promise.reject(Object.assign(err, { message: redact(err.message, to.values.secrets) })));
|
|
40
39
|
await healthy(child.port, deps.healthMs).catch(async (err) => {
|
|
41
40
|
await child.stop();
|
|
42
41
|
throw err;
|
|
43
42
|
});
|
|
44
|
-
Object.assign(entry, { dir, version: pkg.version });
|
|
45
43
|
const lane = laneOf(child);
|
|
46
|
-
|
|
44
|
+
Object.assign(entry, { dir, version: pkg.version, lane, state: 'running', error: undefined });
|
|
45
|
+
if (fresh)
|
|
46
|
+
resetRestarts(entry);
|
|
47
|
+
// Watched only once it is the route: an exit that came before this line is seen, not lost.
|
|
48
|
+
const words = (how) => redact(reasonOf(how), to.values.secrets);
|
|
47
49
|
void child.exited.then((how) => exitedEarly(deps, entry, lane, words(how)));
|
|
48
50
|
healthyFor(deps, entry, lane);
|
|
49
|
-
return lane;
|
|
50
51
|
};
|
|
51
52
|
// A child that exits while it is the route restarts (C6); a retired one was detached first.
|
|
52
53
|
const exitedEarly = (deps, entry, lane, why) => {
|
|
@@ -60,12 +61,11 @@ const failed = (deps, entry, err) => {
|
|
|
60
61
|
deps.log.warn('extension failed to start', { id: entry.id, reason: entry.error });
|
|
61
62
|
};
|
|
62
63
|
// The install and values of installation.json, as they are on disk; the error when it failed.
|
|
63
|
-
export const startEntry = async (deps, entry) => {
|
|
64
|
+
export const startEntry = async (deps, entry, fresh = false) => {
|
|
64
65
|
entry.state = 'starting';
|
|
65
66
|
try {
|
|
66
67
|
const values = await valuesOf(deps.configDir, entry.id);
|
|
67
|
-
|
|
68
|
-
Object.assign(entry, { state: 'running', error: undefined });
|
|
68
|
+
await launchFor(deps, entry, { install: entry.install, values }, fresh);
|
|
69
69
|
return undefined;
|
|
70
70
|
}
|
|
71
71
|
catch (err) {
|
|
@@ -80,7 +80,7 @@ const restartEntry = async (deps, entry) => {
|
|
|
80
80
|
crashed(deps, entry, err.message, () => restartEntry(deps, entry));
|
|
81
81
|
};
|
|
82
82
|
// The old install again after a refused change; a disabled one stays stopped.
|
|
83
|
-
|
|
83
|
+
const restore = async (deps, entry) => {
|
|
84
84
|
if (entry.install.enabled === false)
|
|
85
85
|
entry.state = 'disabled';
|
|
86
86
|
else
|
|
@@ -96,9 +96,7 @@ export const startFailed = (entry, err) => {
|
|
|
96
96
|
export const swap = async (deps, entry, to, undo = async () => undefined) => {
|
|
97
97
|
try {
|
|
98
98
|
await halt(entry, deps.drainMs);
|
|
99
|
-
|
|
100
|
-
Object.assign(entry, { state: 'running', error: undefined });
|
|
101
|
-
resetRestarts(entry);
|
|
99
|
+
await launchFor(deps, entry, to, true);
|
|
102
100
|
}
|
|
103
101
|
catch (err) {
|
|
104
102
|
await undo();
|