@oakgreencc/cambium 0.0.12 → 0.0.14

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 CHANGED
@@ -115,6 +115,7 @@ One JSON file. Everything but `repos` is optional; these are the defaults:
115
115
  }
116
116
  ```
117
117
 
118
+ - The daemon re-reads the file before every tick, so caps, limits, `tickSeconds`, `repos`, priority labels, `release` and `worker` take effect without a restart (logged as `config reloaded: <keys>`). `host`, `engine`, `paths`, `logs` and `graft` are baked in at start: a change to them logs `restart cambium to apply` once and is not applied. A file that does not load, or has no `repos`, keeps the running config and logs one warning until it is fixed.
118
119
  - `repos[].path` is a local clone; worktrees are added from it and `graft` runs in it.
119
120
  - `graft.app` is the App cambium acts as (`GRAFT_APP` for its own `graft` calls); `worker.graftApp` the one workers act as. Neither leaks into the other.
120
121
  - `engine` picks how the Task phase runs: `"legacy"` (the stage tick, the default) or `"graph"` (the [`task` graph](#the-task-graph)). Both make the same moves; see [The task graph](#the-task-graph). Only `"graph"` runs the [`dispatch` graph](#the-dispatch-graph) (gated by `phases.dispatch.cap`); legacy leaves `q:intake` alone.
@@ -136,7 +137,7 @@ How a repo's worktrees are made is the repo's call: a `"cambium"` key in its roo
136
137
  ```
137
138
 
138
139
  - **Branch**: `{issue}` (required) and `{slug}`, the issue title in kebab case, at most 40 characters cut at a word. A retry keeps the branch its first attempt chose (it is in the marker), and an existing PR's branch wins over both.
139
- - **Worktree**: `<repos[].path>/<root>/<topic>`, the topic being the branch less its first segment (`agent/12-fix-the-tick` → `12-fix-the-tick`), so every attempt of an issue uses the same directory. Created with `git fetch origin <baseBranch>`, then `git worktree add --no-track -B <branch> <dir> origin/<baseBranch>`: always from the fetched remote branch, never a local one. Add `<root>` to the repo's `.gitignore`.
140
+ - **Worktree**: `<repos[].path>/<root>/<topic>`, the topic being the branch less its first segment (`agent/12-fix-the-tick` → `12-fix-the-tick`), so every attempt of an issue uses the same directory. Created with `git fetch origin <baseBranch>`, then `git worktree add --no-track -B <branch> <dir> origin/<baseBranch>`: always from the fetched remote branch, never a local one. Between the two, the central checkout is fast-forwarded to `origin/<baseBranch>` when it sits on that branch with no tracked changes, and the convention re-read, so install, hydrate and prepare come from current content; otherwise it is left as it is. Add `<root>` to the repo's `.gitignore`.
140
141
  - **Teardown**: once the worker is reconciled, `graft status <branch> -C <dir>` (as `graft.app`) must end with `nothing local that GitHub does not have`; then the worktree and its local branch are removed. Anything else (local commits, uncommitted paths, graft failing) keeps it and logs why.
141
142
  - **Retry**: a leftover worktree in the topic directory is removed under the same rule and re-created fresh; one that still holds local-only work is continued in instead (logged as `continuing in the previous worktree`), so nothing is lost.
142
143
  - **Prepare**, after the worktree is created and before the worker starts, in order:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/cambium",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "The scheduler daemon: reads the q:* queues from GitHub every tick, runs headless claude workers in their own worktrees up to a cap, and makes every queue transition itself.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
package/src/cli.mjs CHANGED
@@ -73,7 +73,7 @@ export async function main(argv, io) {
73
73
  if (!config.repos.length) return fail(io, `no repos in ${cfgPath}; see the README for the config file`)
74
74
  const deps = depsFor(config)
75
75
  acquireLock(deps.store, deps.procs, deps.pid ?? process.pid)
76
- await runDaemon(deps, { signal: io.signal, once: has(rest, '--once') })
76
+ await runDaemon(deps, { signal: io.signal, once: has(rest, '--once'), reload: () => loadConfig(cfgPath) })
77
77
  return 0
78
78
  }
79
79
  case 'status':
package/src/config.mjs CHANGED
@@ -109,6 +109,40 @@ export function resolveConfig(raw = {}) {
109
109
  }
110
110
 
111
111
  /** @param {string} path */
