@notation/factory 0.1.3 → 0.1.6

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 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. */
@@ -18,6 +18,7 @@ export type {
18
18
  Runtime,
19
19
  TriggerEntry,
20
20
  } from "./outpost.ts";
21
+ export type { CodeHostRunner, RunContext, RuntimeOutput, RuntimeRunner } from "./runner.ts";
21
22
  export type {
22
23
  CodeHostEntry,
23
24
  ConnectionEntry,
@@ -1,8 +1,14 @@
1
1
  import type { ResourceGroup } from "@notation/resource";
2
2
  import type { ChannelEntry } from "./channel.ts";
3
3
 
4
- /** A kind of agent, which a container's image installs the CLI of. `setup` is a root shell script that installs the CLI system-wide; the deployer adds it to the container's build context. */
5
- export type Runtime = { id: string; setup?: string };
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 {
@@ -18,6 +24,8 @@ export interface PolicyContract extends ResourceGroup {
18
24
  export type ContainerEntry = {
19
25
  /** The Dockerfile, relative to `.factory/`. Without it, the container gets Factory's default image. */
20
26
  image?: string;
27
+ /** The size of the machine, in the provider's names. */
28
+ instanceType?: string;
21
29
  runtimes: string[];
22
30
  policies: string[];
23
31
  files: Record<string, string | Record<string, unknown>>;
@@ -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
+ };
@@ -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/config.ts declares no outpost resources");
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/config.ts declares no trigger ${id}`);
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
  };
@@ -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: { "config.ts": "" } }, ctx);
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: { "config.ts": "" } }, ctx);
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 config.ts loses its outpost", async () => {
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\/config\.ts/);
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 (!(`config.ts` in files)) throw new Error(`There's no ${FACTORY_DIR}/config.ts.`);
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}/config.ts"));`,
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}/config.ts"), ${JSON.stringify(trigger)}, payload);`,
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/config.ts` loses its outpost. A config that fails leaves the
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 (!("config.ts" in files)) {
142
+ if (!("infra.ts" in files)) {
143
143
  await context.deleteOutpost(repo);
144
- return { state: "failed", ref, error: `There's no ${FACTORY_DIR}/config.ts.`, at: new Date().toISOString() };
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);