@oakgreencc/cambium 0.0.9 → 0.0.11
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/README.md +107 -8
- package/package.json +2 -2
- package/src/config.mjs +17 -3
- package/src/dispatch-engine.mjs +135 -0
- package/src/github.mjs +140 -19
- package/src/graph-engine.mjs +17 -3
- package/src/graphs/dispatch.mjs +453 -0
- package/src/graphs/release.mjs +438 -0
- package/src/labels.mjs +1 -0
- package/src/marker.mjs +3 -1
- package/src/phases/dispatch.mjs +145 -0
- package/src/phases/release.mjs +1 -0
- package/src/phases/task.mjs +8 -1
- package/src/tick.mjs +8 -4
- package/src/workers.mjs +86 -18
package/src/tick.mjs
CHANGED
|
@@ -14,6 +14,7 @@ import { prFor, taskExit, taskPrompt } from './phases/task.mjs'
|
|
|
14
14
|
import { branchName } from './convention.mjs'
|
|
15
15
|
import { PREPARE_FAILED_LINE } from './prepare.mjs'
|
|
16
16
|
import { releaseDue, releaseNotBuilt } from './phases/release.mjs'
|
|
17
|
+
import { releaseGraphs } from './graphs/release.mjs'
|
|
17
18
|
import { DISPATCH_NOT_BUILT, dispatchNotBuilt } from './phases/dispatch.mjs'
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -46,9 +47,10 @@ import { DISPATCH_NOT_BUILT, dispatchNotBuilt } from './phases/dispatch.mjs'
|
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* @typedef {(deps: Deps, ctx: Ctx) => Promise<void>} StepFn
|
|
49
|
-
* @typedef {{ reconcile: StepFn, watch: StepFn, sweep: StepFn, scheduleTasks: StepFn }} Engine
|
|
50
|
+
* @typedef {{ reconcile: StepFn, watch: StepFn, sweep: StepFn, scheduleTasks: StepFn, scheduleDispatch?: StepFn }} Engine
|
|
50
51
|
* The Task-phase steps. `config.engine` picks the legacy stage tick (this file) or the `task` graph
|
|
51
|
-
* (graph-engine.mjs)
|
|
52
|
+
* (graph-engine.mjs). The graph engine also folds the Dispatch phase into reconcile and sweep and brings its own
|
|
53
|
+
* `scheduleDispatch` (without one, the dispatch stub runs). Read and Heartbeat are shared.
|
|
52
54
|
*/
|
|
53
55
|
|
|
54
56
|
/** @param {Deps} deps @returns {Ctx} */
|
|
@@ -74,7 +76,7 @@ export async function tick(deps) {
|
|
|
74
76
|
/** @type {StepFn} */
|
|
75
77
|
const schedule = async (d, c) => {
|
|
76
78
|
await engine.scheduleTasks(d, c)
|
|
77
|
-
await scheduleDispatch(d, c)
|
|
79
|
+
await (engine.scheduleDispatch ?? scheduleDispatch)(d, c)
|
|
78
80
|
}
|
|
79
81
|
/** @type {[string, StepFn][]} */
|
|
80
82
|
const steps = [
|
|
@@ -560,10 +562,12 @@ export async function startTask(deps, ctx, slug, issue, snap, { foreground = fal
|
|
|
560
562
|
return { ...started, attempt: marker.attempt }
|
|
561
563
|
}
|
|
562
564
|
|
|
563
|
-
// ---- Release (
|
|
565
|
+
// ---- Release (no agent). Legacy: once a day, the run is a seam. Graph: the release-prepare / release-promote
|
|
566
|
+
// graphs (graphs/release.mjs), per repo.
|
|
564
567
|
|
|
565
568
|
/** @param {Deps} deps @param {Ctx} ctx */
|
|
566
569
|
async function release(deps, ctx) {
|
|
570
|
+
if (deps.config.engine === 'graph') return releaseGraphs(deps, ctx)
|
|
567
571
|
const { config, store } = deps
|
|
568
572
|
if (!config.release.enabled) return
|
|
569
573
|
const last = store.get('release.lastRun')
|
package/src/workers.mjs
CHANGED
|
@@ -28,7 +28,10 @@ import { exec as defaultExec } from './github.mjs'
|
|
|
28
28
|
* @typedef {{
|
|
29
29
|
* repo: import('./config.mjs').Repo, convention: Convention, issue: number, attempt: number, branch: string,
|
|
30
30
|
* phase: string, maxTurns: number, buildPrompt: (agentsMd: string | null) => string, foreground?: boolean,
|
|
31
|
+
* readOnly?: boolean,
|
|
31
32
|
* }} StartSpec
|
|
33
|
+
* `readOnly`: a disposable checkout for a worker that only reads (triage): detached at the base branch, no
|
|
34
|
+
* install, hydrate or prepare, no Edit/Write; `branch` only names its directory (see {@link topicOf}).
|
|
32
35
|
* @typedef {{
|
|
33
36
|
* pid: number, worktree: string, stdout: string, stderr: string, prompt: string, exited: Promise<number | null>,
|
|
34
37
|
* reused: string | null,
|
|
@@ -37,8 +40,9 @@ import { exec as defaultExec } from './github.mjs'
|
|
|
37
40
|
* @typedef {{
|
|
38
41
|
* convention(repo: import('./config.mjs').Repo): Convention,
|
|
39
42
|
* start(spec: StartSpec): Promise<Started>,
|
|
40
|
-
* cleanup(w: { repo: import('./config.mjs').Repo, worktree: string, branch: string }): Promise<Teardown>,
|
|
43
|
+
* cleanup(w: { repo: import('./config.mjs').Repo, worktree: string, branch: string, readOnly?: boolean }): Promise<Teardown>,
|
|
41
44
|
* tail(path: string, lines?: number): string,
|
|
45
|
+
* readLog(path: string): string,
|
|
42
46
|
* }} Spawner
|
|
43
47
|
* @typedef {{ alive(pid: number): boolean, kill(pid: number): void }} Procs
|
|
44
48
|
*/
|
|
@@ -47,8 +51,26 @@ import { exec as defaultExec } from './github.mjs'
|
|
|
47
51
|
export const WORKER_TOOLS = ['Read', 'Edit', 'Write', 'Glob', 'Grep', 'Bash']
|
|
48
52
|
export const WORKER_DENIED = ['Bash(graft issue label:*)', 'Bash(graft issue edit:*)', 'Bash(gh:*)']
|
|
49
53
|
|
|
50
|
-
/**
|
|
51
|
-
export const
|
|
54
|
+
/** A read-only worker's tools: it reads code and issues; cambium makes every write on its verdict. */
|
|
55
|
+
export const READONLY_TOOLS = ['Read', 'Glob', 'Grep', 'Bash']
|
|
56
|
+
export const READONLY_DENIED = [
|
|
57
|
+
...WORKER_DENIED,
|
|
58
|
+
'Edit',
|
|
59
|
+
'Write',
|
|
60
|
+
'NotebookEdit',
|
|
61
|
+
'Bash(graft publish:*)',
|
|
62
|
+
'Bash(graft merge:*)',
|
|
63
|
+
'Bash(graft pr:*)',
|
|
64
|
+
'Bash(graft issue create:*)',
|
|
65
|
+
'Bash(graft issue comment:*)',
|
|
66
|
+
'Bash(graft issue close:*)',
|
|
67
|
+
'Bash(graft issue assign:*)',
|
|
68
|
+
'Bash(git commit:*)',
|
|
69
|
+
'Bash(git push:*)',
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
/** @param {import('./config.mjs').Config} config @param {number} maxTurns @param {{ readOnly?: boolean }} [opts] */
|
|
73
|
+
export const workerArgs = (config, maxTurns, { readOnly = false } = {}) => [
|
|
52
74
|
'-p',
|
|
53
75
|
'--output-format',
|
|
54
76
|
'stream-json',
|
|
@@ -56,9 +78,9 @@ export const workerArgs = (config, maxTurns) => [
|
|
|
56
78
|
'--max-turns',
|
|
57
79
|
String(maxTurns),
|
|
58
80
|
'--allowedTools',
|
|
59
|
-
WORKER_TOOLS.join(','),
|
|
81
|
+
(readOnly ? READONLY_TOOLS : WORKER_TOOLS).join(','),
|
|
60
82
|
'--disallowedTools',
|
|
61
|
-
WORKER_DENIED.join(','),
|
|
83
|
+
(readOnly ? READONLY_DENIED : WORKER_DENIED).join(','),
|
|
62
84
|
...config.worker.args,
|
|
63
85
|
]
|
|
64
86
|
|
|
@@ -104,6 +126,15 @@ export function tailFile(path, lines = 30) {
|
|
|
104
126
|
}
|
|
105
127
|
}
|
|
106
128
|
|
|
129
|
+
/** A whole log file, or '' when it cannot be read. @param {string} path */
|
|
130
|
+
export function readLogFile(path) {
|
|
131
|
+
try {
|
|
132
|
+
return readFileSync(path, 'utf8')
|
|
133
|
+
} catch {
|
|
134
|
+
return ''
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
107
138
|
/**
|
|
108
139
|
* @param {{ config: import('./config.mjs').Config, exec?: import('./github.mjs').Exec,
|
|
109
140
|
* spawnFn?: typeof spawn, stamp?: () => string }} opts
|
|
@@ -125,7 +156,8 @@ export function processSpawner({
|
|
|
125
156
|
* @param {{ repo: import('./config.mjs').Repo, worktree: string, branch: string }} w
|
|
126
157
|
* @returns {Promise<Teardown>}
|
|
127
158
|
*/
|
|
128
|
-
async function teardown({ repo, worktree, branch }) {
|
|
159
|
+
async function teardown({ repo, worktree, branch, readOnly = false }) {
|
|
160
|
+
if (readOnly) return dispose(repo, worktree)
|
|
129
161
|
if (!existsSync(worktree)) {
|
|
130
162
|
await git(repo.path, ['worktree', 'prune']).catch(() => {})
|
|
131
163
|
return { removed: true, reason: null }
|
|
@@ -147,21 +179,52 @@ export function processSpawner({
|
|
|
147
179
|
return { removed: true, reason: null }
|
|
148
180
|
}
|
|
149
181
|
|
|
182
|
+
/**
|
|
183
|
+
* A read-only checkout holds nothing of value: it is removed outright, never checked with `graft status`.
|
|
184
|
+
* @param {import('./config.mjs').Repo} repo @param {string} worktree @returns {Promise<Teardown>}
|
|
185
|
+
*/
|
|
186
|
+
async function dispose(repo, worktree) {
|
|
187
|
+
if (existsSync(join(worktree, '.git'))) await git(repo.path, ['worktree', 'remove', '--force', worktree])
|
|
188
|
+
else {
|
|
189
|
+
rmSync(worktree, { recursive: true, force: true })
|
|
190
|
+
await git(repo.path, ['worktree', 'prune']).catch(() => {})
|
|
191
|
+
}
|
|
192
|
+
return { removed: true, reason: null }
|
|
193
|
+
}
|
|
194
|
+
|
|
150
195
|
return {
|
|
151
196
|
convention: (repo) => readConvention(repo.path),
|
|
152
|
-
async start({
|
|
197
|
+
async start({
|
|
198
|
+
repo,
|
|
199
|
+
convention,
|
|
200
|
+
issue,
|
|
201
|
+
attempt,
|
|
202
|
+
branch,
|
|
203
|
+
phase,
|
|
204
|
+
maxTurns,
|
|
205
|
+
buildPrompt,
|
|
206
|
+
foreground = false,
|
|
207
|
+
readOnly = false,
|
|
208
|
+
}) {
|
|
153
209
|
const worktree = join(convention.root, topicOf(branch))
|
|
154
210
|
if (config.worker.fetch) await git(repo.path, ['fetch', 'origin', repo.baseBranch])
|
|
155
211
|
/** @type {string | null} */
|
|
156
212
|
let reused = null
|
|
213
|
+
if (readOnly) await dispose(repo, worktree)
|
|
157
214
|
// The same topic every attempt: a leftover is removed if GitHub holds it all, else continued in.
|
|
158
|
-
if (existsSync(join(worktree, '.git'))) {
|
|
215
|
+
else if (existsSync(join(worktree, '.git'))) {
|
|
159
216
|
const left = await teardown({ repo, worktree, branch })
|
|
160
217
|
if (!left.removed) reused = left.reason
|
|
161
218
|
} else rmSync(worktree, { recursive: true, force: true })
|
|
162
219
|
if (!reused) {
|
|
163
220
|
mkdirSync(dirname(worktree), { recursive: true })
|
|
164
|
-
|
|
221
|
+
const at = `origin/${repo.baseBranch}`
|
|
222
|
+
await git(
|
|
223
|
+
repo.path,
|
|
224
|
+
readOnly
|
|
225
|
+
? ['worktree', 'add', '--detach', worktree, at]
|
|
226
|
+
: ['worktree', 'add', '--no-track', '-B', branch, worktree, at],
|
|
227
|
+
)
|
|
165
228
|
}
|
|
166
229
|
const agentsPath = join(worktree, 'AGENTS.md')
|
|
167
230
|
const agentsMd = existsSync(agentsPath) ? readFileSync(agentsPath, 'utf8') : null
|
|
@@ -179,14 +242,18 @@ export function processSpawner({
|
|
|
179
242
|
CAMBIUM_REPO: repo.slug,
|
|
180
243
|
CAMBIUM_ISSUE: String(issue),
|
|
181
244
|
CAMBIUM_PHASE: phase,
|
|
182
|
-
CAMBIUM_BRANCH: branch,
|
|
183
|
-
CAMBIUM_PREPARE: JSON.stringify(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
245
|
+
...(readOnly ? {} : { CAMBIUM_BRANCH: branch }),
|
|
246
|
+
CAMBIUM_PREPARE: JSON.stringify(
|
|
247
|
+
readOnly
|
|
248
|
+
? { worktree, source: repo.path, install: null, hydrate: { dirs: [], files: [] }, prepare: [] }
|
|
249
|
+
: {
|
|
250
|
+
worktree,
|
|
251
|
+
source: repo.path,
|
|
252
|
+
install: convention.install,
|
|
253
|
+
hydrate: convention.hydrate,
|
|
254
|
+
prepare: convention.prepare,
|
|
255
|
+
},
|
|
256
|
+
),
|
|
190
257
|
...(config.worker.graftApp ? { GRAFT_APP: config.worker.graftApp } : {}),
|
|
191
258
|
}
|
|
192
259
|
/** @type {import('node:child_process').ChildProcess} */
|
|
@@ -194,7 +261,7 @@ export function processSpawner({
|
|
|
194
261
|
try {
|
|
195
262
|
child = spawnFn(
|
|
196
263
|
process.execPath,
|
|
197
|
-
[PREPARE_MAIN, '--', config.worker.command, ...workerArgs(config, maxTurns)],
|
|
264
|
+
[PREPARE_MAIN, '--', config.worker.command, ...workerArgs(config, maxTurns, { readOnly })],
|
|
198
265
|
{ cwd: worktree, detached: !foreground, stdio: [fds[0], fds[1], fds[2]], env },
|
|
199
266
|
)
|
|
200
267
|
} finally {
|
|
@@ -210,6 +277,7 @@ export function processSpawner({
|
|
|
210
277
|
},
|
|
211
278
|
cleanup: teardown,
|
|
212
279
|
tail: tailFile,
|
|
280
|
+
readLog: readLogFile,
|
|
213
281
|
}
|
|
214
282
|
}
|
|
215
283
|
|