@notation/factory 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/contracts/channel.ts +43 -0
- package/contracts/connection.ts +15 -0
- package/contracts/hub.ts +33 -0
- package/contracts/index.ts +36 -0
- package/contracts/outpost.ts +54 -0
- package/contracts/router.ts +104 -0
- package/contracts/vault.ts +22 -0
- package/deployer/collect.ts +31 -0
- package/deployer/deploy.test.ts +50 -0
- package/deployer/index.ts +152 -0
- package/deployer/sdk.gen.ts +2 -0
- package/deployer/sdk.ts +18 -0
- package/implementations/container.ts +67 -0
- package/implementations/outpost.ts +50 -0
- package/implementations/policy.ts +26 -0
- package/implementations/trigger.ts +67 -0
- package/index.ts +3 -0
- package/lib/binding-name.ts +9 -0
- package/lib/collect.ts +21 -0
- package/lib/outpost.ts +17 -0
- package/lib/scoped-name.ts +10 -0
- package/outpost.ts +12 -0
- package/package.json +39 -3
- package/resources/outpost-config.ts +102 -0
- package/resources/random-key.ts +54 -0
- package/resources/router-sync.ts +53 -0
- package/router/channel.ts +114 -0
- package/router/handle.test.ts +419 -0
- package/router/handle.ts +442 -0
- package/router/index.ts +52 -0
- package/router/keys.ts +36 -0
- package/router/run.ts +91 -0
- package/runner/index.ts +628 -0
- package/runner/runner.test.ts +322 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@notation/factory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"author": "Daniel Grant",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/notationlabs/factory.git",
|
|
9
|
+
"directory": "packages/factory"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"factory-outpost": "./outpost.ts",
|
|
15
|
+
"default": "./index.ts"
|
|
16
|
+
},
|
|
17
|
+
"./router": "./router/index.ts",
|
|
18
|
+
"./deployer": "./deployer/index.ts",
|
|
19
|
+
"./runner": "./runner/index.ts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"contracts",
|
|
23
|
+
"implementations",
|
|
24
|
+
"index.ts",
|
|
25
|
+
"lib",
|
|
26
|
+
"resources",
|
|
27
|
+
"router",
|
|
28
|
+
"outpost.ts",
|
|
29
|
+
"deployer",
|
|
30
|
+
"runner"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@notation/core": "0.28.0",
|
|
37
|
+
"@notation/resource": "0.28.0",
|
|
38
|
+
"@notation/std": "0.28.0",
|
|
39
|
+
"sucrase": "^3.35.1",
|
|
40
|
+
"valibot": "^1.1.0"
|
|
41
|
+
}
|
|
6
42
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { mergeContributions, resource } from "@notation/resource";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
import type { OutpostConfig as Config } from "../contracts/outpost.ts";
|
|
4
|
+
import { deleteOutpost, putOutpost, readOutpost } from "../lib/outpost.ts";
|
|
5
|
+
|
|
6
|
+
type OutpostConfigApi = {
|
|
7
|
+
Key: {};
|
|
8
|
+
CreateParams: {
|
|
9
|
+
channels?: Config["channels"];
|
|
10
|
+
containers?: Config["containers"];
|
|
11
|
+
policies?: Config["policies"];
|
|
12
|
+
agents?: Config["agents"];
|
|
13
|
+
triggers?: Config["triggers"];
|
|
14
|
+
};
|
|
15
|
+
UpdateParams: undefined;
|
|
16
|
+
ReadResult: undefined;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const channelEntry = v.object({
|
|
20
|
+
provider: v.string(),
|
|
21
|
+
connection: v.string(),
|
|
22
|
+
scope: v.optional(v.string()),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const containerEntry = v.object({
|
|
26
|
+
image: v.string(),
|
|
27
|
+
runtimes: v.array(v.string()),
|
|
28
|
+
policies: v.array(v.string()),
|
|
29
|
+
files: v.record(
|
|
30
|
+
v.string(),
|
|
31
|
+
v.union([v.string(), v.record(v.string(), v.unknown())]),
|
|
32
|
+
),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const policyEntry = v.looseObject({ provider: v.string() });
|
|
36
|
+
|
|
37
|
+
const agentEntry = v.looseObject({ runtime: v.string() });
|
|
38
|
+
|
|
39
|
+
const triggerEntry = v.object({
|
|
40
|
+
channel: v.string(),
|
|
41
|
+
event: v.string(),
|
|
42
|
+
match: v.unknown(),
|
|
43
|
+
agent: v.string(),
|
|
44
|
+
container: v.string(),
|
|
45
|
+
worktree: v.optional(v.object({ base: v.optional(v.string()) })),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const outpostConfig = resource<OutpostConfigApi>({
|
|
49
|
+
type: "factory/deployer/OutpostConfig",
|
|
50
|
+
provider: "@notation/factory",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const outpostConfigSchema = outpostConfig.defineSchema({
|
|
54
|
+
channels: {
|
|
55
|
+
propertyType: "param",
|
|
56
|
+
presence: "optional",
|
|
57
|
+
value: v.record(v.string(), channelEntry),
|
|
58
|
+
collect: mergeContributions,
|
|
59
|
+
},
|
|
60
|
+
containers: {
|
|
61
|
+
propertyType: "param",
|
|
62
|
+
presence: "optional",
|
|
63
|
+
value: v.record(v.string(), containerEntry),
|
|
64
|
+
collect: mergeContributions,
|
|
65
|
+
},
|
|
66
|
+
policies: {
|
|
67
|
+
propertyType: "param",
|
|
68
|
+
presence: "optional",
|
|
69
|
+
value: v.record(v.string(), policyEntry),
|
|
70
|
+
collect: mergeContributions,
|
|
71
|
+
},
|
|
72
|
+
agents: {
|
|
73
|
+
propertyType: "param",
|
|
74
|
+
presence: "optional",
|
|
75
|
+
value: v.record(v.string(), agentEntry),
|
|
76
|
+
collect: mergeContributions,
|
|
77
|
+
},
|
|
78
|
+
triggers: {
|
|
79
|
+
propertyType: "param",
|
|
80
|
+
presence: "optional",
|
|
81
|
+
value: v.record(v.string(), triggerEntry),
|
|
82
|
+
collect: mergeContributions,
|
|
83
|
+
},
|
|
84
|
+
} as const);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A repo's outpost, as the one entry the deployer applies. Its channels,
|
|
88
|
+
* containers, policies, agents and triggers each contribute their part. The
|
|
89
|
+
* deployer runs Notation for one repo at a time, and stores the entry under
|
|
90
|
+
* that repo.
|
|
91
|
+
*/
|
|
92
|
+
export const OutpostConfig = outpostConfigSchema.defineOperations({
|
|
93
|
+
create: async (config) => putOutpost(config),
|
|
94
|
+
read: async () => {
|
|
95
|
+
await readOutpost();
|
|
96
|
+
return {};
|
|
97
|
+
},
|
|
98
|
+
update: async (_key, _patch, config) => putOutpost(config),
|
|
99
|
+
delete: async () => deleteOutpost(),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export type OutpostConfigInstance = InstanceType<typeof OutpostConfig>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { resource } from "@notation/resource";
|
|
3
|
+
import * as v from "valibot";
|
|
4
|
+
|
|
5
|
+
type RandomKeyApi = {
|
|
6
|
+
Key: { KeyId: string };
|
|
7
|
+
CreateParams: { Bytes: number };
|
|
8
|
+
UpdateParams: undefined;
|
|
9
|
+
ReadResult: {};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const randomKey = resource<RandomKeyApi>({
|
|
13
|
+
type: "factory/keys/RandomKey",
|
|
14
|
+
provider: "@notation/factory",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const randomKeySchema = randomKey.defineSchema({
|
|
18
|
+
Bytes: {
|
|
19
|
+
propertyType: "param",
|
|
20
|
+
presence: "required",
|
|
21
|
+
immutable: true,
|
|
22
|
+
value: v.pipe(v.number(), v.integer(), v.minValue(16)),
|
|
23
|
+
},
|
|
24
|
+
KeyId: {
|
|
25
|
+
propertyType: "computed",
|
|
26
|
+
presence: "required",
|
|
27
|
+
primaryKey: true,
|
|
28
|
+
value: v.string(),
|
|
29
|
+
},
|
|
30
|
+
/** `Bytes` random bytes, in hex. */
|
|
31
|
+
Value: { propertyType: "credential", value: v.string() },
|
|
32
|
+
} as const);
|
|
33
|
+
|
|
34
|
+
const generate = (bytes: number) => randomBytes(bytes).toString("hex");
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A random key, such as the hub's root key. Its value is the credential
|
|
38
|
+
* `Value`, which is made in memory and never recorded. When a resource that
|
|
39
|
+
* takes the value needs it and deploy holds none, deploy rotates the key, so
|
|
40
|
+
* the key gets a new value and every resource that takes it is written again.
|
|
41
|
+
*/
|
|
42
|
+
export const RandomKey = randomKeySchema.defineOperations({
|
|
43
|
+
create: async (params) => ({
|
|
44
|
+
KeyId: randomUUID(),
|
|
45
|
+
Value: generate(params.Bytes),
|
|
46
|
+
}),
|
|
47
|
+
rotate: {
|
|
48
|
+
Value: async (_key, state) => generate(state.Bytes),
|
|
49
|
+
},
|
|
50
|
+
read: async (key) => key,
|
|
51
|
+
delete: async () => {},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export type RandomKeyInstance = InstanceType<typeof RandomKey>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { resource } from "@notation/resource";
|
|
3
|
+
import * as v from "valibot";
|
|
4
|
+
|
|
5
|
+
type RouterSyncApi = {
|
|
6
|
+
Key: { SyncId: string };
|
|
7
|
+
CreateParams: { Url: string; Config: string };
|
|
8
|
+
UpdateParams: { Url: string; Config: string };
|
|
9
|
+
ReadResult: {};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const routerSync = resource<RouterSyncApi>({
|
|
13
|
+
type: "factory/router/Sync",
|
|
14
|
+
provider: "@notation/factory",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const routerSyncSchema = routerSync.defineSchema({
|
|
18
|
+
Url: { propertyType: "param", presence: "required", value: v.string() },
|
|
19
|
+
/** The router's config, as JSON. A change to it syncs again. */
|
|
20
|
+
Config: { propertyType: "param", presence: "required", value: v.string() },
|
|
21
|
+
SyncId: {
|
|
22
|
+
propertyType: "computed",
|
|
23
|
+
presence: "required",
|
|
24
|
+
primaryKey: true,
|
|
25
|
+
value: v.string(),
|
|
26
|
+
},
|
|
27
|
+
} as const);
|
|
28
|
+
|
|
29
|
+
const sync = async (url: string) => {
|
|
30
|
+
const response = await fetch(new URL("/sync", url), { method: "POST" });
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`The router's sync at ${url} failed with ${response.status}: ${await response.text()}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Calls the router's `/sync` when the router's URL or config changes, so
|
|
40
|
+
* each connection's platform (for GitHub, the App's webhook URL and secret)
|
|
41
|
+
* matches the router.
|
|
42
|
+
*/
|
|
43
|
+
export const RouterSync = routerSyncSchema.defineOperations({
|
|
44
|
+
create: async (params) => {
|
|
45
|
+
await sync(params.Url);
|
|
46
|
+
return { SyncId: randomUUID() };
|
|
47
|
+
},
|
|
48
|
+
update: async (_key, _patch, params) => {
|
|
49
|
+
await sync(params.Url);
|
|
50
|
+
},
|
|
51
|
+
read: async (key) => key,
|
|
52
|
+
delete: async () => {},
|
|
53
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a platform gives a channel: the secrets and values that the
|
|
3
|
+
* connection declared for it, by name.
|
|
4
|
+
*/
|
|
5
|
+
export type ChannelContext = {
|
|
6
|
+
secret(name: string): Promise<string>;
|
|
7
|
+
value(name: string): string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A verified delivery, in the terms the router routes by. `data` is the
|
|
12
|
+
* provider's own view of the event, which only its `match` reads.
|
|
13
|
+
*/
|
|
14
|
+
export type Delivery<E> = {
|
|
15
|
+
id: string;
|
|
16
|
+
/** What a channel's `scope` is compared with, such as `owner/repo` for GitHub. */
|
|
17
|
+
scope: string;
|
|
18
|
+
/** The repo whose outpost the delivery may be for. */
|
|
19
|
+
repo: string;
|
|
20
|
+
event: string;
|
|
21
|
+
data: E;
|
|
22
|
+
/** The event as a trigger's worktree function reads it, such as GitHub's webhook payload. */
|
|
23
|
+
payload: unknown;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Where a delivery's run reads and replies. `id` names the conversation,
|
|
28
|
+
* such as an issue. `thread` is the channel's own reference to it, which
|
|
29
|
+
* only its `reply` reads.
|
|
30
|
+
*/
|
|
31
|
+
export type Conversation = { id: string; prompt: string; thread: unknown };
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A repo whose outpost may need deploying again. `ref` is the commit to read
|
|
35
|
+
* `.factory/` at, or the default branch's head if there's none. `removed`
|
|
36
|
+
* means the hub can no longer read the repo.
|
|
37
|
+
*/
|
|
38
|
+
export type SourceChange = { repo: string; ref?: string; removed?: boolean };
|
|
39
|
+
|
|
40
|
+
export type Received<E> =
|
|
41
|
+
| { status: "accepted"; delivery: Delivery<E> }
|
|
42
|
+
/** The delivery changed what repos hold, or which repos the hub can read, such as a push or an install. */
|
|
43
|
+
| { status: "source"; changes: SourceChange[] }
|
|
44
|
+
| { status: "ignored"; reason: string }
|
|
45
|
+
| { status: "rejected"; reason: string };
|
|
46
|
+
|
|
47
|
+
/** The files under a directory of a repo at one commit, by their path in the directory. */
|
|
48
|
+
export type SourceFiles = { ref: string; files: Record<string, string> };
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether a selector's `from` lets the delivery start a run. `check` holds
|
|
52
|
+
* the author sets that need a call to the platform, which the router doesn't
|
|
53
|
+
* make yet.
|
|
54
|
+
*/
|
|
55
|
+
export type AuthorCheck =
|
|
56
|
+
| { result: "allowed" }
|
|
57
|
+
| { result: "denied" }
|
|
58
|
+
| { result: "check"; sets: unknown[] };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* One connection's deliveries, as the router handles them. A provider's
|
|
62
|
+
* package implements it, and each platform decides where it runs.
|
|
63
|
+
*/
|
|
64
|
+
export interface Channel<E = unknown> {
|
|
65
|
+
/** Checks the delivery's signature and parses it. */
|
|
66
|
+
receive(request: Request): Promise<Received<E>>;
|
|
67
|
+
/** Whether `selector` selects the delivery, and if so, whether its author may start a run. */
|
|
68
|
+
match(selector: unknown, delivery: Delivery<E>): AuthorCheck | undefined;
|
|
69
|
+
/** Sets up the platform side of the connection, such as a webhook, to deliver to `url`. Throws if it fails. */
|
|
70
|
+
sync(url: string): Promise<void>;
|
|
71
|
+
/** For a code host's channel: the files under `dir` in `repo` at `ref`, or at the default branch's head. */
|
|
72
|
+
read?(repo: string, dir: string, ref?: string): Promise<SourceFiles>;
|
|
73
|
+
/** For a code host's channel: shows the outpost's status on the repo, as `FACTORY_STATUS`. */
|
|
74
|
+
writeStatus?(repo: string, status: FactoryStatus): Promise<void>;
|
|
75
|
+
/** For a code host's channel: creates or updates the check named `check.name` on commit `ref`. */
|
|
76
|
+
writeCheck?(repo: string, ref: string, check: BuildCheck): Promise<void>;
|
|
77
|
+
/** The conversation that a run for the delivery belongs to. A channel without it can't start runs. */
|
|
78
|
+
conversation?(delivery: Delivery<E>): Conversation;
|
|
79
|
+
/** Posts `text` to a conversation's thread. */
|
|
80
|
+
reply?(thread: unknown, text: string): Promise<void>;
|
|
81
|
+
/** For a code host's channel: a short-lived token for `repo`, with only `permissions`. */
|
|
82
|
+
token?(repo: string, permissions: Record<string, "read" | "write">): Promise<string>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The outcome of a repo's last deploy, as the repo shows it. */
|
|
86
|
+
export type FactoryStatus = {
|
|
87
|
+
state: "deployed" | "failed";
|
|
88
|
+
/** The commit that `.factory/` was read at, if it was read. */
|
|
89
|
+
ref?: string;
|
|
90
|
+
error?: string;
|
|
91
|
+
/** When the deploy finished, as an ISO date. */
|
|
92
|
+
at: string;
|
|
93
|
+
/** The containers whose images started building. */
|
|
94
|
+
building?: string[];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** A container image's build, as a check on the commit it was built from. */
|
|
98
|
+
export type BuildCheck = {
|
|
99
|
+
name: string;
|
|
100
|
+
state: "queued" | "in_progress" | "success" | "failure" | "cancelled";
|
|
101
|
+
/** The build's log. */
|
|
102
|
+
url?: string;
|
|
103
|
+
summary?: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** What a channel module exports. */
|
|
107
|
+
export type ChannelModule = { channel(ctx: ChannelContext): Channel<any> };
|
|
108
|
+
|
|
109
|
+
/** A channel as the router calls it, where each call may cross to another process. */
|
|
110
|
+
export type RemoteChannel<E = unknown> = {
|
|
111
|
+
[K in keyof Channel<E>]-?: (
|
|
112
|
+
...args: Parameters<NonNullable<Channel<E>[K]>>
|
|
113
|
+
) => Promise<Awaited<ReturnType<NonNullable<Channel<E>[K]>>>>;
|
|
114
|
+
};
|