@notation/factory 0.1.2 → 0.1.5
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/contracts/hub.ts +2 -0
- package/contracts/index.ts +1 -0
- package/contracts/outpost.ts +8 -2
- package/contracts/router.ts +3 -0
- package/contracts/runner.ts +57 -0
- package/deployer/collect.ts +2 -2
- package/deployer/deploy.test.ts +5 -5
- package/deployer/index.ts +6 -6
- package/deployer/sdk.gen.ts +1 -1
- package/deployer/sdk.ts +1 -0
- package/implementations/outpost.ts +1 -1
- package/package.json +4 -4
- package/runner/index.ts +67 -118
- package/runner/runner.test.ts +84 -1
package/contracts/hub.ts
CHANGED
|
@@ -8,6 +8,8 @@ import type { Vault, VaultSecret } from "./vault.ts";
|
|
|
8
8
|
export interface CodeHost extends ResourceGroup {
|
|
9
9
|
readonly connection: Connection;
|
|
10
10
|
readonly allow: RepoPattern[];
|
|
11
|
+
/** The source of its runner module, a `CodeHostRunner`, which the runner imports to clone and push. */
|
|
12
|
+
readonly runner?: string;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
/** Who may run the CLI against the running hub. Declared, not managed: it contributes to the router's config. */
|
package/contracts/index.ts
CHANGED
package/contracts/outpost.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { ResourceGroup } from "@notation/resource";
|
|
2
2
|
import type { ChannelEntry } from "./channel.ts";
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* A kind of agent, which a container's image installs the CLI of. `setup`
|
|
6
|
+
* is a root shell script that installs the CLI system-wide, which the
|
|
7
|
+
* deployer adds to the container's image. `runner` is the source of its
|
|
8
|
+
* runner module, a `RuntimeRunner`, which the runner imports to run the
|
|
9
|
+
* runtime's agents.
|
|
10
|
+
*/
|
|
11
|
+
export type Runtime = { id: string; setup?: string; runner?: string };
|
|
6
12
|
|
|
7
13
|
/** An agent persona for a runtime. Its provider decides its settings. */
|
|
8
14
|
export interface Agent extends ResourceGroup {
|
package/contracts/router.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
OutputRef,
|
|
5
5
|
ResourceGroup,
|
|
6
6
|
} from "@notation/resource";
|
|
7
|
+
import type { CodeHost } from "./hub.ts";
|
|
7
8
|
import type { Vault, VaultSecret } from "./vault.ts";
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -89,6 +90,8 @@ export interface Router extends ResourceGroup {
|
|
|
89
90
|
readonly store: RouterConfigStore;
|
|
90
91
|
readonly vault: Vault;
|
|
91
92
|
readonly rootKey: VaultSecret;
|
|
93
|
+
/** The infra's code host, which sets itself here when it's declared. */
|
|
94
|
+
codeHost?: CodeHost;
|
|
92
95
|
/**
|
|
93
96
|
* Binds a secret from the router's vault to the router, and returns the
|
|
94
97
|
* name the router reads it as. The name is made from the secret's id,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Run } from "../router/run.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A plugin's runner module is TypeScript that the hub serves to its
|
|
5
|
+
* containers, and the runner imports for the runs that need it. A
|
|
6
|
+
* container has no packages, so the module imports only Node's built-ins,
|
|
7
|
+
* and types. Its default export is a `RuntimeRunner` or a `CodeHostRunner`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** What the runner gives a runtime for one run. */
|
|
11
|
+
export type RunContext = {
|
|
12
|
+
/** The run's worktree. */
|
|
13
|
+
cwd: string;
|
|
14
|
+
/** The runner's system prompt, followed by the agent's own. */
|
|
15
|
+
systemPrompt: string;
|
|
16
|
+
/** The runner's state directory, `/data`. */
|
|
17
|
+
dataDir: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** What a runtime's CLI said at the end of a run. */
|
|
21
|
+
export type RuntimeOutput = { result?: string; isError?: boolean; sessionId?: string };
|
|
22
|
+
|
|
23
|
+
/** How the runner runs a runtime's CLI: a runtime's runner module. */
|
|
24
|
+
export type RuntimeRunner = {
|
|
25
|
+
/** The command the image must have. */
|
|
26
|
+
command: string;
|
|
27
|
+
/** The CLI's name, for errors. */
|
|
28
|
+
name: string;
|
|
29
|
+
/** Where the CLI keeps its sessions' transcripts, in a directory per working directory. */
|
|
30
|
+
transcripts: string;
|
|
31
|
+
/** The CLI's arguments for a run. */
|
|
32
|
+
args(run: Run, context: RunContext): Promise<string[]>;
|
|
33
|
+
/** The arguments that resume a session. */
|
|
34
|
+
resume(sessionId: string): string[];
|
|
35
|
+
/** Reads the CLI's output. Returns undefined if it isn't the CLI's output. */
|
|
36
|
+
parse(stdout: string): RuntimeOutput | undefined;
|
|
37
|
+
/** Extra environment for the CLI. */
|
|
38
|
+
env?: Record<string, string>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* How the runner reaches the code host: the code host's runner module. Runs
|
|
43
|
+
* get the path of a file that holds the repo's token, as
|
|
44
|
+
* `FACTORY_TOKEN_FILE`, which the router refreshes during long runs.
|
|
45
|
+
*/
|
|
46
|
+
export type CodeHostRunner = {
|
|
47
|
+
/** The URL that the runner clones `repo` from. */
|
|
48
|
+
cloneUrl(repo: string): string;
|
|
49
|
+
/** Git's options that authenticate one command with `token`, without keeping it in the repo's config. */
|
|
50
|
+
gitAuth(token: string): string[];
|
|
51
|
+
/**
|
|
52
|
+
* Sets up git, and the code host's CLI if the image has it, to read the
|
|
53
|
+
* token in `FACTORY_TOKEN_FILE`. `bin` is first on the agent's PATH, for
|
|
54
|
+
* wrappers. The runner calls it once for each version of the module.
|
|
55
|
+
*/
|
|
56
|
+
setup(context: { bin: string }): Promise<void>;
|
|
57
|
+
};
|
package/deployer/collect.ts
CHANGED
|
@@ -11,7 +11,7 @@ export const collectOutpost = async (
|
|
|
11
11
|
const store = graph.resources.find(
|
|
12
12
|
(resource) => resource.type === "factory/deployer/OutpostConfig",
|
|
13
13
|
);
|
|
14
|
-
if (!store) throw new Error(".factory/
|
|
14
|
+
if (!store) throw new Error(".factory/infra.ts declares no outpost resources");
|
|
15
15
|
return (await store.getParams()) as OutpostConfig;
|
|
16
16
|
};
|
|
17
17
|
|
|
@@ -25,7 +25,7 @@ export const collectWorktree = async (
|
|
|
25
25
|
await load();
|
|
26
26
|
});
|
|
27
27
|
const trigger = graph.resourceGroups.find((group) => group.type === "factory/Trigger" && group.id === id);
|
|
28
|
-
if (!trigger) throw new Error(`.factory/
|
|
28
|
+
if (!trigger) throw new Error(`.factory/infra.ts declares no trigger ${id}`);
|
|
29
29
|
const worktree = trigger.config.worktree as ((event: unknown) => string | undefined) | undefined;
|
|
30
30
|
return worktree?.(event);
|
|
31
31
|
};
|
package/deployer/deploy.test.ts
CHANGED
|
@@ -23,7 +23,7 @@ test("deploy stores the evaluated outpost, with the repo as the scope of unscope
|
|
|
23
23
|
const { ctx, stored } = context(async () => ({
|
|
24
24
|
channels: { issues: { provider: "github", connection: "gh" } },
|
|
25
25
|
}));
|
|
26
|
-
const status = await deploy("acme/api", { ref: "abc", files: { "
|
|
26
|
+
const status = await deploy("acme/api", { ref: "abc", files: { "infra.ts": "" } }, ctx);
|
|
27
27
|
assert.equal(status.state, "deployed");
|
|
28
28
|
assert.equal(status.ref, "abc");
|
|
29
29
|
assert.deepEqual(stored["acme/api"], {
|
|
@@ -35,16 +35,16 @@ test("a config that fails leaves the outpost as it was", async () => {
|
|
|
35
35
|
const { ctx, stored } = context(async () => {
|
|
36
36
|
throw new Error("bad config");
|
|
37
37
|
});
|
|
38
|
-
const status = await deploy("acme/api", { ref: "abc", files: { "
|
|
38
|
+
const status = await deploy("acme/api", { ref: "abc", files: { "infra.ts": "" } }, ctx);
|
|
39
39
|
assert.equal(status.state, "failed");
|
|
40
40
|
assert.equal(status.error, "bad config");
|
|
41
41
|
assert.equal(stored["acme/api"], previous);
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
test("a repo with no
|
|
44
|
+
test("a repo with no infra.ts loses its outpost, even with the CLI's config.ts", async () => {
|
|
45
45
|
const { ctx, stored } = context(async () => ({}));
|
|
46
|
-
const status = await deploy("acme/api", { ref: "abc", files: { Dockerfile: "" } }, ctx);
|
|
46
|
+
const status = await deploy("acme/api", { ref: "abc", files: { Dockerfile: "", "config.ts": "export default {};" } }, ctx);
|
|
47
47
|
assert.equal(status.state, "failed");
|
|
48
|
-
assert.match(status.error!, /no \.factory\/
|
|
48
|
+
assert.match(status.error!, /no \.factory\/infra\.ts/);
|
|
49
49
|
assert.equal("acme/api" in stored, false);
|
|
50
50
|
});
|
package/deployer/index.ts
CHANGED
|
@@ -40,7 +40,7 @@ const MAIN = "main.js";
|
|
|
40
40
|
* re-exports the SDK's build of it. The SDK's modules are under `sdk/`.
|
|
41
41
|
*/
|
|
42
42
|
const configModules = (files: Record<string, string>) => {
|
|
43
|
-
if (!(`
|
|
43
|
+
if (!(`infra.ts` in files)) throw new Error(`There's no ${FACTORY_DIR}/infra.ts.`);
|
|
44
44
|
const modules: Record<string, string> = {};
|
|
45
45
|
for (const [path, code] of Object.entries(files)) {
|
|
46
46
|
if (/\.(m?js)$/.test(path)) modules[`${FACTORY_DIR}/${path}`] = code;
|
|
@@ -75,7 +75,7 @@ export const outpostProgram = (files: Record<string, string>): Program => {
|
|
|
75
75
|
const modules = configModules(files);
|
|
76
76
|
modules[MAIN] = [
|
|
77
77
|
`import { collectOutpost } from "./${SDK_DIR}/${SDK_COLLECT}.js";`,
|
|
78
|
-
`export default () => collectOutpost(() => import("./${FACTORY_DIR}/
|
|
78
|
+
`export default () => collectOutpost(() => import("./${FACTORY_DIR}/infra.ts"));`,
|
|
79
79
|
].join("\n");
|
|
80
80
|
return { main: MAIN, modules };
|
|
81
81
|
};
|
|
@@ -85,7 +85,7 @@ export const worktreeProgram = (files: Record<string, string>, trigger: string,
|
|
|
85
85
|
const modules = configModules(files);
|
|
86
86
|
modules[MAIN] = [
|
|
87
87
|
`import { collectWorktree } from "./${SDK_DIR}/${SDK_COLLECT}.js";`,
|
|
88
|
-
`export default (payload) => collectWorktree(() => import("./${FACTORY_DIR}/
|
|
88
|
+
`export default (payload) => collectWorktree(() => import("./${FACTORY_DIR}/infra.ts"), ${JSON.stringify(trigger)}, payload);`,
|
|
89
89
|
].join("\n");
|
|
90
90
|
return { main: MAIN, modules, input: payload };
|
|
91
91
|
};
|
|
@@ -131,7 +131,7 @@ const message = (error: unknown) => (error instanceof Error ? error.message : St
|
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
133
|
* Deploys a repo's outpost from its `.factory/` files. A repo with no
|
|
134
|
-
* `.factory/
|
|
134
|
+
* `.factory/infra.ts` loses its outpost. A config that fails leaves the
|
|
135
135
|
* outpost as it was.
|
|
136
136
|
*/
|
|
137
137
|
export const deploy = async (
|
|
@@ -139,9 +139,9 @@ export const deploy = async (
|
|
|
139
139
|
{ ref, files }: SourceFiles,
|
|
140
140
|
context: DeployerContext,
|
|
141
141
|
): Promise<FactoryStatus> => {
|
|
142
|
-
if (!("
|
|
142
|
+
if (!("infra.ts" in files)) {
|
|
143
143
|
await context.deleteOutpost(repo);
|
|
144
|
-
return { state: "failed", ref, error: `There's no ${FACTORY_DIR}/
|
|
144
|
+
return { state: "failed", ref, error: `There's no ${FACTORY_DIR}/infra.ts.`, at: new Date().toISOString() };
|
|
145
145
|
}
|
|
146
146
|
try {
|
|
147
147
|
const building = await context.putOutpost(repo, await evaluateOutpost(repo, files, context), files, ref);
|