@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/detect.mjs ADDED
@@ -0,0 +1,279 @@
1
+ import { accessSync, constants, existsSync, readFileSync, statSync } from 'node:fs'
2
+ import { homedir } from 'node:os'
3
+ import { delimiter, join } from 'node:path'
4
+ import { inspectCommand, inspectRpc } from './cli-process.mjs'
5
+ import { home, isolated } from './paths.mjs'
6
+ import { agyQuota, codexQuota } from './quota.mjs'
7
+
8
+ const CACHE_TTL_MS = 45_000
9
+ const detectCache = new Map()
10
+ const inFlight = new Map()
11
+ const FIXTURE_FILES = ['codex-signed-out', 'claude-signed-out', 'fixture-signed-out', 'fixture-models.json', 'fixture-pi-models.json', 'quota-spent', 'exec-only']
12
+ const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max']
13
+ const CLAUDE_MODELS = [
14
+ { id: 'opus', name: 'Claude Opus', efforts: CLAUDE_EFFORTS },
15
+ { id: 'sonnet', name: 'Claude Sonnet', efforts: CLAUDE_EFFORTS },
16
+ { id: 'haiku', name: 'Claude Haiku', efforts: ['low', 'medium', 'high'] },
17
+ { id: 'fable', name: 'Claude Fable', efforts: CLAUDE_EFFORTS }
18
+ ]
19
+
20
+ const value = v => typeof v === 'string' && v.length < 300 ? v : null
21
+ function settings(path) { try { return JSON.parse(readFileSync(path, 'utf8')) } catch { return {} } }
22
+
23
+ function parseJson(text) {
24
+ if (!text) return null
25
+ try { return JSON.parse(text) } catch {}
26
+ for (const line of text.trim().split('\n').reverse()) {
27
+ if (line.trim().startsWith('{')) {
28
+ try { return JSON.parse(line) } catch {}
29
+ }
30
+ }
31
+ const start = text.indexOf('{')
32
+ const end = text.lastIndexOf('}')
33
+ if (start !== -1 && end > start) {
34
+ try { return JSON.parse(text.slice(start, end + 1)) } catch {}
35
+ }
36
+ return null
37
+ }
38
+
39
+ function resolveCli(name) {
40
+ const dirs = [
41
+ ...(isolated ? [join(home, 'tools', 'bin')] : []),
42
+ ...(process.env.PATH ?? '').split(delimiter).filter(Boolean)
43
+ ]
44
+ for (const file of name.includes('/') ? [name] : dirs.map(d => join(d, name))) {
45
+ try {
46
+ accessSync(file, constants.X_OK)
47
+ if (statSync(file).isFile()) return file
48
+ } catch {}
49
+ }
50
+ return null
51
+ }
52
+
53
+ function stamp(file) {
54
+ try {
55
+ const s = statSync(file)
56
+ return s.mtimeMs + ':' + s.size
57
+ } catch {
58
+ return ''
59
+ }
60
+ }
61
+
62
+ export function pruneDetectCache(now = Date.now()) {
63
+ for (const [key, entry] of detectCache) {
64
+ if (entry.expiresAt <= now) detectCache.delete(key)
65
+ }
66
+ }
67
+
68
+ export function detectCacheSize() {
69
+ return detectCache.size
70
+ }
71
+
72
+ function cacheKey(id, cwd) {
73
+ const piDir = process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent')
74
+ const cursorDir = process.env.CURSOR_CONFIG_DIR || join(homedir(), '.cursor')
75
+ const claudeDir = process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude')
76
+ const fixtureStamps = cwd ? FIXTURE_FILES.map(f => stamp(join(cwd, f))) : []
77
+ const hasLocalFixtures = fixtureStamps.some(Boolean)
78
+ const scopedCwd = id === 'codex' || hasLocalFixtures ? (cwd ?? '') : ''
79
+ const files = [
80
+ stamp(join(piDir, 'auth.json')),
81
+ stamp(join(cursorDir, 'cli-config.json')),
82
+ stamp(join(claudeDir, 'settings.json')),
83
+ ...(hasLocalFixtures ? fixtureStamps : [])
84
+ ].join('|')
85
+ return [id, scopedCwd, process.env.PATH ?? '', process.env.PI_CODING_AGENT_DIR ?? '', process.env.CURSOR_CONFIG_DIR ?? '', process.env.CLAUDE_CONFIG_DIR ?? '', files].join('\0')
86
+ }
87
+
88
+ export function clearDetectCache() {
89
+ detectCache.clear()
90
+ inFlight.clear()
91
+ }
92
+
93
+ export const builtins = {
94
+ codex: { cli: 'codex', capability: 'full', steering: 'inject', cache: true, login: 'codex login', install: 'npm install -g @openai/codex', upgrade: 'npm install -g @openai/codex@latest' },
95
+ claude: { cli: 'claude', capability: 'full', steering: 'queue', cache: true, login: 'claude auth login', install: 'curl -fsSL https://claude.ai/install.sh | bash', upgrade: 'claude update' },
96
+ pi: { cli: 'pi', capability: 'full', steering: 'inject', cache: true, login: 'pi', loginHint: 'Use /login, then choose the provider.', install: 'npm install -g @earendil-works/pi-coding-agent', upgrade: 'npm install -g @earendil-works/pi-coding-agent@latest' },
97
+ agy: { cli: 'agy', capability: 'full', steering: 'queue', cache: true, login: 'agy', loginHint: 'Complete the CLI sign-in flow.', install: 'curl -fsSL https://antigravity.google/cli/install.sh | bash -s -- --skip-aliases --skip-path', upgrade: 'agy update' },
98
+ 'cursor-agent': { cli: 'cursor-agent', capability: 'full', steering: 'queue', cache: false, login: 'cursor-agent login', install: 'curl -fsSL https://cursor.com/install | bash', upgrade: 'cursor-agent update' }
99
+ }
100
+
101
+ export const cliActions = {
102
+ ...builtins,
103
+ gemini: { login: 'gemini', loginHint: 'Sign in inside Gemini CLI.', install: 'npm install -g @google/gemini-cli', upgrade: 'npm install -g @google/gemini-cli@latest' },
104
+ opencode: { login: 'opencode auth login', install: 'curl -fsSL https://opencode.ai/install | bash', upgrade: 'curl -fsSL https://opencode.ai/install | bash' }
105
+ }
106
+
107
+ export async function readAgyQuota(cwd) {
108
+ const bin = resolveCli('agy')
109
+ if (!bin) return null
110
+ const usage = await inspectCommand(bin, ['-p', '/usage', '--output-format', 'json'], cwd)
111
+ if (!usage.ok) return null
112
+ return agyQuota(parseJson(usage.text))
113
+ }
114
+
115
+ async function runDetectCli(id, cwd) {
116
+ const cli = id.startsWith('pi:') ? 'pi' : id
117
+ const bin = resolveCli(cli)
118
+ const base = { ...builtins[cli], cli, installed: !!bin, version: null, loginStatus: 'unknown', models: [], defaultModel: null, defaultEffort: null, efforts: [], source: 'CLI inspection', warnings: [] }
119
+ if (!bin) return { ...base, loginStatus: 'not-installed' }
120
+ try {
121
+ if (cli === 'agy') {
122
+ const [version, models, effort, model, usage] = await Promise.all([
123
+ inspectCommand(bin, ['--version'], cwd),
124
+ inspectCommand(bin, ['models'], cwd),
125
+ inspectCommand(bin, ['-p', '/effort', '--output-format', 'json'], cwd),
126
+ inspectCommand(bin, ['-p', '/model', '--output-format', 'json'], cwd),
127
+ inspectCommand(bin, ['-p', '/usage', '--output-format', 'json'], cwd)
128
+ ])
129
+ base.version = version.ok ? value(version.text.trim().split('\n')[0]) : null
130
+ base.models = models.text.split('\n').filter(l => /^\S+\t/.test(l)).map(l => { const [mid, name] = l.split('\t'); return { id: mid, name, efforts: [] } })
131
+ base.loginStatus = models.ok && base.models.length ? 'ready' : 'unknown'
132
+ if (effort.ok) {
133
+ const e = parseJson(effort.text)?.command?.data
134
+ if (e) { base.defaultEffort = value(e.current); base.efforts = Array.isArray(e.available) ? e.available : [] }
135
+ }
136
+ if (model.ok) {
137
+ const m = parseJson(model.text)?.command?.data
138
+ if (m) base.defaultModel = value(m.id)
139
+ }
140
+ if (usage.ok) {
141
+ const q = agyQuota(parseJson(usage.text))
142
+ if (q) base.quota = q
143
+ }
144
+ base.source = 'agy models and read-only /model, /effort, /usage'
145
+ return base
146
+ }
147
+ if (cli === 'claude') {
148
+ const [version, auth] = await Promise.all([
149
+ inspectCommand(bin, ['--version'], cwd),
150
+ inspectCommand(bin, ['auth', 'status', '--json'], cwd)
151
+ ])
152
+ base.version = version.ok ? value(version.text.trim().split('\n')[0]) : null
153
+ const authRaw = auth.text || auth.output || ''
154
+ const authJson = parseJson(authRaw)
155
+ if (authJson && typeof authJson.loggedIn === 'boolean') {
156
+ if (!authJson.loggedIn) base.loginStatus = 'signed-out'
157
+ else if (typeof authJson.loginExpiresAt === 'string' && Number.isFinite(Date.parse(authJson.loginExpiresAt)) && Date.parse(authJson.loginExpiresAt) <= Date.now()) base.loginStatus = 'expired'
158
+ else base.loginStatus = 'ready'
159
+ } else if (/not logged|not authenticated|logged out/i.test(authRaw)) {
160
+ base.loginStatus = 'signed-out'
161
+ } else if (auth.ok && /logged in|authenticated/i.test(auth.text)) {
162
+ base.loginStatus = 'ready'
163
+ }
164
+ const s = settings(join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude'), 'settings.json'))
165
+ const configuredModel = value(s.model)
166
+ base.defaultModel = configuredModel && configuredModel !== 'default' ? configuredModel : null
167
+ base.defaultEffort = value(s.effortLevel) ?? null
168
+ base.efforts = [...CLAUDE_EFFORTS]
169
+ base.models = CLAUDE_MODELS.map(m => ({ ...m, efforts: [...m.efforts], isDefault: m.id === base.defaultModel }))
170
+ if (base.defaultModel && !base.models.some(m => m.id === base.defaultModel)) {
171
+ base.models.unshift({ id: base.defaultModel, name: base.defaultModel, efforts: [...CLAUDE_EFFORTS], isDefault: true })
172
+ }
173
+ base.source = 'claude --version, claude auth status --json; public settings model'
174
+ return base
175
+ }
176
+ const version = await inspectCommand(bin, ['--version'], cwd)
177
+ base.version = version.ok ? value(version.text.trim().split('\n')[0]) : null
178
+ if (cli === 'codex') {
179
+ await inspectRpc(bin, ['app-server'], cwd, async (request, send) => {
180
+ await request('initialize', { clientInfo: { name: 'broco-roster', version: '1.0.0' }, capabilities: {} })
181
+ send({ method: 'initialized' })
182
+ let cursor = null
183
+ do {
184
+ const page = await request('model/list', { limit: 100, cursor })
185
+ base.models.push(...(page.data ?? []).filter(m => !m.hidden).map(m => ({ input: m.inputModalities, contextWindow: m.contextWindow, maxTokens: m.maxOutputTokens, id: m.model, name: m.displayName, efforts: (m.supportedReasoningEfforts ?? []).map(e => e.reasoningEffort), defaultEffort: m.defaultReasoningEffort, isDefault: m.isDefault })))
186
+ cursor = page.nextCursor
187
+ } while (cursor)
188
+ const { config } = await request('config/read', { cwd, includeLayers: false })
189
+ base.defaultModel = value(config?.model) ?? base.models.find(m => m.isDefault)?.id ?? null
190
+ base.defaultEffort = value(config?.model_reasoning_effort) ?? base.models.find(m => m.id === base.defaultModel)?.defaultEffort ?? null
191
+ const account = await request('account/read', { refreshToken: false })
192
+ base.loginStatus = account.account ? 'ready' : account.requiresOpenaiAuth === false ? 'not-required' : 'signed-out'
193
+ base.efforts = base.models.find(m => m.id === base.defaultModel)?.efforts ?? []
194
+ const limits = account.account ? await request('account/rateLimits/read', {}).catch(() => null) : null
195
+ base.quota = codexQuota(limits?.rateLimits, limits?.ordinaryUsageAllowed)
196
+ })
197
+ } else if (cli === 'pi') {
198
+ const provider = id.slice(3)
199
+ await inspectRpc(bin, ['--mode', 'rpc', '--models', provider + '/*', '--no-session', '--no-extensions', '--no-mcp', '--no-skills', '--no-prompt-templates', '--no-themes', '--no-context-files', '--no-approve', '--offline'], cwd, async request => {
200
+ const state = await request('get_state')
201
+ const models = await request('get_available_models')
202
+ const thinking = await request('get_available_thinking_levels').catch(() => null)
203
+ const thinkingLevels = Array.isArray(thinking?.levels) ? thinking.levels : []
204
+ base.models = (models.models ?? []).filter(m => m.provider === provider).map(m => ({
205
+ input: m.input,
206
+ contextWindow: m.contextWindow,
207
+ maxTokens: m.maxTokens,
208
+ id: provider + '/' + m.id,
209
+ name: m.name,
210
+ efforts: m.reasoning ? (thinkingLevels.length ? [...thinkingLevels] : ['low', 'medium', 'high']) : ['off']
211
+ }))
212
+ base.defaultModel = state.model?.provider === provider ? provider + '/' + state.model.id : null
213
+ base.defaultEffort = value(state.thinkingLevel)
214
+ base.efforts = thinkingLevels.length ? [...thinkingLevels] : base.models.find(m => m.id === base.defaultModel)?.efforts ?? []
215
+ }, 'pi')
216
+ const auth = await inspectCommand(bin, ['auth', 'check', '--no-refresh', '--json', '--provider', provider], cwd)
217
+ if (auth.ok) { const a = JSON.parse(auth.text); base.loginStatus = ['ready', 'expired', 'missing', 'unavailable'].includes(a.status) ? a.status : 'unknown' }
218
+ base.credentialFilePresent = existsSync(join(process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent'), 'auth.json'))
219
+ } else if (cli === 'cursor-agent') {
220
+ const models = await inspectCommand(bin, ['--list-models'], cwd)
221
+ base.models = models.text.split('\n').map(l => /^(\S+) - (.+)$/.exec(l.trim())).filter(Boolean).map(([, mid, name]) => ({ id: mid, name, efforts: [], isDefault: name.includes('(default)') }))
222
+ base.defaultModel = base.models.find(m => m.isDefault)?.id ?? null
223
+ const s = settings(join(process.env.CURSOR_CONFIG_DIR || join(homedir(), '.cursor'), 'cli-config.json'))
224
+ base.defaultModel = value(s.model?.modelId) ?? value(s.model) ?? base.defaultModel
225
+ base.defaultEffort = value(s.model?.effort) ?? /-(none|minimal|low|medium|high|xhigh|max)(?:-fast)?$/.exec(base.defaultModel ?? '')?.[1] ?? null
226
+ base.efforts = [...new Set(base.models.flatMap(m => { const level = /-(none|minimal|low|medium|high|xhigh|max)(?:-fast)?$/.exec(m.id)?.[1]; return level ? [level] : [] }))]
227
+ const auth = await inspectCommand(bin, ['status'], cwd)
228
+ base.loginStatus = /not logged|not authenticated/i.test(auth.text) ? 'signed-out' : auth.ok && /logged in|authenticated/i.test(auth.text) ? 'ready' : 'unknown'
229
+ base.source = 'cursor-agent models/status; public settings model'
230
+ base.warnings.push('Cursor --effort selects an exact listed model variant when available; otherwise it uses a [effort=...] override, which the selected model must support.')
231
+ }
232
+ } catch { base.warnings.push('Some inspection commands are unavailable. Unknown values are not a sign-in failure.') }
233
+ return base
234
+ }
235
+
236
+ export async function detectCli(id, cwd, { refresh = false, now = Date.now(), ttlMs = CACHE_TTL_MS } = {}) {
237
+ pruneDetectCache(now)
238
+ const key = cacheKey(id, cwd)
239
+ if (!refresh) {
240
+ const hit = detectCache.get(key)
241
+ if (hit && hit.expiresAt > now) return structuredClone(hit.value)
242
+ const pending = inFlight.get(key)
243
+ if (pending) return structuredClone(await pending)
244
+ }
245
+ const promise = runDetectCli(id, cwd)
246
+ inFlight.set(key, promise)
247
+ try {
248
+ const result = await promise
249
+ detectCache.set(key, { expiresAt: Date.now() + ttlMs, value: structuredClone(result) })
250
+ return structuredClone(result)
251
+ } finally {
252
+ if (inFlight.get(key) === promise) inFlight.delete(key)
253
+ }
254
+ }
255
+
256
+ export async function discoverPi(cwd, { refresh = false, now = Date.now(), ttlMs = CACHE_TTL_MS } = {}) {
257
+ pruneDetectCache(now)
258
+ const key = cacheKey('__discover_pi__', cwd)
259
+ if (!refresh) {
260
+ const hit = detectCache.get(key)
261
+ if (hit && hit.expiresAt > now) return [...hit.value]
262
+ const pending = inFlight.get(key)
263
+ if (pending) return [...await pending]
264
+ }
265
+ const promise = (async () => {
266
+ const bin = resolveCli('pi')
267
+ if (!bin) return []
268
+ const listed = await inspectCommand(bin, ['--list-models', '--no-extensions', '--no-mcp', '--no-skills', '--no-prompt-templates', '--no-themes', '--no-context-files', '--no-approve', '--offline'], cwd)
269
+ return [...new Set(listed.text.split('\n').map(line => /^([a-z0-9][a-z0-9_.-]*)\s+\S+\s+[0-9.]+[KM]?\s+[0-9.]+[KM]?\s+(?:yes|no)\s+(?:yes|no)\s*$/.exec(line)?.[1]).filter(Boolean))].map(p => 'pi:' + p)
270
+ })()
271
+ inFlight.set(key, promise)
272
+ try {
273
+ const result = await promise
274
+ detectCache.set(key, { expiresAt: Date.now() + ttlMs, value: [...result] })
275
+ return [...result]
276
+ } finally {
277
+ if (inFlight.get(key) === promise) inFlight.delete(key)
278
+ }
279
+ }
@@ -0,0 +1,32 @@
1
+ import { realpathSync, statSync } from 'node:fs'
2
+ import { homedir } from 'node:os'
3
+ import { join, resolve } from 'node:path'
4
+
5
+ export function expandHome(value) {
6
+ if (typeof value !== 'string') return value
7
+ if (value === '~') return homedir()
8
+ if (value.startsWith('~/') || value.startsWith('~\\')) return join(homedir(), value.slice(2))
9
+ return value
10
+ }
11
+
12
+ export function directory(path, label = 'Working directory') {
13
+ if (typeof path !== 'string' || !path.trim()) throw new Error(label + ' must not be empty')
14
+ const expanded = expandHome(path)
15
+ try {
16
+ const cwd = realpathSync.native(expanded)
17
+ if (!statSync(cwd).isDirectory()) throw new Error(label + ' must be a directory: ' + path)
18
+ return cwd
19
+ } catch (err) {
20
+ if (err.code === 'ENOENT') throw new Error(label + ' does not exist: ' + path)
21
+ if (err.code === 'ENOTDIR') throw new Error(label + ' must be a directory: ' + path)
22
+ if (['EACCES', 'EPERM'].includes(err.code)) throw new Error(label + ' is not accessible: ' + path)
23
+ throw err
24
+ }
25
+ }
26
+
27
+ export function directoryOption(value, flag) {
28
+ if (value === undefined) return undefined
29
+ const trimmed = value.trim()
30
+ if (!trimmed) throw new Error(flag + ' must not be empty')
31
+ return directory(resolve(expandHome(trimmed)), flag)
32
+ }
package/lib/map.mjs ADDED
@@ -0,0 +1,336 @@
1
+ import { outcomes } from './store.mjs'
2
+ import { formatDateTime } from './time.mjs'
3
+
4
+ export function formatShardPrompt(brief, { input, index, count, name }) {
5
+ const vars = { input: String(input), index: String(index), count: String(count), name }
6
+ if (brief.includes('{input}')) {
7
+ return brief.replace(/\{(input|index|count|name)\}/g, (_, key) => vars[key])
8
+ }
9
+ const rendered = brief.replace(/\{(index|count|name)\}/g, (_, key) => vars[key])
10
+ return rendered + '\n\n---\nShard ' + vars.index + ' of ' + vars.count + '\nInput: ' + vars.input
11
+ }
12
+
13
+ export function isBatch(store, prefix) {
14
+ if (typeof prefix !== 'string' || !prefix.trim() || !store?.hasTable?.('batches')) return false
15
+ return Boolean(store.stmt('SELECT 1 FROM batches WHERE prefix=?').get(prefix.trim()))
16
+ }
17
+
18
+ function classifyShardStatus(convStatus, ticket) {
19
+ if (!convStatus) return 'queued'
20
+ if (ticket?.status === 'done') return 'done'
21
+ if (convStatus === 'error' || ticket?.flag === 'error') return 'error'
22
+ if (convStatus === 'stopped' || ticket?.flag === 'stopped') return 'stopped'
23
+ if (convStatus === 'running') return 'working'
24
+ if (convStatus === 'queued') return 'queued'
25
+ return 'review'
26
+ }
27
+
28
+ function previewReply(text) {
29
+ if (typeof text !== 'string' || !text.trim()) return null
30
+ const compact = text.trim().replace(/\s+/g, ' ')
31
+ return compact.length > 200 ? compact.slice(0, 197) + '...' : compact
32
+ }
33
+
34
+ function parseStatusFilter(status) {
35
+ if (!status) return null
36
+ const list = Array.isArray(status)
37
+ ? status
38
+ : String(status).split(',').map(s => s.trim()).filter(Boolean)
39
+ return list.length ? new Set(list) : null
40
+ }
41
+
42
+ export function batchDetail(store, prefix, { full = false, status = null } = {}) {
43
+ if (!isBatch(store, prefix)) return null
44
+ const row = store.stmt('SELECT * FROM batches WHERE prefix=?').get(prefix.trim())
45
+ const items = JSON.parse(row.shards)
46
+ const counts = { queued: 0, working: 0, review: 0, done: 0, error: 0, stopped: 0 }
47
+
48
+ const namesJson = JSON.stringify(items.map(i => i.name))
49
+ const convByName = new Map()
50
+ for (const c of store.stmt('SELECT id,name,status,turn,activity FROM conversations WHERE name IN (SELECT value FROM json_each(?))').all(namesJson)) {
51
+ convByName.set(c.name, c)
52
+ }
53
+ const ticketByName = new Map()
54
+ for (const t of store.ticketRows('WHERE c.name IN (SELECT value FROM json_each(?))', namesJson)) {
55
+ ticketByName.set(t.id, t)
56
+ }
57
+ const replyByName = new Map()
58
+ for (const r of store.stmt("SELECT c.name,m.content FROM messages m JOIN conversations c ON c.id=m.cid AND c.turn=m.turn WHERE m.kind='reply' AND c.name IN (SELECT value FROM json_each(?)) ORDER BY m.id ASC").all(namesJson)) {
59
+ replyByName.set(r.name, r.content)
60
+ }
61
+
62
+ const allShards = items.map(item => {
63
+ const c = convByName.get(item.name)
64
+ const ticket = ticketByName.get(item.name) ?? null
65
+ const shardStatus = classifyShardStatus(c?.status, ticket)
66
+ counts[shardStatus] += 1
67
+ const fullReply = replyByName.get(item.name) ?? null
68
+ const entry = {
69
+ name: item.name,
70
+ index: item.index,
71
+ turn: c?.turn ?? 0,
72
+ input: item.input,
73
+ status: shardStatus,
74
+ stage: ticket?.status ?? 'queued',
75
+ flag: ticket?.flag ?? null,
76
+ outcome: ticket?.outcome ?? null,
77
+ problem: ticket?.problem ?? null,
78
+ activity: ticket?.activity ?? c?.activity ?? 'Queued',
79
+ currentStep: ticket?.currentStep ?? null,
80
+ lastHeartbeatAt: ticket?.lastHeartbeatAt ?? null,
81
+ queue: ticket?.queue ?? null,
82
+ replyPreview: previewReply(fullReply),
83
+ elapsedMs: ticket?.elapsedMs ?? 0
84
+ }
85
+ if (full) {
86
+ entry.reply = fullReply
87
+ entry.changedFiles = ticket?.changedFiles ?? null
88
+ entry.worktree = ticket?.worktree ?? null
89
+ }
90
+ return entry
91
+ })
92
+
93
+ const filterSet = parseStatusFilter(status)
94
+ const shards = filterSet ? allShards.filter(s => filterSet.has(s.status)) : allShards
95
+
96
+ return {
97
+ isBatch: true,
98
+ prefix: row.prefix,
99
+ title: row.title,
100
+ brief: row.brief,
101
+ worker: row.worker,
102
+ model: row.model,
103
+ effort: row.effort,
104
+ cwd: row.cwd,
105
+ createdAt: new Date(row.created).toISOString(),
106
+ createdLocal: formatDateTime(row.created),
107
+ count: allShards.length,
108
+ counts,
109
+ finished: counts.queued + counts.working === 0,
110
+ shards
111
+ }
112
+ }
113
+
114
+ export function listBatches(store) {
115
+ if (!store?.hasTable?.('batches')) return []
116
+ return store.stmt('SELECT prefix FROM batches ORDER BY created ASC, prefix ASC').all().map(r => batchDetail(store, r.prefix)).filter(Boolean)
117
+ }
118
+
119
+ export async function dispatchMap(crew, { prefix, brief, inputs, worker, model, effort, cwd, defaultCwd, worktree, title, env } = {}) {
120
+ if (typeof prefix !== 'string' || !prefix.trim() || /[\x00-\x1f\x7f\s]/.test(prefix.trim())) {
121
+ throw new Error('Invalid batch prefix')
122
+ }
123
+ prefix = prefix.trim()
124
+ if (typeof brief !== 'string' || !brief.trim()) throw new Error('Batch brief is empty')
125
+ if (!Array.isArray(inputs) || inputs.length < 1 || inputs.length > 200) {
126
+ throw new Error('Expected 1..200 batch inputs')
127
+ }
128
+ const normalizedInputs = inputs.map(item => {
129
+ const value = typeof item === 'number' ? String(item) : item
130
+ if (typeof value !== 'string' || !value.trim()) throw new Error('Batch inputs must be non-empty strings')
131
+ return value
132
+ })
133
+ const store = crew.store
134
+ if (isBatch(store, prefix)) throw new Error('Batch already exists: ' + prefix)
135
+ if (store.byName(prefix)) throw new Error('Conversation already exists: ' + prefix)
136
+ const count = normalizedInputs.length
137
+ const planned = normalizedInputs.map((input, idx) => {
138
+ const index = idx + 1
139
+ const name = prefix + '-' + index
140
+ if (store.byName(name)) throw new Error('Conversation already exists: ' + name)
141
+ return {
142
+ index,
143
+ name,
144
+ input,
145
+ prompt: formatShardPrompt(brief, { input, index, count, name })
146
+ }
147
+ })
148
+ const batchTitle = typeof title === 'string' && title.trim() ? title.trim() : prefix
149
+ store.stmt('INSERT INTO batches(prefix,title,brief,worker,model,effort,cwd,created,shards) VALUES (?,?,?,?,?,?,?,?,?)').run(
150
+ prefix,
151
+ batchTitle,
152
+ brief,
153
+ worker ?? null,
154
+ model ?? null,
155
+ effort ?? null,
156
+ cwd ?? null,
157
+ Date.now(),
158
+ JSON.stringify(planned.map(({ name, index, input }) => ({ name, index, input })))
159
+ )
160
+ const shards = []
161
+ const prevSuppress = Boolean(crew.slots?.suppressNotify)
162
+ if (crew.slots) crew.slots.suppressNotify = true
163
+ try {
164
+ for (const shard of planned) {
165
+ let receipt = await crew.serial(shard.name, () => crew.say({
166
+ name: shard.name,
167
+ text: shard.prompt,
168
+ title: batchTitle + ' [' + shard.index + '/' + count + ']',
169
+ worker,
170
+ model,
171
+ effort,
172
+ cwd,
173
+ defaultCwd,
174
+ worktree,
175
+ env
176
+ }))
177
+ if (receipt.delivery) receipt = await receipt.delivery
178
+ shards.push({
179
+ name: shard.name,
180
+ index: shard.index,
181
+ input: shard.input,
182
+ turn: receipt.turn,
183
+ status: receipt.kind === 'queued' ? 'queued' : 'working',
184
+ worker: receipt.worker,
185
+ model: receipt.model,
186
+ effort: receipt.effort,
187
+ cwd: receipt.cwd,
188
+ queue: receipt.queue ?? null
189
+ })
190
+ }
191
+ if (shards[0]) {
192
+ store.stmt('UPDATE batches SET worker=coalesce(worker,?),model=coalesce(model,?),effort=coalesce(effort,?),cwd=coalesce(cwd,?) WHERE prefix=?').run(
193
+ shards[0].worker ?? null,
194
+ shards[0].model ?? null,
195
+ shards[0].effort ?? null,
196
+ shards[0].cwd ?? null,
197
+ prefix
198
+ )
199
+ }
200
+ } catch (err) {
201
+ const createdItems = planned
202
+ .filter(s => store.byName(s.name))
203
+ .map(({ name, index, input }) => ({ name, index, input }))
204
+ if (!createdItems.length) {
205
+ store.stmt('DELETE FROM batches WHERE prefix=?').run(prefix)
206
+ } else {
207
+ store.stmt('UPDATE batches SET shards=? WHERE prefix=?').run(JSON.stringify(createdItems), prefix)
208
+ }
209
+ throw err
210
+ } finally {
211
+ if (crew.slots && !prevSuppress) crew.slots.flushNotifications?.()
212
+ }
213
+ return { prefix, title: batchTitle, count: shards.length, shards }
214
+ }
215
+
216
+ export function doneBatch(store, prefix, { result, why } = {}) {
217
+ if (!isBatch(store, prefix)) throw new Error('Unknown batch: ' + prefix)
218
+ if (result !== undefined && !outcomes.includes(result)) throw new Error('Expected --result ' + outcomes.join('|'))
219
+ if (why !== undefined && (typeof why !== 'string' || why.length > 2000)) throw new Error('Invalid --why')
220
+ const row = store.stmt('SELECT shards FROM batches WHERE prefix=?').get(prefix.trim())
221
+ const items = JSON.parse(row.shards)
222
+ const closed = []
223
+ const skipped = []
224
+ for (const item of items) {
225
+ const c = store.byName(item.name)
226
+ if (!c) {
227
+ skipped.push({ name: item.name, status: 'missing', reason: 'Conversation not found' })
228
+ continue
229
+ }
230
+ const ticket = store.ticket(c.id)
231
+ const status = classifyShardStatus(c.status, ticket)
232
+ if (ticket.status === 'done') {
233
+ skipped.push({ name: item.name, status: 'done', outcome: ticket.outcome, reason: 'Already closed' })
234
+ continue
235
+ }
236
+ const canClose = ticket.status === 'review' || (['fail', 'blocked'].includes(result) && ['error', 'stopped'].includes(c.status))
237
+ if (!canClose) {
238
+ const reason = ['queued', 'working'].includes(status)
239
+ ? 'Shard is still ' + status
240
+ : 'Pass --result fail or blocked to close a ' + status + ' shard'
241
+ skipped.push({ name: item.name, status, reason })
242
+ continue
243
+ }
244
+ const updated = store.done(item.name, { result, why })
245
+ closed.push({
246
+ name: item.name,
247
+ outcome: updated.outcome,
248
+ task: updated.task,
249
+ startTurn: updated.startTurn,
250
+ endTurn: updated.endTurn
251
+ })
252
+ }
253
+ return { prefix: prefix.trim(), closed, skipped }
254
+ }
255
+
256
+ export async function stopBatch(crew, prefix) {
257
+ const store = crew.store
258
+ if (!isBatch(store, prefix)) throw new Error('Unknown batch: ' + prefix)
259
+ const row = store.stmt('SELECT shards FROM batches WHERE prefix=?').get(prefix.trim())
260
+ const items = JSON.parse(row.shards)
261
+ const queued = []
262
+ const running = []
263
+ const skipped = []
264
+ for (const item of items) {
265
+ const c = store.byName(item.name)
266
+ if (!c) {
267
+ skipped.push({ name: item.name, status: 'missing' })
268
+ } else if (c.status === 'queued') {
269
+ queued.push(item.name)
270
+ } else if (c.status === 'running') {
271
+ running.push(item.name)
272
+ } else {
273
+ skipped.push({ name: item.name, status: c.status })
274
+ }
275
+ }
276
+ const stopped = []
277
+ const prevSuppress = Boolean(crew.slots?.suppressNotify)
278
+ if (crew.slots) crew.slots.suppressNotify = true
279
+ try {
280
+ for (const name of queued) {
281
+ const res = await crew.serial(name, () => crew.stop(name))
282
+ stopped.push(res)
283
+ }
284
+ if (running.length) {
285
+ const runningResults = await Promise.all(running.map(name => crew.serial(name, () => crew.stop(name))))
286
+ stopped.push(...runningResults)
287
+ }
288
+ } finally {
289
+ if (crew.slots && !prevSuppress) crew.slots.flushNotifications?.()
290
+ }
291
+ return { prefix: prefix.trim(), stopped, skipped }
292
+ }
293
+
294
+ export async function retryBatch(crew, prefix, { model, effort, worker, env } = {}) {
295
+ const store = crew.store
296
+ if (!isBatch(store, prefix)) throw new Error('Unknown batch: ' + prefix)
297
+ const row = store.stmt('SELECT * FROM batches WHERE prefix=?').get(prefix.trim())
298
+ const items = JSON.parse(row.shards)
299
+ const retried = []
300
+ const skipped = []
301
+ const prevSuppress = Boolean(crew.slots?.suppressNotify)
302
+ if (crew.slots) crew.slots.suppressNotify = true
303
+ try {
304
+ for (const item of items) {
305
+ const c = store.byName(item.name)
306
+ if (!c) {
307
+ skipped.push({ name: item.name, index: item.index, input: item.input, status: 'missing', reason: 'Conversation not found' })
308
+ continue
309
+ }
310
+ const ticketRow = store.stmt('SELECT stage,flag FROM tickets WHERE cid=?').get(c.id)
311
+ const lastTurn = store.stmt('SELECT status FROM turns WHERE cid=? AND turn=?').get(c.id, c.turn)
312
+ const shardStatus = classifyShardStatus(c.status, { status: ticketRow?.stage, flag: ticketRow?.flag })
313
+ if (!['error', 'stopped'].includes(shardStatus) || !['error', 'stopped'].includes(lastTurn?.status)) {
314
+ skipped.push({ name: item.name, index: item.index, input: item.input, status: shardStatus, reason: 'Shard is ' + shardStatus })
315
+ continue
316
+ }
317
+ let receipt = await crew.serial(item.name, () => crew.retry(item.name, { model, effort, worker, env }))
318
+ if (receipt.delivery) receipt = await receipt.delivery
319
+ retried.push({
320
+ name: item.name,
321
+ index: item.index,
322
+ input: item.input,
323
+ turn: receipt.turn,
324
+ retriedTurn: receipt.retriedTurn,
325
+ status: receipt.kind === 'queued' ? 'queued' : 'working',
326
+ worker: receipt.worker,
327
+ model: receipt.model,
328
+ effort: receipt.effort,
329
+ queue: receipt.queue ?? null
330
+ })
331
+ }
332
+ } finally {
333
+ if (crew.slots && !prevSuppress) crew.slots.flushNotifications?.()
334
+ }
335
+ return { isBatch: true, prefix: row.prefix, retried, skipped }
336
+ }