@yanauto/broco 0.0.0-stage → 1.0.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +209 -0
  2. package/LICENSE +202 -0
  3. package/README.md +188 -2
  4. package/README.zh-CN.md +187 -0
  5. package/assets/broco-board-icon-128.png +0 -0
  6. package/assets/broco-favicon-192.png +0 -0
  7. package/assets/broco-favicon-32.png +0 -0
  8. package/assets/broco-favicon-64.png +0 -0
  9. package/assets/broco-foreman-256.png +0 -0
  10. package/assets/broco-hero-1024.png +0 -0
  11. package/assets/broco-icon-512.png +0 -0
  12. package/assets/broco-readme-logo-dark.png +0 -0
  13. package/assets/broco-readme-logo-light.png +0 -0
  14. package/assets/broco-social-preview-1280x640.png +0 -0
  15. package/assets/favicon.ico +0 -0
  16. package/bin/broco +1077 -0
  17. package/bundle/board/assets/index-Bue0HpFh.css +1 -0
  18. package/bundle/board/assets/index-DTqMQtD9.js +16 -0
  19. package/bundle/board/broco-board-icon-128.png +0 -0
  20. package/bundle/board/broco-favicon-32.png +0 -0
  21. package/bundle/board/broco-favicon-64.png +0 -0
  22. package/bundle/board/broco-foreman-256.png +0 -0
  23. package/bundle/board/favicon.ico +0 -0
  24. package/bundle/board/fonts/instrument-sans-latin.woff2 +0 -0
  25. package/bundle/board/fonts/jetbrains-mono-latin.woff2 +0 -0
  26. package/bundle/board/index.html +18 -0
  27. package/lib/attachments.mjs +104 -0
  28. package/lib/claude-md.mjs +132 -0
  29. package/lib/cli-args.mjs +292 -0
  30. package/lib/cli-process.mjs +75 -0
  31. package/lib/client.mjs +198 -0
  32. package/lib/credential-proxy.mjs +165 -0
  33. package/lib/credential-source.mjs +23 -0
  34. package/lib/crew.mjs +947 -0
  35. package/lib/detect.mjs +279 -0
  36. package/lib/directories.mjs +32 -0
  37. package/lib/map.mjs +336 -0
  38. package/lib/model-catalog.json +864 -0
  39. package/lib/model-catalog.mjs +138 -0
  40. package/lib/model-env.mjs +11 -0
  41. package/lib/native.mjs +228 -0
  42. package/lib/paths.mjs +63 -0
  43. package/lib/pi-profile.mjs +29 -0
  44. package/lib/pi-secrets.mjs +48 -0
  45. package/lib/pi.mjs +232 -0
  46. package/lib/platform/index.mjs +13 -0
  47. package/lib/platform/launchd.mjs +138 -0
  48. package/lib/platform/linux.mjs +168 -0
  49. package/lib/platform/macos.mjs +29 -0
  50. package/lib/platform/processes.mjs +102 -0
  51. package/lib/problems.mjs +50 -0
  52. package/lib/projects.mjs +530 -0
  53. package/lib/quota.mjs +132 -0
  54. package/lib/roster-output.mjs +136 -0
  55. package/lib/roster.mjs +913 -0
  56. package/lib/runner.mjs +403 -0
  57. package/lib/settings-audit.mjs +104 -0
  58. package/lib/setup.mjs +416 -0
  59. package/lib/slots.mjs +177 -0
  60. package/lib/stats.mjs +92 -0
  61. package/lib/store.mjs +737 -0
  62. package/lib/stream-workers.mjs +464 -0
  63. package/lib/templates.mjs +173 -0
  64. package/lib/time.mjs +25 -0
  65. package/lib/web.mjs +260 -0
  66. package/lib/worker-policy.mjs +17 -0
  67. package/lib/worktree.mjs +269 -0
  68. package/package.json +50 -4
  69. package/scripts/package-board.mjs +53 -0
  70. package/skills/broco/SKILL.md +59 -0
