@opensoftware/foreman 0.0.0-stage → 0.2.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,348 @@
1
- # Temporary Holding Version
1
+ ---
2
+ covers: [modules/foreman/src/**, modules/foreman/scripts/**]
3
+ ---
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ # foreman
6
+
7
+ Run the factory's workflow for each work item: the stages of `factory.yaml` one after the other, each with its agent
8
+ through the harness port, with human gates, steering and cancel, on Temporal. A run survives a restart of the
9
+ foreman, and Temporal's UI shows its history. Decisions: [ADR 0067](../../docs/decisions/0067-foreman.md), on
10
+ [ADR 0004](../../docs/decisions/0004-temporal.md), [ADR 0068](../../docs/decisions/0068-foreman-sandboxes.md) for
11
+ sandboxes, and [ADR 0069](../../docs/decisions/0069-foreman-checks.md) for checks.
12
+
13
+ ## Quick start
14
+
15
+ This quick start is scenario `SCN-FORE-001` and is tested on every change. You need the
16
+ [Temporal CLI](https://docs.temporal.io/cli) for a local Temporal server and Git. Save a factory of two stages:
17
+
18
+ ```yaml factory.yaml
19
+ version: 1
20
+ factory: hello
21
+ providers:
22
+ harness:
23
+ acp: "^0"
24
+ agents:
25
+ plan:
26
+ harness: acp
27
+ model: routes/default
28
+ prompt: ./agents/plan.md
29
+ implement:
30
+ harness: acp
31
+ model: routes/default
32
+ prompt: ./agents/implement.md
33
+ workflow:
34
+ - stage: plan
35
+ - stage: implement
36
+ ```
37
+
38
+ ```markdown agents/plan.md
39
+ Write a plan for the work item into plan.md.
40
+ ```
41
+
42
+ ```markdown agents/implement.md
43
+ Implement plan.md.
44
+ ```
45
+
46
+ Commit it, start Temporal, and serve the foreman with Claude Code behind the `acp` harness adapter:
47
+
48
+ ```sh
49
+ git init -q && git add -A && git commit -qm "the hello factory"
50
+ temporal server start-dev --headless &
51
+ export FOREMAN_API_TOKEN=dev-token
52
+ export FOREMAN_HARNESSES='{"acp": "npx -y @opensoftware/harness-acp run --allow read,edit -- npx -y @agentclientprotocol/claude-agent-acp"}'
53
+ npx @opensoftware/foreman serve
54
+ ```
55
+
56
+ Save a work item, as intake publishes it:
57
+
58
+ ```json work.json
59
+ {
60
+ "id": "wi_5d1c1f0a9e7b3c2d4e6f",
61
+ "type": "idea",
62
+ "title": "Export reports as CSV",
63
+ "requester": { "kind": "person", "id": "slack:U0PERSON", "displayName": "Varun" },
64
+ "origin": { "source": "api" },
65
+ "dedupeKey": "idea:export-csv"
66
+ }
67
+ ```
68
+
69
+ Start a run and read it:
70
+
71
+ ```sh
72
+ curl -H "Authorization: Bearer dev-token" -H "content-type: application/json" \
73
+ --data @work.json http://localhost:8080/v1/runs
74
+ curl -H "Authorization: Bearer dev-token" http://localhost:8080/v1/runs/run_…
75
+ ```
76
+
77
+ The first call answers 201 with the run `run_…` in the state `running`. Once both agents ended their turns, the run is
78
+ `complete` and both stages are `succeeded`. Temporal's UI at http://localhost:8233 shows the workflow `run_…`.
79
+
80
+ ## Sandboxes
81
+
82
+ The quick start runs both agents on your machine. Run an agent you do not trust with that machine in a sandbox: give
83
+ it `sandbox` in `factory.yaml`. This factory runs both stages on Docker, each in a fresh sandbox with 2 CPUs and 4 GiB,
84
+ that may reach only npm and Anthropic's API:
85
+
86
+ ```yaml sandboxed/factory.yaml
87
+ version: 1
88
+ factory: hello
89
+ providers:
90
+ harness:
91
+ acp: "^0"
92
+ sandbox:
93
+ docker: "^0"
94
+ agents:
95
+ plan:
96
+ harness: acp
97
+ model: routes/default
98
+ prompt: ./agents/plan.md
99
+ secrets: [ref:file/anthropic-api-key]
100
+ sandbox:
101
+ template: node:24
102
+ cpu: 2
103
+ memory: 4Gi
104
+ egress: [registry.npmjs.org, api.anthropic.com]
105
+ implement:
106
+ harness: acp
107
+ model: routes/default
108
+ prompt: ./agents/implement.md
109
+ secrets: [ref:file/anthropic-api-key]
110
+ sandbox:
111
+ template: node:24
112
+ cpu: 2
113
+ memory: 4Gi
114
+ egress: [registry.npmjs.org, api.anthropic.com]
115
+ workflow:
116
+ - stage: plan
117
+ - stage: implement
118
+ ```
119
+
120
+ Put your Anthropic API key in the file `secrets/anthropic-api-key` and serve the foreman as in the quick start, with
121
+ `export OSF_SECRETS_DIR=$PWD/secrets` for the [docker sandbox adapter](../../adapters/sandbox/docker/README.md). Each
122
+ stage then goes like this:
123
+
124
+ 1. The foreman creates a sandbox with the adapter that the factory names under `providers.sandbox` (`docker` when it
125
+ names none) from the agent's `sandbox`. The spec holds each secret as a reference under a variable,
126
+ `ref:file/anthropic-api-key` as `ANTHROPIC_API_KEY`, and `OTEL_RESOURCE_ATTRIBUTES` with the run. The sandbox gets
127
+ a placeholder; its egress proxy puts the key in on the way to `api.anthropic.com`
128
+ ([sandbox port](../../ports/sandbox/README.md)). The foreman never reads a secret.
129
+ 2. It copies the run's folder into the sandbox, below the sandbox's working folder, as `<run ID>`.
130
+ 3. It runs the harness adapter's command line from `FOREMAN_HARNESSES` in the sandbox with the adapter's `exec`, and
131
+ speaks ACP to it. The template must hold that program; `node:24` holds `npx`.
132
+ 4. When the turn ended, it copies the folder back into the run's folder, so the next stage finds the files.
133
+ 5. It destroys the sandbox, also when the stage failed or was cancelled.
134
+
135
+ `FOREMAN_SANDBOXES` maps an adapter's name to its command line; an adapter it does not name runs
136
+ `npx -y @opensoftware/sandbox-<name>`. A factory names at most one sandbox adapter. An agent without `sandbox` runs on
137
+ the foreman's machine in the run's folder. A sandboxed agent reaches no MCP server yet.
138
+
139
+ ## Checks
140
+
141
+ A stage can wait for CI: give it `checks`, the names of the checks that must succeed on the run's pull request before
142
+ it starts ([ADR 0069](../../docs/decisions/0069-foreman-checks.md)). This factory merges the pull request only when
143
+ its check `verify` succeeded:
144
+
145
+ ```yaml checks/factory.yaml
146
+ version: 1
147
+ factory: hello
148
+ providers:
149
+ harness:
150
+ acp: "^0"
151
+ forge:
152
+ github: "^0"
153
+ agents:
154
+ plan:
155
+ harness: acp
156
+ model: routes/default
157
+ prompt: ./agents/plan.md
158
+ implement:
159
+ harness: acp
160
+ model: routes/default
161
+ prompt: ./agents/implement-pr.md
162
+ ship:
163
+ harness: acp
164
+ model: routes/default
165
+ prompt: ./agents/ship.md
166
+ workflow:
167
+ - stage: plan
168
+ - stage: implement
169
+ - stage: ship
170
+ checks: [verify]
171
+ ```
172
+
173
+ ```markdown agents/implement-pr.md
174
+ Implement plan.md on a new branch, push it and open a pull request. End with the pull request's link.
175
+ ```
176
+
177
+ ```markdown agents/ship.md
178
+ Merge the pull request.
179
+ ```
180
+
181
+ Serve it as in the quick start, with the forge adapter's token: `export FOREMAN_FORGE_TOKEN=…`. Then:
182
+
183
+ 1. The run's pull request is the last pull request link that an agent of an earlier stage wrote, in its messages or
184
+ in its tool calls' output: `https://<host>/<owner>/<name>/pull/<n>` (GitHub) or `…/pulls/<n>` (Forgejo). The run
185
+ shows it as `pullRequest`, every later stage's prompt ends with `Pull request: <link>`, and `run.completed@v1` lists
186
+ it in `pullRequests`.
187
+ 2. Before the stage `ship` starts, and before its gate if it has one, the foreman runs the
188
+ [forge adapter](../../ports/forge/README.md)'s `pr <owner>/<name> <n>` and reads the named checks. The stage lists
189
+ them under `checks`, each with `name` and `state`.
190
+ 3. Every check `success`: the stage starts. A check `failure` or `error`: the stage is `failed` without running its
191
+ agent, with the `reason` `the check verify ended with failure`, and the run is `failed`. A check `pending`, or one
192
+ the pull request does not have yet: the run and the stage are `blocked`, and the foreman reads the checks again
193
+ every `FOREMAN_CHECK_POLL_MS`. A cancel ends the wait.
194
+ 4. Without a pull request link, the stage is `failed` with the reason `no earlier stage linked a pull request`.
195
+
196
+ The forge adapter is the one the factory names under `providers.forge`; a factory with `checks` names exactly one.
197
+ `FOREMAN_FORGES` maps an adapter's name to its command line; an adapter it does not name runs
198
+ `npx -y @opensoftware/forge-<name>`. It gets `FORGE_URL` and `FORGE_TOKEN` from `FOREMAN_FORGE_URL` and
199
+ `FOREMAN_FORGE_TOKEN`.
200
+
201
+ ## Commands
202
+
203
+ | Command | What it does |
204
+ | --- | --- |
205
+ | `foreman serve` | Serve the run API on `PORT` and run the workflow of `FOREMAN_FACTORY` on the Temporal server at `FOREMAN_TEMPORAL_ADDRESS`; with `FOREMAN_NATS_URL`, start a run for each `work.received@v1`. |
206
+
207
+ ## API
208
+
209
+ | Route | What it does |
210
+ | --- | --- |
211
+ | `POST /v1/runs` | Start the run of a work item (the [work item](../../contracts/work-item/work-item.schema.json) format). 201 with the new run, 200 with the run the same work item started before, 400 with a problem that names the field. |
212
+ | `GET /v1/runs/{runId}` | The run: `state`, `factory`, `factoryVersion`, `pullRequest`, `startedAt`, `endedAt` and its `stages`. 404 for a run the server does not know. |
213
+ | `POST /v1/runs/{runId}/signals` | `{ "type": "steer", "message": "…" }` sends the message to the running stage's agent; `{ "type": "cancel" }` cancels the run. 202 with the run, 409 when the run ended. |
214
+
215
+ The OpenAPI of the routes is in [`contracts/openapi/openapi.json`](../../contracts/openapi/openapi.json); the
216
+ TypeScript type `paths` of `@opensoftware/contracts` types a client. Every route needs `Authorization: Bearer
217
+ <FOREMAN_API_TOKEN>`, else 401. Errors are `application/problem+json` with `title`, `status` and `detail`.
218
+
219
+ A run's ID is `run_` and 20 hex digits of the SHA-256 of the work item's ID, so a work item has one run. A run is
220
+ `running`, `blocked` (at a gate or waiting for checks), `complete`, `failed` or `cancelled`. A stage is `pending`,
221
+ `blocked`, `running`, `succeeded`, `failed`, `rejected` (by its gate) or `cancelled`; it has its harness `sessionId` once
222
+ it started, its `gateId` once its gate was asked, its `checks` once they were read, and a `reason` when it failed for a
223
+ reason the foreman knows.
224
+
225
+ ## How a run goes
226
+
227
+ - **The factory.** `foreman serve` checks `FOREMAN_FACTORY` against the
228
+ [factory definition](../../contracts/factory-definition/factory-definition.schema.json) when it starts, and that each
229
+ stage's agent is declared; a wrong file exits 2 with the path in the file. Each run reads the file again when it
230
+ starts and keeps that version until it ends. Its `factoryVersion` is the commit of the repository that holds the
231
+ file.
232
+ - **Stages.** The stages run in the order of `workflow`, each in the run's folder `OSF_DATA/runs/<run ID>`, or in a
233
+ sandbox when the stage's agent declares one ([Sandboxes](#sandboxes)). The foreman
234
+ starts the stage's harness adapter from `FOREMAN_HARNESSES`, speaks ACP to it, opens a session and sends one prompt:
235
+ the agent's prompt file, then the work item's ID, title and description. It allows the tool calls the adapter asks
236
+ about; limit them with the adapter's `--allow`. A turn that ends with `end_turn` succeeds the stage; any other end
237
+ fails or cancels it, ends the run, and leaves the later stages `pending`. The adapter gets
238
+ `OTEL_RESOURCE_ATTRIBUTES` with `osf.run.id`, `osf.work_item.id`, `osf.stage` and `osf.factory.version`.
239
+ - **Checks.** Before a stage with `checks` starts, the foreman waits until those checks succeeded on the run's pull
240
+ request; a failing one fails the run ([Checks](#checks)).
241
+ - **Gates.** Before a stage with a `gate` starts, the foreman asks gatekeeper at `FOREMAN_GATEKEEPER_URL`
242
+ ([gatekeeper](../gatekeeper/README.md#server)): the work item's ID, the stage, the summary `<title>: start the
243
+ stage <stage>`, the origin's link, and the gate's `policy` and `approvers` in `change`. The run is `blocked` until a
244
+ `gate.decided@v1` for that gate arrives on NATS. `approved` starts the stage; `rejected` or `expired` fails the run.
245
+ - **Signals.** A steering message reaches the running stage's agent within `FOREMAN_POLL_MS` as a prompt in its
246
+ session, which the harness port treats as steering. A cancel ends the agent's turn, or the wait at a gate, and the
247
+ run is `cancelled`.
248
+ - **Restarts.** The runs live in Temporal's history. A foreman that stops and starts again goes on with them; a stage
249
+ that ran when it stopped runs again, up to 3 times.
250
+
251
+ ## Events
252
+
253
+ With `FOREMAN_NATS_URL`, the foreman starts a run for each [`work.received@v1`](../../contracts/events/work-received/work-received.schema.json)
254
+ on `osf.work.received.v1`, and hands each [`gate.decided@v1`](../../contracts/events/gate-decided/gate-decided.schema.json)
255
+ on `osf.gate.decided.v1` to the run of its work item. A run publishes
256
+ [`run.started@v1`](../../contracts/events/run-started/run-started.schema.json) on `osf.run.started.v1` when it starts and
257
+ [`run.completed@v1`](../../contracts/events/run-completed/run-completed.schema.json) on `osf.run.completed.v1` when it
258
+ ends, with the outcome `succeeded`, `failed` or `cancelled`. Their IDs are the run's ID with `.started` and
259
+ `.completed`. The subjects are core NATS: work published while no foreman listens is not started.
260
+
261
+ ## Releases
262
+
263
+ release-please opens a release pull request from the Conventional Commits under `modules/foreman/`. Merging it creates
264
+ the tag `foreman-v<version>`. The workflow `.github/workflows/publish-foreman.yml` then builds the package and
265
+ publishes it to npm as `@opensoftware/foreman`. The first release is `0.1.0`; until `1.0.0`, a breaking change raises
266
+ the minor version. The one-time setup is the same as for contracts
267
+ ([`contracts/README.md`](../../contracts/README.md#releases)).
268
+
269
+ ## Configuration
270
+
271
+ <!-- generated by make docs from src/config.schema.json: do not edit -->
272
+
273
+ | Variable | Type | Default | Meaning |
274
+ | --- | --- | --- | --- |
275
+ | `PORT` | string | `"8080"` | serve: the port the HTTP server listens on. |
276
+ | `OSF_DATA` | string | `".foreman"` | serve: the folder of the runs' working folders, runs/<run ID>, and of the harness adapters' session records. |
277
+ | `OSF_NOW` | string | | The current time, like 2026-01-01T00:00:00Z, for the times of runs and stages. Default: the clock. |
278
+ | `FOREMAN_API_TOKEN` | string | | serve: the token that callers of /v1 send as Authorization: Bearer <token>. Required. |
279
+ | `FOREMAN_FACTORY` | string | `"factory.yaml"` | serve: the factory definition. Its folder must be in a Git repository: a run's factory version is its commit. |
280
+ | `FOREMAN_TEMPORAL_ADDRESS` | string | `"localhost:7233"` | serve: the gRPC address of the Temporal server. |
281
+ | `FOREMAN_TEMPORAL_NAMESPACE` | string | `"default"` | serve: the Temporal namespace of the runs. |
282
+ | `FOREMAN_TASK_QUEUE` | string | `"foreman"` | serve: the Temporal task queue of the runs' workflows and activities. |
283
+ | `FOREMAN_HARNESSES` | string | `"{}"` | serve: a JSON object from harness name to the command line of its adapter. A harness it does not name runs npx -y @opensoftware/harness-<name> run. |
284
+ | `FOREMAN_SANDBOXES` | string | `"{}"` | serve: a JSON object from sandbox adapter name to the command line of the adapter. An adapter it does not name runs npx -y @opensoftware/sandbox-<name>. The factory names its adapter under providers.sandbox, docker by default. |
285
+ | `FOREMAN_FORGES` | string | `"{}"` | serve: a JSON object from forge adapter name to the command line of the adapter. An adapter it does not name runs npx -y @opensoftware/forge-<name>. The factory names its adapter under providers.forge. |
286
+ | `FOREMAN_FORGE_URL` | string | | serve: the forge adapter's FORGE_URL, for a factory whose stages have checks. Default: the adapter's own default. |
287
+ | `FOREMAN_FORGE_TOKEN` | string | | serve: the forge adapter's FORGE_TOKEN: a token that may read the pull requests and their checks. Never written to a file. |
288
+ | `FOREMAN_CHECK_POLL_MS` | string | `"30000"` | serve: how long a stage that waits for pending checks waits before it reads them again, in milliseconds. |
289
+ | `FOREMAN_POLL_MS` | string | `"1000"` | serve: how often a running stage looks for steering messages and a cancel, in milliseconds. |
290
+ | `FOREMAN_GATEKEEPER_URL` | string | | serve: the URL of gatekeeper serve, which a stage with a gate asks for a decision. Required for a factory with gates. |
291
+ | `FOREMAN_GATEKEEPER_TOKEN` | string | | serve: gatekeeper's API token, GATEKEEPER_API_TOKEN. |
292
+ | `FOREMAN_NATS_URL` | string | | serve: the NATS server for work.received@v1 and gate.decided@v1 in, and run.started@v1 and run.completed@v1 out. Unset: no events. |
293
+
294
+ ## Scenarios
295
+
296
+ <!-- generated by make docs from features/: do not edit -->
297
+
298
+ | ID | Status | Scenario |
299
+ | --- | --- | --- |
300
+ | SCN-FORE-001 | implemented | A program starts a run of the README's factory and it completes |
301
+ | SCN-FORE-002 | implemented | Each stage's agent gets its prompt and the work item |
302
+ | SCN-FORE-003 | implemented | The same work item again is the same run |
303
+ | SCN-FORE-004 | implemented | A stage whose turn ends with "refusal" fails the run |
304
+ | SCN-FORE-005 | implemented | A run the server does not know is 404 |
305
+ | SCN-FORE-006 | implemented | A call without the API token is refused |
306
+ | SCN-FORE-007 | implemented | A work item without a title is refused |
307
+ | SCN-FORE-008 | implemented | A run carries the commit of the factory definition |
308
+ | SCN-FORE-009 | implemented | A run keeps its factory version when the factory changes during the run |
309
+ | SCN-FORE-010 | implemented | A stage that names an unknown agent stops the server at the start |
310
+ | SCN-FORE-011 | implemented | The server needs an API token |
311
+ | SCN-FORE-012 | implemented | A stage with a gate asks gatekeeper and the run waits |
312
+ | SCN-FORE-013 | implemented | An approval of the gate starts the stage |
313
+ | SCN-FORE-014 | implemented | A rejection of the gate fails the run |
314
+ | SCN-FORE-015 | implemented | A decision for another gate does not start the stage |
315
+ | SCN-FORE-016 | implemented | A steering message reaches the running stage's agent |
316
+ | SCN-FORE-017 | implemented | A cancel ends the running stage's turn and the run |
317
+ | SCN-FORE-018 | implemented | A cancel ends a run that waits at a gate |
318
+ | SCN-FORE-019 | implemented | A signal to a run that ended is refused |
319
+ | SCN-FORE-020 | implemented | An unknown signal is refused |
320
+ | SCN-FORE-021 | implemented | A run goes on from its history after the foreman restarts |
321
+ | SCN-FORE-022 | implemented | A work item from intake starts a run |
322
+ | SCN-FORE-023 | implemented | A run that ends publishes its outcome and its factory version |
323
+ | SCN-FORE-024 | implemented | The foreman allows a tool call that the stage's agent asks about |
324
+ | SCN-FORE-025 | implemented | A stage whose harness does not start fails the run |
325
+ | SCN-FORE-026 | implemented | A factory outside a Git repository stops the server at the start |
326
+ | SCN-FORE-027 | implemented | An event without a valid work item starts no run |
327
+ | SCN-FORE-028 | implemented | A stage's harness session carries the run |
328
+ | SCN-FORE-029 | implemented | A run that fails publishes the outcome "failed" and the stage that failed |
329
+ | SCN-FORE-030 | implemented | A cancelled run publishes the outcome "cancelled" |
330
+ | SCN-FORE-031 | implemented | Two steering messages reach the agent once each, in order |
331
+ | SCN-FORE-032 | implemented | A stage whose agent declares a sandbox runs in a sandbox created from it |
332
+ | SCN-FORE-033 | implemented | The sandbox spec holds the agent's secret as a reference under its variable |
333
+ | SCN-FORE-034 | implemented | A stage's sandbox carries the run in its environment |
334
+ | SCN-FORE-035 | implemented | A file a stage writes in its sandbox reaches the run's folder and the next stage's sandbox |
335
+ | SCN-FORE-036 | implemented | Each stage's sandbox is destroyed when the stage ends |
336
+ | SCN-FORE-037 | implemented | A cancel destroys the sandbox of the running stage |
337
+ | SCN-FORE-038 | implemented | A sandbox the adapter cannot create fails the run |
338
+ | SCN-FORE-039 | implemented | The sandbox adapter is the one the factory names under "providers.sandbox" |
339
+ | SCN-FORE-040 | implemented | A factory that names two sandbox adapters stops the server at the start |
340
+ | SCN-FORE-041 | implemented | A stage whose agent declares no sandbox runs on the foreman's machine |
341
+ | SCN-FORE-042 | implemented | A failing check stops the stage that waits for it |
342
+ | SCN-FORE-043 | implemented | A stage starts when its checks succeeded |
343
+ | SCN-FORE-044 | implemented | A pending check holds the stage until it succeeds |
344
+ | SCN-FORE-045 | implemented | A check the pull request does not have yet holds the stage |
345
+ | SCN-FORE-046 | implemented | A stage with checks fails when no earlier stage linked a pull request |
346
+ | SCN-FORE-047 | implemented | A run shows its pull request and publishes it when it ends |
347
+ | SCN-FORE-048 | implemented | A cancel ends a run that waits for a check |
348
+ | SCN-FORE-049 | implemented | A factory with checks but without a forge adapter stops the server at the start |
@@ -0,0 +1,33 @@
1
+ // Reads the factory (ADR 0067): factory.yaml, checked against the contract and for its stages' agents, the text of
2
+ // each agent's prompt, and the factory version, the commit of the folder that holds the file.
3
+ import { execFile } from "node:child_process";
4
+ import { readFileSync } from "node:fs";
5
+ import { basename, dirname, resolve } from "node:path";
6
+ import { promisify } from "node:util";
7
+ import { UsageError } from "@opensoftware/cli";
8
+ import { validate } from "@opensoftware/contracts";
9
+ import { parse } from "yaml";
10
+ import { definitionOf, stageProblems } from "../domain/definition.js";
11
+ /** The factory at `path`; a file that cannot run is a usage error that names the file, the path in it and why. */
12
+ export async function readFactory(path) {
13
+ const file = resolve(path);
14
+ const name = basename(file);
15
+ const document = parse(readFileSync(file, "utf8"));
16
+ const contract = validate("factory-definition", document);
17
+ const problems = contract.length ? contract : stageProblems(document);
18
+ if (problems.length)
19
+ throw new UsageError(problems.map((problem) => `${name}: ${problem.path}: ${problem.message}`).join("\n"));
20
+ const folder = dirname(file);
21
+ const read = (prompt) => readFileSync(resolve(folder, prompt), "utf8");
22
+ return { definition: definitionOf(document, read), version: await commitOf(folder, name) };
23
+ }
24
+ async function commitOf(folder, name) {
25
+ try {
26
+ const { stdout } = await promisify(execFile)("git", ["rev-parse", "HEAD"], { cwd: folder });
27
+ return stdout.trim();
28
+ }
29
+ catch {
30
+ throw new UsageError(`${name}: commit it in a Git repository; a run's factory version is that commit`);
31
+ }
32
+ }
33
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/adapters/factory.ts"],"names":[],"mappings":"AAAA,mHAAmH;AACnH,8FAA8F;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAA0B,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAmB,YAAY,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAQvF,kHAAkH;AAClH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAY;IAC5C,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,QAAQ,GAAY,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAC5D,MAAM,QAAQ,GAAG,QAAQ,CAAC,oBAAoB,EAAE,QAAQ,CAAC,CAAC;IAC1D,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,QAA6B,CAAC,CAAC;IAC3F,IAAI,QAAQ,CAAC,MAAM;QACjB,MAAM,IAAI,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7G,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;IAC/E,OAAO,EAAE,UAAU,EAAE,YAAY,CAAC,QAA6B,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC;AAClH,CAAC;AAED,KAAK,UAAU,QAAQ,CAAC,MAAc,EAAE,IAAY;IAClD,IAAI,CAAC;QACH,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;QAC5F,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,yEAAyE,CAAC,CAAC;IACzG,CAAC;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ // The run's pull request through the forge port (ADR 0057, ADR 0069). The adapter is a program; the foreman runs its
2
+ // `pr` command and reads the checks of the pull request's head commit.
3
+ import { execFile } from "node:child_process";
4
+ /** The checks of the pull request `<repo>#<number>`; a failure names the adapter's error. */
5
+ export function pullRequestChecks(adapter, env, repo, number) {
6
+ const [file, ...args] = adapter;
7
+ return new Promise((resolve, reject) => {
8
+ execFile(file, [...args, "pr", repo, number], { env }, (error, stdout, stderr) => {
9
+ if (error)
10
+ reject(new Error(`the forge adapter's pr failed: ${stderr.trim() || error.message}`));
11
+ else
12
+ resolve(JSON.parse(stdout).checks ?? []);
13
+ });
14
+ });
15
+ }
16
+ //# sourceMappingURL=forge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"forge.js","sourceRoot":"","sources":["../../src/adapters/forge.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,uEAAuE;AACvE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAQ9C,6FAA6F;AAC7F,MAAM,UAAU,iBAAiB,CAC/B,OAAiB,EACjB,GAAuC,EACvC,IAAY,EACZ,MAAc;IAEd,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,OAAgC,CAAC;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YAC/E,IAAI,KAAK;gBAAE,MAAM,CAAC,IAAI,KAAK,CAAC,kCAAkC,MAAM,CAAC,IAAI,EAAE,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;;gBAC5F,OAAO,CAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAiC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;QACjF,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,18 @@
1
+ export function gatekeeper(url, token) {
2
+ return {
3
+ async request(request) {
4
+ if (!url)
5
+ throw new Error("set FOREMAN_GATEKEEPER_URL: a stage has a gate");
6
+ const response = await fetch(new URL("/v1/gates", url), {
7
+ method: "POST",
8
+ headers: { "content-type": "application/json", ...(token ? { authorization: `Bearer ${token}` } : {}) },
9
+ body: JSON.stringify(request),
10
+ });
11
+ const body = await response.text();
12
+ if (!response.ok)
13
+ throw new Error(`gatekeeper answered ${response.status} to the gate request: ${body}`);
14
+ return JSON.parse(body);
15
+ },
16
+ };
17
+ }
18
+ //# sourceMappingURL=gatekeeper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gatekeeper.js","sourceRoot":"","sources":["../../src/adapters/gatekeeper.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,UAAU,CAAC,GAAuB,EAAE,KAAyB;IAC3E,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,OAAO;YACnB,IAAI,CAAC,GAAG;gBAAE,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;YAC5E,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,IAAI,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE;gBACtD,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE;gBACvG,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;aAC9B,CAAC,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,IAAI,CAAC,QAAQ,CAAC,EAAE;gBAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,QAAQ,CAAC,MAAM,yBAAyB,IAAI,EAAE,CAAC,CAAC;YACzG,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAa,CAAC;QACtC,CAAC;KACF,CAAC;AACJ,CAAC"}