@agentage/extension 0.13.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/discovery/discovery.d.ts +1 -0
- package/dist/discovery/discovery.schema.d.ts +2 -0
- package/dist/discovery/discovery.schema.js +22 -10
- 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.d.ts +9 -0
- package/dist/host/daemon/add.js +84 -0
- 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 +12 -4
- package/dist/host/daemon/child.js +78 -44
- package/dist/host/daemon/daemon-files.d.ts +6 -5
- package/dist/host/daemon/daemon-files.js +20 -12
- package/dist/host/daemon/daemon-owner.d.ts +1 -0
- package/dist/host/daemon/daemon-owner.js +32 -0
- package/dist/host/daemon/daemon.d.ts +3 -3
- package/dist/host/daemon/daemon.js +72 -35
- package/dist/host/daemon/entries.d.ts +21 -0
- package/dist/host/daemon/entries.js +24 -0
- 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 +17 -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/installation.d.ts +17 -4
- package/dist/host/daemon/installation.js +52 -8
- package/dist/host/daemon/installer-call.d.ts +16 -2
- package/dist/host/daemon/installer-call.js +33 -7
- package/dist/host/daemon/json-file.d.ts +3 -2
- package/dist/host/daemon/json-file.js +21 -10
- package/dist/host/daemon/lane.d.ts +1 -3
- package/dist/host/daemon/lane.js +4 -9
- 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 +15 -0
- package/dist/host/daemon/limits.js +29 -0
- package/dist/host/daemon/ops.d.ts +2 -26
- package/dist/host/daemon/ops.js +46 -103
- package/dist/host/daemon/patch-values.d.ts +6 -0
- package/dist/host/daemon/patch-values.js +39 -0
- package/dist/host/daemon/pending.d.ts +15 -0
- package/dist/host/daemon/pending.js +18 -0
- package/dist/host/daemon/proxy.d.ts +3 -2
- package/dist/host/daemon/proxy.js +54 -24
- package/dist/host/daemon/queue.d.ts +1 -0
- package/dist/host/daemon/queue.js +19 -0
- 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 +3 -9
- package/dist/host/daemon/routes.js +54 -22
- 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/stopping.d.ts +10 -0
- package/dist/host/daemon/stopping.js +28 -0
- package/dist/host/daemon/supervisor.d.ts +19 -11
- package/dist/host/daemon/supervisor.js +39 -47
- package/dist/host/daemon/write-all.d.ts +8 -0
- package/dist/host/daemon/write-all.js +45 -0
- package/dist/host/data-lock.d.ts +3 -1
- package/dist/host/data-lock.js +88 -29
- package/dist/host/host-env.d.ts +3 -1
- package/dist/host/host-env.js +10 -2
- package/dist/host/host-extension.d.ts +8 -0
- package/dist/host/host-extension.js +30 -45
- package/dist/host/host-parts.d.ts +25 -0
- package/dist/host/host-parts.js +75 -0
- package/dist/host/host-store.d.ts +11 -1
- package/dist/host/host-store.js +45 -12
- package/dist/host/index.d.ts +4 -0
- package/dist/host/index.js +4 -0
- 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.d.ts +2 -1
- package/dist/host/serve-host.js +16 -10
- 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 +5 -1
- package/dist/server/app.js +17 -17
- package/dist/server/serve.d.ts +3 -0
- package/dist/server/serve.js +9 -4
- package/dist/tool/build.d.ts +3 -0
- package/dist/tool/build.js +42 -27
- package/dist/tool/capabilities.d.ts +1 -0
- package/dist/tool/capabilities.js +8 -0
- package/dist/tool/check.d.ts +2 -0
- package/dist/tool/check.js +23 -8
- package/dist/tool/container-parts.d.ts +7 -0
- package/dist/tool/container-parts.js +10 -0
- package/dist/tool/container-recipe.d.ts +4 -0
- package/dist/tool/container-recipe.js +55 -0
- package/dist/tool/container-stage.d.ts +3 -0
- package/dist/tool/container-stage.js +51 -0
- package/dist/tool/container.d.ts +73 -0
- package/dist/tool/container.js +28 -0
- package/dist/tool/docker.d.ts +5 -0
- package/dist/tool/docker.js +15 -0
- package/dist/tool/format.js +23 -9
- package/dist/tool/index.d.ts +2 -0
- package/dist/tool/index.js +2 -0
- package/dist/tool/kit-version.d.ts +3 -0
- package/dist/tool/kit-version.js +28 -2
- package/dist/tool/labels.d.ts +1 -1
- package/dist/tool/labels.js +4 -2
- package/dist/tool/main.js +10 -1
- package/dist/tool/stage.d.ts +1 -0
- package/dist/tool/stage.js +1 -1
- package/dist/tool/up/local-record.d.ts +1 -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/extension-discovery.schema.json +8 -1
- package/schemas/extension-record.schema.json +8 -1
- package/schemas/health.schema.json +17 -0
- package/dist/host/daemon/secret-names.d.ts +0 -1
- package/dist/host/daemon/secret-names.js +0 -14
- package/dist/host/daemon/setting-values.d.ts +0 -1
- package/dist/host/daemon/setting-values.js +0 -26
package/README.md
CHANGED
|
@@ -12,7 +12,9 @@ npm install @agentage/extension express zod
|
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
`agentage extension check [dir] --json` prints the conformance report (exit 1 when `conforms` is
|
|
15
|
-
false); `file` and `line` are there only when a problem has them
|
|
15
|
+
false); `file` and `line` are there only when a problem has them. `warnings` is there only when a
|
|
16
|
+
rule warned: an `engines.agentage` of an older kit minor from the floor `0.12` up conforms with a
|
|
17
|
+
warning; below the floor or above the running kit it is a problem:
|
|
16
18
|
|
|
17
19
|
```json
|
|
18
20
|
{
|
|
@@ -47,6 +47,7 @@ export declare const discoveryDocument: (record: ExtensionRecord, host: string)
|
|
|
47
47
|
};
|
|
48
48
|
secrets?: string[] | undefined;
|
|
49
49
|
secretParts?: Record<string, ("pages" | "api" | "mcp" | "worker")[]> | undefined;
|
|
50
|
+
image?: string | undefined;
|
|
50
51
|
openapi?: string | undefined;
|
|
51
52
|
tools?: {
|
|
52
53
|
name: string;
|
|
@@ -43,6 +43,7 @@ export declare const ExtensionRecordSchema: z.ZodObject<{
|
|
|
43
43
|
migrations: z.ZodOptional<z.ZodInt>;
|
|
44
44
|
}, z.core.$strict>>;
|
|
45
45
|
}, z.core.$strict>;
|
|
46
|
+
image: z.ZodOptional<z.ZodString>;
|
|
46
47
|
openapi: z.ZodOptional<z.ZodString>;
|
|
47
48
|
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
48
49
|
name: z.ZodString;
|
|
@@ -87,6 +88,7 @@ export declare const DiscoveryDocumentSchema: z.ZodObject<{
|
|
|
87
88
|
migrations: z.ZodOptional<z.ZodInt>;
|
|
88
89
|
}, z.core.$strict>>;
|
|
89
90
|
}, z.core.$strict>;
|
|
91
|
+
image: z.ZodOptional<z.ZodString>;
|
|
90
92
|
openapi: z.ZodOptional<z.ZodString>;
|
|
91
93
|
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
92
94
|
name: z.ZodString;
|
|
@@ -6,8 +6,12 @@ const ImageDigestRefSchema = z
|
|
|
6
6
|
.max(256)
|
|
7
7
|
.regex(/^[a-z0-9][a-z0-9.-]*(:[0-9]{1,5})?\/[a-z0-9][a-z0-9-]*\/[a-z][a-z0-9-]*-(pages|ui|api|mcp|worker)@sha256:[0-9a-f]{64}$/);
|
|
8
8
|
const PartImageSchema = z.strictObject({ image: ImageDigestRefSchema });
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// The one image: registry host[:port]/owner[/team...]/{id}@sha256.
|
|
10
|
+
const OneImageRefSchema = z
|
|
11
|
+
.string()
|
|
12
|
+
.max(256)
|
|
13
|
+
.regex(/^[a-z0-9][a-z0-9.-]*(:[0-9]{1,5})?(\/[a-z0-9][a-z0-9_.-]*)+\/[a-z][a-z0-9-]*@sha256:[0-9a-f]{64}$/);
|
|
14
|
+
const PartsRecordSchema = z.strictObject({
|
|
11
15
|
pages: PartImageSchema.optional(),
|
|
12
16
|
ui: PartImageSchema.optional(),
|
|
13
17
|
api: PartImageSchema.optional(),
|
|
@@ -16,9 +20,6 @@ const PartsRecordSchema = z
|
|
|
16
20
|
store: z
|
|
17
21
|
.strictObject({ kind: StoreKindSchema, migrations: z.int().min(0).optional() })
|
|
18
22
|
.optional(),
|
|
19
|
-
})
|
|
20
|
-
.refine((parts) => IMAGE_PART_KINDS.some((kind) => parts[kind] !== undefined), {
|
|
21
|
-
message: 'at least one part with an image',
|
|
22
23
|
});
|
|
23
24
|
export const OriginSchema = z
|
|
24
25
|
.string()
|
|
@@ -39,20 +40,29 @@ const recordShape = {
|
|
|
39
40
|
commit: CommitSchema,
|
|
40
41
|
buildTime: z.iso.datetime(),
|
|
41
42
|
parts: PartsRecordSchema,
|
|
43
|
+
image: OneImageRefSchema.optional().describe('The one image (build --container). A record has part images or this image: never both, never none.'),
|
|
42
44
|
// /api/v{N}: an extension still on a kit before 0.13, read by a newer one; it goes in phase 4.
|
|
43
45
|
openapi: z
|
|
44
46
|
.string()
|
|
45
47
|
.regex(/^\/api\/(?:v[0-9]+|[a-z][a-z0-9-]*)\/openapi\.json$/)
|
|
48
|
+
.describe('/api/{id}/openapi.json, or the old /api/v{N}; only the kit checks that {id} is the id')
|
|
46
49
|
.optional(),
|
|
47
50
|
tools: z.array(ToolRecordSchema).min(1).optional(),
|
|
48
51
|
widgets: z.array(WidgetSchema).min(1).optional(),
|
|
49
52
|
commands: z.array(CommandNameSchema).min(1).optional(),
|
|
50
53
|
};
|
|
51
|
-
const imagesNameTheirExtension = (doc) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
});
|
|
55
|
-
|
|
54
|
+
const imagesNameTheirExtension = (doc) => (!doc.image || doc.image.includes(`/${doc.id}@sha256:`)) &&
|
|
55
|
+
IMAGE_PART_KINDS.every((kind) => {
|
|
56
|
+
const image = doc.parts[kind]?.image;
|
|
57
|
+
return image === undefined || image.includes(`/${doc.id}-${kind}@sha256:`);
|
|
58
|
+
});
|
|
59
|
+
const IMAGE_RULE = { message: 'every image is {id}-{part}, the one image {id}', path: ['parts'] };
|
|
60
|
+
// One image per part, or the one image of every part (build --container): never both, never none.
|
|
61
|
+
const oneImageForm = (doc) => IMAGE_PART_KINDS.some((kind) => doc.parts[kind] !== undefined) !== Boolean(doc.image);
|
|
62
|
+
const FORM_RULE = {
|
|
63
|
+
message: 'at least one part with an image, or the one image and no part image',
|
|
64
|
+
path: ['parts'],
|
|
65
|
+
};
|
|
56
66
|
// The api prefix is /api/{id}, or the old /api/v{N}: never another extension's id.
|
|
57
67
|
const openapiNamesItsExtension = ({ id, openapi }) => !openapi || /^\/api\/v[0-9]+\//.test(openapi) || openapi === `/api/${id}/openapi.json`;
|
|
58
68
|
const OPENAPI_RULE = { message: 'openapi is /api/{id}/openapi.json', path: ['openapi'] };
|
|
@@ -66,6 +76,7 @@ export const ExtensionRecordSchema = z
|
|
|
66
76
|
secrets: z.array(SecretNameSchema).min(1).optional(),
|
|
67
77
|
secretParts: z.record(SecretNameSchema, z.array(SecretPartSchema)).optional(),
|
|
68
78
|
})
|
|
79
|
+
.refine(oneImageForm, FORM_RULE)
|
|
69
80
|
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
70
81
|
.refine(openapiNamesItsExtension, OPENAPI_RULE)
|
|
71
82
|
.superRefine((doc, ctx) => {
|
|
@@ -76,5 +87,6 @@ export const ExtensionRecordSchema = z
|
|
|
76
87
|
// What a deployment serves at /.well-known/agentage-extension.json: the record plus its host.
|
|
77
88
|
export const DiscoveryDocumentSchema = z
|
|
78
89
|
.strictObject({ ...recordShape, host: OriginSchema })
|
|
90
|
+
.refine(oneImageForm, FORM_RULE)
|
|
79
91
|
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
80
92
|
.refine(openapiNamesItsExtension, OPENAPI_RULE);
|
package/dist/health/health.d.ts
CHANGED
|
@@ -12,7 +12,9 @@ export interface HealthOptions {
|
|
|
12
12
|
checks?: Record<string, HealthCheck>;
|
|
13
13
|
instance?: string;
|
|
14
14
|
startedAt?: number;
|
|
15
|
+
parts?: ImagePartKind[];
|
|
15
16
|
now?: () => Date;
|
|
17
|
+
clockRate?: number;
|
|
16
18
|
log?: Log;
|
|
17
19
|
}
|
|
18
20
|
export declare const worstOf: (statuses: HealthStatus[]) => HealthStatus;
|
package/dist/health/health.js
CHANGED
|
@@ -36,9 +36,11 @@ export const healthBody = async (options) => {
|
|
|
36
36
|
const now = (options.now ?? (() => new Date()))();
|
|
37
37
|
const checks = await runChecks(options.checks ?? {});
|
|
38
38
|
const status = worstOf(Object.values(checks));
|
|
39
|
-
const { extension, part, version, build, instance } = options;
|
|
39
|
+
const { extension, part, parts, version, build, instance } = options;
|
|
40
40
|
const uptimeSeconds = Math.max(0, Math.floor((now.getTime() - options.startedAt) / 1000));
|
|
41
|
-
const
|
|
41
|
+
const clock = options.clockRate && options.clockRate !== 1 ? { clockRate: options.clockRate } : {};
|
|
42
|
+
const listed = parts ? { parts } : {};
|
|
43
|
+
const data = { status, extension, part, ...listed, version, ...build, instance, ...clock };
|
|
42
44
|
return {
|
|
43
45
|
success: status !== 'unavailable',
|
|
44
46
|
data: { ...data, uptimeSeconds, checkedAt: isoSeconds(now), checks },
|
|
@@ -21,9 +21,18 @@ export declare const HealthSchema: z.ZodObject<{
|
|
|
21
21
|
worker: "worker";
|
|
22
22
|
store: "store";
|
|
23
23
|
}>;
|
|
24
|
+
parts: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
25
|
+
pages: "pages";
|
|
26
|
+
ui: "ui";
|
|
27
|
+
api: "api";
|
|
28
|
+
mcp: "mcp";
|
|
29
|
+
worker: "worker";
|
|
30
|
+
store: "store";
|
|
31
|
+
}>>>;
|
|
24
32
|
version: z.ZodString;
|
|
25
33
|
commit: z.ZodString;
|
|
26
34
|
buildTime: z.ZodISODateTime;
|
|
35
|
+
clockRate: z.ZodOptional<z.ZodNumber>;
|
|
27
36
|
instance: z.ZodString;
|
|
28
37
|
uptimeSeconds: z.ZodInt;
|
|
29
38
|
checkedAt: z.ZodISODateTime;
|
|
@@ -9,9 +9,12 @@ export const HealthSchema = z.strictObject({
|
|
|
9
9
|
status: HealthStatusSchema,
|
|
10
10
|
extension: ExtensionIdSchema,
|
|
11
11
|
part: DeployedPartKindSchema,
|
|
12
|
+
// A host process that runs several parts (the daemon's child, the one container) lists them.
|
|
13
|
+
parts: z.array(DeployedPartKindSchema).optional(),
|
|
12
14
|
version: VersionSchema,
|
|
13
15
|
commit: z.string().regex(/^[0-9a-f]{7,40}$/),
|
|
14
16
|
buildTime: z.iso.datetime(),
|
|
17
|
+
clockRate: z.number().optional(),
|
|
15
18
|
instance: z.string().regex(/^[0-9a-f]{8,32}$/),
|
|
16
19
|
uptimeSeconds: z.int().min(0),
|
|
17
20
|
checkedAt: z.iso.datetime(),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const startHostClock: (env: Record<string, string | undefined>) => number;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createLog } from '../log/log.js';
|
|
2
|
+
import { CLOCK_RATE_ENV, parseClockRate, setHostClockRate } from '../worker/clock.js';
|
|
3
|
+
// Only the kit host child reads the test clock from env; a bad value stops the boot.
|
|
4
|
+
export const startHostClock = (env) => {
|
|
5
|
+
const rate = parseClockRate(env[CLOCK_RATE_ENV]);
|
|
6
|
+
setHostClockRate(rate);
|
|
7
|
+
if (rate !== 1) {
|
|
8
|
+
const log = createLog({ extension: 'host', part: 'api' });
|
|
9
|
+
log.warn('test clock on: job times run faster, for tests only', { clockRate: rate });
|
|
10
|
+
}
|
|
11
|
+
return rate;
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Entries } from './entries.js';
|
|
2
|
+
import { type SupervisorDeps } from './supervisor.js';
|
|
3
|
+
export declare const addExtension: (deps: SupervisorDeps, entries: Entries, body: unknown) => Promise<{
|
|
4
|
+
error?: string | undefined;
|
|
5
|
+
pid?: number | undefined;
|
|
6
|
+
id: string;
|
|
7
|
+
version: string;
|
|
8
|
+
state: import("./supervisor.js").State;
|
|
9
|
+
}>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { fail } from '../../envelope/envelope.js';
|
|
6
|
+
import { parse } from '../../envelope/parse.js';
|
|
7
|
+
import { ExtensionIdSchema } from '../../manifest/primitives.schema.js';
|
|
8
|
+
import { readManifest } from '../../manifest/read-manifest.js';
|
|
9
|
+
import { persist, summary } from './entries.js';
|
|
10
|
+
import { runInstaller } from './installer-call.js';
|
|
11
|
+
import {} from './installation.js';
|
|
12
|
+
import { clearRunDir, dataDirOf } from './daemon-files.js';
|
|
13
|
+
import { needsSecret } from './start-checks.js';
|
|
14
|
+
import { startEntry, startFailed } from './supervisor.js';
|
|
15
|
+
const RESERVED_ID = 'host';
|
|
16
|
+
// enabled: false records the extension and never starts it (a profile "off" entry).
|
|
17
|
+
const AddSchema = z.union([
|
|
18
|
+
z.strictObject({
|
|
19
|
+
id: ExtensionIdSchema,
|
|
20
|
+
version: z.string().min(1).optional(),
|
|
21
|
+
enabled: z.boolean().optional(),
|
|
22
|
+
}),
|
|
23
|
+
z.strictObject({
|
|
24
|
+
source: z.string().refine(isAbsolute, 'an absolute path'),
|
|
25
|
+
enabled: z.boolean().optional(),
|
|
26
|
+
}),
|
|
27
|
+
]);
|
|
28
|
+
// The entry a 409 names, so a client can tell a repeat from another package of that id.
|
|
29
|
+
const existing = ({ id, version, install }) => ({
|
|
30
|
+
id,
|
|
31
|
+
...(version ? { version } : {}),
|
|
32
|
+
...(install.source ? { source: install.source } : {}),
|
|
33
|
+
});
|
|
34
|
+
const reserve = (entries, id) => {
|
|
35
|
+
if (id === RESERVED_ID)
|
|
36
|
+
throw fail('VALIDATION_ERROR', 'the id host is reserved');
|
|
37
|
+
const has = entries.get(id);
|
|
38
|
+
if (has)
|
|
39
|
+
throw fail('CONFLICT', `${id} is installed`, { details: existing(has) });
|
|
40
|
+
};
|
|
41
|
+
const sourceEntry = async (source) => {
|
|
42
|
+
const pkg = await readManifest(source).catch((err) => {
|
|
43
|
+
throw fail('VALIDATION_ERROR', `${source}: ${err.message}`);
|
|
44
|
+
});
|
|
45
|
+
return { install: { id: pkg.agentage.id, source }, dir: source, version: pkg.version };
|
|
46
|
+
};
|
|
47
|
+
const marketEntry = async (deps, entries, id, version) => {
|
|
48
|
+
reserve(entries, id);
|
|
49
|
+
const got = await runInstaller(deps, { id, version });
|
|
50
|
+
return { install: { id: got.id, version: got.version }, dir: got.dir, version: got.version };
|
|
51
|
+
};
|
|
52
|
+
// C6: a new id that did not start leaves no row; only a data folder this start made goes too.
|
|
53
|
+
const rollBack = async (deps, entries, entry, fresh) => {
|
|
54
|
+
entries.delete(entry.id);
|
|
55
|
+
await persist(deps, entry.id);
|
|
56
|
+
await clearRunDir(deps.configDir, entry.id);
|
|
57
|
+
if (fresh)
|
|
58
|
+
await rm(dataDirOf(deps.configDir, entry.id), { recursive: true, force: true });
|
|
59
|
+
};
|
|
60
|
+
// The first start; refused and rolled back unless it waits for a secret or the daemon stops (kept).
|
|
61
|
+
const firstStart = async (deps, entries, entry) => {
|
|
62
|
+
const fresh = !existsSync(dataDirOf(deps.configDir, entry.id));
|
|
63
|
+
const err = await startEntry(deps, entry);
|
|
64
|
+
if (!err || needsSecret(err) || deps.stopping.signal.aborted)
|
|
65
|
+
return;
|
|
66
|
+
await rollBack(deps, entries, entry, fresh);
|
|
67
|
+
throw startFailed(entry, err);
|
|
68
|
+
};
|
|
69
|
+
// POST: installs (or links a folder), records it, then starts it (C6: a failed start is refused).
|
|
70
|
+
export const addExtension = async (deps, entries, body) => {
|
|
71
|
+
const spec = parse(AddSchema, body, 'Invalid body');
|
|
72
|
+
const got = 'source' in spec
|
|
73
|
+
? await sourceEntry(spec.source)
|
|
74
|
+
: await marketEntry(deps, entries, spec.id, spec.version);
|
|
75
|
+
reserve(entries, got.install.id);
|
|
76
|
+
const off = spec.enabled === false;
|
|
77
|
+
const install = off ? { ...got.install, enabled: false } : got.install;
|
|
78
|
+
await persist(deps, install.id, install);
|
|
79
|
+
const entry = { ...got, id: install.id, install, state: 'disabled' };
|
|
80
|
+
entries.set(entry.id, entry);
|
|
81
|
+
if (!off)
|
|
82
|
+
await firstStart(deps, entries, entry);
|
|
83
|
+
return summary(entry);
|
|
84
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface RunSpec {
|
|
2
|
+
id: string;
|
|
3
|
+
dir: string;
|
|
4
|
+
dataDir: string;
|
|
5
|
+
secrets: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export interface RunFolders {
|
|
8
|
+
runDir: string;
|
|
9
|
+
secretsDir: string;
|
|
10
|
+
tmpDir: string;
|
|
11
|
+
made: {
|
|
12
|
+
run: boolean;
|
|
13
|
+
data: boolean;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare const runDirFor: (spec: Pick<RunSpec, "id" | "dataDir">) => string;
|
|
17
|
+
export declare const makeRun: (spec: RunSpec) => Promise<RunFolders>;
|
|
18
|
+
export declare const fillRun: (run: RunFolders, spec: RunSpec) => Promise<void>;
|
|
19
|
+
export declare const undoRun: ({ runDir, made }: RunFolders, dataDir: string) => Promise<void>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { mkdir, rm, rmdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { secretNames } from '../../manifest/parts.js';
|
|
4
|
+
import { SecretNameSchema } from '../../manifest/primitives.schema.js';
|
|
5
|
+
import { readManifest } from '../../manifest/read-manifest.js';
|
|
6
|
+
import { clearOldLock } from '../data-lock.js';
|
|
7
|
+
import { runDirOf } from './daemon-files.js';
|
|
8
|
+
import { clearRunLock } from './run-lock.js';
|
|
9
|
+
// The supervisor's dataDir is <configDir>/data/{id}, so run/{id} sits beside data/ (C2).
|
|
10
|
+
export const runDirFor = (spec) => runDirOf(dirname(dirname(spec.dataDir)), spec.id);
|
|
11
|
+
// Only the names the package declares now: a row can still hold one an older version declared.
|
|
12
|
+
const declared = async (spec) => {
|
|
13
|
+
const names = new Set(secretNames(await readManifest(spec.dir)));
|
|
14
|
+
return Object.entries(spec.secrets).filter(([name]) => names.has(name));
|
|
15
|
+
};
|
|
16
|
+
// The folders first, so undoRun knows what this launch made; data/{id} before the fork (PGlite).
|
|
17
|
+
export const makeRun = async (spec) => {
|
|
18
|
+
const runDir = runDirFor(spec);
|
|
19
|
+
const run = await mkdir(runDir, { recursive: true, mode: 0o700 });
|
|
20
|
+
const data = await mkdir(spec.dataDir, { recursive: true }).catch(async (err) => {
|
|
21
|
+
if (run !== undefined)
|
|
22
|
+
await rm(runDir, { recursive: true, force: true });
|
|
23
|
+
throw err;
|
|
24
|
+
});
|
|
25
|
+
const made = { run: run !== undefined, data: data !== undefined };
|
|
26
|
+
return { runDir, secretsDir: join(runDir, 'secrets'), tmpDir: join(runDir, 'tmp'), made };
|
|
27
|
+
};
|
|
28
|
+
// Fresh secrets and tmp; the old lock beside data/{id} and a left-over run lock are cleared.
|
|
29
|
+
export const fillRun = async (run, spec) => {
|
|
30
|
+
for (const dir of [run.secretsDir, run.tmpDir]) {
|
|
31
|
+
await rm(dir, { recursive: true, force: true });
|
|
32
|
+
await mkdir(dir, { mode: 0o700 });
|
|
33
|
+
}
|
|
34
|
+
await clearOldLock(`${spec.dataDir}.lock`);
|
|
35
|
+
await clearRunLock(join(run.runDir, 'store.lock'), run.runDir);
|
|
36
|
+
for (const [name, value] of await declared(spec))
|
|
37
|
+
await writeFile(join(run.secretsDir, SecretNameSchema.parse(name)), value, { mode: 0o600 });
|
|
38
|
+
};
|
|
39
|
+
// A child that never reported its port: what this launch made goes; data/{id} only while empty.
|
|
40
|
+
export const undoRun = async ({ runDir, made }, dataDir) => {
|
|
41
|
+
if (made.run)
|
|
42
|
+
await rm(runDir, { recursive: true, force: true });
|
|
43
|
+
if (made.data)
|
|
44
|
+
await rmdir(dataDir).catch(() => undefined);
|
|
45
|
+
};
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { type Seal } from './seal.js';
|
|
2
|
+
interface LaunchSpec {
|
|
2
3
|
id: string;
|
|
3
4
|
dir: string;
|
|
4
5
|
dataDir: string;
|
|
5
6
|
settings: Record<string, unknown>;
|
|
6
7
|
secrets: Record<string, string>;
|
|
7
8
|
origin: string;
|
|
9
|
+
storeOpenMs?: number;
|
|
8
10
|
}
|
|
9
11
|
export interface Exit {
|
|
10
12
|
code: number | null;
|
|
11
13
|
signal: string | null;
|
|
14
|
+
words?: string;
|
|
12
15
|
}
|
|
13
16
|
export interface Child {
|
|
14
17
|
port: number;
|
|
@@ -16,6 +19,11 @@ export interface Child {
|
|
|
16
19
|
exited: Promise<Exit>;
|
|
17
20
|
stop(): Promise<void>;
|
|
18
21
|
}
|
|
19
|
-
export type Launch = (spec: LaunchSpec) => Promise<Child
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
export type Launch = ((spec: LaunchSpec) => Promise<Child>) & {
|
|
23
|
+
clockRate?: number;
|
|
24
|
+
seal?: Seal;
|
|
25
|
+
node?: string;
|
|
26
|
+
};
|
|
27
|
+
export declare const childEnv: (spec: LaunchSpec, secretsDir: string, runDir?: string) => Record<string, string>;
|
|
28
|
+
export { lastWords } from './last-words.js';
|
|
29
|
+
export declare const forkLaunch: (main?: string, startMs?: number, rate?: number, node?: string) => Launch;
|
|
@@ -1,44 +1,28 @@
|
|
|
1
1
|
import { fork } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { join } from 'node:path';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
5
4
|
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import { CLOCK_RATE_ENV, parseClockRate } from '../../worker/clock.js';
|
|
6
|
+
import { fillRun, makeRun, undoRun } from './child-files.js';
|
|
7
|
+
import { lastWords } from './last-words.js';
|
|
8
|
+
import { LIMITS } from './limits.js';
|
|
9
|
+
import { permissionFlag, sealFlags, sealOf } from './seal.js';
|
|
10
|
+
const HOST_MAIN = fileURLToPath(new URL('../main.js', import.meta.url));
|
|
9
11
|
const STOP_MS = 5_000;
|
|
10
12
|
const PASSED = ['PATH', 'HOME', 'TMPDIR', 'LANG', 'NODE_EXTRA_CA_CERTS'];
|
|
11
|
-
//
|
|
12
|
-
const
|
|
13
|
-
const dir = await mkdtemp(join(tmpdir(), `agentage-${spec.id}-`));
|
|
14
|
-
for (const [name, value] of Object.entries(spec.secrets))
|
|
15
|
-
await writeFile(join(dir, SecretNameSchema.parse(name)), value, { mode: 0o600 });
|
|
16
|
-
return dir;
|
|
17
|
-
};
|
|
18
|
-
const envOf = (spec, secretsDir) => ({
|
|
19
|
-
...Object.fromEntries(PASSED.flatMap((k) => (process.env[k] ? [[k, process.env[k]]] : []))),
|
|
13
|
+
// What dist/host/main.js reads (docs/part-image.md), on a port the OS picks.
|
|
14
|
+
export const childEnv = (spec, secretsDir, runDir) => ({
|
|
20
15
|
PORT: '0',
|
|
21
16
|
AGENTAGE_EXTENSION_DIR: spec.dir,
|
|
22
17
|
AGENTAGE_DATA_DIR: spec.dataDir,
|
|
23
18
|
AGENTAGE_SETTINGS: JSON.stringify(spec.settings),
|
|
24
19
|
AGENTAGE_SECRETS_DIR: secretsDir,
|
|
25
20
|
AGENTAGE_ORIGIN: spec.origin,
|
|
21
|
+
...(runDir && { AGENTAGE_RUN_DIR: runDir }),
|
|
22
|
+
...(spec.storeOpenMs && { AGENTAGE_STORE_OPEN_MS: String(spec.storeOpenMs) }),
|
|
26
23
|
});
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
const lines = tail
|
|
30
|
-
.trim()
|
|
31
|
-
.split('\n')
|
|
32
|
-
.filter((l) => !/^Node\.js v\d/.test(l));
|
|
33
|
-
const line = lines.at(-1) ?? '';
|
|
34
|
-
try {
|
|
35
|
-
const msg = String(JSON.parse(line).msg ?? line);
|
|
36
|
-
return msg.replace(/^[A-Za-z]*Error: /, '');
|
|
37
|
-
}
|
|
38
|
-
catch {
|
|
39
|
-
return line || `exited with code ${code}`;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
24
|
+
export { lastWords } from './last-words.js';
|
|
25
|
+
const passedEnv = () => Object.fromEntries(PASSED.flatMap((k) => (process.env[k] ? [[k, process.env[k]]] : [])));
|
|
42
26
|
const captured = (child) => {
|
|
43
27
|
let tail = '';
|
|
44
28
|
child.stdout?.pipe(process.stdout);
|
|
@@ -48,13 +32,25 @@ const captured = (child) => {
|
|
|
48
32
|
});
|
|
49
33
|
return () => tail;
|
|
50
34
|
};
|
|
35
|
+
// 'exit' can come before the last stderr chunk: wait for the stream end, at most 500 ms.
|
|
36
|
+
const flushed = (child) => new Promise((resolve) => {
|
|
37
|
+
if (!child.stderr || child.stderr.readableEnded)
|
|
38
|
+
return resolve();
|
|
39
|
+
child.stderr.once('end', resolve);
|
|
40
|
+
setTimeout(resolve, 500).unref();
|
|
41
|
+
});
|
|
42
|
+
const exitOf = (child, tail) => new Promise((resolve) => child.once('exit', (code, signal) => {
|
|
43
|
+
const how = { code, signal };
|
|
44
|
+
void flushed(child).then(() => resolve({ ...how, words: lastWords(tail(), how) }));
|
|
45
|
+
}));
|
|
51
46
|
const portOf = (msg) => {
|
|
52
47
|
const { type, port } = (msg ?? {});
|
|
53
48
|
return type === 'ready' && typeof port === 'number' ? port : undefined;
|
|
54
49
|
};
|
|
55
|
-
// The port the child bound, or why it exited first; a child silent for
|
|
56
|
-
const readyOf = (child, tail) => new Promise((resolve, reject) => {
|
|
57
|
-
|
|
50
|
+
// The port the child bound, or why it exited first; a child silent for startMs is killed.
|
|
51
|
+
const readyOf = (child, tail, startMs) => new Promise((resolve, reject) => {
|
|
52
|
+
let killed = false;
|
|
53
|
+
const timer = setTimeout(() => ((killed = true), child.kill('SIGKILL')), startMs);
|
|
58
54
|
const settle = (fn) => {
|
|
59
55
|
clearTimeout(timer);
|
|
60
56
|
fn();
|
|
@@ -64,7 +60,12 @@ const readyOf = (child, tail) => new Promise((resolve, reject) => {
|
|
|
64
60
|
if (port)
|
|
65
61
|
settle(() => resolve(port));
|
|
66
62
|
});
|
|
67
|
-
child.once('exit', (code
|
|
63
|
+
child.once('exit', (code, signal) => {
|
|
64
|
+
const why = killed
|
|
65
|
+
? `reported no port in ${startMs} ms: killed`
|
|
66
|
+
: lastWords(tail(), { code, signal });
|
|
67
|
+
settle(() => reject(new Error(why)));
|
|
68
|
+
});
|
|
68
69
|
});
|
|
69
70
|
// SIGTERM drains; a child still there after STOP_MS (a request that never ends) gets SIGKILL.
|
|
70
71
|
const stopOf = (child, exited) => async () => {
|
|
@@ -74,19 +75,52 @@ const stopOf = (child, exited) => async () => {
|
|
|
74
75
|
await exited;
|
|
75
76
|
clearTimeout(kill);
|
|
76
77
|
};
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
// The daemon's own node flags (a loader, --input-type) are never the child's; C2 where Node has --permission.
|
|
79
|
+
const execArgvOf = (node, main, spec, run) => {
|
|
80
|
+
if (!permissionFlag(node))
|
|
81
|
+
return [];
|
|
82
|
+
const configDir = dirname(dirname(spec.dataDir));
|
|
83
|
+
const { id, dir, dataDir } = spec;
|
|
84
|
+
return sealFlags({ id, main, dir, configDir, runDir: run.runDir, dataDir });
|
|
85
|
+
};
|
|
86
|
+
// The listed env of the daemon, then the child's own: TMPDIR in run/{id}, the test clock when not 1.
|
|
87
|
+
const envOf = (spec, run, clockRate) => ({
|
|
88
|
+
...passedEnv(),
|
|
89
|
+
...childEnv(spec, run.secretsDir, run.runDir),
|
|
90
|
+
TMPDIR: run.tmpDir,
|
|
91
|
+
...(clockRate === 1 ? {} : { [CLOCK_RATE_ENV]: String(clockRate) }),
|
|
92
|
+
});
|
|
93
|
+
const started = async (at, spec, run) => {
|
|
94
|
+
const execArgv = execArgvOf(at.node, at.main, spec, run);
|
|
95
|
+
await fillRun(run, spec);
|
|
80
96
|
const stdio = ['ignore', 'pipe', 'pipe', 'ipc'];
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
97
|
+
const child = fork(at.main, [], {
|
|
98
|
+
env: envOf(spec, run, at.clockRate),
|
|
99
|
+
execArgv,
|
|
100
|
+
stdio: [...stdio],
|
|
101
|
+
});
|
|
102
|
+
const tail = captured(child);
|
|
103
|
+
const exited = exitOf(child, tail);
|
|
104
|
+
const port = await readyOf(child, tail, at.startMs);
|
|
105
|
+
return { port, pid: child.pid ?? 0, exited, stop: stopOf(child, exited) };
|
|
106
|
+
};
|
|
107
|
+
// Anything before the port (files, the seal check, the start) undoes the folders this launch made.
|
|
108
|
+
const launchAt = (at) => async (spec) => {
|
|
109
|
+
const run = await makeRun(spec);
|
|
85
110
|
try {
|
|
86
|
-
|
|
87
|
-
|
|
111
|
+
return await started(at, spec, run);
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
await undoRun(run, spec.dataDir);
|
|
115
|
+
throw err;
|
|
88
116
|
}
|
|
89
117
|
finally {
|
|
90
|
-
await rm(secretsDir, { recursive: true, force: true });
|
|
118
|
+
await rm(run.secretsDir, { recursive: true, force: true });
|
|
91
119
|
}
|
|
92
120
|
};
|
|
121
|
+
// One child process per extension on a port the OS picks; node: the version the seal gate reads.
|
|
122
|
+
export const forkLaunch = (main = HOST_MAIN, startMs = LIMITS.startMs, rate, node = process.versions.node) => {
|
|
123
|
+
const clockRate = parseClockRate(rate);
|
|
124
|
+
const launch = launchAt({ main, startMs, clockRate, node });
|
|
125
|
+
return Object.assign(launch, { clockRate, seal: sealOf(node), node });
|
|
126
|
+
};
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
export declare const DAEMON_FILE = "daemon.json";
|
|
2
1
|
export declare const DAEMON_PROTOCOL = 1;
|
|
3
|
-
|
|
2
|
+
interface DaemonFile {
|
|
4
3
|
pid: number;
|
|
5
4
|
port: number;
|
|
6
5
|
version: string;
|
|
7
6
|
protocol: number;
|
|
8
7
|
startedBy: string;
|
|
8
|
+
hostToken: string;
|
|
9
9
|
}
|
|
10
10
|
export declare const writeDaemonFile: (configDir: string, file: DaemonFile) => Promise<void>;
|
|
11
11
|
export declare const removeDaemonFile: (configDir: string) => Promise<void>;
|
|
12
12
|
export declare const dataDirOf: (configDir: string, id: string) => string;
|
|
13
|
+
export declare const runDirOf: (configDir: string, id: string) => string;
|
|
14
|
+
export declare const clearRunDir: (configDir: string, id: string) => Promise<void>;
|
|
13
15
|
export declare const purgeData: (configDir: string, id: string) => Promise<void>;
|
|
14
|
-
|
|
16
|
+
type ValueKind = 'settings' | 'secrets';
|
|
15
17
|
type Values = Record<string, unknown>;
|
|
16
18
|
export declare const readValues: (configDir: string, kind: ValueKind, id: string) => Promise<Values>;
|
|
17
|
-
export declare const writeValues: (configDir: string, kind: ValueKind, id: string, values?: Values) => Promise<void>;
|
|
18
|
-
export declare const ensureConfigDir: (configDir: string) => Promise<string | undefined>;
|
|
19
|
+
export declare const writeValues: (configDir: string, kind: ValueKind, id: string, values?: Values, waitMs?: number) => Promise<void>;
|
|
19
20
|
export {};
|
|
@@ -1,23 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rm, unlink } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { readJsonFile, withFileLock, writeJsonFile } from './json-file.js';
|
|
4
|
-
|
|
4
|
+
const DAEMON_FILE = 'daemon.json';
|
|
5
5
|
export const DAEMON_PROTOCOL = 1;
|
|
6
|
-
export const writeDaemonFile = (configDir, file) => writeJsonFile(join(configDir, DAEMON_FILE), file);
|
|
6
|
+
export const writeDaemonFile = (configDir, file) => writeJsonFile(join(configDir, DAEMON_FILE), file, 0o600);
|
|
7
7
|
export const removeDaemonFile = (configDir) => unlink(join(configDir, DAEMON_FILE)).catch(() => undefined);
|
|
8
8
|
export const dataDirOf = (configDir, id) => join(configDir, 'data', id);
|
|
9
|
+
// C2: the folder only this child reads and writes (secrets, TMPDIR, the store lock).
|
|
10
|
+
export const runDirOf = (configDir, id) => join(configDir, 'run', id);
|
|
11
|
+
// No user data in it: removed with the extension; each start makes its secrets and tmp anew.
|
|
12
|
+
export const clearRunDir = (configDir, id) => rm(runDirOf(configDir, id), { recursive: true, force: true });
|
|
9
13
|
export const purgeData = (configDir, id) => rm(dataDirOf(configDir, id), { recursive: true, force: true });
|
|
10
14
|
const fileOf = (configDir, kind) => join(configDir, `${kind}.json`);
|
|
11
15
|
export const readValues = async (configDir, kind, id) => (await readJsonFile(fileOf(configDir, kind), {}))[id] ?? {};
|
|
12
|
-
|
|
16
|
+
// The rows of one file changed under its lock; the same object back writes nothing.
|
|
17
|
+
const changeValues = (configDir, kind, change, waitMs) => {
|
|
13
18
|
const file = fileOf(configDir, kind);
|
|
14
|
-
|
|
19
|
+
const write = async () => {
|
|
15
20
|
const all = await readJsonFile(file, {});
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
21
|
+
const next = change(all);
|
|
22
|
+
if (next !== all)
|
|
23
|
+
await writeJsonFile(file, next, kind === 'secrets' ? 0o600 : 0o644);
|
|
24
|
+
};
|
|
25
|
+
return withFileLock(file, write, waitMs);
|
|
22
26
|
};
|
|
23
|
-
export const
|
|
27
|
+
export const writeValues = (configDir, kind, id, values, waitMs) => changeValues(configDir, kind, (all) => {
|
|
28
|
+
const rest = { ...all };
|
|
29
|
+
delete rest[id];
|
|
30
|
+
return values ? { ...rest, [id]: values } : rest;
|
|
31
|
+
}, waitMs);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const assertNoOtherDaemon: (configDir: string) => Promise<void>;
|