@@ -0,0 +1,50 @@
1
+ import { formatShortTime, resolvedTimeZone } from './time.mjs'
2
+
3
+ export const classes = ['quota', 'region', 'policy', 'network', 'auth', 'context', 'worker']
4
+ export const infra = ['quota', 'region', 'policy', 'network', 'auth']
5
+
6
+ const codexClasses = {
7
+ usageLimitExceeded: 'quota', sessionBudgetExceeded: 'quota', rateLimitExceeded: 'quota',
8
+ cyberPolicy: 'policy', misalignmentPolicyViolation: 'policy',
9
+ serverOverloaded: 'network', internalServerError: 'network', flexUnavailable: 'network',
10
+ httpConnectionFailed: 'network', responseStreamConnectionFailed: 'network', responseStreamDisconnected: 'network', responseTooManyFailedAttempts: 'network',
11
+ unauthorized: 'auth', contextWindowExceeded: 'context'
12
+ }
13
+
14
+ const patterns = [
15
+ ['region', /location is not supported|unsupported[_ ](?:country|region|location)(?:[_ ]territory)?|country,\s*region,\s*or\s*territory\s*not\s*supported|not available in your (?:country|region)/i],
16
+ ['policy', /flagged for possible cybersecurity|content (?:filtering )?(?:policy|was flagged)|safety (?:system|filter)|blocked due to safety|finish_?reason[:=\s]+["']?(?:safety|prohibited_content|blocklist)\b|PROHIBITED_CONTENT|violat\w* (?:our|the) (?:usage )?polic|包含不安全或敏感内容/i],
17
+ ['network', /RESOURCE_EXHAUSTED[\s\S]*?\b(?:no capacity|capacity (?:exhausted|unavailable)|overloaded|high demand)\b|\bno capacity available\b|\bmodel capacity exhausted\b|并发数过高|请求频率过高|\bhigh concurrency\b/i],
18
+ ['quota', /usage limit|(?:5|five)-hour limit|(?:weekly|session) limit(?: reached| exceeded)?|(?:you(?:'ve| have) )?(?:hit|reached) your (?:[\w-]+\s+)?limit\b|rate.?limit(?:ed|[_ ]error| exceeded| reached)|(?:api|usage|token) quota|(?<!disk )quota (?:exceeded|exhausted|reached)|exceeded (?:your|the) (?:current )?quota|insufficient (?:account )?(?:balance|credits?|funds)|credit balance is too low|out of credits|(?:status(?: code)?|HTTP)[ :]{1,2}429\b|429 Too Many|too many requests|RESOURCE_EXHAUSTED|余额不足|无可用资源包/i],
19
+ ['auth', /unauthori[sz]ed|(?:status(?: code)?|HTTP)[ :]{1,2}401\b|401 Unauthori|not (?:logged|signed) in|sign in to this provider|(?:invalid|incorrect)[_ ]api[_ ]key|authentication(?:[_ ]error| (?:failed|required))|pi auth could not resolve|no api-key credential|api-key lookup failed|token (?:has )?expired|please (?:re)?login|please run (?:\/login|claude auth login)|oauth_not_allowed_for_organization|oauth_org_not_allowed|oauth_token_revoked|can't use your claude subscription|invalid_grant|无效的\s*api\s*key|(?:authentication\s*)?token\s*已过期/i],
20
+ ['context', /context (?:window|length|limit)|maximum context|exceeds? (?:the )?maximum (?:number of )?tokens|max_input_tokens|input tokens exceed|too many tokens|(?:prompt|input|conversation) is too long|request_too_large/i],
21
+ ['network', /ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|socket hang up|network error|fetch failed|timed out|(?:status(?: code)?|HTTP)[ :]{1,2}(?:50[0234]|529)\b|(?:50[0234]|529) (?:Internal Server Error|Bad Gateway|Service Unavailable|Gateway Time|Overloaded)|internal_server_error|overloaded_error|overloaded|service unavailable|stream disconnected/i]
22
+ ]
23
+
24
+ export function codexClass(info) {
25
+ if (!info) return null
26
+ const key = typeof info === 'string' ? info : Object.keys(info)[0]
27
+ return codexClasses[key] ?? null
28
+ }
29
+
30
+ export function classify(message, { codex, resetsAt } = {}) {
31
+ const text = String(message ?? '')
32
+ const cls = codexClass(codex) ?? patterns.find(([, re]) => re.test(text))?.[0] ?? 'worker'
33
+ const problem = { class: cls, retryable: ['quota', 'network'].includes(cls), message: text.slice(0, 600) }
34
+ if (resetsAt) problem.resetsAt = new Date(resetsAt * 1000).toISOString()
35
+ return problem
36
+ }
37
+
38
+ export function hint(problem) {
39
+ if (!problem) return null
40
+ const when = problem.resetsAt ? ',' + formatShortTime(problem.resetsAt) + ' (' + resolvedTimeZone() + ') 重置' : ''
41
+ return {
42
+ quota: '额度用尽' + when + ';换一个 bro 派 / quota exhausted; dispatch to another bro',
43
+ region: '供应商按地区拒绝(多半是代理出口);换 bro 或换出口 / provider rejected the region; switch bro or network exit',
44
+ policy: '被供应商安全过滤拦下;改写措辞后同名续派,或换 bro / blocked by provider safety filter; rephrase and continue, or switch bro',
45
+ network: '网络或服务端故障;稍后同名重试 / network or provider outage; retry the same name later',
46
+ auth: '登录失效;请用户在该 CLI 里重新登录 / signed out; ask the user to log in again in that CLI',
47
+ context: '上下文超限;换新名字开新会话,交代写短 / context window exceeded; start a new name with a shorter brief',
48
+ worker: null
49
+ }[problem.class]
50
+ }
@@ -0,0 +1,530 @@
1
+ import { execFileSync } from 'node:child_process'
2
+ import { randomUUID } from 'node:crypto'
3
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs'
4
+ import { basename, dirname, join, sep } from 'node:path'
5
+ import { homedir } from 'node:os'
6
+ import { DatabaseSync } from 'node:sqlite'
7
+ import { Crew } from './crew.mjs'
8
+ import { ensureAgentsMd, removeAgentsMd, resolveAgentsDocPath } from './claude-md.mjs'
9
+ import { listBatches } from './map.mjs'
10
+ import { canonical, home } from './paths.mjs'
11
+ import { formatDateTime } from './time.mjs'
12
+ import { Store } from './store.mjs'
13
+ import { Roster } from './roster.mjs'
14
+ import { supportedWorker, workerPolicy } from './worker-policy.mjs'
15
+
16
+ const ticketsPath = id => join(home, 'projects', id, 'tickets.sqlite')
17
+ const within = (path, root) => path === root || path.startsWith(root + sep)
18
+ const info = path => lstatSync(path, { throwIfNoEntry: false })
19
+ const NO_PROJECT_OR_GLOBAL = 'Project is not registered and no global worker allowlist is configured. Run broco setup, broco allow <workers> --global, or broco init in the project root.'
20
+
21
+ function validProjectRoot(repoPath) {
22
+ return Boolean(
23
+ existsSync(repoPath) &&
24
+ statSync(repoPath).isDirectory() &&
25
+ basename(repoPath) &&
26
+ !within(home, repoPath) &&
27
+ !within(repoPath, home) &&
28
+ repoPath !== homedir()
29
+ )
30
+ }
31
+
32
+ function gitRoot(path) {
33
+ const env = { ...process.env }
34
+ delete env.GIT_DIR
35
+ delete env.GIT_WORK_TREE
36
+ delete env.GIT_INDEX_FILE
37
+ delete env.GIT_COMMON_DIR
38
+ delete env.GIT_OBJECT_DIRECTORY
39
+ try {
40
+ const out = execFileSync('git', ['--no-optional-locks', '-C', path, 'rev-parse', '--show-toplevel'], {
41
+ encoding: 'utf8',
42
+ stdio: ['ignore', 'pipe', 'ignore'],
43
+ timeout: 2000,
44
+ env
45
+ }).trim()
46
+ if (!out) return null
47
+ const resolved = canonical(out)
48
+ if (!within(path, resolved) || !validProjectRoot(resolved)) return null
49
+ return resolved
50
+ } catch {
51
+ return null
52
+ }
53
+ }
54
+
55
+ export function workspaceRoot(cwd) {
56
+ const path = canonical(cwd)
57
+ return gitRoot(path) ?? path
58
+ }
59
+
60
+ function binding(root) {
61
+ const dir = join(root, '.broco')
62
+ if (info(dir) && !info(dir).isDirectory()) throw new Error('Expected a .broco directory at ' + root)
63
+ const file = join(dir, 'project-id')
64
+ if (info(file)) {
65
+ if (!info(file).isFile()) throw new Error('Invalid project-id at ' + root)
66
+ return readFileSync(file, 'utf8').trim()
67
+ }
68
+ const legacyDir = join(root, '.cc-bro')
69
+ if (info(legacyDir)?.isDirectory()) {
70
+ const legacyFile = join(legacyDir, 'project-id')
71
+ if (info(legacyFile)?.isFile()) return readFileSync(legacyFile, 'utf8').trim()
72
+ }
73
+ return null
74
+ }
75
+
76
+ function bind(root, id) {
77
+ const dir = join(root, '.broco')
78
+ mkdirSync(dir, { recursive: true })
79
+ for (const [name, content] of [['.gitignore', '*\n'], ['project-id', id + '\n']]) {
80
+ const file = join(dir, name)
81
+ if (!info(file)) writeFileSync(file, content, { flag: 'wx' })
82
+ else if (!info(file).isFile() || readFileSync(file, 'utf8').trim() !== content.trim()) throw new Error('Conflicting project marker: ' + file)
83
+ }
84
+ }
85
+
86
+ export class Projects {
87
+ constructor(slots, publish = () => {}, hasListeners = () => true) {
88
+ mkdirSync(home, { recursive: true })
89
+ this.db = new DatabaseSync(join(home, 'registry-v2.sqlite'))
90
+ this.db.exec('PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA busy_timeout=5000; PRAGMA foreign_keys=ON')
91
+ this.db.exec(
92
+ 'CREATE TABLE IF NOT EXISTS projects(id TEXT PRIMARY KEY,name TEXT NOT NULL,repoPath TEXT NOT NULL UNIQUE,created INTEGER NOT NULL,allow TEXT NOT NULL,worker TEXT NOT NULL,concurrency INTEGER,implicit INTEGER NOT NULL DEFAULT 0); ' +
93
+ 'CREATE TABLE IF NOT EXISTS runner_state (id INTEGER PRIMARY KEY CHECK (id=1), pid INTEGER, ppid INTEGER, started INTEGER, stop_reason TEXT, stopped INTEGER, web_port INTEGER)'
94
+ )
95
+ const projectCols = this.db.prepare('PRAGMA table_info(projects)').all()
96
+ if (!projectCols.some(c => c.name === 'concurrency')) {
97
+ this.db.exec('ALTER TABLE projects ADD COLUMN concurrency INTEGER')
98
+ }
99
+ if (!projectCols.some(c => c.name === 'implicit')) {
100
+ this.db.exec('ALTER TABLE projects ADD COLUMN implicit INTEGER NOT NULL DEFAULT 0')
101
+ }
102
+ if (!this.db.prepare('PRAGMA table_info(runner_state)').all().some(c => c.name === 'web_port')) {
103
+ this.db.exec('ALTER TABLE runner_state ADD COLUMN web_port INTEGER')
104
+ }
105
+ this.slots = slots
106
+ this.publish = publish ?? (() => {})
107
+ this.hasListeners = typeof hasListeners === 'function' ? hasListeners : () => true
108
+ this.onTicketChange = null
109
+ this._dirty = new Map()
110
+ this._flushScheduled = false
111
+ this.entries = new Map()
112
+ this.roster = new Roster(this)
113
+ const globalConcurrency = this.roster.read('global').concurrency
114
+ if (Number.isSafeInteger(globalConcurrency) && globalConcurrency >= 1) {
115
+ this.slots?.setLimit?.('global', globalConcurrency)
116
+ }
117
+ }
118
+
119
+ saveWebPort(port) {
120
+ const val = port === null || port === undefined ? null : Number(port)
121
+ if (val !== null && (!Number.isSafeInteger(val) || val < 1 || val > 65535)) {
122
+ throw new Error('Invalid web port: ' + port)
123
+ }
124
+ this.db.prepare('INSERT INTO runner_state(id,web_port) VALUES (1,?) ON CONFLICT(id) DO UPDATE SET web_port=excluded.web_port').run(val)
125
+ return val
126
+ }
127
+
128
+ readWebPort() {
129
+ const port = this.db.prepare('SELECT web_port FROM runner_state WHERE id=1').get()?.web_port ?? null
130
+ return Number.isSafeInteger(port) && port >= 1 && port <= 65535 ? port : null
131
+ }
132
+
133
+ markTicketChanged(projectId, cid) {
134
+ if (!this.entries.has(projectId)) return
135
+ let set = this._dirty.get(projectId)
136
+ if (!set) {
137
+ set = new Set()
138
+ this._dirty.set(projectId, set)
139
+ }
140
+ if (cid) set.add(cid)
141
+ if (this._flushScheduled) return
142
+ this._flushScheduled = true
143
+ queueMicrotask(() => this.flushChanges())
144
+ }
145
+
146
+ flushChanges() {
147
+ this._flushScheduled = false
148
+ const batches = [...this._dirty.entries()]
149
+ this._dirty.clear()
150
+ const shouldPublish = Boolean(this.hasListeners?.())
151
+ for (const [projectId, cids] of batches) {
152
+ const p = this.entries.get(projectId)
153
+ if (!p) continue
154
+ const list = [...cids]
155
+ try { this.onTicketChange?.(projectId, list) } catch {}
156
+ if (!shouldPublish) continue
157
+ try {
158
+ if (list.length > 4) {
159
+ this.publish?.({ type: 'tickets', projectId, tickets: p.crew.store.ticketRows() })
160
+ } else {
161
+ for (const cid of list) {
162
+ const ticket = p.crew.store.ticket(cid)
163
+ if (ticket) this.publish?.({ type: 'ticket', projectId, ticket })
164
+ }
165
+ }
166
+ this.publish?.({ type: 'project', project: this.summary(projectId) })
167
+ } catch (err) {
168
+ if (!/not open/i.test(err?.message ?? '')) throw err
169
+ }
170
+ }
171
+ }
172
+
173
+ attach(row) {
174
+ const allow = JSON.parse(row.allow).filter(supportedWorker)
175
+ const worker = allow.includes(row.worker) ? row.worker : ''
176
+ const concurrency = Number.isSafeInteger(row.concurrency) && row.concurrency >= 1 ? row.concurrency : null
177
+ const implicit = Boolean(row.implicit)
178
+ if (JSON.stringify(allow) !== row.allow || worker !== row.worker) {
179
+ this.db.prepare('UPDATE projects SET allow=?,worker=? WHERE id=?').run(JSON.stringify(allow), worker, row.id)
180
+ }
181
+ const p = { ...row, allow, worker, concurrency, implicit }
182
+ mkdirSync(dirname(ticketsPath(p.id)), { recursive: true })
183
+ const store = new Store(false, ticketsPath(p.id))
184
+ p.crew = new Crew(store, this.slots, () => ({ ...p, ...this.roster.effective(p) }))
185
+ this.entries.set(p.id, p)
186
+ this.slots?.setLimit?.(p.id, this.roster.effective(p).concurrency)
187
+ store.onChange = cid => this.markTicketChanged(p.id, cid)
188
+ return p
189
+ }
190
+
191
+ async recover(reason) {
192
+ this.slots.paused = true
193
+ for (const row of this.db.prepare('SELECT * FROM projects ORDER BY created').all()) await this.attach(row).crew.recover(reason)
194
+ const queued = this.crews().flatMap(crew => crew.store.stmt("SELECT c.*,r.started FROM conversations c JOIN turns r ON r.cid=c.id AND r.turn=c.turn WHERE c.status='queued'").all().map(c => ({ crew, c })))
195
+ for (const { crew, c } of queued.sort((a, b) => a.c.started - b.c.started)) this.slots.reserve(crew, c)
196
+ this.slots.paused = false
197
+ this.slots.drain()
198
+ this.reconcileQueue()
199
+ }
200
+
201
+ reconcileQueue() {
202
+ let work = false
203
+ for (const p of this.entries.values()) {
204
+ const scope = this.slots.scopeOf(p.crew)
205
+ const pool = this.slots.pool(scope, p.crew)
206
+ for (const [cid] of pool.active) {
207
+ work = true
208
+ const c = p.crew.store.stmt('SELECT status FROM conversations WHERE id=?').get(cid)
209
+ if (c && c.status !== 'running') this.slots.release(cid)
210
+ }
211
+ const hasQueued = pool.queue.size > 0 || Boolean(p.crew.store.stmt("SELECT 1 FROM conversations WHERE status='queued' LIMIT 1").get())
212
+ if (!hasQueued) continue
213
+ work = true
214
+ const queued = p.crew.store.stmt("SELECT c.id,c.name,c.turn,r.started FROM conversations c JOIN turns r ON r.cid=c.id AND r.turn=c.turn WHERE c.status='queued' ORDER BY r.started ASC, r.rowid ASC, c.rowid ASC").all()
215
+ for (const c of queued) {
216
+ if (pool.active.has(c.id) || pool.queue.has(c.id)) continue
217
+ this.slots.reserve(p.crew, c)
218
+ }
219
+ }
220
+ if (work) this.slots.drain()
221
+ }
222
+
223
+ startReconciler() {
224
+ if (this._reconcileTimer) return
225
+ this._reconcileTimer = setInterval(() => {
226
+ if (this.slots.paused) return
227
+ this.reconcileQueue()
228
+ }, 3000)
229
+ this._reconcileTimer.unref()
230
+ }
231
+
232
+ stopReconciler() {
233
+ if (this._reconcileTimer) {
234
+ clearInterval(this._reconcileTimer)
235
+ this._reconcileTimer = null
236
+ }
237
+ }
238
+
239
+ ensureImplicit(cwd, precomputedRoot = null) {
240
+ const repoPath = precomputedRoot ?? workspaceRoot(cwd)
241
+ if (!validProjectRoot(repoPath)) throw new Error('Choose a project directory separate from broco data')
242
+ const global = this.roster?.read?.('global')
243
+ const globalAllow = (global?.allow ?? []).filter(supportedWorker)
244
+ if (!globalAllow.length) throw new Error(NO_PROJECT_OR_GLOBAL)
245
+ const existing = [...this.entries.values()].find(item => item.repoPath === repoPath)
246
+ if (existing) return existing
247
+ const worker = globalAllow.includes(global.worker) ? global.worker : globalAllow[0]
248
+ const key = randomUUID()
249
+ const name = basename(repoPath)
250
+ this.db.exec('BEGIN IMMEDIATE')
251
+ try {
252
+ this.db.prepare('INSERT INTO projects (id,name,repoPath,created,allow,worker,concurrency,implicit) VALUES (?,?,?,?,?,?,?,1)').run(
253
+ key, name, repoPath, Date.now(), JSON.stringify(globalAllow), worker, null
254
+ )
255
+ this.db.prepare('INSERT INTO roster_settings VALUES (?,?,?) ON CONFLICT(scope) DO UPDATE SET revision=excluded.revision,data=excluded.data').run(
256
+ key, 1, JSON.stringify({ entries: {}, rules: [], inheritPolicy: true })
257
+ )
258
+ this.db.exec('COMMIT')
259
+ } catch (err) { this.db.exec('ROLLBACK'); throw err }
260
+ const p = this.attach(this.db.prepare('SELECT * FROM projects WHERE id=?').get(key))
261
+ if (!this.hasListeners || this.hasListeners()) {
262
+ this.publish?.({ type: 'project', project: this.summary(p.id) })
263
+ }
264
+ return p
265
+ }
266
+
267
+ register(cwd, allow, worker, opts = {}) {
268
+ const { claudeMd, agentsMd, host, managerMode = false, concurrency } = opts ?? {}
269
+ const wantAgentsMd = agentsMd !== undefined ? agentsMd !== false : claudeMd !== false
270
+ const repoPath = realpathSync.native(cwd)
271
+ if (!validProjectRoot(repoPath)) throw new Error('Choose a project directory separate from broco data')
272
+ if (wantAgentsMd) resolveAgentsDocPath(repoPath, { host })
273
+ const id = binding(repoPath)
274
+ let p = id === null ? [...this.entries.values()].find(p => p.repoPath === repoPath) : this.get(id)
275
+ if (p && p.repoPath !== repoPath && existsSync(p.repoPath)) throw new Error('Project ID already bound to ' + p.repoPath)
276
+ const global = this.roster.read('global')
277
+ const wasImplicit = Boolean(p?.implicit)
278
+ const currentPolicy = p ? this.roster.effective(p) : (global.allow?.length ? global : undefined)
279
+ const inheritNew = (!p || wasImplicit) && allow === undefined && worker === undefined && Boolean(global.allow?.length)
280
+ const settings = workerPolicy(allow, worker, currentPolicy)
281
+ const entries = p ? this.roster.effective(p).entries : global.entries
282
+ if (settings.allow.some(id => id.startsWith('custom:') && !entries[id]?.template)) throw new Error('Configure and test custom workers in broco ui first')
283
+ let nextConcurrency = p?.concurrency ?? null
284
+ if (concurrency !== undefined) {
285
+ if (concurrency === null || concurrency === 'inherit') nextConcurrency = null
286
+ else {
287
+ const n = typeof concurrency === 'string' && /^\d+$/.test(concurrency.trim()) ? Number(concurrency.trim()) : concurrency
288
+ if (!Number.isSafeInteger(n) || n < 1 || n > 64) throw new Error('Concurrency must be an integer between 1 and 64')
289
+ nextConcurrency = n
290
+ }
291
+ }
292
+ const key = p?.id ?? randomUUID()
293
+ const name = basename(repoPath)
294
+ this.db.exec('BEGIN IMMEDIATE')
295
+ try {
296
+ this.db.prepare('INSERT INTO projects (id,name,repoPath,created,allow,worker,concurrency,implicit) VALUES (?,?,?,?,?,?,?,0) ON CONFLICT(id) DO UPDATE SET repoPath=excluded.repoPath,name=excluded.name,allow=excluded.allow,worker=excluded.worker,concurrency=excluded.concurrency,implicit=0').run(key, name, repoPath, Date.now(), JSON.stringify(settings.allow), settings.worker, nextConcurrency)
297
+ bind(repoPath, key)
298
+ this.db.exec('COMMIT')
299
+ } catch (err) { this.db.exec('ROLLBACK'); throw err }
300
+ if (p) Object.assign(p, { name, repoPath, ...settings, concurrency: nextConcurrency, implicit: false })
301
+ else p = this.attach(this.db.prepare('SELECT * FROM projects WHERE id=?').get(key))
302
+ if (inheritNew && !wasImplicit) {
303
+ this.db.prepare('INSERT INTO roster_settings VALUES (?,?,?) ON CONFLICT(scope) DO UPDATE SET revision=excluded.revision,data=excluded.data').run(p.id, 1, JSON.stringify({ entries: {}, rules: [], inheritPolicy: true }))
304
+ }
305
+ if (allow !== undefined || worker !== undefined || concurrency !== undefined) {
306
+ const local = this.roster.read(p.id)
307
+ const { revision, ...data } = local
308
+ let dirty = false
309
+ if ((allow !== undefined || worker !== undefined) && data.inheritPolicy) { data.inheritPolicy = false; dirty = true }
310
+ if (concurrency !== undefined) {
311
+ if (nextConcurrency === null) delete data.concurrency
312
+ else data.concurrency = nextConcurrency
313
+ dirty = true
314
+ }
315
+ if (dirty) this.db.prepare('INSERT INTO roster_settings VALUES (?,?,?) ON CONFLICT(scope) DO UPDATE SET revision=excluded.revision,data=excluded.data').run(p.id, revision + 1, JSON.stringify(data))
316
+ }
317
+ this.slots?.setLimit?.(p.id, this.roster.effective(p).concurrency)
318
+ const doc = wantAgentsMd ? ensureAgentsMd(repoPath, { host, managerMode: !!managerMode }) : null
319
+ const project = this.summary(p.id)
320
+ if (!this.hasListeners || this.hasListeners()) {
321
+ this.publish?.({ type: 'project', project })
322
+ }
323
+ return doc ? { ...project, agentsMd: doc, claudeMd: doc } : project
324
+ }
325
+
326
+ uninit(cwd, opts = {}) {
327
+ const repoPath = realpathSync.native(cwd)
328
+ const wantAgentsMd = opts?.agentsMd !== undefined ? opts.agentsMd !== false : opts?.claudeMd !== false
329
+ if (wantAgentsMd) resolveAgentsDocPath(repoPath, { host: opts?.host })
330
+ const markerDir = join(repoPath, '.broco')
331
+ const legacyDir = join(repoPath, '.cc-bro')
332
+ const legacyIdFile = join(legacyDir, 'project-id')
333
+ const hasBrocoId = Boolean(info(join(markerDir, 'project-id'))?.isFile())
334
+ const hasLegacyId = Boolean(info(legacyDir)?.isDirectory() && info(legacyIdFile)?.isFile())
335
+ const id = binding(repoPath)
336
+ const p = (id && this.entries.get(id)) || [...this.entries.values()].find(x => x.repoPath === repoPath) || null
337
+ if (p) {
338
+ const active = p.crew.store.stmt("SELECT count(*) AS n FROM conversations WHERE status IN ('queued','running')").get().n
339
+ if (active > 0) throw new Error('Stop running or queued tickets before uninitializing this project')
340
+ }
341
+ const complete = () => {
342
+ if (p) {
343
+ this.db.exec('BEGIN IMMEDIATE')
344
+ try {
345
+ this.db.prepare('DELETE FROM projects WHERE id=?').run(p.id)
346
+ this.db.prepare('DELETE FROM roster_settings WHERE scope=?').run(p.id)
347
+ this.roster?.audit?.archiveScope?.(p.id)
348
+ this.db.exec('COMMIT')
349
+ } catch (err) { this.db.exec('ROLLBACK'); throw err }
350
+ this._dirty.delete(p.id)
351
+ this.slots?.setLimit?.(p.id, null)
352
+ this.entries.delete(p.id)
353
+ p.crew.store.db.close()
354
+ if (!this.hasListeners || this.hasListeners()) {
355
+ this.publish?.({ type: 'project-removed', projectId: p.id })
356
+ }
357
+ }
358
+ const markerRemoved = !!info(markerDir)
359
+ if (markerRemoved) rmSync(markerDir, { recursive: true, force: true })
360
+ let legacyMarkerRemoved = false
361
+ if (!hasBrocoId && hasLegacyId) {
362
+ rmSync(legacyIdFile, { force: true })
363
+ legacyMarkerRemoved = true
364
+ try {
365
+ const rest = readdirSync(legacyDir)
366
+ if (rest.length === 0 || (rest.length === 1 && rest[0] === '.gitignore' && readFileSync(join(legacyDir, '.gitignore'), 'utf8').trim() === '*')) {
367
+ rmSync(legacyDir, { recursive: true, force: true })
368
+ }
369
+ } catch {}
370
+ }
371
+ const doc = !wantAgentsMd
372
+ ? { path: resolveAgentsDocPath(repoPath, { host: opts?.host }), file: basename(resolveAgentsDocPath(repoPath, { host: opts?.host })), action: 'skipped', removed: false }
373
+ : removeAgentsMd(repoPath, { host: opts?.host })
374
+ return { unregistered: !!p, projectId: p?.id ?? id ?? null, repoPath, markerRemoved: markerRemoved || legacyMarkerRemoved, legacyMarkerRemoved, agentsMd: doc, claudeMd: doc }
375
+ }
376
+ const hasLiveWorkers = Boolean(p && (
377
+ p.crew.workers?.size > 0 ||
378
+ p.crew.store.stmt('SELECT 1 FROM conversations WHERE worker_pid IS NOT NULL LIMIT 1').get()
379
+ ))
380
+ if (!hasLiveWorkers) return complete()
381
+ return (async () => {
382
+ if (typeof p.crew.releaseAll === 'function') {
383
+ await p.crew.releaseAll()
384
+ } else {
385
+ for (const [cid, w] of [...(p.crew.workers?.entries() ?? [])]) {
386
+ await p.crew.release({ id: cid }, w)
387
+ }
388
+ for (const c of p.crew.store.stmt('SELECT * FROM conversations WHERE worker_pid IS NOT NULL').all()) {
389
+ await p.crew.release(c, p.crew.workers?.get(c.id))
390
+ }
391
+ }
392
+ return complete()
393
+ })()
394
+ }
395
+
396
+ get(id) {
397
+ const p = this.entries.get(id)
398
+ if (!p) throw new Error('Unknown project ID; check BROCO_HOME and run broco init')
399
+ return p
400
+ }
401
+
402
+ context(cwd, { createImplicit = true, optional = false } = {}) {
403
+ const path = realpathSync.native(cwd)
404
+ let p
405
+ for (let root = path; ; root = dirname(root)) {
406
+ if (within(home, root)) break
407
+ const id = binding(root)
408
+ if (id !== null) {
409
+ p = this.get(id)
410
+ if (p.repoPath !== root) this.register(root, undefined, undefined, { claudeMd: false })
411
+ break
412
+ }
413
+ if (dirname(root) === root) break
414
+ }
415
+ if (!p) {
416
+ p = [...this.entries.values()]
417
+ .filter(item => !item.implicit && within(path, item.repoPath))
418
+ .sort((a, b) => b.repoPath.length - a.repoPath.length)[0]
419
+ }
420
+ if (!p && within(path, home)) {
421
+ p = [...this.entries.values()].find(project =>
422
+ project.crew.store.stmt('SELECT DISTINCT worktree FROM collaboration WHERE worktree IS NOT NULL').all().some(row => within(path, row.worktree)))
423
+ }
424
+ if (!p) {
425
+ const wsRoot = workspaceRoot(path)
426
+ const gitBounded = wsRoot !== path
427
+ p = [...this.entries.values()].find(item => item.repoPath === wsRoot)
428
+ if (!p && !gitBounded) {
429
+ p = [...this.entries.values()]
430
+ .filter(item => item.implicit && within(path, item.repoPath))
431
+ .sort((a, b) => b.repoPath.length - a.repoPath.length)[0]
432
+ }
433
+ if (!p) {
434
+ if (!createImplicit) {
435
+ if (optional) {
436
+ const globalAllow = (this.roster?.read?.('global')?.allow ?? []).filter(supportedWorker)
437
+ return { found: false, projectId: null, database: null, defaultCwd: wsRoot, implicit: false, globalConfigured: globalAllow.length > 0 }
438
+ }
439
+ throw new Error(NO_PROJECT_OR_GLOBAL)
440
+ }
441
+ p = this.ensureImplicit(path, wsRoot)
442
+ }
443
+ }
444
+ return { found: true, projectId: p.id, database: ticketsPath(p.id), defaultCwd: p.repoPath, implicit: Boolean(p.implicit), globalConfigured: true }
445
+ }
446
+
447
+ summary(id) {
448
+ const p = this.get(id)
449
+ const counts = { queued: 0, working: 0, review: 0, done: 0 }
450
+ let attention = 0, runningCount = 0, updated = p.created
451
+ for (const r of p.crew.store.stmt('SELECT stage,flag,count(*) AS n,max(updated) AS updated FROM tickets GROUP BY stage,flag').all()) {
452
+ counts[r.stage] += r.n
453
+ if (r.stage === 'working' && !r.flag) runningCount += r.n
454
+ if (r.flag || r.stage === 'review') attention += r.n
455
+ updated = Math.max(updated, r.updated)
456
+ }
457
+ const policy = this.roster.effective(p)
458
+ return { id, name: p.name, repoPath: p.repoPath, implicit: Boolean(p.implicit), allow: policy.allow, worker: policy.worker, concurrency: policy.concurrency, counts, runningCount, attention, updatedAt: new Date(updated).toISOString() }
459
+ }
460
+
461
+ projectStatus(id) {
462
+ const p = this.get(id)
463
+ const base = this.summary(id)
464
+ const store = p.crew.store
465
+ let errorCount = 0, stoppedCount = 0
466
+ for (const r of store.stmt("SELECT flag, count(*) AS n FROM tickets WHERE flag IN ('error','stopped') GROUP BY flag").all()) {
467
+ if (r.flag === 'error') errorCount += r.n
468
+ if (r.flag === 'stopped') stoppedCount += r.n
469
+ }
470
+ const activeNames = store.stmt("SELECT name FROM conversations WHERE status='running' ORDER BY name").all().map(r => r.name)
471
+ const queuedNames = store.stmt("SELECT c.name FROM conversations c LEFT JOIN turns r ON r.cid=c.id AND r.turn=c.turn WHERE c.status='queued' ORDER BY r.started ASC, r.rowid ASC, c.rowid ASC").all().map(r => r.name)
472
+ const batches = listBatches(store).map(b => ({
473
+ prefix: b.prefix,
474
+ title: b.title,
475
+ count: b.count,
476
+ counts: b.counts,
477
+ finished: b.finished
478
+ }))
479
+ const worktreeRows = store.stmt('SELECT m.worktree, t.stage FROM collaboration m JOIN tickets t ON t.cid=m.cid WHERE m.worktree IS NOT NULL').all()
480
+ let retainedWorktrees = 0, cleanableWorktrees = 0
481
+ for (const row of worktreeRows) {
482
+ if (existsSync(row.worktree)) {
483
+ retainedWorktrees += 1
484
+ if (row.stage === 'done') cleanableWorktrees += 1
485
+ }
486
+ }
487
+ return {
488
+ ...base,
489
+ errorCount,
490
+ stoppedCount,
491
+ activeNames,
492
+ queuedNames,
493
+ batches,
494
+ activeBatches: batches.filter(b => !b.finished),
495
+ retainedWorktrees,
496
+ cleanableWorktrees
497
+ }
498
+ }
499
+
500
+ statusSnapshot({ projectId = null, cwd = null, all = false } = {}) {
501
+ let resolvedId = projectId
502
+ if (!resolvedId && cwd) {
503
+ try { resolvedId = this.context(cwd, { createImplicit: false }).projectId } catch { resolvedId = null }
504
+ }
505
+ const project = resolvedId && this.entries.has(resolvedId) ? this.projectStatus(resolvedId) : null
506
+ const summaries = this.list()
507
+ const projects = all ? [...this.entries.keys()].map(id => this.projectStatus(id)) : summaries
508
+ const totals = {
509
+ projects: summaries.length,
510
+ running: summaries.reduce((n, s) => n + (s.runningCount ?? 0), 0),
511
+ queued: summaries.reduce((n, s) => n + (s.counts?.queued ?? 0), 0),
512
+ review: summaries.reduce((n, s) => n + (s.counts?.review ?? 0), 0),
513
+ done: summaries.reduce((n, s) => n + (s.counts?.done ?? 0), 0),
514
+ attention: summaries.reduce((n, s) => n + (s.attention ?? 0), 0)
515
+ }
516
+ const rs = this.db.prepare('SELECT * FROM runner_state WHERE id=1').get() ?? null
517
+ const runnerState = rs ? {
518
+ pid: rs.pid ?? null,
519
+ ppid: rs.ppid ?? null,
520
+ startedAt: rs.started ? new Date(rs.started).toISOString() : null,
521
+ startedLocal: rs.started ? formatDateTime(rs.started) : null,
522
+ lastStopReason: rs.stop_reason ?? null,
523
+ webPort: rs.web_port ?? null
524
+ } : null
525
+ return { project, projects, totals, runnerState }
526
+ }
527
+
528
+ list() { return [...this.entries.keys()].map(id => this.summary(id)) }
529
+ crews() { return [...this.entries.values()].map(p => p.crew) }
530
+ }