@agentage/extension 0.15.0 → 0.15.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/contracts/index.d.ts +1 -1
- package/dist/contracts/index.js +1 -1
- package/dist/discovery/discovery.d.ts +10 -25
- package/dist/discovery/discovery.js +7 -1
- package/dist/discovery/discovery.schema.d.ts +45 -0
- package/dist/discovery/discovery.schema.js +33 -4
- package/dist/discovery/served-extras.d.ts +8 -0
- package/dist/discovery/served-extras.js +10 -0
- package/dist/host/daemon/add.d.ts +2 -0
- package/dist/host/daemon/add.js +10 -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 +8 -11
- 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/redact-stream.d.ts +4 -0
- package/dist/host/daemon/redact-stream.js +24 -0
- 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-reach.js +1 -1
- 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 +1 -1
- package/dist/host/daemon/start-checks.js +6 -5
- package/dist/host/daemon/supervisor.d.ts +1 -4
- package/dist/host/daemon/supervisor.js +15 -17
- package/dist/host/host-extension.d.ts +3 -0
- package/dist/host/host-extension.js +12 -2
- package/dist/host/host-parts.d.ts +1 -1
- package/dist/host/host-parts.js +11 -4
- package/dist/host/serve-host.js +4 -3
- 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 +3 -1
- package/dist/runtime/part-mounts.js +7 -2
- package/dist/server/app.d.ts +2 -3
- package/dist/server/app.js +0 -1
- package/dist/tool/build.d.ts +1 -0
- package/dist/tool/build.js +3 -2
- package/dist/tool/check.d.ts +1 -1
- package/dist/tool/check.js +2 -1
- package/dist/tool/format.d.ts +1 -0
- package/dist/tool/format.js +3 -1
- package/dist/tool/record-extras.js +5 -12
- 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/schemas/extension-discovery.schema.json +61 -34
- package/schemas/extension-record.schema.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,5 @@
|
|
|
1
1
|
export { AGCID_HEADER, isAgcid } from '../agcid/header.js';
|
|
2
|
-
export { DiscoveryDocumentSchema, ExtensionRecordSchema, type DiscoveryDocument, type ExtensionRecord, } from '../discovery/discovery.schema.js';
|
|
2
|
+
export { CloudDiscoveryDocumentSchema, DiscoveryDocumentSchema, ExtensionRecordSchema, type DiscoveryDocument, type ExtensionRecord, } from '../discovery/discovery.schema.js';
|
|
3
3
|
export { ERROR_CODES, type ErrorCode } from '../envelope/codes.js';
|
|
4
4
|
export { ErrorEnvelopeSchema, OkEnvelopeSchema } from '../envelope/envelope.schema.js';
|
|
5
5
|
export { type ErrorEnvelope, type OkEnvelope } from '../envelope/envelope.js';
|
package/dist/contracts/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { AGCID_HEADER, isAgcid } from '../agcid/header.js';
|
|
2
|
-
export { DiscoveryDocumentSchema, ExtensionRecordSchema, } from '../discovery/discovery.schema.js';
|
|
2
|
+
export { CloudDiscoveryDocumentSchema, DiscoveryDocumentSchema, ExtensionRecordSchema, } from '../discovery/discovery.schema.js';
|
|
3
3
|
export { ERROR_CODES } from '../envelope/codes.js';
|
|
4
4
|
export { ErrorEnvelopeSchema, OkEnvelopeSchema } from '../envelope/envelope.schema.js';
|
|
5
5
|
export {} from '../envelope/envelope.js';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
2
3
|
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
3
|
-
import { type ExtensionRecord } from './discovery.schema.js';
|
|
4
|
+
import { type DiscoveryDocument, type ExtensionRecord } from './discovery.schema.js';
|
|
4
5
|
export interface BuildInfo {
|
|
5
6
|
commit: string;
|
|
6
7
|
buildTime: string;
|
|
@@ -21,34 +22,18 @@ export interface RecordExtras {
|
|
|
21
22
|
migrations?: number;
|
|
22
23
|
}
|
|
23
24
|
export declare const recordOf: (pkg: ExtensionPackage, build: BuildInfo, images: PartImages, extras?: RecordExtras) => ExtensionRecord;
|
|
25
|
+
export type ServedRecord = Omit<ExtensionRecord, 'parts'> & {
|
|
26
|
+
parts: DiscoveryDocument['parts'];
|
|
27
|
+
};
|
|
28
|
+
export declare const hostedRecordOf: (pkg: ExtensionPackage, build: BuildInfo, hosted: readonly HostPartKind[], extras: RecordExtras) => ServedRecord;
|
|
24
29
|
export declare const validateRecord: (value: unknown) => ExtensionRecord;
|
|
25
30
|
export declare const loadRecord: (file: string) => Promise<ExtensionRecord | null>;
|
|
26
|
-
export declare const discoveryDocument: (record:
|
|
27
|
-
|
|
28
|
-
pages?: {
|
|
29
|
-
image: string;
|
|
30
|
-
} | undefined;
|
|
31
|
-
ui?: {
|
|
32
|
-
image: string;
|
|
33
|
-
} | undefined;
|
|
34
|
-
api?: {
|
|
35
|
-
image: string;
|
|
36
|
-
} | undefined;
|
|
37
|
-
mcp?: {
|
|
38
|
-
image: string;
|
|
39
|
-
} | undefined;
|
|
40
|
-
worker?: {
|
|
41
|
-
image: string;
|
|
42
|
-
} | undefined;
|
|
43
|
-
store?: {
|
|
44
|
-
kind: "postgres" | "signoz" | "git" | "clickhouse";
|
|
45
|
-
migrations?: number | undefined;
|
|
46
|
-
} | undefined;
|
|
47
|
-
};
|
|
31
|
+
export declare const discoveryDocument: (record: ServedRecord, host: string) => {
|
|
32
|
+
commands?: string[] | undefined;
|
|
48
33
|
secrets?: string[] | undefined;
|
|
49
|
-
secretParts?: Record<string, ("pages" | "api" | "mcp" | "worker")[]> | undefined;
|
|
50
34
|
image?: string | undefined;
|
|
51
35
|
openapi?: string | undefined;
|
|
36
|
+
secretParts?: Record<string, ("pages" | "api" | "mcp" | "worker")[]> | undefined;
|
|
52
37
|
tools?: {
|
|
53
38
|
name: string;
|
|
54
39
|
scope: string;
|
|
@@ -58,7 +43,7 @@ export declare const discoveryDocument: (record: ExtensionRecord, host: string)
|
|
|
58
43
|
element: string;
|
|
59
44
|
module: string;
|
|
60
45
|
}[] | undefined;
|
|
61
|
-
|
|
46
|
+
parts: DiscoveryDocument["parts"];
|
|
62
47
|
id: string;
|
|
63
48
|
version: string;
|
|
64
49
|
commit: string;
|
|
@@ -2,7 +2,7 @@ import { formatIssues, readJsonIfExists } from '../manifest/read-manifest.js';
|
|
|
2
2
|
import {} from '../manifest/manifest.schema.js';
|
|
3
3
|
import { normalizeParts, scopedSecrets } from '../manifest/parts.js';
|
|
4
4
|
import {} from '../manifest/primitives.schema.js';
|
|
5
|
-
import { ExtensionRecordSchema } from './discovery.schema.js';
|
|
5
|
+
import { ExtensionRecordSchema, } from './discovery.schema.js';
|
|
6
6
|
export const DEFAULT_RECORD_FILE = '/run/agentage/agentage-extension.json';
|
|
7
7
|
const partsOf = (pkg, images, migrations) => {
|
|
8
8
|
const parts = {};
|
|
@@ -34,6 +34,12 @@ export const recordOf = (pkg, build, images, extras = {}) => {
|
|
|
34
34
|
record.secretParts = scopedSecrets(pkg);
|
|
35
35
|
return record;
|
|
36
36
|
};
|
|
37
|
+
// The kit host never knows its digest: the parts it runs have no image; pages and ui are not listed.
|
|
38
|
+
export const hostedRecordOf = (pkg, build, hosted, extras) => {
|
|
39
|
+
const record = recordOf(pkg, build, {}, extras);
|
|
40
|
+
const bare = Object.fromEntries(hosted.map((kind) => [kind, {}]));
|
|
41
|
+
return { ...record, parts: { ...bare, ...record.parts } };
|
|
42
|
+
};
|
|
37
43
|
export const validateRecord = (value) => {
|
|
38
44
|
const result = ExtensionRecordSchema.safeParse(value);
|
|
39
45
|
if (result.success)
|
|
@@ -57,6 +57,51 @@ export declare const ExtensionRecordSchema: z.ZodObject<{
|
|
|
57
57
|
commands: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
58
58
|
}, z.core.$strict>;
|
|
59
59
|
export declare const DiscoveryDocumentSchema: z.ZodObject<{
|
|
60
|
+
parts: z.ZodObject<{
|
|
61
|
+
pages: z.ZodOptional<z.ZodObject<{
|
|
62
|
+
image: z.ZodString;
|
|
63
|
+
}, z.core.$strict>>;
|
|
64
|
+
ui: z.ZodOptional<z.ZodObject<{
|
|
65
|
+
image: z.ZodString;
|
|
66
|
+
}, z.core.$strict>>;
|
|
67
|
+
api: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
|
|
68
|
+
image: z.ZodString;
|
|
69
|
+
}, z.core.$strict>, z.ZodObject<{}, z.core.$strict>]>>;
|
|
70
|
+
mcp: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
|
|
71
|
+
image: z.ZodString;
|
|
72
|
+
}, z.core.$strict>, z.ZodObject<{}, z.core.$strict>]>>;
|
|
73
|
+
worker: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
|
|
74
|
+
image: z.ZodString;
|
|
75
|
+
}, z.core.$strict>, z.ZodObject<{}, z.core.$strict>]>>;
|
|
76
|
+
store: z.ZodOptional<z.ZodObject<{
|
|
77
|
+
kind: z.ZodEnum<{
|
|
78
|
+
postgres: "postgres";
|
|
79
|
+
signoz: "signoz";
|
|
80
|
+
git: "git";
|
|
81
|
+
clickhouse: "clickhouse";
|
|
82
|
+
}>;
|
|
83
|
+
migrations: z.ZodOptional<z.ZodInt>;
|
|
84
|
+
}, z.core.$strict>>;
|
|
85
|
+
}, z.core.$strict>;
|
|
86
|
+
host: z.ZodString;
|
|
87
|
+
id: z.ZodString;
|
|
88
|
+
version: z.ZodString;
|
|
89
|
+
commit: z.ZodString;
|
|
90
|
+
buildTime: z.ZodISODateTime;
|
|
91
|
+
image: z.ZodOptional<z.ZodString>;
|
|
92
|
+
openapi: z.ZodOptional<z.ZodString>;
|
|
93
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
94
|
+
name: z.ZodString;
|
|
95
|
+
scope: z.ZodString;
|
|
96
|
+
}, z.core.$strict>>>;
|
|
97
|
+
widgets: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
98
|
+
name: z.ZodString;
|
|
99
|
+
element: z.ZodString;
|
|
100
|
+
module: z.ZodString;
|
|
101
|
+
}, z.core.$strict>>>;
|
|
102
|
+
commands: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
103
|
+
}, z.core.$strict>;
|
|
104
|
+
export declare const CloudDiscoveryDocumentSchema: z.ZodObject<{
|
|
60
105
|
host: z.ZodString;
|
|
61
106
|
id: z.ZodString;
|
|
62
107
|
version: z.ZodString;
|
|
@@ -11,15 +11,27 @@ const OneImageRefSchema = z
|
|
|
11
11
|
.string()
|
|
12
12
|
.max(256)
|
|
13
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 StoreRecordSchema = z.strictObject({
|
|
15
|
+
kind: StoreKindSchema,
|
|
16
|
+
migrations: z.int().min(0).optional(),
|
|
17
|
+
});
|
|
14
18
|
const PartsRecordSchema = z.strictObject({
|
|
15
19
|
pages: PartImageSchema.optional(),
|
|
16
20
|
ui: PartImageSchema.optional(),
|
|
17
21
|
api: PartImageSchema.optional(),
|
|
18
22
|
mcp: PartImageSchema.optional(),
|
|
19
23
|
worker: PartImageSchema.optional(),
|
|
20
|
-
store:
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
store: StoreRecordSchema.optional(),
|
|
25
|
+
});
|
|
26
|
+
// A part the kit host runs has no image: the host never knows its own digest.
|
|
27
|
+
const HostedPartSchema = z.strictObject({});
|
|
28
|
+
const ServedPartsSchema = z.strictObject({
|
|
29
|
+
pages: PartImageSchema.optional(),
|
|
30
|
+
ui: PartImageSchema.optional(),
|
|
31
|
+
api: z.union([PartImageSchema, HostedPartSchema]).optional(),
|
|
32
|
+
mcp: z.union([PartImageSchema, HostedPartSchema]).optional(),
|
|
33
|
+
worker: z.union([PartImageSchema, HostedPartSchema]).optional(),
|
|
34
|
+
store: StoreRecordSchema.optional(),
|
|
23
35
|
});
|
|
24
36
|
export const OriginSchema = z
|
|
25
37
|
.string()
|
|
@@ -40,7 +52,7 @@ const recordShape = {
|
|
|
40
52
|
commit: CommitSchema,
|
|
41
53
|
buildTime: z.iso.datetime(),
|
|
42
54
|
parts: PartsRecordSchema,
|
|
43
|
-
image: OneImageRefSchema.optional().describe('The one image (build --container). A record has part images or this image: never both, never none.'),
|
|
55
|
+
image: OneImageRefSchema.optional().describe('The one image (build --container). A build record has part images or this image: never both, never none.'),
|
|
44
56
|
// /api/v{N}: an extension still on a kit before 0.13, read by a newer one; it goes in phase 4.
|
|
45
57
|
openapi: z
|
|
46
58
|
.string()
|
|
@@ -63,6 +75,17 @@ const FORM_RULE = {
|
|
|
63
75
|
message: 'at least one part with an image, or the one image and no part image',
|
|
64
76
|
path: ['parts'],
|
|
65
77
|
};
|
|
78
|
+
// Discovery also takes the kit host's form: one or more parts, none with an image, no one image.
|
|
79
|
+
const servedForm = (doc) => {
|
|
80
|
+
const bare = IMAGE_PART_KINDS.filter((kind) => doc.parts[kind] && !doc.parts[kind].image);
|
|
81
|
+
if (!bare.length)
|
|
82
|
+
return oneImageForm(doc);
|
|
83
|
+
return !doc.image && IMAGE_PART_KINDS.every((kind) => doc.parts[kind]?.image === undefined);
|
|
84
|
+
};
|
|
85
|
+
const HOSTED_RULE = {
|
|
86
|
+
message: 'at least one part with an image, the one image, or only parts with no image',
|
|
87
|
+
path: ['parts'],
|
|
88
|
+
};
|
|
66
89
|
// The api prefix is /api/{id}, or the old /api/v{N}: never another extension's id.
|
|
67
90
|
const openapiNamesItsExtension = ({ id, openapi }) => !openapi || /^\/api\/v[0-9]+\//.test(openapi) || openapi === `/api/${id}/openapi.json`;
|
|
68
91
|
const OPENAPI_RULE = { message: 'openapi is /api/{id}/openapi.json', path: ['openapi'] };
|
|
@@ -86,6 +109,12 @@ export const ExtensionRecordSchema = z
|
|
|
86
109
|
});
|
|
87
110
|
// What a deployment serves at /.well-known/agentage-extension.json: the record plus its host.
|
|
88
111
|
export const DiscoveryDocumentSchema = z
|
|
112
|
+
.strictObject({ ...recordShape, parts: ServedPartsSchema, host: OriginSchema })
|
|
113
|
+
.refine(servedForm, HOSTED_RULE)
|
|
114
|
+
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
115
|
+
.refine(openapiNamesItsExtension, OPENAPI_RULE);
|
|
116
|
+
// The form the cloud serves: every part by its image, or the one image; never a part with no image.
|
|
117
|
+
export const CloudDiscoveryDocumentSchema = z
|
|
89
118
|
.strictObject({ ...recordShape, host: OriginSchema })
|
|
90
119
|
.refine(oneImageForm, FORM_RULE)
|
|
91
120
|
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
import { type ToolEntry } from '../mcp/tools.js';
|
|
3
|
+
import { type RecordExtras } from './discovery.js';
|
|
4
|
+
export declare const toolRecords: (id: string, tools: Pick<ToolEntry, "name" | "spec">[]) => {
|
|
5
|
+
name: string;
|
|
6
|
+
scope: string;
|
|
7
|
+
}[];
|
|
8
|
+
export declare const migrationsOf: (dir: string, pkg: ExtensionPackage) => Promise<RecordExtras>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { scopeOf } from '../auth/token-check.js';
|
|
3
|
+
import {} from '../manifest/manifest.schema.js';
|
|
4
|
+
import { migratesStore } from '../manifest/parts.js';
|
|
5
|
+
import { prefixed } from '../mcp/tools.js';
|
|
6
|
+
import { sqlFiles } from '../store/migrations.js';
|
|
7
|
+
import {} from './discovery.js';
|
|
8
|
+
// Each tool as tools/list names it, with the scope tools/call needs.
|
|
9
|
+
export const toolRecords = (id, tools) => tools.map((tool) => ({ name: prefixed(id, tool.name), scope: scopeOf(id, tool.spec.scope) }));
|
|
10
|
+
export const migrationsOf = async (dir, pkg) => migratesStore(pkg) ? { migrations: (await sqlFiles(join(dir, 'migrations'))).length } : {};
|
|
@@ -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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { rm } from 'node:fs/promises';
|
|
3
3
|
import { isAbsolute } from 'node:path';
|
|
4
|
+
import { validRange } from 'semver';
|
|
4
5
|
import { z } from 'zod';
|
|
5
6
|
import { fail } from '../../envelope/envelope.js';
|
|
6
7
|
import { parse } from '../../envelope/parse.js';
|
|
@@ -13,11 +14,19 @@ import { clearRunDir, dataDirOf } from './daemon-files.js';
|
|
|
13
14
|
import { needsSecret } from './start-checks.js';
|
|
14
15
|
import { startEntry, startFailed } from './supervisor.js';
|
|
15
16
|
const RESERVED_ID = 'host';
|
|
17
|
+
// The cli's rule for what follows `<id>@`: a version, a range or a dist-tag; never a path, URL or flag.
|
|
18
|
+
const DIST_TAG = /^[A-Za-z][\w.-]*$/;
|
|
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');
|
|
16
25
|
// enabled: false records the extension and never starts it (a profile "off" entry).
|
|
17
26
|
const AddSchema = z.union([
|
|
18
27
|
z.strictObject({
|
|
19
28
|
id: ExtensionIdSchema,
|
|
20
|
-
version:
|
|
29
|
+
version: VersionSpecSchema.optional(),
|
|
21
30
|
enabled: z.boolean().optional(),
|
|
22
31
|
}),
|
|
23
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,11 +1,11 @@
|
|
|
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';
|
|
8
|
+
import { pipeRedacted } from './redact-stream.js';
|
|
9
9
|
import { permissionFlag, sealFlags, sealOf } from './seal.js';
|
|
10
10
|
const HOST_MAIN = fileURLToPath(new URL('../main.js', import.meta.url));
|
|
11
11
|
const STOP_MS = 5_000;
|
|
@@ -21,15 +21,12 @@ export const childEnv = (spec, secretsDir, runDir) => ({
|
|
|
21
21
|
...(runDir && { AGENTAGE_RUN_DIR: runDir }),
|
|
22
22
|
...(spec.storeOpenMs && { AGENTAGE_STORE_OPEN_MS: String(spec.storeOpenMs) }),
|
|
23
23
|
});
|
|
24
|
-
export { lastWords } from './last-words.js';
|
|
25
24
|
const passedEnv = () => Object.fromEntries(PASSED.flatMap((k) => (process.env[k] ? [[k, process.env[k]]] : [])));
|
|
26
|
-
const captured = (child) => {
|
|
25
|
+
const captured = (child, secrets) => {
|
|
27
26
|
let tail = '';
|
|
28
|
-
child.stdout
|
|
29
|
-
child.stderr
|
|
30
|
-
|
|
31
|
-
tail = (tail + chunk.toString()).slice(-4000);
|
|
32
|
-
});
|
|
27
|
+
pipeRedacted(child.stdout, process.stdout, secrets);
|
|
28
|
+
pipeRedacted(child.stderr, process.stderr, secrets);
|
|
29
|
+
child.stderr?.on('data', (chunk) => (tail = (tail + chunk.toString()).slice(-4000)));
|
|
33
30
|
return () => tail;
|
|
34
31
|
};
|
|
35
32
|
// 'exit' can come before the last stderr chunk: wait for the stream end, at most 500 ms.
|
|
@@ -79,7 +76,7 @@ const stopOf = (child, exited) => async () => {
|
|
|
79
76
|
const execArgvOf = (node, main, spec, run) => {
|
|
80
77
|
if (!permissionFlag(node))
|
|
81
78
|
return [];
|
|
82
|
-
const configDir =
|
|
79
|
+
const configDir = configDirOf(spec.dataDir);
|
|
83
80
|
const { id, dir, dataDir } = spec;
|
|
84
81
|
return sealFlags({ id, main, dir, configDir, runDir: run.runDir, dataDir });
|
|
85
82
|
};
|
|
@@ -99,7 +96,7 @@ const started = async (at, spec, run) => {
|
|
|
99
96
|
execArgv,
|
|
100
97
|
stdio: [...stdio],
|
|
101
98
|
});
|
|
102
|
-
const tail = captured(child);
|
|
99
|
+
const tail = captured(child, spec.secrets);
|
|
103
100
|
const exited = exitOf(child, tail);
|
|
104
101
|
const port = await readyOf(child, tail, at.startMs);
|
|
105
102
|
return { port, pid: child.pid ?? 0, exited, stop: stopOf(child, exited) };
|
|
@@ -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 {};
|