@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/quota.mjs ADDED
@@ -0,0 +1,132 @@
1
+ import { formatShortTime, resolvedTimeZone } from './time.mjs'
2
+
3
+ const iso = seconds => Number.isSafeInteger(seconds) ? new Date(seconds * 1000).toISOString() : null
4
+ const resetIso = value => typeof value === 'string' && value && Number.isFinite(Date.parse(value)) ? value : iso(value)
5
+
6
+ function windowMinutes(value) {
7
+ if (Number.isSafeInteger(value) && value > 0) return value
8
+ if (typeof value !== 'string') return null
9
+ const clean = value.trim().toLowerCase()
10
+ if (clean === 'weekly' || clean === 'week') return 10080
11
+ if (clean === 'daily') return 1440
12
+ const match = /^(\d+)([mhd])$/.exec(clean)
13
+ if (!match) return null
14
+ const amount = Number(match[1])
15
+ return match[2] === 'h' ? amount * 60 : match[2] === 'd' ? amount * 1440 : amount
16
+ }
17
+
18
+ export function codexQuota(snapshot, allowed = null) {
19
+ if (!snapshot || typeof snapshot !== 'object') return null
20
+ const windows = [snapshot.primary, snapshot.secondary].filter(w => w && Number.isFinite(w.usedPercent)).map(w => ({
21
+ minutes: Number.isSafeInteger(w.windowDurationMins) ? w.windowDurationMins : null, usedPercent: w.usedPercent, resetsAt: iso(w.resetsAt)
22
+ }))
23
+ const reached = !!snapshot.rateLimitReachedType || allowed === false || windows.some(w => w.usedPercent >= 100)
24
+ const avoid = reached || windows.some(w => w.usedPercent >= 90)
25
+ const plentiful = !avoid && windows.length > 0 && windows.every(w => w.usedPercent <= 25)
26
+ return { source: 'native', plan: snapshot.planType ?? null, windows, reached, avoid: Boolean(avoid), plentiful, at: new Date().toISOString() }
27
+ }
28
+
29
+ export function agyQuota(usageData) {
30
+ if (!usageData || typeof usageData !== 'object') return null
31
+ if (usageData.status && usageData.status !== 'SUCCESS') return null
32
+ const data = usageData.command?.data ?? usageData.data ?? usageData
33
+ if (!data || typeof data !== 'object' || !Array.isArray(data.groups) || !data.groups.length) return null
34
+ const groups = data.groups.filter(g => g && typeof g === 'object').map(group => {
35
+ const buckets = (Array.isArray(group.buckets) ? group.buckets : [])
36
+ .filter(b => b && Number.isFinite(b.remaining_fraction))
37
+ .map(b => {
38
+ const usedPercent = Math.min(100, Math.max(0, Math.round((1 - b.remaining_fraction) * 1000) / 10))
39
+ return {
40
+ ...(b.id ? { id: b.id } : {}),
41
+ ...(b.name ? { name: b.name } : {}),
42
+ ...(b.window ? { window: b.window } : {}),
43
+ minutes: windowMinutes(b.window),
44
+ usedPercent,
45
+ remainingFraction: b.remaining_fraction,
46
+ resetsAt: resetIso(b.reset_time)
47
+ }
48
+ })
49
+ .sort((a, b) => (a.minutes ?? Number.MAX_SAFE_INTEGER) - (b.minutes ?? Number.MAX_SAFE_INTEGER))
50
+ return { name: group.name ?? '', windows: buckets }
51
+ }).filter(g => g.windows.length > 0)
52
+ if (!groups.length) return null
53
+ const primary = groups.find(g => g.name === 'Gemini Models') ?? groups.find(g => /gemini/i.test(g.name)) ?? groups[0]
54
+ const duplicateMinutes = new Set(primary.windows.map(w => w.minutes)).size < primary.windows.length
55
+ const windows = primary.windows.map(({ id, name, minutes, usedPercent, resetsAt }) => {
56
+ const win = {
57
+ ...(duplicateMinutes && id ? { id } : {}),
58
+ ...(duplicateMinutes && name ? { name } : {}),
59
+ minutes,
60
+ usedPercent,
61
+ resetsAt
62
+ }
63
+ if (!duplicateMinutes) {
64
+ if (id) Object.defineProperty(win, 'id', { value: id, enumerable: false })
65
+ if (name) Object.defineProperty(win, 'name', { value: name, enumerable: false })
66
+ }
67
+ return win
68
+ })
69
+ const tightGroups = groups
70
+ .filter(g => g !== primary && g.windows.some(w => w.usedPercent >= 90))
71
+ .map(g => ({
72
+ name: g.name || 'Secondary models',
73
+ reached: g.windows.some(w => w.usedPercent >= 100),
74
+ windows: g.windows.filter(w => w.usedPercent >= 90).map(({ id, name, minutes, usedPercent, resetsAt }) => ({
75
+ ...(id ? { id } : {}),
76
+ ...(name ? { name } : {}),
77
+ minutes,
78
+ usedPercent,
79
+ resetsAt
80
+ }))
81
+ }))
82
+ const reached = windows.some(w => w.usedPercent >= 100)
83
+ const avoid = reached || windows.some(w => w.usedPercent >= 90)
84
+ const plentiful = !avoid && tightGroups.length === 0 && windows.length > 0 && windows.every(w => w.usedPercent <= 25)
85
+ return {
86
+ source: 'native',
87
+ plan: null,
88
+ windows,
89
+ groups,
90
+ reached,
91
+ avoid: Boolean(avoid),
92
+ plentiful,
93
+ ...(tightGroups.length ? { tightGroups } : {}),
94
+ at: new Date().toISOString()
95
+ }
96
+ }
97
+
98
+ export function windowLabel(minutes) {
99
+ if (minutes === 10080) return 'week'
100
+ if (minutes && minutes % 1440 === 0) return minutes / 1440 + 'd'
101
+ if (minutes && minutes % 60 === 0) return minutes / 60 + 'h'
102
+ return minutes ? minutes + 'm' : 'window'
103
+ }
104
+
105
+ export const tight = quota => !!quota && Boolean(quota.avoid || quota.reached || quota.windows?.some(w => w.usedPercent >= 90))
106
+
107
+ function secondaryTightGroups(quota) {
108
+ if (Array.isArray(quota?.tightGroups)) return quota.tightGroups
109
+ if (!Array.isArray(quota?.groups) || quota.groups.length < 2) return []
110
+ const primary = quota.groups.find(g => g.name === 'Gemini Models') ?? quota.groups.find(g => /gemini/i.test(g.name)) ?? quota.groups[0]
111
+ return quota.groups
112
+ .filter(g => g !== primary && Array.isArray(g.windows) && g.windows.some(w => w.usedPercent >= 90))
113
+ .map(g => ({
114
+ name: g.name || 'Secondary models',
115
+ windows: g.windows.filter(w => w.usedPercent >= 90)
116
+ }))
117
+ }
118
+
119
+ export function quotaText(quota, { unreported = false } = {}) {
120
+ if (!quota) return unreported ? 'quota=unreported (CLI does not report usage)' : 'quota=unknown'
121
+ if (quota.source === 'observed') return 'quota=observed: ' + quota.class + ' at ' + formatShortTime(quota.at) + (quota.resetsAt ? ', expected back ' + formatShortTime(quota.resetsAt) : '') + ' (' + resolvedTimeZone() + ') · 先别派 / avoid'
122
+ const wins = Array.isArray(quota.windows) ? quota.windows : []
123
+ const dup = new Set(wins.map(w => w.minutes)).size < wins.length
124
+ const parts = wins.map(w => (dup && (w.name || w.id) ? (w.name || w.id) + ' ' : '') + windowLabel(w.minutes) + ' ' + w.usedPercent + '% used' + (w.resetsAt ? ' (resets ' + formatShortTime(w.resetsAt) + ')' : ''))
125
+ const secondary = secondaryTightGroups(quota).map(g => {
126
+ const detail = g.windows.map(w => windowLabel(w.minutes) + ' ' + w.usedPercent + '% used' + (w.resetsAt ? ' (resets ' + formatShortTime(w.resetsAt) + ')' : '')).join(' · ')
127
+ return g.name + ': ' + detail + ' (先别派该组 / avoid group)'
128
+ })
129
+ const avoid = tight(quota)
130
+ const ample = !avoid && secondary.length === 0 && (quota.plentiful ?? (wins.length > 0 && wins.every(w => w.usedPercent <= 25)))
131
+ return 'quota=' + (parts.join(' · ') || 'no windows reported') + (secondary.length ? ' · ' + secondary.join(' · ') : '') + (avoid ? ' · 先别派 / avoid' : ample ? ' · 额度宽裕,可调高并发 / plenty of quota' : '')
132
+ }
@@ -0,0 +1,136 @@
1
+ import { modalities } from './model-catalog.mjs'
2
+ import { quotaText } from './quota.mjs'
3
+ import { minimumTickets } from './stats.mjs'
4
+
5
+ const levels = ['off','none','minimal','low','medium','high','extra-high','xhigh','max','ultra']
6
+ const older = model => ['previous generation','superseded','being retired'].includes(model.catalog?.tier)
7
+ const line = value => String(value ?? '').replace(/\s+/g, ' ').trim()
8
+ const sentence = value => line(value).match(/^.*?(?:[。!?]|[.!?](?=\s|$)|$)/u)?.[0] ?? ''
9
+
10
+ function variant(id) {
11
+ const options = /\[([^\]]*)\]$/.exec(id)?.[1]?.split(',') ?? []
12
+ const base = id.replace(/\[[^\]]*\]$/, '')
13
+ const suffix = /(?:-(?:extra-high|off|none|minimal|low|medium|high|xhigh|max|ultra|fast|thinking))+$/.exec(base)?.[0] ?? ''
14
+ const other = options.filter(o => !/^(?:effort|fast)=/.test(o))
15
+ return { family: base.slice(0, base.length - suffix.length) + (other.length ? '[' + other.join(',') + ']' : ''),
16
+ efforts: [...suffix.matchAll(/-(extra-high|off|none|minimal|low|medium|high|xhigh|max|ultra)(?=-|$)/g)].map(m => m[1]).concat(options.filter(o => o.startsWith('effort=')).map(o => o.slice(7))),
17
+ fast: /(?:^|-)fast(?:-|$)/.test(suffix) || options.includes('fast=true') }
18
+ }
19
+
20
+ function record(records, worker, family) {
21
+ const rows = (records ?? []).filter(r => r.worker === worker && variant(r.model).family === family)
22
+ const tickets = rows.reduce((n, r) => n + r.tickets, 0)
23
+ if (tickets < minimumTickets) return null
24
+ const sum = key => rows.reduce((n, r) => n + (r[key] ?? 0), 0)
25
+ const weighted = key => { const w = rows.filter(r => r[key] !== null); const n = w.reduce((a, r) => a + r.tickets, 0); return n ? Math.round(w.reduce((a, r) => a + r[key] * r.tickets, 0) / n * 10) / 10 : null }
26
+ return { tickets, pass: sum('pass'), rework: sum('rework'), fail: sum('fail'), blocked: sum('blocked'), medianMinutes: weighted('medianMinutes'), weekQuotaPerTicket: weighted('weekQuotaPerTicket') }
27
+ }
28
+
29
+ function compactWorker(worker, records) {
30
+ const groups = new Map()
31
+ for (const model of worker.models.filter(m => !m.hidden)) {
32
+ const family = variant(model.id).family
33
+ if (!groups.has(family)) groups.set(family, [])
34
+ groups.get(family).push(model)
35
+ }
36
+ const models = [], defaultFamily = variant(worker.defaultModel ?? '').family
37
+ let olderModels = 0, defaultFolded = false
38
+ for (const [family, group] of groups) {
39
+ if (group.every(older)) { olderModels++; defaultFolded ||= family === defaultFamily; continue }
40
+ const chosen = group.find(m => m.id === worker.defaultModel) ?? group.find(m => m.id === family) ?? (worker.id === 'agy' ? group.find(m => m.id === family + '-medium') : null) ?? group[0]
41
+ const variants = group.map(m => variant(m.id))
42
+ const efforts = [...new Set(group.flatMap(m => m.efforts ?? []).concat(variants.flatMap(v => v.efforts)))].sort((a,b) => (levels.indexOf(a) === -1 ? 99 : levels.indexOf(a)) - (levels.indexOf(b) === -1 ? 99 : levels.indexOf(b)) || a.localeCompare(b))
43
+ const input = Object.fromEntries(modalities.map(type => {
44
+ const values = group.map(m => m.capabilities?.input?.[type] ?? 'unknown')
45
+ return [type, values.every(v => v === true) ? true : values.every(v => v === false) ? false : 'unknown']
46
+ }))
47
+ const catalog = chosen.catalog
48
+ const yours = record(records, worker.id, family)
49
+ const useFamilyId = worker.id === 'agy' && !group.some(m => m.id === family) && variants.filter(v => v.efforts.length > 0).length > 1
50
+ models.push({
51
+ id: useFamilyId ? family : chosen.id,
52
+ ...(useFamilyId ? { defaultVariant: chosen.id } : {}),
53
+ efforts,
54
+ ...(yours ? { record: yours } : {}),
55
+ ...(group.length > 1 ? { variantCount: group.length } : {}),
56
+ ...(variants.some(v => v.fast) ? { fast: true } : {}),
57
+ capabilities: { input },
58
+ ...(catalog ? { catalog: { id: catalog.id, tier: catalog.tier, grade: catalog.grade, summary: sentence(catalog.summary?.en || catalog.summary?.zh), as_of: catalog.as_of, stale: group.some(m => m.catalog?.stale) } } : {})
59
+ })
60
+ }
61
+ return { id: worker.id, allowed: Boolean(worker.allowed), cli: worker.cli, loginStatus: worker.loginStatus, capability: worker.capability, steering: worker.steering, cache: worker.cache,
62
+ defaultModel: worker.defaultModel, defaultEffort: worker.defaultEffort, efforts: worker.efforts, ...(worker.defaultBlocked ? { defaultBlocked: true } : {}), ...(defaultFolded ? { defaultFolded: true } : {}),
63
+ quota: worker.quota ?? null, ...(worker.recentProblem ? { recentProblem: worker.recentProblem } : {}),
64
+ note: worker.note, rules: worker.rules, ...(worker.warnings?.length ? { warnings: worker.warnings } : {}), models, ...(olderModels ? { olderModels } : {}) }
65
+ }
66
+
67
+ export function rosterOutput(roster, { full = false, worker } = {}) {
68
+ const selected = roster.workers.filter(w => !worker || w.id === worker)
69
+ if (worker && !selected.length) {
70
+ const hint = (roster.warnings ?? []).find(msg => msg.startsWith('Allowed bro ' + worker + ' '))
71
+ throw new Error('Worker is not in the detected roster: ' + worker + (hint ? ' (' + hint + ')' : ''))
72
+ }
73
+ const preview = roster.globalAllowConfigured === false
74
+ const workers = selected.map(w => !w.allowed && !preview
75
+ ? { id: w.id, allowed: false }
76
+ : full
77
+ ? { ...w, models: (w.models ?? []).filter(m => !m.hidden) }
78
+ : compactWorker(w, roster.records))
79
+ const visible = new Set(workers.flatMap(w => w.allowed ? (w.models ?? []).map(m => m.catalog?.id).filter(Boolean) : []))
80
+ const picks = Object.fromEntries(Object.entries(roster.picks ?? {}).map(([key, p]) => [key, { ...p, first: p.first.filter(id => visible.has(id)), backup: p.backup.filter(id => visible.has(id)) }]))
81
+ return {
82
+ ...roster,
83
+ concurrency: roster.concurrency ?? 3,
84
+ mode: full ? 'full' : 'compact',
85
+ workers,
86
+ picks,
87
+ ...(roster.warnings?.length ? { warnings: roster.warnings } : {}),
88
+ ...(!full ? { catalog: { as_of: roster.catalog.as_of, stale_after_days: roster.catalog.stale_after_days, warnings: roster.catalog.warnings } } : {})
89
+ }
90
+ }
91
+
92
+ function inputs(model) {
93
+ const input = model.capabilities.input
94
+ const yes = modalities.filter(k => input[k] === true), unknown = modalities.filter(k => input[k] === 'unknown')
95
+ return (yes.join(',') || 'none confirmed') + (unknown.length ? '; unknown=' + unknown.join(',') : '')
96
+ }
97
+
98
+ export function rosterText(roster) {
99
+ const full = roster.mode === 'full'
100
+ const preview = roster.globalAllowConfigured === false
101
+ const conc = Number.isSafeInteger(roster.concurrency) ? ' · concurrency=' + roster.concurrency : ''
102
+ const output = ['Default bro: ' + (roster.defaultWorker || (preview ? 'none (unconfigured)' : '')) + conc + ' · ' + roster.mode, ...roster.rules.map(r => 'Rule: ' + line(r))]
103
+ for (const w of roster.workers) {
104
+ if (!w.allowed && !preview) { output.push(w.id + ' · NOT ALLOWED / 不允许'); continue }
105
+ const defaultModel = w.defaultBlocked ? 'hidden; choose a visible default' : w.defaultModel || 'CLI default'
106
+ output.push(w.id + (!w.allowed ? ' (not allowed)' : '') + ' · ' + w.cli + '/' + w.capability + ' · login=' + w.loginStatus + ' · default=' + defaultModel + '/' + (w.defaultEffort || 'CLI default') + (w.defaultFolded ? ' (older; --full)' : ''))
107
+ output.push(' ' + quotaText(w.quota, { unreported: true }) + (w.quota?.sharedWith ? ' (shared with ' + w.quota.sharedWith + ')' : ''))
108
+ if (w.recentProblem) output.push(' Recent failure: ' + w.recentProblem.class + ' at ' + w.recentProblem.at + ' · ' + line(w.recentProblem.message) + ' · 先别派 / avoid')
109
+ if (w.efforts?.length) output.push(' Default-model efforts: ' + w.efforts.join(','))
110
+ if (w.note) output.push(' Note: ' + line(w.note))
111
+ for (const rule of w.rules ?? []) if (!roster.rules.includes(rule)) output.push(' Rule: ' + line(rule))
112
+ for (const m of w.models) {
113
+ const c = m.catalog, summary = full ? c?.summary?.en || c?.summary?.zh : c?.summary
114
+ output.push(' ' + m.id + ' · input=' + inputs(m) + ' · ' + ([c?.tier, c?.grade].filter(Boolean).join('/') || 'unrated') + ' · effort=' + (m.efforts?.join(',') || 'unknown') + (m.fast ? ' · fast available' : '') +
115
+ (m.variantCount ? ' · ' + m.variantCount + ' variants' : '') + (m.record ? ' · yours: ' + m.record.tickets + ' tickets, ' + m.record.pass + ' pass/' + m.record.rework + ' rework/' + m.record.fail + ' fail/' + (m.record.blocked ?? 0) + ' blocked, median ' + m.record.medianMinutes + 'm' + (m.record.weekQuotaPerTicket !== null ? ', ' + m.record.weekQuotaPerTicket + '% week quota each' : '') : '') + (summary ? ' · ' + line(summary) : '') + (c?.stale ? ' · STALE ' + c.as_of + '; refresh catalog' : ''))
116
+ if (full) {
117
+ const cap = m.capabilities
118
+ output.push(' Context: ' + cap.context_tokens + ' (' + cap.sources.context_tokens + ') · Max output: ' + cap.max_output_tokens + (' (' + cap.sources.max_output_tokens + ')'))
119
+ }
120
+ }
121
+ if (w.olderModels) output.push(' Older/superseded: ' + w.olderModels + ' model families folded; use --full')
122
+ for (const warning of w.warnings ?? []) output.push(' Notice: ' + line(warning))
123
+ }
124
+ for (const [key, pick] of Object.entries(roster.picks)) {
125
+ if (!pick.first.length && !pick.backup.length) continue
126
+ const body = pick.first.length
127
+ ? pick.first.join(',') + (pick.backup.length ? ' / backup=' + pick.backup.join(',') : '')
128
+ : pick.backup.join(',') + ' (backup; top pick not enabled)'
129
+ output.push('Pick ' + key + ': ' + body + (pick.note ? ' · ' + line(pick.note) : ''))
130
+ }
131
+ if (roster.effort_advice) output.push('Effort advice: ' + line(roster.effort_advice))
132
+ for (const warning of roster.warnings ?? []) output.push('Notice: ' + line(warning))
133
+ for (const warning of roster.catalog.warnings ?? []) output.push('Notice: ' + line(warning))
134
+ if (!full) output.push('Details: roster --full [--json]; one bro: roster --worker ID [--full] [--json]')
135
+ return output.join('\n')
136
+ }