@notation/factory 0.2.2 → 0.5.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/router/index.ts CHANGED
@@ -1,54 +1,40 @@
1
- export type {
2
- AgentEntry,
3
- ChannelEntry,
4
- ContainerEntry,
5
- ConnectionEntry,
6
- OutpostConfig,
7
- RouterConfig,
8
- TriggerEntry,
9
- } from "../contracts/index.ts";
1
+ export type { OutpostConfig, RouterConfig } from "../contracts/index.ts";
10
2
  export type {
11
3
  AuthorCheck,
12
4
  BuildCheck,
13
5
  Channel,
14
6
  ChannelContext,
15
7
  ChannelModule,
16
- Conversation,
17
8
  ConversationTrigger,
18
9
  Delivery,
19
10
  FactoryStatus,
20
- Received,
11
+ Operator,
21
12
  RemoteChannel,
22
13
  SourceChange,
23
14
  SourceFiles,
15
+ ThreadMessage,
24
16
  } from "./channel.ts";
17
+ export type {
18
+ ContainerBuild,
19
+ DeliveriesResult,
20
+ DeployLog,
21
+ ReplayResult,
22
+ RepoDeploy,
23
+ ReposResult,
24
+ SessionResult,
25
+ SignInResult,
26
+ } from "./admin.ts";
25
27
  export {
26
- buildCheckName,
27
- FACTORY_DIR,
28
- handle,
29
- outpostKey,
30
- type RemoteDeployer,
31
- type RouterStore,
32
- type TriggerMatch,
33
- webhookPath,
34
- } from "./handle.ts";
28
+ type DeliveryRecord,
29
+ type FailedDelivery,
30
+ failedDelivery,
31
+ type Inbox,
32
+ KEEP_DELIVERY_MS,
33
+ replayRecord,
34
+ settled,
35
+ } from "./deliveries.ts";
36
+ export { advance, handle, outpostKey, redeployRepo, type RemoteDeployer, type RouterStore, webhookPath } from "./handle.ts";
35
37
  export { deriveKey, hmacHex, timingSafeEqual } from "./keys.ts";
36
38
  export { allowsRepo } from "../lib/repos.ts";
37
- export {
38
- type BuildReport,
39
- type BuildReportRequest,
40
- isBranchName,
41
- type Relay,
42
- REPLY_TTL_MS,
43
- type ReplyTicket,
44
- type Run,
45
- signBuildReport,
46
- signTicket,
47
- signTokenTicket,
48
- type TokenRequest,
49
- type Tokens,
50
- type TokenTicket,
51
- verifyBuildReport,
52
- verifyTicket,
53
- verifyTokenTicket,
54
- } from "./run.ts";
39
+ export { type BuildReport, type Relay, type Run, type RunResult, signBuildReport } from "./run.ts";
40
+ export { SYNC_HEADER, signSync } from "./sync.ts";
package/router/run.ts CHANGED
@@ -1,11 +1,28 @@
1
1
  import type { AgentEntry, ContainerEntry } from "../contracts/outpost.ts";
2
+ import type { ThreadMessage } from "./channel.ts";
2
3
  import { deriveKey, hmacHex, timingSafeEqual } from "./keys.ts";
3
4
 
4
5
  /** Where a run's reply goes: the connection whose channel posts it, and the channel's thread. `expires` is in ms since the epoch. */
5
6
  export type ReplyTicket = { connection: string; thread: unknown; expires: number };
6
7
 
7
- /** What the runner posts to a run's `reply.url`. */
8
- export type Relay = { ticket: ReplyTicket; signature: string; text: string };
8
+ /**
9
+ * A message that a run's reply posted, which a later reply of the run
10
+ * replaces. The router signs its id together with the reply ticket's
11
+ * signature, so a run can only replace its own messages.
12
+ */
13
+ export type ReplyMessage = { id: string; signature: string };
14
+
15
+ /** What the runner posts to a run's `reply.url`. With `message`, the reply replaces that message. */
16
+ export type Relay = { ticket: ReplyTicket; signature: string; text: string; message?: ReplyMessage };
17
+
18
+ /** What the router answers to a `Relay`: the message that the reply posted or replaced, if the channel names it. */
19
+ export type RelayResult = { ok: true; message?: ReplyMessage };
20
+
21
+ /** What the runner posts to the router's `/messages` route, next to `reply.url`. The router answers with `MessagesResult`. */
22
+ export type MessagesRequest = { ticket: ReplyTicket; signature: string };
23
+
24
+ /** The messages in a run's thread, oldest first. */
25
+ export type MessagesResult = { messages: ThreadMessage[] };
9
26
 
10
27
  /** Which container's policy tokens the router mints again: a container of the repo's outpost. `expires` is in ms since the epoch. */
