crewboss 0.0.0-stage → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +633 -2
- package/bin/crewboss.js +14 -0
- package/package.json +43 -4
- package/src/agent.js +179 -0
- package/src/candidates.js +66 -0
- package/src/cli.js +531 -0
- package/src/collect.js +26 -0
- package/src/config.js +384 -0
- package/src/due.js +10 -0
- package/src/errors.js +47 -0
- package/src/events.js +243 -0
- package/src/fsutil.js +37 -0
- package/src/init.js +174 -0
- package/src/label.js +4 -0
- package/src/launchd.js +243 -0
- package/src/lock.js +62 -0
- package/src/mcp.js +293 -0
- package/src/memory.js +81 -0
- package/src/mods.js +354 -0
- package/src/node-command.js +34 -0
- package/src/personas.js +154 -0
- package/src/plugin.js +43 -0
- package/src/presence.js +160 -0
- package/src/process.js +37 -0
- package/src/quota.js +34 -0
- package/src/routines.js +401 -0
- package/src/rundir.js +43 -0
- package/src/runners/agy.js +26 -0
- package/src/runners/claude.js +39 -0
- package/src/runners/codex.js +26 -0
- package/src/runners/index.js +186 -0
- package/src/runners/opencode.js +28 -0
- package/src/schedule.js +21 -0
- package/src/server.js +773 -0
- package/src/session.js +40 -0
- package/src/store.js +615 -0
- package/src/task-schedule.js +6 -0
- package/src/task-time.js +30 -0
- package/src/tasks.js +129 -0
- package/src/trackers/command.js +126 -0
- package/src/trackers/file.js +160 -0
- package/src/trackers/index.js +16 -0
- package/src/trackers/local.js +57 -0
- package/src/units.js +117 -0
- package/src/warnings.js +19 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/** Command-line entry point for the `crewboss` binary. */
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
|
|
6
|
+
import { runAgent } from './agent.js';
|
|
7
|
+
import { findConfig, loadConfig, TIER_SIZES } from './config.js';
|
|
8
|
+
import { UnsupportedError } from './errors.js';
|
|
9
|
+
import { EVENTS_ACTIONS, resolveRunId, runEventsCommand } from './events.js';
|
|
10
|
+
import { isValidScope, MEMORY_ACTIONS, runMemoryCommand } from './memory.js';
|
|
11
|
+
import { PERSONA_MODES } from './personas.js';
|
|
12
|
+
import { LOG_NAME, logPresenceError, PRESENCE_ACTIONS, runPresenceCommand } from './presence.js';
|
|
13
|
+
import { listRoutines, runRoutine } from './routines.js';
|
|
14
|
+
import { resolveNotify } from './session.js';
|
|
15
|
+
import {
|
|
16
|
+
DEFAULT_HOST, DEFAULT_PORT, exposureWarning, isLoopback, isWildcard, noTokenWarning, parsePublicUrl, startServer,
|
|
17
|
+
} from './server.js';
|
|
18
|
+
import {
|
|
19
|
+
LIST_STATUSES, runTaskCommand, TASK_ACTIONS, TASK_PRIORITIES, TASK_STATUSES,
|
|
20
|
+
} from './tasks.js';
|
|
21
|
+
import { isDueDate } from './due.js';
|
|
22
|
+
import { parseDoneSince } from './task-time.js';
|
|
23
|
+
import { initWorkspace } from './init.js';
|
|
24
|
+
import { startMcpServer } from './mcp.js';
|
|
25
|
+
import { manageSchedules } from './schedule.js';
|
|
26
|
+
import { runModCommand } from './mods.js';
|
|
27
|
+
import { NOTIFY_KINDS } from './store.js';
|
|
28
|
+
|
|
29
|
+
export const USAGE = `usage: crewboss [--config PATH] <command> ...
|
|
30
|
+
|
|
31
|
+
commands:
|
|
32
|
+
init [--dir DIR] [--force] [--json]
|
|
33
|
+
agent <persona> "<task>" [--size small|medium|large] [--mode read|write]
|
|
34
|
+
[--model tool:model[@effort]] [--dir DIR] [--allow-paid] [--dry-run] [--attachment FILE]
|
|
35
|
+
[--notify SESSION] [--run-id ID] (reserved MCP run)
|
|
36
|
+
(--notify defaults to $CREWBOSS_NOTIFY, else uds:$CLAUDE_CODE_MESSAGING_SOCKET, unless run inside an agent)
|
|
37
|
+
notify [--kind blocked|question|note] [--run ID] [--json] "<message>" (for agents: tell whoever launched you)
|
|
38
|
+
inbox [--run ID] [--json] (for agents: read their messages)
|
|
39
|
+
events [--follow] [--since ID] [--run ID] [--all] [--limit N] [--json] (for the launcher: watch the agents)
|
|
40
|
+
say <run id> "<message>" [--json] (for the launcher: answer an agent)
|
|
41
|
+
presence busy|idle|gone [--session NAME] [--label TEXT] [--cwd DIR] (for main session hooks)
|
|
42
|
+
routines list
|
|
43
|
+
routines run <name> [--dry-run]
|
|
44
|
+
routines install | uninstall
|
|
45
|
+
mod add <source> [--ref REF] [--yes] [--json]
|
|
46
|
+
mod search [query] [--json] [--limit N]
|
|
47
|
+
mod list [--json]
|
|
48
|
+
mod update <name> [--ref REF] [--yes] [--json]
|
|
49
|
+
mod remove <name> [--yes]
|
|
50
|
+
mod install [--json]
|
|
51
|
+
task list [--project P] [--status open|in_progress|done|all] [--assignee A] [--due-before YYYY-MM-DD] [--done-since DATE] [--json]
|
|
52
|
+
task projects [--json]
|
|
53
|
+
task tidy [--stale-days N] [--dry-run] [--json]
|
|
54
|
+
task show <id> [--json]
|
|
55
|
+
task add <project> "<title>" [--notes N] [--priority high|medium|low] [--assignee A] [--due YYYY-MM-DD] [--json]
|
|
56
|
+
task update <id> [--title T] [--notes N] [--status S] [--priority P] [--assignee A] [--due YYYY-MM-DD|none] [--json]
|
|
57
|
+
task done <id> [--json]
|
|
58
|
+
task comment <id> "<text>" [--author A] [--json]
|
|
59
|
+
memory add "<text>" [--scope S] [--author A] [--json]
|
|
60
|
+
memory list [--scope S] [--limit N] [--json]
|
|
61
|
+
memory search "<query>" [--scope S] [--limit N] [--json]
|
|
62
|
+
memory rm <id> [--json]
|
|
63
|
+
serve [--port 4317] [--host 127.0.0.1] [--public-url URL] [--ui DIR] [--no-token]
|
|
64
|
+
mcp (stdio MCP server)
|
|
65
|
+
(--public-url is required with --host 0.0.0.0 or ::)
|
|
66
|
+
|
|
67
|
+
Memory scopes: global (default for add), persona:<name>, project:<id>, routine:<name>.
|
|
68
|
+
presence is silent and always exits 0 (errors go to <logs_dir>/presence.log); the session is --session, else
|
|
69
|
+
uds:$CLAUDE_CODE_MESSAGING_SOCKET, else claude-<first 8 characters of session_id> read from the hook JSON on stdin;
|
|
70
|
+
the main session label is --label, else <cwd folder>·<short id>; with $CREWBOSS_RUN_ID it does nothing.
|
|
71
|
+
notify and inbox use $CREWBOSS_RUN_ID and $CREWBOSS_CONFIG, which crewboss sets for the agents it launches.
|
|
72
|
+
The config is read from --config, then $CREWBOSS_CONFIG, then ./crewboss.json.
|
|
73
|
+
`;
|
|
74
|
+
|
|
75
|
+
const AGENT_OPTIONS = {
|
|
76
|
+
size: { type: 'string' },
|
|
77
|
+
mode: { type: 'string' },
|
|
78
|
+
model: { type: 'string' },
|
|
79
|
+
dir: { type: 'string' },
|
|
80
|
+
'allow-paid': { type: 'boolean' },
|
|
81
|
+
'dry-run': { type: 'boolean' },
|
|
82
|
+
attachment: { type: 'string' },
|
|
83
|
+
notify: { type: 'string' },
|
|
84
|
+
'run-id': { type: 'string' },
|
|
85
|
+
help: { type: 'boolean', short: 'h' },
|
|
86
|
+
};
|
|
87
|
+
const ROUTINE_ACTIONS = ['list', 'run', 'install', 'uninstall'];
|
|
88
|
+
|
|
89
|
+
class UsageError extends Error {}
|
|
90
|
+
|
|
91
|
+
function parseGlobal(argv) {
|
|
92
|
+
const args = [...argv];
|
|
93
|
+
let configPath = null;
|
|
94
|
+
while (args.length && args[0].startsWith('-')) {
|
|
95
|
+
const arg = args.shift();
|
|
96
|
+
if (arg === '-h' || arg === '--help') return { help: true };
|
|
97
|
+
if (arg === '--config') {
|
|
98
|
+
configPath = args.shift();
|
|
99
|
+
if (!configPath) throw new UsageError('--config needs a path');
|
|
100
|
+
} else if (arg.startsWith('--config=')) {
|
|
101
|
+
configPath = arg.slice('--config='.length);
|
|
102
|
+
} else {
|
|
103
|
+
throw new UsageError(`unknown option '${arg}'`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { configPath, command: args.shift(), rest: args };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function parseAgent(rest) {
|
|
110
|
+
const { values, positionals } = parseArgs({ args: rest, options: AGENT_OPTIONS, allowPositionals: true, strict: true });
|
|
111
|
+
if (values.help) return { help: true };
|
|
112
|
+
if (positionals.length !== 2) throw new UsageError('agent needs <persona> and "<task>"');
|
|
113
|
+
if (values.size && !TIER_SIZES.includes(values.size)) {
|
|
114
|
+
throw new UsageError(`--size must be one of ${TIER_SIZES.join(', ')}`);
|
|
115
|
+
}
|
|
116
|
+
if (values.mode && !PERSONA_MODES.includes(values.mode)) {
|
|
117
|
+
throw new UsageError(`--mode must be one of ${PERSONA_MODES.join(', ')}`);
|
|
118
|
+
}
|
|
119
|
+
if (values['run-id'] !== undefined && !/^[1-9][0-9]*$/.test(values['run-id'])) throw new UsageError('--run-id must be a positive integer');
|
|
120
|
+
return { persona: positionals[0], task: positionals[1], values };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function parseInit(rest) {
|
|
124
|
+
const { values, positionals } = parseArgs({
|
|
125
|
+
args: rest,
|
|
126
|
+
options: { dir: { type: 'string' }, force: { type: 'boolean' }, json: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } },
|
|
127
|
+
allowPositionals: true, strict: true,
|
|
128
|
+
});
|
|
129
|
+
if (values.help) return { help: true };
|
|
130
|
+
if (positionals.length) throw new UsageError('init takes no positional arguments');
|
|
131
|
+
if (values.dir !== undefined && !values.dir.trim()) throw new UsageError('--dir must not be empty');
|
|
132
|
+
return { initArgs: { dir: values.dir, force: values.force, json: values.json } };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function parseRoutines(rest) {
|
|
136
|
+
const [action, ...args] = rest;
|
|
137
|
+
if (!ROUTINE_ACTIONS.includes(action)) throw new UsageError(`routines needs one of: ${ROUTINE_ACTIONS.join(', ')}`);
|
|
138
|
+
const options = action === 'run'
|
|
139
|
+
? { 'dry-run': { type: 'boolean' }, help: { type: 'boolean', short: 'h' } }
|
|
140
|
+
: { help: { type: 'boolean', short: 'h' } };
|
|
141
|
+
const { values, positionals } = parseArgs({ args, options, allowPositionals: true, strict: true });
|
|
142
|
+
if (values.help) return { help: true };
|
|
143
|
+
if (action === 'run' && positionals.length !== 1) throw new UsageError('routines run needs <name>');
|
|
144
|
+
if (action !== 'run' && positionals.length) throw new UsageError(`routines ${action} takes no arguments`);
|
|
145
|
+
return { action, name: positionals[0], dryRun: values['dry-run'] ?? false };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function parseMod(rest) {
|
|
149
|
+
const [action, ...remaining] = rest;
|
|
150
|
+
const positions = { add: ['source'], search: [], list: [], update: ['name'], remove: ['name'], install: [] };
|
|
151
|
+
if (!Object.hasOwn(positions, action)) throw new UsageError('mod needs one of: add, search, list, update, remove, install');
|
|
152
|
+
const options = { help: { type: 'boolean', short: 'h' } };
|
|
153
|
+
if (action === 'add' || action === 'update') options.ref = { type: 'string' };
|
|
154
|
+
if (action === 'search') options.limit = { type: 'string' };
|
|
155
|
+
if (['add', 'update', 'remove'].includes(action)) options.yes = { type: 'boolean' };
|
|
156
|
+
if (action !== 'remove') options.json = { type: 'boolean' };
|
|
157
|
+
const { values, positionals } = parseArgs({ args: remaining, options, allowPositionals: true, strict: true });
|
|
158
|
+
if (values.help) return { help: true };
|
|
159
|
+
if (action === 'search' ? positionals.length > 1 : positionals.length !== positions[action].length) {
|
|
160
|
+
throw new UsageError(action === 'search' ? 'mod search accepts at most one query' :
|
|
161
|
+
`mod ${action} needs ${positions[action].map((x) => `<${x}>`).join(' ') || 'no arguments'}`);
|
|
162
|
+
}
|
|
163
|
+
if (values.ref !== undefined && !values.ref.trim()) throw new UsageError('--ref must not be empty');
|
|
164
|
+
if (values.limit !== undefined && (!/^[1-9][0-9]*$/.test(values.limit) || Number(values.limit) > 50)) {
|
|
165
|
+
throw new UsageError('--limit must be an integer from 1 to 50');
|
|
166
|
+
}
|
|
167
|
+
const modArgs = { ...values };
|
|
168
|
+
if (action === 'search') { modArgs.query = positionals[0] ?? ''; modArgs.limit = values.limit === undefined ? 20 : Number(values.limit); }
|
|
169
|
+
positions[action].forEach((key, i) => { modArgs[key] = positionals[i]; });
|
|
170
|
+
return { modAction: action, modArgs };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const TASK_SPECS = {
|
|
174
|
+
list: { positionals: [], options: ['project', 'status', 'assignee', 'due-before', 'done-since'] },
|
|
175
|
+
projects: { positionals: [], options: [] },
|
|
176
|
+
tidy: { positionals: [], options: ['stale-days', 'dry-run'] },
|
|
177
|
+
show: { positionals: ['id'], options: [] },
|
|
178
|
+
add: { positionals: ['project', 'title'], options: ['notes', 'priority', 'assignee', 'due'] },
|
|
179
|
+
update: { positionals: ['id'], options: ['title', 'notes', 'status', 'priority', 'assignee', 'due'] },
|
|
180
|
+
done: { positionals: ['id'], options: [] },
|
|
181
|
+
comment: { positionals: ['id', 'text'], options: ['author'] },
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
function parseTask(rest) {
|
|
185
|
+
const [action, ...args] = rest;
|
|
186
|
+
const spec = TASK_SPECS[action];
|
|
187
|
+
if (!spec) throw new UsageError(`task needs one of: ${TASK_ACTIONS.join(', ')}`);
|
|
188
|
+
const options = { json: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } };
|
|
189
|
+
for (const name of spec.options) options[name] = { type: name === 'dry-run' ? 'boolean' : 'string' };
|
|
190
|
+
const { values, positionals } = parseArgs({ args, options, allowPositionals: true, strict: true });
|
|
191
|
+
if (values.help) return { help: true };
|
|
192
|
+
if (positionals.length !== spec.positionals.length) {
|
|
193
|
+
throw new UsageError(`task ${action} needs ${spec.positionals.map((p) => `<${p}>`).join(' ') || 'no arguments'}`);
|
|
194
|
+
}
|
|
195
|
+
const taskArgs = { ...values };
|
|
196
|
+
if (taskArgs['done-since'] !== undefined) {
|
|
197
|
+
try { parseDoneSince(taskArgs['done-since']); } catch { throw new UsageError('--done-since must be YYYY-MM-DD or an ISO datetime'); }
|
|
198
|
+
taskArgs.doneSince = taskArgs['done-since'];
|
|
199
|
+
delete taskArgs['done-since'];
|
|
200
|
+
}
|
|
201
|
+
if (action === 'tidy') {
|
|
202
|
+
if (taskArgs['stale-days'] !== undefined && (!/^[1-9][0-9]*$/.test(taskArgs['stale-days']) || Number(taskArgs['stale-days']) > 365)) {
|
|
203
|
+
throw new UsageError('--stale-days must be an integer from 1 to 365');
|
|
204
|
+
}
|
|
205
|
+
taskArgs.staleDays = taskArgs['stale-days'] === undefined ? undefined : Number(taskArgs['stale-days']);
|
|
206
|
+
taskArgs.dryRun = taskArgs['dry-run'] ?? false;
|
|
207
|
+
}
|
|
208
|
+
if (taskArgs['due-before'] !== undefined) {
|
|
209
|
+
taskArgs.dueBefore = taskArgs['due-before'];
|
|
210
|
+
delete taskArgs['due-before'];
|
|
211
|
+
if (!isDueDate(taskArgs.dueBefore)) throw new UsageError('--due-before must be a date in YYYY-MM-DD format');
|
|
212
|
+
}
|
|
213
|
+
if (taskArgs.due !== undefined) {
|
|
214
|
+
if (action === 'update' && taskArgs.due === 'none') taskArgs.due = null;
|
|
215
|
+
else if (!isDueDate(taskArgs.due)) throw new UsageError('--due must be a date in YYYY-MM-DD format (or none for update)');
|
|
216
|
+
}
|
|
217
|
+
spec.positionals.forEach((name, i) => { taskArgs[name] = positionals[i]; });
|
|
218
|
+
const statuses = action === 'list' ? LIST_STATUSES : TASK_STATUSES;
|
|
219
|
+
if (taskArgs.status !== undefined && !statuses.includes(taskArgs.status)) {
|
|
220
|
+
throw new UsageError(`--status must be one of ${statuses.join(', ')}`);
|
|
221
|
+
}
|
|
222
|
+
if (taskArgs.priority !== undefined && !TASK_PRIORITIES.includes(taskArgs.priority)) {
|
|
223
|
+
throw new UsageError(`--priority must be one of ${TASK_PRIORITIES.join(', ')}`);
|
|
224
|
+
}
|
|
225
|
+
for (const name of [...spec.positionals, 'title']) {
|
|
226
|
+
if (taskArgs[name] !== undefined && !taskArgs[name].trim()) throw new UsageError(`${name} must not be empty`);
|
|
227
|
+
}
|
|
228
|
+
if (action === 'update' && !spec.options.some((name) => taskArgs[name] !== undefined)) {
|
|
229
|
+
throw new UsageError('task update needs at least one of --title, --notes, --status, --priority, --assignee, --due');
|
|
230
|
+
}
|
|
231
|
+
return { action, taskArgs };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const MEMORY_SPECS = {
|
|
235
|
+
add: { positionals: ['text'], options: ['scope', 'author'] },
|
|
236
|
+
list: { positionals: [], options: ['scope', 'limit'] },
|
|
237
|
+
search: { positionals: ['query'], options: ['scope', 'limit'] },
|
|
238
|
+
rm: { positionals: ['id'], options: [] },
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
function parseMemory(rest) {
|
|
242
|
+
const [action, ...args] = rest;
|
|
243
|
+
const spec = MEMORY_SPECS[action];
|
|
244
|
+
if (!spec) throw new UsageError(`memory needs one of: ${MEMORY_ACTIONS.join(', ')}`);
|
|
245
|
+
const options = { json: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } };
|
|
246
|
+
for (const name of spec.options) options[name] = { type: 'string' };
|
|
247
|
+
const { values, positionals } = parseArgs({ args, options, allowPositionals: true, strict: true });
|
|
248
|
+
if (values.help) return { help: true };
|
|
249
|
+
if (positionals.length !== spec.positionals.length) {
|
|
250
|
+
throw new UsageError(`memory ${action} needs ${spec.positionals.map((p) => `<${p}>`).join(' ') || 'no arguments'}`);
|
|
251
|
+
}
|
|
252
|
+
const memoryArgs = { ...values };
|
|
253
|
+
spec.positionals.forEach((name, i) => { memoryArgs[name] = positionals[i]; });
|
|
254
|
+
for (const name of spec.positionals) {
|
|
255
|
+
if (!memoryArgs[name].trim()) throw new UsageError(`${name} must not be empty`);
|
|
256
|
+
}
|
|
257
|
+
if (memoryArgs.scope !== undefined && !isValidScope(memoryArgs.scope)) {
|
|
258
|
+
throw new UsageError(`invalid scope '${memoryArgs.scope}'; use global, persona:<name>, project:<id> or routine:<name>`);
|
|
259
|
+
}
|
|
260
|
+
if (memoryArgs.limit !== undefined && !/^[1-9][0-9]*$/.test(memoryArgs.limit)) {
|
|
261
|
+
throw new UsageError('--limit must be a positive integer');
|
|
262
|
+
}
|
|
263
|
+
if (action === 'rm' && !/^[1-9][0-9]*$/.test(memoryArgs.id)) throw new UsageError('memory rm needs a numeric <id>');
|
|
264
|
+
return { action, memoryArgs };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const EVENTS_SPECS = {
|
|
268
|
+
notify: { positionals: ['message'], options: ['kind', 'run'], flags: ['json'] },
|
|
269
|
+
inbox: { positionals: [], options: ['run'], flags: ['json'] },
|
|
270
|
+
events: { positionals: [], options: ['since', 'run', 'limit'], flags: ['follow', 'all', 'json'] },
|
|
271
|
+
say: { positionals: ['run', 'message'], options: [], flags: ['json'] },
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/** notify, inbox, events and say: the channel between an agent and whoever launched it. */
|
|
275
|
+
function parseEvents(action, rest) {
|
|
276
|
+
const spec = EVENTS_SPECS[action];
|
|
277
|
+
const options = { help: { type: 'boolean', short: 'h' } };
|
|
278
|
+
for (const name of spec.options) options[name] = { type: 'string' };
|
|
279
|
+
for (const name of spec.flags) options[name] = { type: 'boolean' };
|
|
280
|
+
const { values, positionals } = parseArgs({ args: rest, options, allowPositionals: true, strict: true });
|
|
281
|
+
if (values.help) return { help: true };
|
|
282
|
+
if (positionals.length !== spec.positionals.length) {
|
|
283
|
+
throw new UsageError(`${action} needs ${spec.positionals.map((p) => `<${p}>`).join(' ') || 'no arguments'}`);
|
|
284
|
+
}
|
|
285
|
+
const eventsArgs = { ...values };
|
|
286
|
+
spec.positionals.forEach((name, i) => { eventsArgs[name] = positionals[i]; });
|
|
287
|
+
if (eventsArgs.message !== undefined && !eventsArgs.message.trim()) throw new UsageError('message must not be empty');
|
|
288
|
+
if (eventsArgs.kind !== undefined && !NOTIFY_KINDS.includes(eventsArgs.kind)) {
|
|
289
|
+
throw new UsageError(`--kind must be one of ${NOTIFY_KINDS.join(', ')}`);
|
|
290
|
+
}
|
|
291
|
+
for (const name of ['run', 'since']) {
|
|
292
|
+
const pattern = name === 'since' ? /^[0-9]+$/ : /^[1-9][0-9]*$/;
|
|
293
|
+
if (eventsArgs[name] !== undefined && !pattern.test(eventsArgs[name])) {
|
|
294
|
+
throw new UsageError(`${name === 'run' ? '--run' : '--since'} must be ${name === 'run' ? 'a positive' : 'a non-negative'} integer`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (action === 'say' && !/^[1-9][0-9]*$/.test(eventsArgs.run)) throw new UsageError('say needs a numeric <run id>');
|
|
298
|
+
if (eventsArgs.limit !== undefined && !/^[1-9][0-9]*$/.test(eventsArgs.limit)) throw new UsageError('--limit must be a positive integer');
|
|
299
|
+
return { eventsAction: action, eventsArgs };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** presence: lenient, because a hook must never fail; anything odd is reported as `invalid` and logged. */
|
|
303
|
+
function parsePresence(rest) {
|
|
304
|
+
try {
|
|
305
|
+
const { values, positionals } = parseArgs({
|
|
306
|
+
args: rest, options: { session: { type: 'string' }, label: { type: 'string' }, cwd: { type: 'string' }, help: { type: 'boolean', short: 'h' } },
|
|
307
|
+
allowPositionals: true, strict: true,
|
|
308
|
+
});
|
|
309
|
+
if (values.help) return { help: true };
|
|
310
|
+
const [action, ...extra] = positionals;
|
|
311
|
+
if (!PRESENCE_ACTIONS.includes(action) || extra.length) throw new Error(`presence needs one of: ${PRESENCE_ACTIONS.join(', ')}`);
|
|
312
|
+
return { presenceArgs: { action, session: values.session, label: values.label, cwd: values.cwd } };
|
|
313
|
+
} catch (err) {
|
|
314
|
+
return { presenceInvalid: err.message };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function parseServe(rest) {
|
|
319
|
+
const { values, positionals } = parseArgs({
|
|
320
|
+
args: rest,
|
|
321
|
+
options: {
|
|
322
|
+
port: { type: 'string' }, host: { type: 'string' }, 'public-url': { type: 'string' }, ui: { type: 'string' }, 'no-token': { type: 'boolean' }, help: { type: 'boolean', short: 'h' },
|
|
323
|
+
},
|
|
324
|
+
allowPositionals: true,
|
|
325
|
+
strict: true,
|
|
326
|
+
});
|
|
327
|
+
if (values.help) return { help: true };
|
|
328
|
+
if (positionals.length) throw new UsageError('serve takes no arguments');
|
|
329
|
+
const port = values.port ?? String(DEFAULT_PORT);
|
|
330
|
+
if (!/^[0-9]+$/.test(port) || Number(port) > 65535) throw new UsageError('--port must be a number between 0 and 65535');
|
|
331
|
+
const host = values.host ?? DEFAULT_HOST;
|
|
332
|
+
if (!host.trim() || /[\s/]/.test(host)) throw new UsageError('--host must be a host name or address');
|
|
333
|
+
const publicUrl = values['public-url'] ?? null;
|
|
334
|
+
if (publicUrl !== null) {
|
|
335
|
+
try {
|
|
336
|
+
parsePublicUrl(publicUrl);
|
|
337
|
+
} catch (err) {
|
|
338
|
+
throw new UsageError(`--public-url: ${err.message}`);
|
|
339
|
+
}
|
|
340
|
+
} else if (isWildcard(host)) {
|
|
341
|
+
throw new UsageError(`--host ${host} listens on every interface; add --public-url with the address people will open, `
|
|
342
|
+
+ `e.g. --public-url http://192.168.1.10:${port}`);
|
|
343
|
+
}
|
|
344
|
+
return { port: Number(port), host, publicUrl, ui: values.ui, noToken: values['no-token'] ?? false };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Resolve on SIGINT or SIGTERM. */
|
|
348
|
+
function untilStopped() {
|
|
349
|
+
return new Promise((resolve) => {
|
|
350
|
+
process.once('SIGINT', resolve);
|
|
351
|
+
process.once('SIGTERM', resolve);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function parseCommand(argv) {
|
|
356
|
+
const global = parseGlobal(argv);
|
|
357
|
+
if (global.help) return global;
|
|
358
|
+
if (!global.command) throw new UsageError('missing command');
|
|
359
|
+
if (global.command === 'init') return { ...global, ...parseInit(global.rest) };
|
|
360
|
+
if (global.command === 'agent') return { ...global, ...parseAgent(global.rest) };
|
|
361
|
+
if (global.command === 'routines') return { ...global, ...parseRoutines(global.rest) };
|
|
362
|
+
if (global.command === 'mod') return { ...global, ...parseMod(global.rest) };
|
|
363
|
+
if (global.command === 'task') return { ...global, ...parseTask(global.rest) };
|
|
364
|
+
if (global.command === 'memory') return { ...global, ...parseMemory(global.rest) };
|
|
365
|
+
if (global.command === 'serve') return { ...global, ...parseServe(global.rest) };
|
|
366
|
+
if (global.command === 'mcp') {
|
|
367
|
+
if (global.rest.length) throw new UsageError('mcp takes no arguments');
|
|
368
|
+
return global;
|
|
369
|
+
}
|
|
370
|
+
if (global.command === 'presence') return { ...global, ...parsePresence(global.rest) };
|
|
371
|
+
if (EVENTS_ACTIONS.includes(global.command)) return { ...global, ...parseEvents(global.command, global.rest) };
|
|
372
|
+
throw new UsageError(`unknown command '${global.command}'`);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async function runRoutinesCommand(config, parsed, deps) {
|
|
376
|
+
const { stdout } = deps;
|
|
377
|
+
if (parsed.action === 'list') {
|
|
378
|
+
listRoutines(config, { stdout });
|
|
379
|
+
return 0;
|
|
380
|
+
}
|
|
381
|
+
if (parsed.action === 'run') {
|
|
382
|
+
const routine = config.routines.find((r) => r.name === parsed.name);
|
|
383
|
+
if (!routine) throw new UsageError(`unknown routine: ${parsed.name}`);
|
|
384
|
+
return runRoutine(config, routine, {
|
|
385
|
+
dryRun: parsed.dryRun,
|
|
386
|
+
tracker: deps.tracker,
|
|
387
|
+
launch: deps.launch,
|
|
388
|
+
collect: deps.collect,
|
|
389
|
+
which: deps.which,
|
|
390
|
+
stdout,
|
|
391
|
+
stderr: deps.stderr,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
const { scheduler, installed, removed } = manageSchedules(config, {
|
|
395
|
+
uninstall: parsed.action === 'uninstall',
|
|
396
|
+
platform: deps.platform,
|
|
397
|
+
unitDir: deps.unitDir,
|
|
398
|
+
systemctl: deps.systemctl,
|
|
399
|
+
agentsDir: deps.agentsDir,
|
|
400
|
+
launchctl: deps.launchctl,
|
|
401
|
+
uid: deps.uid,
|
|
402
|
+
});
|
|
403
|
+
for (const name of removed) stdout.write(`removed ${config.unitPrefix}${name} (${scheduler})\n`);
|
|
404
|
+
for (const name of installed) stdout.write(`installed ${config.unitPrefix}${name} (${scheduler})\n`);
|
|
405
|
+
return 0;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Run the CLI and resolve with the process exit code: 0 ok, 1 runtime error, 2 usage error.
|
|
410
|
+
* The extra options inject collaborators for tests (launch, tracker, collect, which, systemctl, unitDir).
|
|
411
|
+
*/
|
|
412
|
+
export async function main(argv, {
|
|
413
|
+
env = process.env,
|
|
414
|
+
cwd = process.cwd(),
|
|
415
|
+
stdin = process.stdin,
|
|
416
|
+
stdout = process.stdout,
|
|
417
|
+
stderr = process.stderr,
|
|
418
|
+
...deps
|
|
419
|
+
} = {}) {
|
|
420
|
+
const usageError = (err) => {
|
|
421
|
+
stderr.write(`crewboss: error: ${err.message}\nRun crewboss --help for usage.\n`);
|
|
422
|
+
return 2;
|
|
423
|
+
};
|
|
424
|
+
let parsed;
|
|
425
|
+
try {
|
|
426
|
+
parsed = parseCommand(argv);
|
|
427
|
+
} catch (err) {
|
|
428
|
+
// A hook never fails, not even with a broken command line.
|
|
429
|
+
if (argv.includes('presence')) {
|
|
430
|
+
logPresenceError(path.join(os.tmpdir(), LOG_NAME), `presence: ${err.message}`);
|
|
431
|
+
return 0;
|
|
432
|
+
}
|
|
433
|
+
return usageError(err);
|
|
434
|
+
}
|
|
435
|
+
if (parsed.command === 'presence' && parsed.presenceInvalid) {
|
|
436
|
+
logPresenceError(path.join(os.tmpdir(), LOG_NAME), `presence: ${parsed.presenceInvalid}`);
|
|
437
|
+
return 0;
|
|
438
|
+
}
|
|
439
|
+
if (parsed.help) {
|
|
440
|
+
stdout.write(USAGE);
|
|
441
|
+
return 0;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (parsed.command === 'presence') {
|
|
445
|
+
return runPresenceCommand(parsed.presenceArgs, { configPath: parsed.configPath, env, cwd, stdin, store: deps.store, now: deps.now });
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
try {
|
|
449
|
+
if (parsed.command === 'init') {
|
|
450
|
+
return await initWorkspace(parsed.initArgs, { cwd, env, stdout, stderr, ...deps });
|
|
451
|
+
}
|
|
452
|
+
// Before the config: an agent without a run should be told that, not about a missing config.
|
|
453
|
+
if (parsed.command === 'notify' || parsed.command === 'inbox') resolveRunId(parsed.eventsArgs.run, env);
|
|
454
|
+
const config = loadConfig(findConfig({ cliPath: parsed.configPath, env, cwd }),
|
|
455
|
+
parsed.command === 'serve' ? { ui: parsed.ui, uiBase: cwd } : { skipMods: parsed.command === 'mod' });
|
|
456
|
+
if (parsed.command === 'mod') {
|
|
457
|
+
return await runModCommand(config, parsed.modAction, parsed.modArgs,
|
|
458
|
+
{ cwd, stdin, stdout, stderr, spawn: deps.spawn, git: deps.git, lock: deps.lock, fetch: deps.fetch, env });
|
|
459
|
+
}
|
|
460
|
+
if (parsed.command === 'mcp') {
|
|
461
|
+
await startMcpServer(config, { stdin, stdout, stderr, env, spawn: deps.spawn, store: deps.store, tracker: deps.tracker });
|
|
462
|
+
return 0;
|
|
463
|
+
}
|
|
464
|
+
if (EVENTS_ACTIONS.includes(parsed.command)) {
|
|
465
|
+
const follow = parsed.command === 'events' && parsed.eventsArgs.follow;
|
|
466
|
+
return await runEventsCommand(config, parsed.command, parsed.eventsArgs, {
|
|
467
|
+
store: deps.store,
|
|
468
|
+
stdout,
|
|
469
|
+
env,
|
|
470
|
+
...(follow ? { stopped: (deps.untilStopped ?? untilStopped)(), pollMs: deps.pollMs } : {}),
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
if (parsed.command === 'routines') {
|
|
474
|
+
return await runRoutinesCommand(config, parsed, { ...deps, stdout, stderr });
|
|
475
|
+
}
|
|
476
|
+
if (parsed.command === 'serve') {
|
|
477
|
+
const warning = exposureWarning(parsed.host);
|
|
478
|
+
if (warning) stderr.write(`crewboss: ${warning}\n`);
|
|
479
|
+
const noToken = parsed.noToken || env.CREWBOSS_NO_TOKEN === '1';
|
|
480
|
+
if (noToken && (parsed.publicUrl || !isLoopback(parsed.host))) {
|
|
481
|
+
stderr.write(`crewboss: ${noTokenWarning()}\n`);
|
|
482
|
+
}
|
|
483
|
+
if (noToken && (parsed.noToken || env.CREWBOSS_NO_TOKEN === '1') && env.CREWBOSS_TOKEN) {
|
|
484
|
+
stderr.write('crewboss: --no-token / CREWBOSS_NO_TOKEN=1 disables the configured CREWBOSS_TOKEN\n');
|
|
485
|
+
}
|
|
486
|
+
const server = await startServer(config, {
|
|
487
|
+
host: parsed.host, port: parsed.port, publicUrl: parsed.publicUrl,
|
|
488
|
+
noToken, ui: parsed.ui, uiBase: cwd, env, stderr, ...deps.serve,
|
|
489
|
+
});
|
|
490
|
+
stdout.write(`crewboss serve: open ${server.url}\n`);
|
|
491
|
+
stdout.write(server.uiPath ? `Serving UI from ${server.uiPath}.\n` : 'No UI configured; serving JSON API only.\n');
|
|
492
|
+
stdout.write('Press Ctrl+C to stop.\n');
|
|
493
|
+
await (deps.untilStopped ?? untilStopped)();
|
|
494
|
+
await server.close();
|
|
495
|
+
return 0;
|
|
496
|
+
}
|
|
497
|
+
if (parsed.command === 'memory') {
|
|
498
|
+
return await runMemoryCommand(config, parsed.action, parsed.memoryArgs, { store: deps.store, stdout, env });
|
|
499
|
+
}
|
|
500
|
+
if (parsed.command === 'task') {
|
|
501
|
+
return await runTaskCommand(config, parsed.action, parsed.taskArgs, {
|
|
502
|
+
tracker: deps.tracker, store: deps.store, stdout, env, now: deps.now,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
const { values } = parsed;
|
|
506
|
+
const resolved = resolveNotify(values.notify, env);
|
|
507
|
+
const notifyOf = { notify: resolved.notify, notifySource: resolved.source };
|
|
508
|
+
return await runAgent(config, parsed.persona, parsed.task, {
|
|
509
|
+
size: values.size,
|
|
510
|
+
mode: values.mode,
|
|
511
|
+
model: values.model,
|
|
512
|
+
dir: values.dir,
|
|
513
|
+
allowPaid: values['allow-paid'] ?? false,
|
|
514
|
+
dryRun: values['dry-run'] ?? false,
|
|
515
|
+
runId: values['run-id'] ? Number(values['run-id']) : null,
|
|
516
|
+
attachment: values.attachment ? path.resolve(cwd, values.attachment) : null,
|
|
517
|
+
...notifyOf,
|
|
518
|
+
launch: deps.launch,
|
|
519
|
+
stdout,
|
|
520
|
+
stderr,
|
|
521
|
+
});
|
|
522
|
+
} catch (err) {
|
|
523
|
+
if (err instanceof UsageError) return usageError(err);
|
|
524
|
+
if (err instanceof UnsupportedError) {
|
|
525
|
+
stderr.write(`crewboss: warning: ${err.message}\n`);
|
|
526
|
+
return 2;
|
|
527
|
+
}
|
|
528
|
+
stderr.write(`crewboss: error: ${err.message}\n`);
|
|
529
|
+
return 1;
|
|
530
|
+
}
|
|
531
|
+
}
|
package/src/collect.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Run a routine's trusted `collect` shell command, killing its process group on timeout. */
|
|
2
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
3
|
+
|
|
4
|
+
import { TimeoutError } from './errors.js';
|
|
5
|
+
import { waitForExit } from './process.js';
|
|
6
|
+
|
|
7
|
+
const STDERR_TAIL = 2000;
|
|
8
|
+
|
|
9
|
+
/** Resolve with the collector's stdout, annotated when it times out or exits non-zero. */
|
|
10
|
+
export async function collectData(command, timeoutSeconds, { cwd, env = process.env, spawn = nodeSpawn, kill } = {}) {
|
|
11
|
+
const child = spawn('/bin/sh', ['-c', command], { cwd, env, stdio: ['ignore', 'pipe', 'pipe'], detached: true });
|
|
12
|
+
let stdout = '';
|
|
13
|
+
let stderr = '';
|
|
14
|
+
child.stdout.setEncoding('utf8');
|
|
15
|
+
child.stderr.setEncoding('utf8');
|
|
16
|
+
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
|
17
|
+
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
|
18
|
+
try {
|
|
19
|
+
const code = await waitForExit(child, { timeoutSeconds, kill });
|
|
20
|
+
if (code) stdout += `\nCollector exit ${code}: ${stderr.slice(-STDERR_TAIL)}\n`;
|
|
21
|
+
return stdout;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
if (err instanceof TimeoutError) return `${stdout}\nCollector timed out. Evidence is incomplete.\n`;
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
}
|