@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,138 @@
1
+ import { readFileSync, existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { home } from './paths.mjs'
4
+
5
+ export const modalities = ['text', 'image', 'audio', 'video', 'pdf']
6
+ export const transportInputs = {
7
+ codex: { text: true, image: true, audio: true, video: false, pdf: false },
8
+ claude: { text: true, image: true, audio: false, video: false, pdf: true },
9
+ pi: { text: true, image: true, audio: false, video: false, pdf: false },
10
+ agy: { text: true, image: true, audio: true, video: true, pdf: true },
11
+ 'cursor-agent': { text: true, image: 'unknown', audio: 'unknown', video: 'unknown', pdf: 'unknown' },
12
+ custom: { text: true, image: 'unknown', audio: 'unknown', video: 'unknown', pdf: 'unknown' }
13
+ }
14
+
15
+ const agyEffortSuffix = /-(?:extra-high|xhigh|medium|minimal|high|low|max|none|off|ultra)$/
16
+ const olderTiers = new Set(['previous generation', 'superseded', 'being retired'])
17
+ const currentTiers = new Set(['flagship', 'workhorse', 'fast'])
18
+
19
+ export function modelKey(id) {
20
+ return id.split('/').at(-1).replace(/\[[^\]]*\]$/, '').replace(/(?:-(?:extra-high|off|none|minimal|low|medium|high|xhigh|max|ultra|fast|thinking))+$/, '')
21
+ }
22
+
23
+ export function hiddenModel(id, hidden = []) {
24
+ return hidden.some(item => item === id || modelKey(item) === modelKey(id))
25
+ }
26
+
27
+ export function matchModel(id, catalog) {
28
+ const exact = catalog.models.find(m => m.id === id || m.aliases?.includes(id))
29
+ if (exact) return exact
30
+ const key = modelKey(id)
31
+ return catalog.models.find(m => m.id === key || m.aliases?.includes(key)) ?? null
32
+ }
33
+
34
+ export function loadCatalog(path = join(home, 'models.json'), now = Date.now()) {
35
+ const catalog = JSON.parse(readFileSync(new URL('./model-catalog.json', import.meta.url), 'utf8'))
36
+ const warnings = []
37
+ if (existsSync(path)) {
38
+ try {
39
+ const extra = JSON.parse(readFileSync(path, 'utf8'))
40
+ if (!extra || !Array.isArray(extra.models) || extra.models.some(m => !m || typeof m.id !== 'string' || !m.id)) throw new Error('Expected models with IDs')
41
+ if (extra.models.some(m => m.aliases !== undefined && (!Array.isArray(m.aliases) || m.aliases.some(id => typeof id !== 'string')) || m.modalities != null && (typeof m.modalities !== 'object' || m.modalities.input !== undefined && (!Array.isArray(m.modalities.input) || m.modalities.input.some(v => !modalities.includes(v)))))) throw new Error('Invalid model metadata')
42
+ if (extra.picks && Object.values(extra.picks).some(p => !p || !Array.isArray(p.first) || !Array.isArray(p.backup))) throw new Error('Invalid model picks')
43
+ if (new Set(extra.models.map(m => m.id)).size !== extra.models.length) throw new Error('Duplicate model IDs')
44
+ for (const entry of extra.models) {
45
+ const i = catalog.models.findIndex(m => m.id === entry.id), old = catalog.models[i] ?? {}
46
+ const merged = { ...old, ...entry, ...((entry.as_of ?? extra.as_of) ? { as_of: entry.as_of ?? extra.as_of } : {}), ...(extra.source ? { source: extra.source } : {}) }
47
+ for (const key of ['summary', 'modalities']) if (entry[key] && typeof entry[key] === 'object') merged[key] = { ...old[key], ...entry[key] }
48
+ if (i === -1) catalog.models.push(merged)
49
+ else catalog.models[i] = merged
50
+ }
51
+ if (extra.picks && typeof extra.picks === 'object') catalog.picks = { ...catalog.picks, ...extra.picks }
52
+ if (typeof extra.effort_advice === 'string') catalog.effort_advice = extra.effort_advice
53
+ } catch { warnings.push('Could not load models.json overrides; using the built-in model catalog.') }
54
+ }
55
+ const age = date => {
56
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(date ?? '')) return null
57
+ const stamp = Date.parse(date + 'T00:00:00Z')
58
+ return Number.isFinite(stamp) && new Date(stamp).toISOString().slice(0, 10) === date ? Math.max(0, Math.floor((now - stamp) / 86400000)) : null
59
+ }
60
+ catalog.models = catalog.models.map(m => ({ ...m, as_of: m.as_of ?? catalog.as_of, source: m.source ?? catalog.source, age_days: age(m.as_of ?? catalog.as_of), stale: age(m.as_of ?? catalog.as_of) === null || age(m.as_of ?? catalog.as_of) > 60 }))
61
+ return { ...catalog, warnings, stale_after_days: 60 }
62
+ }
63
+
64
+ const limit = n => Number.isSafeInteger(n) && n > 0 ? n : null
65
+ export function modelCapabilities(worker, model, description) {
66
+ const kind = worker.startsWith('pi:') ? 'pi' : worker.startsWith('custom:') ? 'custom' : worker
67
+ const transport = transportInputs[kind] ?? transportInputs.custom
68
+ const fallback = description?.modalities ?? {}
69
+ const declared = Array.isArray(model.input) ? model.input : Array.isArray(fallback.input) ? fallback.input : null
70
+ const inputs = Object.fromEntries(modalities.map(type => {
71
+ const canModel = declared ? declared.includes(type) : 'unknown'
72
+ const canCLI = transport[type]
73
+ if (type === 'text') return [type, canModel !== false && canCLI !== false]
74
+ return [type, canModel === false || canCLI === false ? false : canModel === true && canCLI === true ? true : 'unknown']
75
+ }))
76
+ const context = limit(model.contextWindow), output = limit(model.maxTokens)
77
+ return {
78
+ input: inputs,
79
+ cli_input: transport,
80
+ model_input: declared ?? 'unknown',
81
+ context_tokens: context ?? limit(fallback.context_tokens) ?? 'unknown',
82
+ max_output_tokens: output ?? limit(fallback.max_output_tokens) ?? 'unknown',
83
+ sources: {
84
+ input: Array.isArray(model.input) ? 'cli' : declared ? 'catalog' : 'unknown',
85
+ context_tokens: context ? 'cli' : limit(fallback.context_tokens) ? 'catalog-api-limit' : 'unknown',
86
+ max_output_tokens: output ? 'cli' : limit(fallback.max_output_tokens) ? 'catalog-api-limit' : 'unknown'
87
+ }
88
+ }
89
+ }
90
+
91
+ export function resolveAgyModel(model, effort, listedIds = []) {
92
+ if (!model || model === 'agy') return { model: model || 'agy', passEffortFlag: !!effort }
93
+ const base = model.replace(agyEffortSuffix, '')
94
+ const list = Array.isArray(listedIds) ? listedIds : []
95
+ const knownFamily = /^gemini-(?!pro-orchestrator(?:$|-))/.test(base)
96
+ if (effort) {
97
+ const candidate = base + '-' + effort
98
+ if ((list.length === 0 && (model !== base || knownFamily)) || list.includes(candidate)) {
99
+ return { model: candidate, passEffortFlag: false }
100
+ }
101
+ if (list.length > 0 && !list.includes(candidate) && list.includes(model)) {
102
+ return { model, passEffortFlag: true }
103
+ }
104
+ if (list.length > 0 && list.some(id => id.startsWith(base + '-') && agyEffortSuffix.test(id))) {
105
+ return { model: candidate, passEffortFlag: false }
106
+ }
107
+ return { model, passEffortFlag: true }
108
+ }
109
+ if (list.length > 0) {
110
+ if (list.includes(model)) return { model, passEffortFlag: false }
111
+ const resolved = list.find(id => id === base + '-medium') ?? list.find(id => id.startsWith(base + '-') && agyEffortSuffix.test(id)) ?? model
112
+ return { model: resolved, passEffortFlag: false }
113
+ }
114
+ if (model === base && knownFamily) return { model: base + '-medium', passEffortFlag: false }
115
+ return { model, passEffortFlag: false }
116
+ }
117
+
118
+ export function staleDefaultWarning(workerId, defaultModel, detectedModels = [], catalog = loadCatalog()) {
119
+ if (!defaultModel || defaultModel === workerId) return null
120
+ const matched = matchModel(defaultModel, catalog)
121
+ if (matched) {
122
+ if (!olderTiers.has(matched.tier)) return null
123
+ return 'Default model ' + defaultModel + ' belongs to an older or superseded family (' + matched.tier + '). Switch it with broco defaults ' + workerId + ' --model ID.'
124
+ }
125
+ const current = [...new Set((detectedModels ?? [])
126
+ .map(m => typeof m === 'string' ? m : m?.id)
127
+ .filter(Boolean)
128
+ .map(id => ({ id, cat: matchModel(id, catalog) }))
129
+ .filter(x => x.cat && currentTiers.has(x.cat.tier))
130
+ .map(x => x.cat.id))]
131
+ if (!current.length) return null
132
+ return 'Default model ' + defaultModel + ' is not in the current model catalog (older or superseded), while ' + workerId + ' offers current models (' + current.slice(0, 3).join(', ') + '). Switch it with broco defaults ' + workerId + ' --model ID.'
133
+ }
134
+
135
+ export function visiblePicks(catalog, workers) {
136
+ const visible = new Set(workers.flatMap(w => w.allowed ? w.models.map(m => m.catalog?.id).filter(Boolean) : []))
137
+ return Object.fromEntries(Object.entries(catalog.picks ?? {}).map(([key, pick]) => [key, { ...pick, first: (pick.first ?? []).filter(id => visible.has(id)), backup: (pick.backup ?? []).filter(id => visible.has(id)) }]))
138
+ }
@@ -0,0 +1,11 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks'
2
+
3
+ const current = new AsyncLocalStorage()
4
+ const reserved = /^(?:BROCO_(?:HOME|PORT|CONCURRENCY|MARKER|BASH_ENV|TZ|IN_WORKER|API_KEY|FORCE_LAUNCHD)|CC_BRO_(?:HOME|PORT|CONCURRENCY|MARKER|BASH_ENV)|PI_(?:CODING_AGENT_.*|OFFLINE|TELEMETRY)|GIT_(?:DIR|WORK_TREE|INDEX_FILE|OBJECT_DIRECTORY|COMMON_DIR)|NPM_CONFIG_PREFIX|npm_config_prefix|CLAUDECODE|NODE_OPTIONS|NODE_PATH|DYLD_.*|LD_.*|BASH_ENV|ENV|PATH|HOME|TMPDIR|PWD|OLDPWD|SHELL|SHLVL|TZ|_)$/
5
+
6
+ export function modelVariables(env) {
7
+ return Object.fromEntries(Object.entries(env).filter(([key, value]) => /^[A-Za-z_]\w*$/.test(key) && !reserved.test(key) && typeof value === 'string'))
8
+ }
9
+
10
+ export function withModelEnvironment(env, run) { return current.run(modelVariables(env ?? {}), run) }
11
+ export function modelEnvironment() { return { ...process.env, ...current.getStore() } }
package/lib/native.mjs ADDED
@@ -0,0 +1,228 @@
1
+ import { execFileSync, spawn } from 'node:child_process'
2
+ import { closeSync, mkdirSync, openSync } from 'node:fs'
3
+ import { dirname } from 'node:path'
4
+ import { createInterface } from 'node:readline'
5
+ import { modelEnvironment } from './model-env.mjs'
6
+ import { parseRedactedJson, redactStderr, secretsFrom } from './pi-secrets.mjs'
7
+ import { classify, codexClass } from './problems.mjs'
8
+ import { codexQuota } from './quota.mjs'
9
+ import { HOST_SESSION_VARS } from './templates.mjs'
10
+
11
+ function commonGitDir(cwd) {
12
+ try {
13
+ const out = execFileSync('git', ['--no-optional-locks', '-C', cwd, 'rev-parse', '--path-format=absolute', '--git-common-dir'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
14
+ return out && out !== cwd ? out : null
15
+ } catch { return null }
16
+ }
17
+
18
+ function channel(file, args, ctx, receive) {
19
+ mkdirSync(dirname(ctx.marker), { recursive: true })
20
+ const fd = openSync(ctx.marker, 'a', 0o600)
21
+ let child, stderr = ''
22
+ const env = { ...modelEnvironment(), BROCO_IN_WORKER: '1' }
23
+ for (const key of HOST_SESSION_VARS) delete env[key]
24
+ const rawSecrets = secretsFrom([], env)
25
+ try {
26
+ child = spawn(file, args, { cwd: ctx.cwd, env, stdio: ['pipe', 'pipe', 'pipe', fd], signal: ctx.signal })
27
+ } finally { closeSync(fd) }
28
+ ctx.owner(child.pid ?? null)
29
+ let failure
30
+ child.on('error', e => { failure = e })
31
+ child.stdin.on('error', () => {})
32
+ child.stderr.on('data', part => { stderr = (stderr + part.toString('utf8')).slice(-4096) })
33
+ const rl = createInterface({ input: child.stdout, crlfDelay: Infinity })
34
+ rl.on('line', line => {
35
+ if (!line.trim() || failure) return
36
+ try { receive(parseRedactedJson(line, rawSecrets)) }
37
+ catch (err) { failure = err; child.kill() }
38
+ })
39
+ const closed = new Promise(resolve => child.once('close', (code, signal) => {
40
+ rl.close()
41
+ if (failure) return resolve(failure)
42
+ if (code === 0) return resolve(null)
43
+ const detail = redactStderr(stderr, env, rawSecrets)
44
+ resolve(new Error(file + ' exited: ' + (signal || code) + (detail ? ': ' + detail : '')))
45
+ }))
46
+ return { child, closed }
47
+ }
48
+
49
+ async function streamed(ctx, file, args, input, parse) {
50
+ const ch = channel(file, args, ctx, parse)
51
+ ch.child.stdin.end(input)
52
+ const error = await ch.closed
53
+ if (error) throw error
54
+ }
55
+
56
+ function cached(ctx, n, total) {
57
+ if (Number.isSafeInteger(n) && n >= 0) ctx.event('usage', { cachedInputTokens: n, inputTokens: Number.isSafeInteger(total) && total >= n ? total : null }, 'Cached input: ' + n + ' tokens')
58
+ }
59
+
60
+ async function codexExec(ctx) {
61
+ if (ctx.attachments?.some(f => f.kind !== 'image')) throw new Error('Codex exec fallback cannot send audio attachments')
62
+ let reply, finished = false, failed
63
+ const gitDir = ctx.worktree && !ctx.review ? commonGitDir(ctx.cwd) : null
64
+ const args = ['exec', ...(ctx.session ? ['resume'] : []), '--json']
65
+ if (!ctx.session && (ctx.review || ctx.worktree)) args.push('--sandbox', ctx.review ? 'read-only' : 'workspace-write')
66
+ if (gitDir) {
67
+ if (!ctx.session) args.push('--add-dir', gitDir)
68
+ args.push('-c', 'sandbox_workspace_write.writable_roots=' + JSON.stringify([gitDir]))
69
+ }
70
+ if (ctx.model !== 'codex') args.push('--model', ctx.model)
71
+ if (ctx.thinking) args.push('-c', 'model_reasoning_effort=' + JSON.stringify(ctx.thinking))
72
+ for (const file of ctx.attachments ?? []) args.push('--image', file.path)
73
+ if (ctx.session) args.push(ctx.session)
74
+ args.push('-')
75
+ await streamed(ctx, 'codex', args, ctx.text, e => {
76
+ if (e.type === 'thread.started') ctx.bind(e.thread_id, 'exec')
77
+ if (e.type === 'item.completed' && e.item?.type === 'agent_message') reply = e.item.text
78
+ else if (e.item?.type) ctx.event(e.type, {}, e.item.type + ': ' + e.type)
79
+ if (e.type === 'turn.failed') failed = e.error?.message || 'Codex turn failed'
80
+ if (e.type === 'turn.completed') { finished = true; cached(ctx, e.usage?.cached_input_tokens, e.usage?.input_tokens) }
81
+ }).catch(err => { throw new Error(failed || err.message) })
82
+ if (!finished || !reply || failed) throw new Error(failed || 'Codex exec returned no final result')
83
+ return reply
84
+ }
85
+
86
+ function resetOf(snapshot) {
87
+ const windows = [snapshot?.primary, snapshot?.secondary].filter(w => w && Number.isSafeInteger(w.resetsAt))
88
+ return windows.sort((a, b) => b.usedPercent - a.usedPercent)[0]?.resetsAt ?? null
89
+ }
90
+
91
+ function failure(message, info, snapshot) {
92
+ const cls = codexClass(info)
93
+ return Object.assign(new Error(message), { problem: classify(message, { codex: info, resetsAt: cls === 'quota' ? resetOf(snapshot) : null }) })
94
+ }
95
+
96
+ async function appServer(ctx) {
97
+ let next = 0, thread, reply, terminal = false, snapshot = null, lastError = null
98
+ const quota = (raw, allowed) => {
99
+ snapshot = { ...snapshot, ...Object.fromEntries(Object.entries(raw ?? {}).filter(([, v]) => v !== null && v !== undefined)) }
100
+ const q = codexQuota(snapshot, allowed)
101
+ if (q) ctx.event('quota', q, '')
102
+ }
103
+ const pending = new Map()
104
+ const done = Promise.withResolvers()
105
+ done.promise.catch(() => {})
106
+ const ch = channel('codex', ['app-server'], ctx, e => {
107
+ if (e.id !== undefined) {
108
+ if (e.method) {
109
+ send({ id: e.id, error: { code: -32601, message: 'No interactive manager attached' } })
110
+ return
111
+ }
112
+ const waiter = pending.get(e.id)
113
+ if (waiter) {
114
+ pending.delete(e.id)
115
+ if (e.error) waiter.reject(Object.assign(new Error(e.error.message), { rpcCode: e.error.code }))
116
+ else waiter.resolve(e.result)
117
+ }
118
+ return
119
+ }
120
+ const p = e.params
121
+ if (e.method === 'account/rateLimits/updated') { if (thread && !terminal) quota(p?.rateLimits); return }
122
+ if (!thread || p?.threadId !== thread || terminal) return
123
+ if (e.method === 'error') {
124
+ lastError = p.error
125
+ const info = p.error?.codexErrorInfo
126
+ const exhausted = ['usageLimitExceeded', 'sessionBudgetExceeded'].includes(typeof info === 'string' ? info : '') || (codexClass(info) === 'quota' && codexQuota(snapshot)?.reached)
127
+ ctx.event('provider.error', { codexErrorInfo: p.error?.codexErrorInfo ?? null, willRetry: !!p.willRetry }, (p.willRetry ? 'Provider error, Codex retrying: ' : 'Provider error: ') + String(p.error?.message ?? '').slice(0, 160))
128
+ if (p.willRetry && exhausted) { terminal = true; done.reject(failure(p.error?.message || 'Codex quota exhausted', p.error?.codexErrorInfo, snapshot)) }
129
+ return
130
+ }
131
+ if (e.method === 'thread/tokenUsage/updated') cached(ctx, p.tokenUsage?.last?.cachedInputTokens, p.tokenUsage?.last?.inputTokens)
132
+ if (e.method === 'item/started' || e.method === 'item/completed') {
133
+ const item = p.item
134
+ if (item.type === 'agentMessage' && e.method === 'item/completed') reply = item.text
135
+ const target = item.type === 'fileChange' ? item.changes?.map(f => f.path).join(', ') : ''
136
+ ctx.event(e.method, { type: item.type }, [item.type, e.method.endsWith('started') ? 'started' : 'completed', target?.slice(0, 180)].filter(Boolean).join(' '))
137
+ }
138
+ if (e.method === 'turn/completed') {
139
+ terminal = true
140
+ if (p.turn.status === 'completed' && reply) done.resolve(reply)
141
+ else {
142
+ const error = p.turn.error ?? lastError
143
+ done.reject(failure(error?.message || 'Codex turn ' + p.turn.status + ' without a final reply', error?.codexErrorInfo, snapshot))
144
+ }
145
+ }
146
+ })
147
+ const send = obj => ch.child.stdin.write(JSON.stringify(obj) + '\n')
148
+ const closed = ch.closed.then(e => { throw e || new Error('Codex app-server closed') })
149
+ closed.catch(() => {})
150
+ async function request(method, params) {
151
+ const id = ++next
152
+ const waiter = Promise.withResolvers()
153
+ pending.set(id, waiter)
154
+ const timer = setTimeout(() => waiter.reject(new Error('Timed out: ' + method)), 30000)
155
+ timer.unref?.()
156
+ try { send({ id, method, params }); return await Promise.race([waiter.promise, closed]) }
157
+ finally { clearTimeout(timer); pending.delete(id) }
158
+ }
159
+ try {
160
+ try { await request('initialize', { clientInfo: { name: 'broco', version: '1.0.0' }, capabilities: {} }) }
161
+ catch (err) { err.beforeThread = true; throw err }
162
+ send({ method: 'initialized' })
163
+ const params = { cwd: ctx.cwd, approvalPolicy: 'never' }
164
+ if (ctx.review || ctx.worktree) params.sandbox = ctx.review ? 'read-only' : 'workspace-write'
165
+ const gitDir = ctx.worktree && !ctx.review ? commonGitDir(ctx.cwd) : null
166
+ if (gitDir) {
167
+ params.writableRoots = [gitDir]
168
+ params.config = { sandbox_workspace_write: { writable_roots: [gitDir] } }
169
+ }
170
+ if (ctx.model !== 'codex') params.model = ctx.model
171
+ let res
172
+ try {
173
+ res = ctx.session ? await request('thread/resume', { ...params, threadId: ctx.session }) : await request('thread/start', params)
174
+ } catch (err) {
175
+ if (err.rpcCode === -32601) err.beforeThread = true
176
+ throw err
177
+ }
178
+ thread = res.thread.id
179
+ ctx.bind(thread, 'app-server')
180
+ let timer
181
+ try {
182
+ const slow = new Promise(resolve => { timer = setTimeout(resolve, 5000, null); timer.unref?.() })
183
+ const limits = await Promise.race([request('account/rateLimits/read', {}).catch(() => null), slow])
184
+ if (limits) quota(limits.rateLimits, limits.ordinaryUsageAllowed)
185
+ } finally { clearTimeout(timer) }
186
+ if (!ctx.session) await request('thread/name/set', { threadId: thread, name: ctx.name })
187
+ const started = await request('turn/start', { threadId: thread, ...(ctx.thinking ? { effort: ctx.thinking } : {}), input: [{ type: 'text', text: ctx.text, text_elements: [] }, ...(ctx.attachments ?? []).map(f => ({ type: f.kind === 'image' ? 'localImage' : 'localAudio', path: f.path }))] })
188
+ const turn = started?.turn?.id
189
+ ctx.ready(turn ? async text => {
190
+ if (terminal) return false
191
+ try {
192
+ const res = await request('turn/steer', { threadId: thread, expectedTurnId: turn, input: [{ type: 'text', text, text_elements: [] }] })
193
+ if (res?.turnId !== turn) throw new Error('Unconfirmed turn/steer acknowledgement')
194
+ return true
195
+ } catch (err) {
196
+ if (err.rpcCode !== undefined) return false
197
+ throw err
198
+ }
199
+ } : null)
200
+ const result = await Promise.race([done.promise, closed])
201
+ let after
202
+ try {
203
+ const slow = new Promise(resolve => { after = setTimeout(resolve, 2000, null); after.unref?.() })
204
+ const limits = await Promise.race([request('account/rateLimits/read', {}).catch(() => null), slow])
205
+ if (limits) quota(limits.rateLimits, limits.ordinaryUsageAllowed)
206
+ } finally { clearTimeout(after) }
207
+ return result
208
+ } finally {
209
+ ch.child.stdin.end()
210
+ ch.child.kill()
211
+ const kill = setTimeout(() => ch.child.kill('SIGKILL'), 2000)
212
+ kill.unref?.()
213
+ try { await ch.closed } finally { clearTimeout(kill) }
214
+ }
215
+ }
216
+
217
+ export async function nativeTurn(ctx) {
218
+ if (ctx.provider !== 'codex') throw new Error('Expected a codex worker')
219
+ if (ctx.transport !== 'exec') {
220
+ try { return await appServer(ctx) }
221
+ catch (err) {
222
+ if (!err.beforeThread || ctx.signal.aborted) throw err
223
+ ctx.event('fallback', {}, 'App-server unavailable; using codex exec')
224
+ }
225
+ }
226
+ ctx.ready(null)
227
+ return codexExec(ctx)
228
+ }
package/lib/paths.mjs ADDED
@@ -0,0 +1,63 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { chmodSync, existsSync, lstatSync, mkdirSync, realpathSync } from 'node:fs'
3
+ import { homedir } from 'node:os'
4
+ import { basename, delimiter, dirname, join, resolve, sep } from 'node:path'
5
+ import { fileURLToPath } from 'node:url'
6
+
7
+ export const repo = fileURLToPath(new URL('../', import.meta.url))
8
+
9
+ export function canonical(path) {
10
+ if (existsSync(path)) return realpathSync.native(path)
11
+ return join(canonical(dirname(path)), basename(path))
12
+ }
13
+
14
+ export const personalPi = canonical(join(homedir(), '.pi'))
15
+ export const checkout = existsSync(join(repo, '.git'))
16
+ export const home = canonical(resolve(process.env.BROCO_HOME || join(homedir(), '.broco')))
17
+ if (home === personalPi || home.startsWith(personalPi + sep) || personalPi.startsWith(home + sep) || home === '/') {
18
+ throw new Error('Choose a broco data directory separate from the personal pi profile.')
19
+ }
20
+
21
+ export function legacyCcBroHome(env = process.env) {
22
+ return canonical(resolve(env?.CC_BRO_HOME || join(homedir(), '.cc-bro')))
23
+ }
24
+
25
+ const sandbox = canonical(join(repo, '.sandbox'))
26
+ export const isolated = home === sandbox || home.startsWith(sandbox + sep)
27
+ export const toolsPath = isolated ? [join(home, 'tools', 'bin'), process.env.PATH].join(delimiter) : process.env.PATH
28
+
29
+ function ensureSecureDir(dir, uid) {
30
+ mkdirSync(dir, { recursive: true, mode: 0o700 })
31
+ const st = lstatSync(dir)
32
+ if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== uid) {
33
+ throw new Error('Unsafe socket directory ownership: ' + dir)
34
+ }
35
+ if ((st.mode & 0o077) !== 0) {
36
+ chmodSync(dir, 0o700)
37
+ if ((lstatSync(dir).mode & 0o077) !== 0) {
38
+ throw new Error('Unsafe socket directory permissions: ' + dir)
39
+ }
40
+ }
41
+ return dir
42
+ }
43
+
44
+ export function socketFor(dir) {
45
+ const resolved = canonical(resolve(dir))
46
+ const key = createHash('sha256').update(resolved).digest('hex').slice(0, 24)
47
+ if (process.platform === 'win32') return '\\\\.\\pipe\\broco-' + key
48
+ const uid = process.getuid()
49
+ const sockName = 's-' + key + '.sock'
50
+ try {
51
+ const runtimeDir = ensureSecureDir(join('/tmp', 'broco-sock-' + uid), uid)
52
+ return join(runtimeDir, sockName)
53
+ } catch (err) {
54
+ const direct = join(resolved, sockName)
55
+ if (Buffer.byteLength(direct) < 96) {
56
+ ensureSecureDir(resolved, uid)
57
+ return direct
58
+ }
59
+ throw err
60
+ }
61
+ }
62
+
63
+ export const socketPath = socketFor(home)
@@ -0,0 +1,29 @@
1
+ import { accessSync, constants, readFileSync, realpathSync, statSync } from 'node:fs'
2
+ import { delimiter, dirname, join } from 'node:path'
3
+
4
+ const sdkCache = new Map()
5
+
6
+ export function sdkRoot(path) {
7
+ const key = String(path ?? '')
8
+ if (sdkCache.has(key)) return sdkCache.get(key)
9
+ let found = null
10
+ for (const part of key.split(delimiter)) {
11
+ if (!part) continue
12
+ const exe = join(part, 'pi')
13
+ try { accessSync(exe, constants.X_OK); if (!statSync(exe).isFile()) continue } catch { continue }
14
+ let dir = dirname(realpathSync(exe))
15
+ while (dirname(dir) !== dir) {
16
+ try {
17
+ if (JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).name === '@earendil-works/pi-coding-agent') {
18
+ found = dir
19
+ break
20
+ }
21
+ } catch {}
22
+ dir = dirname(dir)
23
+ }
24
+ break
25
+ }
26
+ if (sdkCache.size > 64) sdkCache.clear()
27
+ sdkCache.set(key, found)
28
+ return found
29
+ }
@@ -0,0 +1,48 @@
1
+ export function secretsFrom(records, env) {
2
+ const values = new Set(
3
+ Object.entries(env ?? {})
4
+ .filter(([name, value]) => /(?:API_KEY|AUTH_KEY|PRIVATE_KEY|SECRET|TOKEN|ACCESS_KEY|PASSWORD|PASSWD)$/i.test(name) && typeof value === 'string' && value)
5
+ .map(([, v]) => v)
6
+ )
7
+ function visit(obj, inHeaders = false) {
8
+ for (const [key, v] of Object.entries(obj ?? {})) {
9
+ if (v && typeof v === 'object') visit(v, key === 'headers')
10
+ if (typeof v !== 'string' || !v || (!inHeaders && !/^(key|apiKey|api-key|api_key|secret|secret_key|client_secret|password|access|refresh|id_token|access_token|refresh_token|authorization|proxy-authorization|x-api-key|cookie|set-cookie)$/i.test(key))) continue
11
+ if (!v.startsWith('$')) values.add(v)
12
+ for (const m of v.matchAll(/\$(?:\{([A-Za-z_]\w*)\}|([A-Za-z_]\w*))/g)) {
13
+ const value = env?.[m[1] ?? m[2]]
14
+ if (value) values.add(value)
15
+ }
16
+ }
17
+ }
18
+ for (const record of records ?? []) visit(record)
19
+ return [...values].filter(v => v.length >= 4).sort((a, b) => b.length - a.length)
20
+ }
21
+
22
+ export function mask(text, secrets) {
23
+ for (const secret of secrets) text = text.replaceAll(secret, '[redacted]')
24
+ return text
25
+ }
26
+
27
+ export function redactKey(key) {
28
+ return /^(authorization|proxy-authorization|x-api-key|api-key|api_key|apiKey|secret_key|client_secret|access_token|refresh_token|id_token|cookie|set-cookie)$/i.test(key)
29
+ }
30
+
31
+ export function parseRedactedJson(line, rawSecrets = []) {
32
+ return JSON.parse(line, (key, value) => {
33
+ if (redactKey(key)) return '[redacted]'
34
+ if (typeof value === 'string' && value && rawSecrets.length) return mask(value, rawSecrets)
35
+ return value
36
+ })
37
+ }
38
+
39
+ export function redactStderr(stderr, env, extraSecrets = []) {
40
+ if (!stderr) return ''
41
+ const secrets = [...new Set([...secretsFrom([], env), ...extraSecrets].filter(v => typeof v === 'string' && v.length >= 4))].sort((a, b) => b.length - a.length)
42
+ return mask(stderr, secrets)
43
+ .replace(/\x1b\[[0-9;]*m/g, '')
44
+ .replace(/(?:Bearer\s+|(?:api[_-]?key|auth[_-]?key|private[_-]?key|token|secret|password)\s*[=:]\s*)\S+/gi, '[redacted]')
45
+ .replace(/\b[\w.+-]+@[\w.-]+\.[a-z]+\b/gi, '[account]')
46
+ .trim()
47
+ .slice(0, 1200)
48
+ }