11
28
  export type TokenTicket = { repo: string; container: string; expires: number };
@@ -41,7 +58,11 @@ export type Run = {
41
58
  /** The container's startup files, read from `.factory/` in the checkout. */
42
59
  files: ContainerEntry["files"];
43
60
  agent: { id: string } & AgentEntry;
44
- conversation: { id: string; prompt: string; channel?: string; context?: string };
61
+ /**
62
+ * The conversation. `message` is the id of the thread's message that
63
+ * started the run, and `event` says what happened when no message did.
64
+ */
65
+ conversation: { id: string; message?: string; event?: string; channel?: string; context?: string };
45
66
  /** The branch to run on, cut from `base` if it doesn't exist. Without it, the run gets a scratch checkout of the default branch. */
46
67
  worktree?: { branch: string; base?: string };
47
68
  /** A read-only token for cloning the repo. */
@@ -50,10 +71,21 @@ export type Run = {
50
71
  tokens: Tokens;
51
72
  /** The runner posts a `TokenRequest` with this ticket and signature to `url` for new tokens, before the old ones expire. */
52
73
  refresh: { url: string; ticket: TokenTicket; signature: string };
53
- /** The runner posts a `Relay` with this ticket and signature to `url`. */
54
- reply: { url: string; ticket: ReplyTicket; signature: string };
74
+ /**
75
+ * The runner posts a `Relay` with this ticket and signature to `url`.
76
+ * `message` is a message that the router posted while the run waited for
77
+ * its container, which the run's first reply replaces.
78
+ */
79
+ reply: { url: string; ticket: ReplyTicket; signature: string; message?: ReplyMessage };
55
80
  };
56
81
 
82
+ /**
83
+ * Whether the container's outpost Worker queued the run, or can't take it
84
+ * yet because the container's first image build hasn't finished. The
85
+ * deployer throws any other failure.
86
+ */
87
+ export type RunResult = "queued" | "building";
88
+
57
89
  /** How long a container may reply to a run, or ask for its tokens again. A run may wait in the queue behind others before it starts. */
58
90
  export const REPLY_TTL_MS = 24 * 60 * 60 * 1000;
59
91
 
@@ -69,6 +101,14 @@ export const signTicket = (rootKey: string, ticket: ReplyTicket) => sign(rootKey
69
101
  export const verifyTicket = (rootKey: string, ticket: ReplyTicket, signature: string) =>
70
102
  verify(rootKey, "router:relay", ticket, signature);
71
103
 
104
+ /** Signs the id of a message that the reply with the ticket signature `ticket` posted. */
105
+ export const signMessage = async (rootKey: string, ticket: string, id: string) =>
106
+ hmacHex(await deriveKey(rootKey, "router:message"), JSON.stringify([ticket, id]));
107
+
108
+ /** Whether the router signed `message` for replies with the ticket signature `ticket`. */
109
+ export const verifyMessage = async (rootKey: string, ticket: string, message: ReplyMessage) =>
110
+ timingSafeEqual(message.signature, await signMessage(rootKey, ticket, message.id));
111
+
72
112
  export const signTokenTicket = (rootKey: string, ticket: TokenTicket) => sign(rootKey, "router:tokens", ticket);
73
113
 
74
114
  /** Whether the router signed `ticket`, and it hasn't expired. */
package/router/sync.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { hmacHex, timingSafeEqual } from "./keys.ts";
2
+
3
+ /** The header of a `POST /sync` request: `<time in milliseconds>.<signature>`. */
4
+ export const SYNC_HEADER = "x-factory-sync";
5
+
6
+ /** How far a sync request's time may be from the router's clock. */
7
+ export const SYNC_MAX_SKEW_MS = 5 * 60 * 1000;
8
+
9
+ // The signature covers the router's origin, so a captured header works only for that router.
10
+ const signature = (syncKey: string, origin: string, at: number) => hmacHex(syncKey, `sync:${at}:${origin}`);
11
+
12
+ /** The `SYNC_HEADER` value of a sync request to the router at `origin`, made at `at`. */
13
+ export const signSync = async (syncKey: string, origin: string, at = Date.now()) =>
14
+ `${at}.${await signature(syncKey, origin, at)}`;
15
+
16
+ /** Whether `header` was signed with the sync key for the router at `origin`, close enough to `now`. */
17
+ export const verifySync = async (syncKey: string, header: string, origin: string, now = Date.now()) => {
18
+ const [at, signed, ...rest] = header.split(".");
19
+ if (!at || !signed || rest.length || !/^\d+$/.test(at)) return false;
20
+ if (Math.abs(now - Number(at)) > SYNC_MAX_SKEW_MS) return false;
21
+ return timingSafeEqual(signed, await signature(syncKey, origin, Number(at)));
22
+ };