112
+ /**
113
+ * What the daemon bakes in at start: the host its leases name, the engine (switching it under live workers is
114
+ * unsafe, see #42), the db/log/heartbeat paths it opened, and graft's App identity in its children's env.
115
+ */
116
+ export const RESTART_KEYS = Object.freeze(['host', 'engine', 'paths', 'logs', 'graft'])
117
+
118
+ /**
119
+ * A re-read config applied to the running one in place, since every collaborator holds that object. Keys in
120
+ * {@link RESTART_KEYS} are never applied; they are reported so the daemon can say a restart is needed.
121
+ *
122
+ * @param {Config} live @param {Config} next
123
+ * @returns {{ applied: string[], restart: string[] }}
124
+ */
125
+ export function applyReload(live, next) {
126
+ if (!next.repos.length) throw new Error('no repos')
127
+ /** @type {Record<string, unknown>} */
128
+ const target = live
129
+ /** @type {Record<string, unknown>} */
130
+ const source = next
131
+ /** @type {string[]} */
132
+ const applied = []
133
+ /** @type {string[]} */
134
+ const restart = []
135
+ for (const k of new Set([...Object.keys(target), ...Object.keys(source)])) {
136
+ if (JSON.stringify(target[k]) === JSON.stringify(source[k])) continue
137
+ if (RESTART_KEYS.includes(k)) restart.push(k)
138
+ else {
139
+ target[k] = source[k]
140
+ applied.push(k)
141
+ }
142
+ }
143
+ return { applied, restart }
144
+ }
145
+
112
146
  export function loadConfig(path) {
113
147
  if (!existsSync(path)) return resolveConfig({})
114
148
  try {
package/src/daemon.mjs CHANGED
@@ -2,6 +2,7 @@
2
2
  import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
3
3
  import { dirname, join } from 'node:path'
4
4
  import { setTimeout as sleep } from 'node:timers/promises'
5
+ import { applyReload } from './config.mjs'
5
6
  import { graftGitHub } from './github.mjs'
6
7
  import { openStore } from './store.mjs'
7
8
  import { tick } from './tick.mjs'
@@ -93,16 +94,40 @@ export function acquireLock(store, procs, pid) {
93
94
  }
94
95
 
95
96
  /**
97
+ * Before each tick, `reload` re-reads the config and {@link applyReload} applies what changed. A file that does
98
+ * not load keeps the running config; that warning, and "restart to apply" for a restart-only key, is logged once
99
+ * per distinct message rather than every tick.
100
+ *
96
101
  * @param {import('./tick.mjs').Deps} deps
97
- * @param {{ signal?: AbortSignal, wait?: (ms: number, signal?: AbortSignal) => Promise<unknown>, once?: boolean }} [opts]
102
+ * @param {{
103
+ * signal?: AbortSignal, wait?: (ms: number, signal?: AbortSignal) => Promise<unknown>, once?: boolean,
104
+ * reload?: () => import('./config.mjs').Config,
105
+ * }} [opts]
98
106
  */
99
107
  export async function runDaemon(
100
108
  deps,
101
- { signal, wait = (ms, s) => sleep(ms, undefined, { signal: s }), once = false } = {},
109
+ { signal, wait = (ms, s) => sleep(ms, undefined, { signal: s }), once = false, reload } = {},
102
110
  ) {
103
111
  let n = 0
112
+ /** @type {string | null} */
113
+ let warned = null
114
+ const reloadConfig = () => {
115
+ if (!reload) return
116
+ /** @type {string | null} */
117
+ let warning = null
118
+ try {
119
+ const { applied, restart } = applyReload(deps.config, reload())
120
+ if (applied.length) deps.log('info', `config reloaded: ${applied.join(', ')}`)
121
+ if (restart.length) warning = `config: ${restart.join(', ')} changed; restart cambium to apply`
122
+ } catch (err) {
123
+ warning = `config not reloaded, keeping the running one: ${/** @type {Error} */ (err)?.message ?? err}`
124
+ }
125
+ if (warning && warning !== warned) deps.log('warn', warning)
126
+ warned = warning
127
+ }
104
128
  deps.log('info', `cambium up on ${deps.config.host}: ${deps.config.repos.map((r) => r.slug).join(', ')}`)
105
129
  while (!signal?.aborted) {
130
+ if (n > 0) reloadConfig()
106
131
  deps.tickNumber = ++n
107
132
  try {
108
133
  await tick(deps)
package/src/workers.mjs CHANGED
@@ -149,6 +149,22 @@ export function processSpawner({
149
149
  const git = (/** @type {string} */ repoPath, /** @type {string[]} */ args) => exec('git', ['-C', repoPath, ...args])
150
150
  const graftEnv = { ...process.env, ...(config.graft.app ? { GRAFT_APP: config.graft.app } : {}) }
151
151
 
152
+ /**
153
+ * Fast-forward the central checkout to the just-fetched base, so the convention
154
+ * and hydrate sources a worker gets are current. Only when that checkout sits on
155
+ * the base branch with no tracked changes; `--ff-only` refuses rather than discards.
156
+ *
157
+ * @param {import('./config.mjs').Repo} repo @returns {Promise<boolean>} whether it moved
158
+ */
159
+ async function syncCentral(repo) {
160
+ const head = (await git(repo.path, ['symbolic-ref', '--short', '-q', 'HEAD']).catch(() => '')).trim()
161
+ if (head !== repo.baseBranch) return false
162
+ if ((await git(repo.path, ['status', '--porcelain', '--untracked-files=no'])).trim()) return false
163
+ const before = (await git(repo.path, ['rev-parse', 'HEAD'])).trim()
164
+ await git(repo.path, ['merge', '--ff-only', '--quiet', `origin/${repo.baseBranch}`]).catch(() => {})
165
+ return (await git(repo.path, ['rev-parse', 'HEAD'])).trim() !== before
166
+ }
167
+
152
168
  /**
153
169
  * Remove a worktree only once `graft status` says GitHub holds everything in
154
170
  * it; otherwise keep it and say why. Its local branch goes with it.
@@ -207,7 +223,16 @@ export function processSpawner({
207
223
  readOnly = false,
208
224
  }) {
209
225
  const worktree = join(convention.root, topicOf(branch))
210
- if (config.worker.fetch) await git(repo.path, ['fetch', 'origin', repo.baseBranch])
226
+ if (config.worker.fetch) {
227
+ await git(repo.path, ['fetch', 'origin', repo.baseBranch])
228
+ if (await syncCentral(repo)) {
229
+ try {
230
+ convention = readConvention(repo.path)
231
+ } catch {
232
+ // The updated package.json does not parse: keep the convention this tick read.
233
+ }
234
+ }
235
+ }
211
236
  /** @type {string | null} */
212
237
  let reused = null
213
238
  if (readOnly) await dispose(repo, worktree)