@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
package/lib/client.mjs ADDED
@@ -0,0 +1,198 @@
1
+ import { lstatSync, rmSync } from 'node:fs'
2
+ import { request } from 'node:http'
3
+ import { setTimeout as sleep } from 'node:timers/promises'
4
+ import { platform } from './platform/index.mjs'
5
+ import { home, legacyCcBroHome, socketFor, socketPath } from './paths.mjs'
6
+
7
+ export const shutdownEffect = 'Shutdown stops running and queued work across all projects on this runner. Unfinished turns will not resume automatically.'
8
+ export const RUNNER_PROTOCOL = 15
9
+
10
+ export function legacySocketsFor(socket = socketPath) {
11
+ if (process.platform === 'win32' || typeof socket !== 'string') return []
12
+ const match = /\/s-([0-9a-f]{24})\.sock$/.exec(socket)
13
+ if (!match) return []
14
+ const uid = process.getuid()
15
+ return [
16
+ '/tmp/cc-bro-' + uid + '/s-' + match[1] + '.sock',
17
+ '/tmp/cc-bro-' + uid + '-' + match[1] + '.sock'
18
+ ]
19
+ }
20
+
21
+ export function legacySocketFor(socket = socketPath) {
22
+ return legacySocketsFor(socket)[1] ?? null
23
+ }
24
+
25
+ function isVerifiedSocket(path) {
26
+ if (process.platform === 'win32' || typeof path !== 'string') return false
27
+ const st = lstatSync(path, { throwIfNoEntry: false })
28
+ return Boolean(st && !st.isSymbolicLink() && st.isSocket() && st.uid === process.getuid())
29
+ }
30
+
31
+ function verifiedLegacySockets(socket = socketPath) {
32
+ return legacySocketsFor(socket).filter(isVerifiedSocket)
33
+ }
34
+
35
+ export function call(path, data, socket = socketPath, options = {}) {
36
+ const opts = typeof options === 'number' ? { timeoutMs: options } : (options ?? {})
37
+ const { timeoutMs = 0, signal } = opts
38
+ return new Promise((resolve, reject) => {
39
+ let settled = false
40
+ const done = (fn, val) => {
41
+ if (settled) return
42
+ settled = true
43
+ fn(val)
44
+ }
45
+ const req = request({ socketPath: socket, path, agent: false, method: data ? 'POST' : 'GET' }, res => {
46
+ const chunks = []
47
+ res.once('error', err => done(reject, err))
48
+ res.once('end', () => {
49
+ try {
50
+ const body = Buffer.concat(chunks).toString('utf8')
51
+ const value = JSON.parse(body)
52
+ if (res.statusCode !== 200) throw new Error(value.error)
53
+ done(resolve, value)
54
+ } catch (err) { done(reject, err) }
55
+ })
56
+ res.on('data', part => { chunks.push(Buffer.isBuffer(part) ? part : Buffer.from(part)) })
57
+ })
58
+ if (timeoutMs > 0) {
59
+ req.setTimeout(timeoutMs, () => {
60
+ const err = new Error('Runner request timed out')
61
+ err.code = 'ETIMEDOUT'
62
+ req.destroy(err)
63
+ })
64
+ }
65
+ if (signal) {
66
+ if (signal.aborted) {
67
+ req.destroy(signal.reason ?? new Error('Aborted'))
68
+ return
69
+ }
70
+ signal.addEventListener('abort', () => req.destroy(signal.reason ?? new Error('Aborted')), { once: true })
71
+ }
72
+ req.once('error', err => done(reject, err))
73
+ req.end(data ? JSON.stringify(data) : undefined)
74
+ })
75
+ }
76
+
77
+ export async function probeReady(socket = socketPath) {
78
+ try {
79
+ const runner = await call('/ready', undefined, socket, { timeoutMs: 2500 })
80
+ const compatible = runner.protocol === RUNNER_PROTOCOL
81
+ return { online: true, runner, compatible, incompatible: !compatible, legacySocket: false }
82
+ } catch (err) {
83
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'].includes(err.code)) throw err
84
+ }
85
+ for (const legacy of verifiedLegacySockets(socket)) {
86
+ try {
87
+ const runner = await call('/ready', undefined, legacy, { timeoutMs: 2500 })
88
+ const compatible = runner.protocol === RUNNER_PROTOCOL
89
+ return { online: true, runner, compatible, incompatible: !compatible, legacySocket: true }
90
+ } catch (err) {
91
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'].includes(err.code)) throw err
92
+ }
93
+ }
94
+ return { online: false, runner: null, compatible: true, incompatible: false, legacySocket: false }
95
+ }
96
+
97
+ function assertCompatible(runner) {
98
+ if (runner.protocol !== RUNNER_PROTOCOL) {
99
+ throw new Error('An incompatible broco runner is active. ' + shutdownEffect +
100
+ ' To let that work finish, wait before shutting down. To stop the runner and retry your command, first run:\nbroco shutdown')
101
+ }
102
+ return runner
103
+ }
104
+
105
+ async function handshake(socket = socketPath) {
106
+ try {
107
+ return assertCompatible(await call('/ready', undefined, socket, { timeoutMs: 2500 }))
108
+ } catch (err) {
109
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET'].includes(err.code)) throw err
110
+ for (const legacy of verifiedLegacySockets(socket)) {
111
+ let legacyRunner = null
112
+ try {
113
+ legacyRunner = await call('/ready', undefined, legacy, { timeoutMs: 2500 })
114
+ } catch (legacyErr) {
115
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'].includes(legacyErr.code)) throw legacyErr
116
+ }
117
+ if (legacyRunner) return assertCompatible(legacyRunner)
118
+ }
119
+ throw err
120
+ }
121
+ }
122
+
123
+ async function waitSocketDown(sock, timeoutMs = 5000) {
124
+ const deadline = Date.now() + timeoutMs
125
+ while (Date.now() < deadline) {
126
+ try {
127
+ await call('/ready', undefined, sock, { timeoutMs: 300 })
128
+ } catch (err) {
129
+ if (['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'EPIPE'].includes(err.code)) return
130
+ }
131
+ await sleep(50)
132
+ }
133
+ }
134
+
135
+ async function stopLegacySocket(sock) {
136
+ let responded = false
137
+ try {
138
+ const reply = await call('/shutdown', { callerPid: process.pid }, sock)
139
+ if (reply?.status !== 'offline') throw new Error('Runner did not confirm shutdown')
140
+ responded = true
141
+ } catch (err) {
142
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'EPIPE'].includes(err.code)) throw err
143
+ if (['ECONNRESET', 'EPIPE'].includes(err.code)) responded = true
144
+ }
145
+ if (responded) await waitSocketDown(sock)
146
+ rmSync(sock, { force: true })
147
+ }
148
+
149
+ function legacyHomeToStop() {
150
+ const explicitLegacy = Boolean(process.env.CC_BRO_HOME)
151
+ const usingDefaultHome = !process.env.BROCO_HOME
152
+ if (!explicitLegacy && !usingDefaultHome) return null
153
+ const legacyDir = legacyCcBroHome()
154
+ return legacyDir !== home ? legacyDir : null
155
+ }
156
+
157
+ export async function shutdown() {
158
+ let stoppedCurrent = false
159
+ try {
160
+ const reply = await call('/shutdown', { callerPid: process.pid }, socketPath)
161
+ if (reply?.status !== 'offline') throw new Error('Runner did not confirm shutdown')
162
+ stoppedCurrent = true
163
+ } catch (err) {
164
+ if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'EPIPE'].includes(err.code)) throw err
165
+ if (['ECONNRESET', 'EPIPE'].includes(err.code)) stoppedCurrent = true
166
+ }
167
+ if (!stoppedCurrent) {
168
+ for (const legacy of verifiedLegacySockets(socketPath)) {
169
+ await stopLegacySocket(legacy)
170
+ }
171
+ }
172
+ const legacyDir = legacyHomeToStop()
173
+ if (legacyDir) {
174
+ const legacyBaseSock = socketFor(legacyDir)
175
+ const candidates = [
176
+ ...(isVerifiedSocket(legacyBaseSock) ? [legacyBaseSock] : []),
177
+ ...verifiedLegacySockets(legacyBaseSock)
178
+ ]
179
+ for (const sock of candidates) {
180
+ await stopLegacySocket(sock)
181
+ }
182
+ await platform.removeLegacyService?.(legacyDir)
183
+ }
184
+ await platform.removeService()
185
+ }
186
+
187
+ export async function ready() {
188
+ try { return await handshake() }
189
+ catch (err) { if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'].includes(err.code)) throw err }
190
+ await platform.startService()
191
+ const deadline = Date.now() + 15000
192
+ while (Date.now() < deadline) {
193
+ await sleep(70)
194
+ try { return await handshake() }
195
+ catch (err) { if (!['ENOENT', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT'].includes(err.code)) throw err }
196
+ }
197
+ throw new Error('Runner did not become ready in ' + home)
198
+ }
@@ -0,0 +1,165 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { createServer, request as httpRequest, validateHeaderName, validateHeaderValue } from 'node:http'
3
+ import { request as httpsRequest } from 'node:https'
4
+ import { performance } from 'node:perf_hooks'
5
+
6
+ const hopHeaders = new Set(['connection', 'keep-alive', 'proxy-connection', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailer', 'transfer-encoding', 'upgrade'])
7
+ const forbiddenHeaders = new Set([...hopHeaders, 'host', 'content-length', 'expect'])
8
+
9
+ function pathOf(target) {
10
+ if (typeof target !== 'string' || !target.startsWith('/') || target.startsWith('//') || target.includes('#')) return null
11
+ const path = target.split('?')[0]
12
+ try {
13
+ const decoded = decodeURIComponent(path)
14
+ const segments = decoded.slice(1).split('/')
15
+ if (segments.at(-1) === '') segments.pop()
16
+ if (/[\\%\x00-\x20\x7f]/.test(decoded) || /%2f/i.test(path) || segments.some(p => !p || p === '.' || p === '..')) return null
17
+ return decoded
18
+ } catch { return null }
19
+ }
20
+
21
+ export function proxyConfig(input) {
22
+ let upstream
23
+ try { upstream = new URL(input.upstream) } catch { throw new Error('Expected an HTTP(S) upstream origin') }
24
+ if (!['http:', 'https:'].includes(upstream.protocol) || upstream.username || upstream.password || upstream.pathname !== '/' || upstream.search || upstream.hash) throw new Error('Upstream must be an HTTP(S) origin without credentials, path, query or fragment')
25
+ if (!Array.isArray(input.paths) || !input.paths.length || input.paths.some(p => p === '/' || pathOf(p) !== p || p.includes('?'))) throw new Error('Provide canonical absolute path prefixes with --path (repeatable)')
26
+ let header
27
+ try { validateHeaderName(input.header); header = input.header.toLowerCase() } catch { throw new Error('Expected a valid injection header name') }
28
+ if (forbiddenHeaders.has(header)) throw new Error('Cannot inject a routing or hop-by-hop header')
29
+ const maxRequests = Number(input.maxRequests)
30
+ const ttl = Number(input.ttl)
31
+ if (!Number.isSafeInteger(maxRequests) || maxRequests < 1) throw new Error('--max-requests must be a positive integer')
32
+ if (!Number.isSafeInteger(ttl) || ttl < 1 || ttl > 2147483) throw new Error('--ttl must be an integer from 1 to 2147483 seconds')
33
+ return { upstream, paths: [...input.paths], header, maxRequests, ttl }
34
+ }
35
+
36
+ function headersWithoutHop(headers, injected) {
37
+ const blocked = new Set([...hopHeaders, injected, ...(headers.connection || '').toLowerCase().split(',').map(h => h.trim())])
38
+ return Object.fromEntries(Object.entries(headers).filter(([name]) => !blocked.has(name)))
39
+ }
40
+
41
+ function fail(res, status, error) {
42
+ if (res.headersSent) return res.destroy()
43
+ res.writeHead(status, { 'content-type': 'application/json', connection: 'close' })
44
+ res.end(JSON.stringify({ error }))
45
+ }
46
+
47
+ export class CredentialProxies {
48
+ constructor(log = entry => console.log(JSON.stringify(entry))) {
49
+ this.entries = new Map()
50
+ this.log = log
51
+ this.closed = false
52
+ }
53
+
54
+ list() { return [...this.entries.values()].map(entry => ({ ...entry.receipt })) }
55
+
56
+ async open(input) {
57
+ if (this.closed) throw new Error('Credential proxies are shutting down')
58
+ const config = proxyConfig(input)
59
+ let secret = input.value
60
+ try {
61
+ if (typeof secret !== 'string' || !secret) throw new Error()
62
+ validateHeaderValue(config.header, (input.valuePrefix || '') + secret)
63
+ } catch { throw new Error('Invalid credential value (output withheld)') }
64
+ const receipt = { id: randomUUID(), url: null, expiresAt: Date.now() + config.ttl * 1000, maxRequests: config.maxRequests, requests: 0 }
65
+ let injected = (input.valuePrefix || '') + secret
66
+ const active = new Map()
67
+ const sockets = new Set()
68
+ let closed = false
69
+ const server = createServer((req, res) => {
70
+ const started = performance.now()
71
+ const path = pathOf(req.url)
72
+ const safe = value => secret ? value.split(secret).join('[redacted]').split(encodeURIComponent(secret)).join('[redacted]') : value
73
+ const logPath = safe(path || '[invalid path]')
74
+ const logMethod = safe(req.method)
75
+ let logged = false
76
+ const log = () => {
77
+ if (logged) return
78
+ logged = true
79
+ this.log({ method: logMethod, path: logPath, status: res.statusCode, durationMs: Math.round(performance.now() - started), requests: receipt.requests })
80
+ }
81
+ res.once('finish', log)
82
+ res.once('close', log)
83
+ if (closed || Date.now() >= receipt.expiresAt) return fail(res, 410, 'Credential proxy expired or closed')
84
+ if (!path || !config.paths.some(prefix => path === prefix || path.startsWith(prefix.endsWith('/') ? prefix : prefix + '/'))) return fail(res, 403, 'Path is not allowed')
85
+ if (receipt.requests >= receipt.maxRequests) return fail(res, 429, 'Credential proxy request limit reached')
86
+ receipt.requests++
87
+ const headers = headersWithoutHop(req.headers, config.header)
88
+ delete headers.expect
89
+ headers.host = config.upstream.host
90
+ headers[config.header] = injected
91
+ const queryIndex = req.url.indexOf('?')
92
+ const upstreamPath = encodeURI(path) + (queryIndex >= 0 ? req.url.slice(queryIndex) : '')
93
+ const request = config.upstream.protocol === 'https:' ? httpsRequest : httpRequest
94
+ const upstream = request(config.upstream, { method: req.method, path: upstreamPath, headers, agent: false, timeout: 60000 }, response => {
95
+ if (response.statusCode >= 300 && response.statusCode < 400) {
96
+ fail(res, 502, 'Upstream redirects are not allowed')
97
+ response.destroy()
98
+ return
99
+ }
100
+ const responseHeaders = headersWithoutHop(response.headers, config.header)
101
+ for (const [key, value] of Object.entries(responseHeaders)) {
102
+ if (JSON.stringify(value).includes(secret)) delete responseHeaders[key]
103
+ }
104
+ res.writeHead(response.statusCode, responseHeaders)
105
+ res.flushHeaders()
106
+ response.once('error', () => fail(res, 502, 'Upstream response failed'))
107
+ response.pipe(res)
108
+ })
109
+ active.set(upstream, res)
110
+ upstream.once('timeout', () => { fail(res, 504, 'Upstream request timed out'); upstream.destroy() })
111
+ upstream.once('error', () => fail(res, 502, 'Upstream request failed'))
112
+ upstream.once('close', () => active.delete(upstream))
113
+ req.once('error', () => upstream.destroy())
114
+ res.once('close', () => upstream.destroy())
115
+ req.pipe(upstream)
116
+ })
117
+ server.on('connection', socket => {
118
+ sockets.add(socket)
119
+ socket.once('close', () => sockets.delete(socket))
120
+ })
121
+ server.on('connect', (req, socket) => socket.end('HTTP/1.1 405 Method Not Allowed\r\nConnection: close\r\n\r\n'))
122
+ server.on('upgrade', (req, socket) => socket.end('HTTP/1.1 405 Method Not Allowed\r\nConnection: close\r\n\r\n'))
123
+ await new Promise((resolve, reject) => {
124
+ server.once('error', () => reject(new Error('Could not listen on credential proxy loopback port')))
125
+ server.listen(0, '127.0.0.1', resolve)
126
+ })
127
+ receipt.url = 'http://127.0.0.1:' + server.address().port
128
+ let timer
129
+ const close = async () => {
130
+ if (closed) return
131
+ closed = true
132
+ clearTimeout(timer)
133
+ this.entries.delete(receipt.id)
134
+ const done = new Promise(resolve => server.close(resolve))
135
+ for (const [upstream, res] of active) {
136
+ fail(res, 410, 'Credential proxy expired or closed')
137
+ upstream.destroy()
138
+ }
139
+ for (const socket of sockets) socket.destroy()
140
+ await done
141
+ secret = ''
142
+ injected = ''
143
+ }
144
+ if (this.closed) {
145
+ await close()
146
+ throw new Error('Credential proxies are shutting down')
147
+ }
148
+ this.entries.set(receipt.id, { receipt, close })
149
+ timer = setTimeout(close, Math.max(1, receipt.expiresAt - Date.now()))
150
+ timer.unref?.()
151
+ return { ...receipt }
152
+ }
153
+
154
+ async close(id) {
155
+ const entry = this.entries.get(id)
156
+ if (!entry) throw new Error('Unknown or expired credential proxy')
157
+ await entry.close()
158
+ return { id: entry.receipt.id, status: 'closed' }
159
+ }
160
+
161
+ async closeAll() {
162
+ this.closed = true
163
+ await Promise.all([...this.entries.values()].map(entry => entry.close()))
164
+ }
165
+ }
@@ -0,0 +1,23 @@
1
+ import { execFile } from 'node:child_process'
2
+ import { validateHeaderValue } from 'node:http'
3
+ import { promisify } from 'node:util'
4
+
5
+ const exec = promisify(execFile)
6
+
7
+ export async function credentialValue(opt) {
8
+ if (Boolean(opt['value-env']) === Boolean(opt['value-command'])) throw new Error('Choose exactly one of --value-env or --value-command')
9
+ let value
10
+ if (opt['value-env']) {
11
+ value = process.env[opt['value-env']]
12
+ delete process.env[opt['value-env']]
13
+ } else {
14
+ try {
15
+ const result = await exec('/bin/sh', ['-c', opt['value-command']], { encoding: 'utf8', timeout: 10000, maxBuffer: 16384 })
16
+ value = result.stdout.replace(/[\r\n]+$/, '')
17
+ } catch { throw new Error('Credential command failed (output withheld)') }
18
+ }
19
+ if (!value) throw new Error('Credential source returned an empty value')
20
+ try { validateHeaderValue('credential', (opt['value-prefix'] || '') + value) }
21
+ catch { throw new Error('Credential source returned an invalid header value (output withheld)') }
22
+ return value
23
+ }