@agentage/extension 0.20.1 → 0.20.2
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/auth/part-auth.js +4 -5
- package/dist/context/context.d.ts +0 -1
- package/dist/host/child-env.d.ts +11 -0
- package/dist/host/child-env.js +12 -0
- package/dist/host/{daemon/guard.js → guard.js} +1 -1
- package/dist/host/host-extension.js +1 -1
- package/dist/host/host-main.d.ts +1 -0
- package/dist/host/host-main.js +3 -0
- package/dist/host/index.d.ts +1 -5
- package/dist/host/index.js +1 -5
- package/dist/host/kit-shell.d.ts +1 -0
- package/dist/host/kit-shell.js +8 -0
- package/dist/host/serve-host.js +1 -1
- package/dist/internal/index.d.ts +29 -0
- package/dist/internal/index.js +30 -0
- package/dist/runtime/part-mounts.d.ts +0 -1
- package/dist/runtime/part-mounts.js +1 -1
- package/dist/runtime/run-part.js +0 -2
- package/dist/server/app.d.ts +0 -1
- package/dist/server/app.js +4 -5
- package/dist/shell/dist/index.html +1 -1
- package/dist/shell/dist/shell.css +1 -1
- package/dist/tool/check.js +3 -0
- package/dist/tool/recipe.js +2 -2
- package/dist/tool/up/gateway.js +0 -3
- package/package.json +5 -1
- package/dist/host/daemon/add.d.ts +0 -11
- package/dist/host/daemon/add.js +0 -93
- package/dist/host/daemon/app-files.d.ts +0 -8
- package/dist/host/daemon/app-files.js +0 -34
- package/dist/host/daemon/app-header.d.ts +0 -2
- package/dist/host/daemon/app-header.js +0 -12
- package/dist/host/daemon/app-reload.d.ts +0 -10
- package/dist/host/daemon/app-reload.js +0 -18
- package/dist/host/daemon/child-files.d.ts +0 -20
- package/dist/host/daemon/child-files.js +0 -53
- package/dist/host/daemon/child.d.ts +0 -30
- package/dist/host/daemon/child.js +0 -126
- package/dist/host/daemon/cli-user-watch.d.ts +0 -9
- package/dist/host/daemon/cli-user-watch.js +0 -47
- package/dist/host/daemon/cli-user.d.ts +0 -2
- package/dist/host/daemon/cli-user.js +0 -42
- package/dist/host/daemon/daemon-files.d.ts +0 -20
- package/dist/host/daemon/daemon-files.js +0 -46
- package/dist/host/daemon/daemon-owner.d.ts +0 -1
- package/dist/host/daemon/daemon-owner.js +0 -32
- package/dist/host/daemon/daemon.d.ts +0 -19
- package/dist/host/daemon/daemon.js +0 -137
- package/dist/host/daemon/entries.d.ts +0 -21
- package/dist/host/daemon/entries.js +0 -24
- package/dist/host/daemon/gate.d.ts +0 -3
- package/dist/host/daemon/gate.js +0 -19
- package/dist/host/daemon/health-routes.d.ts +0 -18
- package/dist/host/daemon/health-routes.js +0 -75
- package/dist/host/daemon/host-token.d.ts +0 -6
- package/dist/host/daemon/host-token.js +0 -28
- package/dist/host/daemon/installation.d.ts +0 -35
- package/dist/host/daemon/installation.js +0 -90
- package/dist/host/daemon/installer-call.d.ts +0 -21
- package/dist/host/daemon/installer-call.js +0 -48
- package/dist/host/daemon/json-file.d.ts +0 -6
- package/dist/host/daemon/json-file.js +0 -90
- package/dist/host/daemon/lane.d.ts +0 -9
- package/dist/host/daemon/lane.js +0 -24
- package/dist/host/daemon/last-words.d.ts +0 -2
- package/dist/host/daemon/last-words.js +0 -28
- package/dist/host/daemon/limits.d.ts +0 -16
- package/dist/host/daemon/limits.js +0 -31
- package/dist/host/daemon/ops.d.ts +0 -12
- package/dist/host/daemon/ops.js +0 -94
- package/dist/host/daemon/patch-values.d.ts +0 -6
- package/dist/host/daemon/patch-values.js +0 -39
- package/dist/host/daemon/pending.d.ts +0 -15
- package/dist/host/daemon/pending.js +0 -18
- package/dist/host/daemon/proxy.d.ts +0 -11
- package/dist/host/daemon/proxy.js +0 -106
- package/dist/host/daemon/queue.d.ts +0 -3
- package/dist/host/daemon/queue.js +0 -27
- package/dist/host/daemon/redact-stream.d.ts +0 -4
- package/dist/host/daemon/redact-stream.js +0 -24
- package/dist/host/daemon/reload-marker.d.ts +0 -8
- package/dist/host/daemon/reload-marker.js +0 -57
- package/dist/host/daemon/reload.d.ts +0 -10
- package/dist/host/daemon/reload.js +0 -72
- package/dist/host/daemon/restart.d.ts +0 -14
- package/dist/host/daemon/restart.js +0 -59
- package/dist/host/daemon/routes.d.ts +0 -6
- package/dist/host/daemon/routes.js +0 -101
- package/dist/host/daemon/run-lock.d.ts +0 -1
- package/dist/host/daemon/run-lock.js +0 -15
- package/dist/host/daemon/seal-reach.d.ts +0 -2
- package/dist/host/daemon/seal-reach.js +0 -38
- package/dist/host/daemon/seal.d.ts +0 -20
- package/dist/host/daemon/seal.js +0 -93
- package/dist/host/daemon/shell-events.d.ts +0 -9
- package/dist/host/daemon/shell-events.js +0 -36
- package/dist/host/daemon/shell-routes.d.ts +0 -29
- package/dist/host/daemon/shell-routes.js +0 -68
- package/dist/host/daemon/sign-in-page.d.ts +0 -3
- package/dist/host/daemon/sign-in-page.js +0 -30
- package/dist/host/daemon/start-checks.d.ts +0 -6
- package/dist/host/daemon/start-checks.js +0 -29
- package/dist/host/daemon/stopping.d.ts +0 -10
- package/dist/host/daemon/stopping.js +0 -28
- package/dist/host/daemon/supervisor.d.ts +0 -59
- package/dist/host/daemon/supervisor.js +0 -107
- package/dist/host/daemon/write-all.d.ts +0 -8
- package/dist/host/daemon/write-all.js +0 -45
- /package/dist/host/{daemon/guard.d.ts → guard.d.ts} +0 -0
package/dist/host/daemon/lane.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
-
import {} from './child.js';
|
|
3
|
-
export const laneOf = (child) => ({ child, open: 0, idle: [] });
|
|
4
|
-
// Counts one request until leave() is called (once).
|
|
5
|
-
export const enter = (lane) => {
|
|
6
|
-
lane.open += 1;
|
|
7
|
-
let left = false;
|
|
8
|
-
return () => {
|
|
9
|
-
if (left)
|
|
10
|
-
return;
|
|
11
|
-
left = true;
|
|
12
|
-
lane.open -= 1;
|
|
13
|
-
if (lane.open === 0)
|
|
14
|
-
lane.idle.splice(0).forEach((resolve) => resolve());
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
const drained = (lane) => lane.open === 0 ? Promise.resolve() : new Promise((resolve) => lane.idle.push(resolve));
|
|
18
|
-
// Waits for the open requests (at most drainMs, so a hung one blocks nothing), then stops the child.
|
|
19
|
-
export const retire = async (lane, drainMs) => {
|
|
20
|
-
if (!lane)
|
|
21
|
-
return;
|
|
22
|
-
await Promise.race([drained(lane), sleep(drainMs, undefined, { ref: false })]);
|
|
23
|
-
await lane.child.stop();
|
|
24
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {} from './child.js';
|
|
2
|
-
// Lines that never say why: blanks, stack frames, error properties, the Node version, the hint.
|
|
3
|
-
const NOISE = [
|
|
4
|
-
/^\s*$/,
|
|
5
|
-
/^\s+at /,
|
|
6
|
-
/^\s*[{}]\s*$/,
|
|
7
|
-
/^\s+[\w$'"]+: /,
|
|
8
|
-
/^Node\.js v\d/,
|
|
9
|
-
/^\(Use `node/,
|
|
10
|
-
];
|
|
11
|
-
// A log row below error (listening, a request, a warning) is not why a child exited.
|
|
12
|
-
const QUIET = /^\{.*"level":"(warn|info|debug|trace)"/;
|
|
13
|
-
const msgOf = (line) => {
|
|
14
|
-
try {
|
|
15
|
-
return String(JSON.parse(line).msg ?? line);
|
|
16
|
-
}
|
|
17
|
-
catch {
|
|
18
|
-
return line;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
// The last line that says why (an error line, a log row's msg, or the thrown text): from stderr.
|
|
22
|
-
export const lastWords = (tail, how) => {
|
|
23
|
-
const lines = tail.split('\n').map((l) => l.trimEnd());
|
|
24
|
-
const line = lines.reverse().find((l) => !QUIET.test(l) && !NOISE.some((re) => re.test(l)));
|
|
25
|
-
if (line)
|
|
26
|
-
return msgOf(line).replace(/^[A-Za-z]*Error(?: \[[A-Z_]+\])?: /, '');
|
|
27
|
-
return `exited with ${how.signal ? `signal ${how.signal}` : `code ${how.code}`}`;
|
|
28
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export declare const LIMITS: {
|
|
2
|
-
drainMs: number;
|
|
3
|
-
startMs: number;
|
|
4
|
-
healthMs: number;
|
|
5
|
-
lockMs: number;
|
|
6
|
-
gateMs: number;
|
|
7
|
-
answerMs: number;
|
|
8
|
-
installMs: number;
|
|
9
|
-
backoffMs: number[];
|
|
10
|
-
crashLimit: number;
|
|
11
|
-
crashWindowMs: number;
|
|
12
|
-
healthyResetMs: number;
|
|
13
|
-
userPollMs: number;
|
|
14
|
-
};
|
|
15
|
-
export type Limits = typeof LIMITS;
|
|
16
|
-
export declare const limitsOf: (given: Partial<Limits>) => Limits;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// Every wait of the daemon has a bound; each is a createDaemon option (docs/part-image.md).
|
|
2
|
-
export const LIMITS = {
|
|
3
|
-
// An old child's open requests before it is stopped.
|
|
4
|
-
drainMs: 10_000,
|
|
5
|
-
// A forked child's port report; then SIGKILL.
|
|
6
|
-
startMs: 60_000,
|
|
7
|
-
// A new child's own /health, once it reported its port.
|
|
8
|
-
healthMs: 60_000,
|
|
9
|
-
// A write waits for another process that holds the lock of a daemon file.
|
|
10
|
-
lockMs: 5_000,
|
|
11
|
-
// A request held while its extension swaps children.
|
|
12
|
-
gateMs: 30_000,
|
|
13
|
-
// A child's answer headers; then 504.
|
|
14
|
-
answerMs: 60_000,
|
|
15
|
-
// One Installer call (POST, PATCH, each entry at boot), and a change queued behind others:
|
|
16
|
-
// above the cli's npm bound (10 min), below its client wait (12 min).
|
|
17
|
-
installMs: 660_000,
|
|
18
|
-
// C6: the waits before each restart of a child that exited; the last one repeats.
|
|
19
|
-
backoffMs: [1_000, 2_000, 4_000, 8_000, 16_000],
|
|
20
|
-
// C6: this many exits inside crashWindowMs is failed, with no more restarts.
|
|
21
|
-
crashLimit: 5,
|
|
22
|
-
crashWindowMs: 600_000,
|
|
23
|
-
// A child healthy this long has its restart count and exits reset.
|
|
24
|
-
healthyResetMs: 600_000,
|
|
25
|
-
// How often auth.json is read: a sign-in or sign-out restarts the running children.
|
|
26
|
-
userPollMs: 2_000,
|
|
27
|
-
};
|
|
28
|
-
export const limitsOf = (given) => {
|
|
29
|
-
const keys = Object.keys(LIMITS);
|
|
30
|
-
return Object.fromEntries(keys.map((key) => [key, given[key] ?? LIMITS[key]]));
|
|
31
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type Entries } from './entries.js';
|
|
2
|
-
import { type SupervisorDeps } from './supervisor.js';
|
|
3
|
-
export declare const changeExtension: (deps: SupervisorDeps, entries: Entries, id: string, body: unknown) => Promise<{
|
|
4
|
-
error?: string | undefined;
|
|
5
|
-
pid?: number | undefined;
|
|
6
|
-
id: string;
|
|
7
|
-
version: string;
|
|
8
|
-
state: import("./supervisor.js").State;
|
|
9
|
-
}>;
|
|
10
|
-
export declare const removeExtension: (deps: SupervisorDeps, entries: Entries, id: string, purge: boolean) => Promise<{
|
|
11
|
-
id: string;
|
|
12
|
-
}>;
|
package/dist/host/daemon/ops.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { fail } from '../../envelope/envelope.js';
|
|
4
|
-
import { parse } from '../../envelope/parse.js';
|
|
5
|
-
import { SecretNameSchema } from '../../manifest/primitives.schema.js';
|
|
6
|
-
import { VersionSpecSchema } from './add.js';
|
|
7
|
-
import { clearRunDir, purgeData } from './daemon-files.js';
|
|
8
|
-
import { unmarkReload } from './reload-marker.js';
|
|
9
|
-
import { entryOf, summary } from './entries.js';
|
|
10
|
-
import { unsynced, writeAll } from './write-all.js';
|
|
11
|
-
import { runInstaller } from './installer-call.js';
|
|
12
|
-
import { checkedSettings } from './patch-values.js';
|
|
13
|
-
import {} from './installation.js';
|
|
14
|
-
import { halt, hold } from './gate.js';
|
|
15
|
-
import { swap, valuesOf, } from './supervisor.js';
|
|
16
|
-
const PatchSchema = z.strictObject({
|
|
17
|
-
version: VersionSpecSchema.optional(),
|
|
18
|
-
enabled: z.boolean().optional(),
|
|
19
|
-
settings: z.record(z.string(), z.unknown()).optional(),
|
|
20
|
-
secrets: z.record(SecretNameSchema, z.string()).optional(),
|
|
21
|
-
});
|
|
22
|
-
// The install entry a patch asks for; a new version is installed first and its folder kept aside.
|
|
23
|
-
const nextInstall = async (deps, entry, patch) => {
|
|
24
|
-
// enabled absent means true (C3), so only false is written.
|
|
25
|
-
const install = { ...entry.install };
|
|
26
|
-
delete install.enabled;
|
|
27
|
-
if ((patch.enabled ?? entry.install.enabled) === false)
|
|
28
|
-
install.enabled = false;
|
|
29
|
-
if (!patch.version || patch.version === entry.install.version)
|
|
30
|
-
return { install };
|
|
31
|
-
if (install.source)
|
|
32
|
-
throw fail('VALIDATION_ERROR', `${entry.id} runs a folder: it has no version`);
|
|
33
|
-
const got = await runInstaller(deps, { id: entry.id, version: patch.version });
|
|
34
|
-
return { install: { ...install, version: got.version }, dir: got.dir };
|
|
35
|
-
};
|
|
36
|
-
// The change, and the values on disk now: what a failed write puts back.
|
|
37
|
-
const nextOf = async (deps, entry, patch) => {
|
|
38
|
-
const next = await nextInstall(deps, entry, patch);
|
|
39
|
-
const dir = next.dir ?? entry.dir ?? entry.install.source;
|
|
40
|
-
const given = await checkedSettings(dir, patch, entry.id);
|
|
41
|
-
const was = await valuesOf(deps.configDir, entry.id);
|
|
42
|
-
const settings = { ...was.settings, ...given };
|
|
43
|
-
const values = { settings, secrets: { ...was.secrets, ...patch.secrets } };
|
|
44
|
-
return { to: { ...next, values }, was };
|
|
45
|
-
};
|
|
46
|
-
// Disabled: written, then stopped. Else written, then swapped; a refused start puts the files back.
|
|
47
|
-
const apply = async (deps, entry, to, was) => {
|
|
48
|
-
const back = { install: entry.install, values: was };
|
|
49
|
-
await writeAll(deps, entry.id, to, back);
|
|
50
|
-
const undo = () => writeAll(deps, entry.id, back, to).catch(() => unsynced(deps, entry.id));
|
|
51
|
-
if (to.install.enabled !== false)
|
|
52
|
-
await swap(deps, entry, to, undo);
|
|
53
|
-
else
|
|
54
|
-
await halt(entry, deps.drainMs);
|
|
55
|
-
Object.assign(entry, { install: to.install }, to.dir && { dir: to.dir });
|
|
56
|
-
if (to.install.enabled === false)
|
|
57
|
-
entry.state = 'disabled';
|
|
58
|
-
};
|
|
59
|
-
// A request sent once the change is decided waits (gateMs) and goes to the result.
|
|
60
|
-
const held = async (deps, entry, to, was) => {
|
|
61
|
-
const release = hold(entry);
|
|
62
|
-
try {
|
|
63
|
-
await apply(deps, entry, to, was);
|
|
64
|
-
}
|
|
65
|
-
finally {
|
|
66
|
-
release();
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
// Nothing to do: the same files, and the entry already runs (or is stopped) as they say.
|
|
70
|
-
const unchanged = (entry, to, was) => entry.state === (to.install.enabled === false ? 'disabled' : 'running') &&
|
|
71
|
-
(!to.dir || to.dir === entry.dir) &&
|
|
72
|
-
isDeepStrictEqual(to.install, entry.install) &&
|
|
73
|
-
isDeepStrictEqual(to.values, was);
|
|
74
|
-
// PATCH: a refused change keeps the old one and is 400 START_FAILED; a failed entry is started again.
|
|
75
|
-
export const changeExtension = async (deps, entries, id, body) => {
|
|
76
|
-
const patch = parse(PatchSchema, body, 'Invalid body');
|
|
77
|
-
const entry = entryOf(entries, id);
|
|
78
|
-
const { to, was } = await nextOf(deps, entry, patch);
|
|
79
|
-
if (!unchanged(entry, to, was))
|
|
80
|
-
await held(deps, entry, to, was);
|
|
81
|
-
return summary(entry);
|
|
82
|
-
};
|
|
83
|
-
// DELETE: the files first (all or none), then the child stops; purge also deletes its data folder.
|
|
84
|
-
export const removeExtension = async (deps, entries, id, purge) => {
|
|
85
|
-
const entry = entryOf(entries, id);
|
|
86
|
-
await writeAll(deps, id, {}, { install: entry.install, values: await valuesOf(deps.configDir, id) });
|
|
87
|
-
entries.delete(id);
|
|
88
|
-
await halt(entry, deps.drainMs);
|
|
89
|
-
await unmarkReload(deps.configDir, id);
|
|
90
|
-
await clearRunDir(deps.configDir, id);
|
|
91
|
-
if (purge)
|
|
92
|
-
await purgeData(deps.configDir, id);
|
|
93
|
-
return { id };
|
|
94
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { fits } from '../../context/settings.js';
|
|
2
|
-
import { fail } from '../../envelope/envelope.js';
|
|
3
|
-
import {} from '../../manifest/manifest.schema.js';
|
|
4
|
-
import { readManifest } from '../../manifest/read-manifest.js';
|
|
5
|
-
const PARSE = {
|
|
6
|
-
number: (t) => (/^-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(t) ? Number(t) : t),
|
|
7
|
-
integer: (t) => (/^-?\d+$/.test(t) ? Number(t) : t),
|
|
8
|
-
boolean: (t) => (t === 'true' ? true : t === 'false' ? false : t),
|
|
9
|
-
};
|
|
10
|
-
const refused = (key, message) => fail('VALIDATION_ERROR', `setting ${key}: ${message}`, { code: 'INVALID_SETTING' });
|
|
11
|
-
const valueOf = (key, setting, given) => {
|
|
12
|
-
if (!setting)
|
|
13
|
-
throw refused(key, 'not declared in the manifest');
|
|
14
|
-
const value = typeof given === 'string' ? (PARSE[setting.type]?.(given) ?? given) : given;
|
|
15
|
-
if (!fits(setting, value))
|
|
16
|
-
throw refused(key, `${JSON.stringify(given)} is not a valid ${setting.type}`);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
// Only a secret the package declares: the name becomes a file name in the child's folder.
|
|
20
|
-
const checkSecrets = (pkg, names, id) => {
|
|
21
|
-
const known = (pkg.agentage.secrets ?? []).map((s) => (typeof s === 'string' ? s : s.name));
|
|
22
|
-
const unknown = names.filter((name) => !known.includes(name));
|
|
23
|
-
if (unknown.length)
|
|
24
|
-
throw fail('VALIDATION_ERROR', `${id} declares no secret ${unknown.join(', ')}`);
|
|
25
|
-
};
|
|
26
|
-
// The secret names checked, and each setting a client sent as text (`limit=5`) in its declared type.
|
|
27
|
-
export const checkedSettings = async (dir, given, id) => {
|
|
28
|
-
const settings = given.settings ?? {};
|
|
29
|
-
const names = Object.keys(given.secrets ?? {});
|
|
30
|
-
if (!names.length && !Object.keys(settings).length)
|
|
31
|
-
return settings;
|
|
32
|
-
if (!dir)
|
|
33
|
-
throw fail('VALIDATION_ERROR', `${id} has no package yet: install it first`);
|
|
34
|
-
const pkg = await readManifest(dir);
|
|
35
|
-
checkSecrets(pkg, names, id);
|
|
36
|
-
const declared = pkg.agentage.settings ?? {};
|
|
37
|
-
const typed = Object.entries(settings).map(([key, v]) => [key, valueOf(key, declared[key], v)]);
|
|
38
|
-
return Object.fromEntries(typed);
|
|
39
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type PendingOp = 'install' | 'update' | 'remove' | 'reload';
|
|
2
|
-
export interface Pending {
|
|
3
|
-
op: PendingOp;
|
|
4
|
-
id?: string;
|
|
5
|
-
source?: string;
|
|
6
|
-
since: string;
|
|
7
|
-
timedOut?: true;
|
|
8
|
-
}
|
|
9
|
-
export declare const pendingChanges: () => {
|
|
10
|
-
track: <T>(change: Omit<Pending, "since">, fn: () => Promise<T>) => Promise<T>;
|
|
11
|
-
linger: (id: string, since: string, work: Promise<unknown>) => void;
|
|
12
|
-
timedOut: (id: string) => Pending | undefined;
|
|
13
|
-
list: () => Pending[];
|
|
14
|
-
};
|
|
15
|
-
export type PendingChanges = ReturnType<typeof pendingChanges>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// The changes taken and not yet answered, queued ones too: /health shows one the client gave up on.
|
|
2
|
-
export const pendingChanges = () => {
|
|
3
|
-
const open = new Set();
|
|
4
|
-
const until = async (row, work) => {
|
|
5
|
-
open.add(row);
|
|
6
|
-
try {
|
|
7
|
-
return await work;
|
|
8
|
-
}
|
|
9
|
-
finally {
|
|
10
|
-
open.delete(row);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
const track = (change, fn) => until({ ...change, since: new Date().toISOString() }, Promise.resolve().then(fn));
|
|
14
|
-
// A timed-out installer call stays listed until it settles.
|
|
15
|
-
const linger = (id, since, work) => void until({ op: 'install', id, since, timedOut: true }, work).catch(() => undefined);
|
|
16
|
-
const timedOut = (id) => [...open].find((row) => row.timedOut && row.id === id);
|
|
17
|
-
return { track, linger, timedOut, list: () => [...open] };
|
|
18
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type IncomingHttpHeaders } from 'node:http';
|
|
2
|
-
import { type RequestHandler } from 'express';
|
|
3
|
-
import { type Entries } from './entries.js';
|
|
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
|
-
};
|
|
9
|
-
export declare const endToEnd: (headers: IncomingHttpHeaders) => IncomingHttpHeaders;
|
|
10
|
-
export declare const proxyTo: (entries: Entries, limits: ProxyLimits, hostToken?: string) => RequestHandler;
|
|
11
|
-
export {};
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { request, } from 'node:http';
|
|
2
|
-
import { setTimeout as sleep } from 'node:timers/promises';
|
|
3
|
-
import {} from 'express';
|
|
4
|
-
import { fail } from '../../envelope/envelope.js';
|
|
5
|
-
import { carriesHostToken } from './host-token.js';
|
|
6
|
-
import { enter } from './lane.js';
|
|
7
|
-
import {} from './entries.js';
|
|
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
|
-
};
|
|
18
|
-
// 404 not installed or disabled (chosen, not to retry); 503 starting, failed, or held past gateMs.
|
|
19
|
-
const laneFor = async (entries, id, limits) => {
|
|
20
|
-
const entry = entries.get(id);
|
|
21
|
-
if (!entry)
|
|
22
|
-
throw fail('NOT_FOUND', `${id} is not installed`);
|
|
23
|
-
if (entry.state === 'disabled')
|
|
24
|
-
throw fail('NOT_FOUND', `${id} is disabled`);
|
|
25
|
-
if (entry.gate)
|
|
26
|
-
await Promise.race([entry.gate, sleep(limits.gateMs, undefined, { ref: false })]);
|
|
27
|
-
const lane = entry.lane;
|
|
28
|
-
if (!lane)
|
|
29
|
-
throw noLane(entry, limits.stopping);
|
|
30
|
-
// Counted in the same tick it is read: a swap cannot stop this child before the request is open.
|
|
31
|
-
return { lane, leave: enter(lane) };
|
|
32
|
-
};
|
|
33
|
-
const HOP = [
|
|
34
|
-
'connection',
|
|
35
|
-
'keep-alive',
|
|
36
|
-
'proxy-authenticate',
|
|
37
|
-
'proxy-authorization',
|
|
38
|
-
'te',
|
|
39
|
-
'trailer',
|
|
40
|
-
'transfer-encoding',
|
|
41
|
-
'upgrade',
|
|
42
|
-
];
|
|
43
|
-
// RFC 9110 7.6.1: hop-by-hop headers, and those the Connection header names, stay on their hop.
|
|
44
|
-
export const endToEnd = (headers) => {
|
|
45
|
-
const named = String(headers['connection'] ?? '')
|
|
46
|
-
.toLowerCase()
|
|
47
|
-
.split(',');
|
|
48
|
-
const drop = new Set([...HOP, ...named.map((h) => h.trim())]);
|
|
49
|
-
return Object.fromEntries(Object.entries(headers).filter(([key]) => !drop.has(key)));
|
|
50
|
-
};
|
|
51
|
-
// No answer headers in answerMs: 504 TIMEOUT; the child may still apply the call.
|
|
52
|
-
const noAnswer = (ms) => Object.assign(fail('UNAVAILABLE', `The extension did not answer in ${ms} ms`), {
|
|
53
|
-
code: 'TIMEOUT',
|
|
54
|
-
status: 504,
|
|
55
|
-
retryAfter: undefined,
|
|
56
|
-
});
|
|
57
|
-
// D11: the host token is for the daemon; a child never sees it, also when a client sends it here.
|
|
58
|
-
const forChild = (req, hostToken) => {
|
|
59
|
-
const headers = endToEnd(req.headers);
|
|
60
|
-
if (hostToken && carriesHostToken(req.headers, hostToken))
|
|
61
|
-
delete headers['authorization'];
|
|
62
|
-
return headers;
|
|
63
|
-
};
|
|
64
|
-
const requestTo = (req, lane, hostToken) => {
|
|
65
|
-
const { method, originalUrl: path } = req;
|
|
66
|
-
const headers = forChild(req, hostToken);
|
|
67
|
-
const options = { host: '127.0.0.1', port: lane.child.port, method, path, headers, agent: false };
|
|
68
|
-
return request(options);
|
|
69
|
-
};
|
|
70
|
-
// The child's answer as it streams; an error before its headers is the envelope (504 when late).
|
|
71
|
-
const relay = (upstream, res, next, ms) => {
|
|
72
|
-
let late = false;
|
|
73
|
-
const timer = setTimeout(() => ((late = true), upstream.destroy()), ms);
|
|
74
|
-
upstream.on('response', (up) => {
|
|
75
|
-
clearTimeout(timer);
|
|
76
|
-
res.writeHead(up.statusCode ?? 502, endToEnd(up.headers));
|
|
77
|
-
// A child gone in the middle of its body: the answer is cut, not left open.
|
|
78
|
-
up.once('close', () => void (up.complete || res.destroy()));
|
|
79
|
-
up.pipe(res);
|
|
80
|
-
});
|
|
81
|
-
upstream.on('error', () => {
|
|
82
|
-
if (res.headersSent)
|
|
83
|
-
return void res.destroy();
|
|
84
|
-
next(late ? noAnswer(ms) : fail('UNAVAILABLE', 'The extension did not answer', { retryAfter: 5 }));
|
|
85
|
-
});
|
|
86
|
-
return () => clearTimeout(timer);
|
|
87
|
-
};
|
|
88
|
-
const forward = (req, res, next, { to, ms, hostToken }) => {
|
|
89
|
-
const { lane, leave } = to;
|
|
90
|
-
const upstream = requestTo(req, lane, hostToken);
|
|
91
|
-
const stop = relay(upstream, res, next, ms);
|
|
92
|
-
upstream.on('error', leave);
|
|
93
|
-
// A client that went away aborts the request to the child too.
|
|
94
|
-
res.on('close', () => {
|
|
95
|
-
stop();
|
|
96
|
-
if (!res.writableFinished)
|
|
97
|
-
upstream.destroy();
|
|
98
|
-
leave();
|
|
99
|
-
});
|
|
100
|
-
req.pipe(upstream);
|
|
101
|
-
};
|
|
102
|
-
// ANY /api/{id}/* and /mcp/{id} (C1): the same lane, the same refusals (404 unknown, 503 not running).
|
|
103
|
-
export const proxyTo = (entries, limits, hostToken) => async (req, res, next) => {
|
|
104
|
-
const to = await laneFor(entries, String(req.params['id']), limits);
|
|
105
|
-
forward(req, res, next, { to, ms: limits.answerMs, hostToken });
|
|
106
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { fail } from '../../envelope/envelope.js';
|
|
2
|
-
export const serialQueue = () => {
|
|
3
|
-
let chain = Promise.resolve();
|
|
4
|
-
return (fn) => {
|
|
5
|
-
const run = chain.then(fn, fn);
|
|
6
|
-
chain = run.catch(() => undefined);
|
|
7
|
-
return run;
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
// A change that waited ms for the ones before it (the boot too) is 503 and never runs.
|
|
11
|
-
export const waitAtMost = (queue, ms, fn) => {
|
|
12
|
-
let gaveUp = false;
|
|
13
|
-
let timer;
|
|
14
|
-
const late = new Promise((_resolve, reject) => {
|
|
15
|
-
timer = setTimeout(() => {
|
|
16
|
-
gaveUp = true;
|
|
17
|
-
reject(fail('UNAVAILABLE', `other changes ran for ${ms} ms: try again`, { retryAfter: 5 }));
|
|
18
|
-
}, ms);
|
|
19
|
-
});
|
|
20
|
-
const run = queue(async () => {
|
|
21
|
-
clearTimeout(timer);
|
|
22
|
-
if (gaveUp)
|
|
23
|
-
return undefined;
|
|
24
|
-
return fn();
|
|
25
|
-
});
|
|
26
|
-
return Promise.race([run, late]);
|
|
27
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { type Readable, type Writable } from 'node:stream';
|
|
2
|
-
export declare const PENDING_MAX: number;
|
|
3
|
-
export declare const redactedLines: (write: (text: string) => void, secrets: Record<string, string>) => (chunk?: string) => void;
|
|
4
|
-
export declare const pipeRedacted: (from: Readable | null, to: Writable, secrets: Record<string, string>) => void;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import {} from 'node:stream';
|
|
2
|
-
import { maskedForms, redact } from './start-checks.js';
|
|
3
|
-
// Past this, a line with no newline is written up to a tail that could hold a split secret.
|
|
4
|
-
export const PENDING_MAX = 64 * 1024;
|
|
5
|
-
// By line, so a value split over two chunks is masked too; a call with no chunk flushes the rest.
|
|
6
|
-
export const redactedLines = (write, secrets) => {
|
|
7
|
-
const keep = Math.max(0, (maskedForms(secrets)[0]?.length ?? 1) - 1);
|
|
8
|
-
let rest = '';
|
|
9
|
-
return (chunk) => {
|
|
10
|
-
const lines = (rest + (chunk ?? '\n')).split('\n');
|
|
11
|
-
rest = lines.pop() ?? '';
|
|
12
|
-
const cut = rest.length > PENDING_MAX ? rest.length - keep : 0;
|
|
13
|
-
const out = `${lines.join('\n')}${lines.length && chunk !== undefined ? '\n' : ''}`;
|
|
14
|
-
if (out || cut)
|
|
15
|
-
write(redact(out + rest.slice(0, cut), secrets));
|
|
16
|
-
rest = rest.slice(cut);
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
// A child stream to the daemon's own, masked; its rest is written at the end.
|
|
20
|
-
export const pipeRedacted = (from, to, secrets) => {
|
|
21
|
-
const out = redactedLines((text) => to.write(text), secrets);
|
|
22
|
-
from?.on('data', (chunk) => out(chunk.toString()));
|
|
23
|
-
from?.once('end', () => out());
|
|
24
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type Log } from '../../log/log.js';
|
|
2
|
-
import { type Entry } from './supervisor.js';
|
|
3
|
-
export declare const markReload: (configDir: string, id: string, dir: string) => Promise<void>;
|
|
4
|
-
export declare const unmarkReload: (configDir: string, id: string) => Promise<void>;
|
|
5
|
-
export declare const swapIn: (dir: string) => Promise<boolean>;
|
|
6
|
-
export declare const swapBack: (dir: string) => Promise<void>;
|
|
7
|
-
export declare const confirmReload: (configDir: string, id: string, dir: string) => Promise<void>;
|
|
8
|
-
export declare const repairReload: (configDir: string, entry: Entry, log: Log) => Promise<void>;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { cp, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import {} from '../../log/log.js';
|
|
5
|
-
import { runDirOf } from './daemon-files.js';
|
|
6
|
-
import {} from './supervisor.js';
|
|
7
|
-
// D14 (revised): the child runs dist/; dist.next/ is the watcher's tree and stays; dist.prev/ is the last good.
|
|
8
|
-
const DIST = 'dist';
|
|
9
|
-
const NEXT = 'dist.next';
|
|
10
|
-
const PREV = 'dist.prev';
|
|
11
|
-
const markerOf = (configDir, id) => join(runDirOf(configDir, id), 'reload.json');
|
|
12
|
-
const gone = (path) => rm(path, { recursive: true, force: true });
|
|
13
|
-
// Written once a stale dist.prev is gone: marker and dist.prev together mean a swap not confirmed.
|
|
14
|
-
export const markReload = async (configDir, id, dir) => {
|
|
15
|
-
await gone(join(dir, PREV));
|
|
16
|
-
const marker = markerOf(configDir, id);
|
|
17
|
-
await mkdir(join(marker, '..'), { recursive: true });
|
|
18
|
-
await writeFile(marker, JSON.stringify({ id, dir, complete: false, at: new Date().toISOString() }));
|
|
19
|
-
};
|
|
20
|
-
export const unmarkReload = (configDir, id) => gone(markerOf(configDir, id));
|
|
21
|
-
// dist to dist.prev, a copy of dist.next to dist; false when there is no new build (a plain restart).
|
|
22
|
-
export const swapIn = async (dir) => {
|
|
23
|
-
if (!existsSync(join(dir, NEXT)))
|
|
24
|
-
return false;
|
|
25
|
-
if (existsSync(join(dir, DIST)))
|
|
26
|
-
await rename(join(dir, DIST), join(dir, PREV));
|
|
27
|
-
await cp(join(dir, NEXT), join(dir, DIST), { recursive: true });
|
|
28
|
-
return true;
|
|
29
|
-
};
|
|
30
|
-
// The copied build goes, the last good one comes back; with no dist.prev, dist is never touched.
|
|
31
|
-
export const swapBack = async (dir) => {
|
|
32
|
-
if (!existsSync(join(dir, PREV)))
|
|
33
|
-
return;
|
|
34
|
-
await gone(join(dir, DIST));
|
|
35
|
-
await rename(join(dir, PREV), join(dir, DIST));
|
|
36
|
-
};
|
|
37
|
-
// The new build is healthy: the marker first, then the old build.
|
|
38
|
-
export const confirmReload = async (configDir, id, dir) => {
|
|
39
|
-
await unmarkReload(configDir, id);
|
|
40
|
-
await gone(join(dir, PREV));
|
|
41
|
-
};
|
|
42
|
-
const markedDir = async (configDir, id) => {
|
|
43
|
-
const text = await readFile(markerOf(configDir, id), 'utf8');
|
|
44
|
-
return JSON.parse(text).dir;
|
|
45
|
-
};
|
|
46
|
-
// At boot: a marker of this row's folder with a dist.prev is a cut reload, put back to the last good build.
|
|
47
|
-
export const repairReload = async (configDir, entry, log) => {
|
|
48
|
-
const dir = entry.install.source;
|
|
49
|
-
if (!dir || !existsSync(markerOf(configDir, entry.id)))
|
|
50
|
-
return;
|
|
51
|
-
const ours = (await markedDir(configDir, entry.id).catch(() => undefined)) === dir;
|
|
52
|
-
if (ours && existsSync(join(dir, PREV))) {
|
|
53
|
-
await swapBack(dir);
|
|
54
|
-
log.warn('a cut reload was put back to the last good build', { id: entry.id });
|
|
55
|
-
}
|
|
56
|
-
await unmarkReload(configDir, entry.id);
|
|
57
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type Entries } from './entries.js';
|
|
2
|
-
import { type SupervisorDeps } from './supervisor.js';
|
|
3
|
-
export declare const reloadExtension: (deps: SupervisorDeps, entries: Entries, id: string, body: unknown) => Promise<{
|
|
4
|
-
refused?: string | undefined;
|
|
5
|
-
reloaded: boolean;
|
|
6
|
-
pid?: number | undefined;
|
|
7
|
-
id: string;
|
|
8
|
-
version: string;
|
|
9
|
-
state: import("./supervisor.js").State;
|
|
10
|
-
}>;
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { fail } from '../../envelope/envelope.js';
|
|
3
|
-
import { parse } from '../../envelope/parse.js';
|
|
4
|
-
import { entryOf } from './entries.js';
|
|
5
|
-
import { confirmReload, markReload, swapBack, swapIn, unmarkReload } from './reload-marker.js';
|
|
6
|
-
import { halt, hold } from './gate.js';
|
|
7
|
-
import { startEntry } from './supervisor.js';
|
|
8
|
-
const ReloadSchema = z.strictObject({});
|
|
9
|
-
// Only a linked working copy has a build to reload; a disabled one stays stopped.
|
|
10
|
-
const folderOf = (entry) => {
|
|
11
|
-
const dir = entry.install.source;
|
|
12
|
-
if (!dir)
|
|
13
|
-
throw fail('VALIDATION_ERROR', `${entry.id} is not a folder: only a linked folder reloads`);
|
|
14
|
-
if (entry.install.enabled === false)
|
|
15
|
-
throw fail('CONFLICT', `${entry.id} is disabled: enable it first`);
|
|
16
|
-
return dir;
|
|
17
|
-
};
|
|
18
|
-
const answer = (entry, refused) => ({
|
|
19
|
-
id: entry.id,
|
|
20
|
-
version: entry.version,
|
|
21
|
-
state: entry.state,
|
|
22
|
-
...(entry.lane && { pid: entry.lane.child.pid }),
|
|
23
|
-
reloaded: refused === undefined,
|
|
24
|
-
...(refused !== undefined && { refused }),
|
|
25
|
-
});
|
|
26
|
-
// Not healthy: the copied build goes, the last good build starts again.
|
|
27
|
-
const restoreLast = async (deps, entry, dir, swapped) => {
|
|
28
|
-
if (swapped)
|
|
29
|
-
await swapBack(dir);
|
|
30
|
-
await unmarkReload(deps.configDir, entry.id);
|
|
31
|
-
if (swapped)
|
|
32
|
-
await startEntry(deps, entry);
|
|
33
|
-
};
|
|
34
|
-
// The new child runs: what is left to clean is best-effort, never a second start.
|
|
35
|
-
const settle = async (deps, entry, dir) => {
|
|
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
|
-
return answer(entry);
|
|
38
|
-
};
|
|
39
|
-
const tryNew = async (deps, entry, dir) => {
|
|
40
|
-
await markReload(deps.configDir, entry.id, dir);
|
|
41
|
-
const swapped = await swapIn(dir);
|
|
42
|
-
const err = await startEntry(deps, entry, true);
|
|
43
|
-
if (!err)
|
|
44
|
-
return settle(deps, entry, dir);
|
|
45
|
-
await restoreLast(deps, entry, dir, swapped);
|
|
46
|
-
return answer(entry, err.message.split('\n')[0]);
|
|
47
|
-
};
|
|
48
|
-
// A file step failed (EPERM, EBUSY): the last good build runs again, the answer says why.
|
|
49
|
-
const fileFailed = async (deps, entry, dir, err) => {
|
|
50
|
-
await swapBack(dir).catch((again) => deps.log.warn('a reload could not put the last good build back', { reason: again.message }));
|
|
51
|
-
await unmarkReload(deps.configDir, entry.id).catch(() => undefined);
|
|
52
|
-
await startEntry(deps, entry);
|
|
53
|
-
return answer(entry, err.message.split('\n')[0]);
|
|
54
|
-
};
|
|
55
|
-
// D14: stop and wait for the exit (one writer on the store), swap the builds, start, wait healthy.
|
|
56
|
-
const reloadIn = async (deps, entry, dir) => {
|
|
57
|
-
await halt(entry, deps.drainMs);
|
|
58
|
-
return tryNew(deps, entry, dir).catch((err) => fileFailed(deps, entry, dir, err));
|
|
59
|
-
};
|
|
60
|
-
// POST /host/extensions/{id}/reload, body {}: requests wait at the gate and go to the new child.
|
|
61
|
-
export const reloadExtension = async (deps, entries, id, body) => {
|
|
62
|
-
parse(ReloadSchema, body ?? {}, 'Invalid body');
|
|
63
|
-
const entry = entryOf(entries, id);
|
|
64
|
-
const dir = folderOf(entry);
|
|
65
|
-
const release = hold(entry);
|
|
66
|
-
try {
|
|
67
|
-
return await reloadIn(deps, entry, dir);
|
|
68
|
-
}
|
|
69
|
-
finally {
|
|
70
|
-
release();
|
|
71
|
-
}
|
|
72
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type Exit } from './child.js';
|
|
2
|
-
import { type Lane } from './lane.js';
|
|
3
|
-
import { type Entry, type SupervisorDeps } from './supervisor.js';
|
|
4
|
-
export interface Retry {
|
|
5
|
-
timer: NodeJS.Timeout;
|
|
6
|
-
}
|
|
7
|
-
type Again = () => Promise<void>;
|
|
8
|
-
export declare const restartsOf: (entry: Entry) => number;
|
|
9
|
-
export declare const cancelRestart: (entry: Entry) => void;
|
|
10
|
-
export declare const resetRestarts: (entry: Entry) => void;
|
|
11
|
-
export declare const healthyFor: (deps: SupervisorDeps, entry: Entry, lane: Lane) => void;
|
|
12
|
-
export declare const reasonOf: (how: Exit) => string;
|
|
13
|
-
export declare const crashed: (deps: SupervisorDeps, entry: Entry, why: string, again: Again) => void;
|
|
14
|
-
export {};
|