@detergent-software/conductor 0.0.0-stage → 1.0.0-dev.10
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/CHANGELOG.md +107 -0
- package/README.md +295 -2
- package/build/claude.js +102 -0
- package/build/claude.js.map +1 -0
- package/build/cli.js +730 -0
- package/build/cli.js.map +1 -0
- package/build/client.js +64 -0
- package/build/client.js.map +1 -0
- package/build/clock.js +17 -0
- package/build/clock.js.map +1 -0
- package/build/conditions.js +197 -0
- package/build/conditions.js.map +1 -0
- package/build/config.js +165 -0
- package/build/config.js.map +1 -0
- package/build/credential.js +52 -0
- package/build/credential.js.map +1 -0
- package/build/daemon.js +182 -0
- package/build/daemon.js.map +1 -0
- package/build/errors.js +17 -0
- package/build/errors.js.map +1 -0
- package/build/events.js +106 -0
- package/build/events.js.map +1 -0
- package/build/inbox.js +385 -0
- package/build/inbox.js.map +1 -0
- package/build/jsonc.js +24 -0
- package/build/jsonc.js.map +1 -0
- package/build/lock.js +64 -0
- package/build/lock.js.map +1 -0
- package/build/log.js +43 -0
- package/build/log.js.map +1 -0
- package/build/manager.js +637 -0
- package/build/manager.js.map +1 -0
- package/build/mod.js +51 -0
- package/build/mod.js.map +1 -0
- package/build/note.js +71 -0
- package/build/note.js.map +1 -0
- package/build/notes.js +188 -0
- package/build/notes.js.map +1 -0
- package/build/notify.js +393 -0
- package/build/notify.js.map +1 -0
- package/build/paths.js +17 -0
- package/build/paths.js.map +1 -0
- package/build/profile.js +81 -0
- package/build/profile.js.map +1 -0
- package/build/pushover.js +67 -0
- package/build/pushover.js.map +1 -0
- package/build/registry.js +35 -0
- package/build/registry.js.map +1 -0
- package/build/resolve.js +28 -0
- package/build/resolve.js.map +1 -0
- package/build/rule-input.js +17 -0
- package/build/rule-input.js.map +1 -0
- package/build/rules-service.js +391 -0
- package/build/rules-service.js.map +1 -0
- package/build/rules.js +842 -0
- package/build/rules.js.map +1 -0
- package/build/run.js +52 -0
- package/build/run.js.map +1 -0
- package/build/server.js +367 -0
- package/build/server.js.map +1 -0
- package/build/spend.js +142 -0
- package/build/spend.js.map +1 -0
- package/build/startup.js +78 -0
- package/build/startup.js.map +1 -0
- package/build/store.js +724 -0
- package/build/store.js.map +1 -0
- package/build/tab.js +26 -0
- package/build/tab.js.map +1 -0
- package/build/token.js +58 -0
- package/build/token.js.map +1 -0
- package/build/upgrade.js +105 -0
- package/build/upgrade.js.map +1 -0
- package/build/wait.js +149 -0
- package/build/wait.js.map +1 -0
- package/build/watchers/ado.js +167 -0
- package/build/watchers/ado.js.map +1 -0
- package/build/watchers/github.js +82 -0
- package/build/watchers/github.js.map +1 -0
- package/build/watchers/health.js +42 -0
- package/build/watchers/health.js.map +1 -0
- package/build/watchers/index.js +226 -0
- package/build/watchers/index.js.map +1 -0
- package/build/watchers/linear-key.js +17 -0
- package/build/watchers/linear-key.js.map +1 -0
- package/build/watchers/linear.js +227 -0
- package/build/watchers/linear.js.map +1 -0
- package/build/watchers/npm.js +35 -0
- package/build/watchers/npm.js.map +1 -0
- package/build/watchers/source.js +94 -0
- package/build/watchers/source.js.map +1 -0
- package/build/worktrees.js +143 -0
- package/build/worktrees.js.map +1 -0
- package/mod/.claude-plugin/marketplace.json +6 -0
- package/mod/.claude-plugin/plugin.json +7 -0
- package/mod/README.md +17 -0
- package/mod/hooks/hooks.json +1 -0
- package/mod/hooks/model.ts +395 -0
- package/mod/hooks/register.tsx +505 -0
- package/mod/types/index.d.ts +57 -0
- package/package.json +61 -5
package/build/cli.js
ADDED
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { mkdirSync, readFileSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { resolve, sep } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { parseArgs } from 'node:util';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { callApi, NotRunningError, ping, readEndpoint } from "./client.js";
|
|
9
|
+
import { loadConfig } from "./config.js";
|
|
10
|
+
import { startDaemon } from "./daemon.js";
|
|
11
|
+
import { ConductorError } from "./errors.js";
|
|
12
|
+
import { eventsCommand } from "./events.js";
|
|
13
|
+
import { inboxLines } from "./inbox.js";
|
|
14
|
+
import { parseJsonc } from "./jsonc.js";
|
|
15
|
+
import { processAlive, readPid } from "./lock.js";
|
|
16
|
+
import { fleetWorkerSchema } from "./manager.js";
|
|
17
|
+
import { installMod, removeMod } from "./mod.js";
|
|
18
|
+
import { noteCommand } from "./note.js";
|
|
19
|
+
import { notifyLines, notifyStatusSchema, parseUntil, testResultSchema } from "./notify.js";
|
|
20
|
+
import { conductorPaths } from "./paths.js";
|
|
21
|
+
import { registerProject } from "./registry.js";
|
|
22
|
+
import { rulesLines, rulesService, rulesTestResultSchema, rulesViewSchema, testLines } from "./rules-service.js";
|
|
23
|
+
import { outputOf, run } from "./run.js";
|
|
24
|
+
import { formatTokens } from "./spend.js";
|
|
25
|
+
import { installStartup, removeStartup, runStartup, startupRunsCli, TASK_NAME } from "./startup.js";
|
|
26
|
+
import { inboxItemSchema, openStore, projectSchema, waitSchema, workerSchema } from "./store.js";
|
|
27
|
+
import { parseTag, upgrade } from "./upgrade.js";
|
|
28
|
+
import { waitCommand } from "./wait.js";
|
|
29
|
+
const USAGE = `Usage: conduct <command>
|
|
30
|
+
|
|
31
|
+
serve run the daemon in this process (one per machine)
|
|
32
|
+
stop stop the running daemon
|
|
33
|
+
status [--json] the daemon's pid, port, projects and workers
|
|
34
|
+
project add <path> register the project whose checkout holds .claude/conductor.json
|
|
35
|
+
project list [--json] the registered projects
|
|
36
|
+
launch <item> start a worker: its worktree, a background session, a tab
|
|
37
|
+
[--project <name>] [--probe] [--prompt <text>] [--conductor <name>]
|
|
38
|
+
retire <worker> [--force] stop a worker, remove its worktree and session; refuses unfinished work
|
|
39
|
+
attach <worker> wake a parked worker (no event text) and open it in a tab
|
|
40
|
+
inbox [--json] what waits on you: each open item with its commands and links
|
|
41
|
+
approve <item> [--text <text>]
|
|
42
|
+
approve a plan: post "approve" on the item as you, wake its worker;
|
|
43
|
+
<item> is ATK-312, or the inbox item's id as #12
|
|
44
|
+
revise <item> <text> ask for a revised plan: post "revise: <text>", wake its worker
|
|
45
|
+
done <id> mark an inbox item done (a physical action, or one you handled)
|
|
46
|
+
notify [--json] the notification profile, quiet hours, and each channel's state
|
|
47
|
+
notify profile [<name>] [--until <when>]
|
|
48
|
+
switch the profile now (built in: default, away, focus); --until 17:00 or
|
|
49
|
+
a date and time hands back to the one before it then
|
|
50
|
+
notify test [--kind <kind>] [--channel remote-control|pushover] [--raise]
|
|
51
|
+
push a sample item (default npm-otp) on the profile's channels now;
|
|
52
|
+
--raise puts a real item in the inbox instead (quiet hours, reminders)
|
|
53
|
+
rules [--json] the rules in force (project, machine, built-in defaults), what replaces
|
|
54
|
+
what, and any rules file refused; machine rules are ~/.conductor/rules.json
|
|
55
|
+
rules test <file> [--json]
|
|
56
|
+
dry-run the rules on a sample, a JSON file such as
|
|
57
|
+
{ "event": "pr.comment", "project": "atk", "fields": { "wait.kind": "merge" } }
|
|
58
|
+
(no event: a state rule's subject); acts on nothing
|
|
59
|
+
note <item> <file> post the file as a phase note on the item ("## <work skill> · <Phase>"), and
|
|
60
|
+
record its phase on the worker (this checkout's, or --worker <worker>);
|
|
61
|
+
exit 3 when nobody posted it (no conductor, or the tracker refused it)
|
|
62
|
+
wait <condition> wait for an outside event: in a worker's worktree with the daemon
|
|
63
|
+
running, register it and end the turn (the daemon resumes the session);
|
|
64
|
+
otherwise check it here and block (exit 0 when it holds, 2 at --timeout)
|
|
65
|
+
pr <n> merged|checks|comment [--repo <owner>/<repo>]
|
|
66
|
+
run <pipeline> <run id> [done]
|
|
67
|
+
npm <version> [--tag <dist-tag>] [--package <name>]
|
|
68
|
+
health <env> --version <x> | --commit <sha>
|
|
69
|
+
approval [<item>] an approve / revise: comment after the item's newest Plan note
|
|
70
|
+
item <item> <state> | item <item> label|note <value>
|
|
71
|
+
time <when> such as 17:00 (the next one) or 2026-10-10T09:00 (local)
|
|
72
|
+
<full form> such as pr:ado:Emergent.AgenticToolkit#1850:merged
|
|
73
|
+
[--local] [--timeout <minutes, local only; 29>] [--worker <worker>] [--item <item>]
|
|
74
|
+
events [--since <id>] the event log after <id> (all of it without), one JSON event per line
|
|
75
|
+
events --follow [--since <id>]
|
|
76
|
+
keep following it (live without --since), reconnecting when the
|
|
77
|
+
conductor restarts; state lines conduct.connected / conduct.disconnected
|
|
78
|
+
install-startup start the daemon at logon (Windows Task Scheduler)
|
|
79
|
+
install-startup --remove remove that logon task
|
|
80
|
+
install-mod install the conductor mod in Claude Code (user scope) from this package:
|
|
81
|
+
the status line in every session, the inbox band, pane and commands in
|
|
82
|
+
the console (a session started with CONDUCTOR_CONSOLE=1)
|
|
83
|
+
install-mod --remove uninstall it
|
|
84
|
+
upgrade [--tag dev|latest] stop the daemon, npm install the newest version on the tag (default:
|
|
85
|
+
the tag the installed version came from), start the daemon again
|
|
86
|
+
[--allow-downgrade] install the tag's version even when it is older than the installed one
|
|
87
|
+
|
|
88
|
+
A <worker> is its item (ATK-309) or its name (probe-ATK-309).
|
|
89
|
+
Files live in ~/.conductor (CONDUCTOR_HOME overrides it).`;
|
|
90
|
+
const STOP_WAIT_MS = 10_000;
|
|
91
|
+
// A restarted daemon opens its store and binds the port; under the logon task node starts cold.
|
|
92
|
+
const START_WAIT_MS = 30_000;
|
|
93
|
+
const NPM_INSTALL_TIMEOUT_MS = 5 * 60_000;
|
|
94
|
+
// A launch makes a worktree (pnpm install) before the session starts.
|
|
95
|
+
const LAUNCH_WAIT_MS = 15 * 60_000;
|
|
96
|
+
// The daemon's worst cases, so the CLI never gives up on an operation that still runs: retire
|
|
97
|
+
// lists sessions (60 s), stops one (120 s + 30 s), removes the worktree (5 min), rms it (120 s);
|
|
98
|
+
// attach waits out the settle time (up to 5 min), resumes (120 s) and opens the tab (60 s).
|
|
99
|
+
const RETIRE_TIMEOUT_MS = 12 * 60_000;
|
|
100
|
+
const ATTACH_TIMEOUT_MS = 9 * 60_000;
|
|
101
|
+
// Approve and revise ask Linear twice (the issue, then the comment), each within 20 s.
|
|
102
|
+
const ANSWER_TIMEOUT_MS = 60_000;
|
|
103
|
+
const projectsBody = z.object({ projects: z.array(projectSchema) });
|
|
104
|
+
const addBody = z.object({ created: z.boolean(), project: projectSchema });
|
|
105
|
+
const errorBody = z.object({ details: z.array(z.string()).optional(), error: z.string() });
|
|
106
|
+
const fleetBody = z.object({ workers: z.array(fleetWorkerSchema) });
|
|
107
|
+
const workerBody = z.object({ worker: workerSchema });
|
|
108
|
+
const workerDetailBody = z.object({
|
|
109
|
+
events: z.array(z.object({ data: z.record(z.string(), z.unknown()), kind: z.string() })),
|
|
110
|
+
worker: workerSchema,
|
|
111
|
+
});
|
|
112
|
+
const retireBody = z.object({
|
|
113
|
+
outcome: z.enum(['partial', 'refused', 'retired']),
|
|
114
|
+
reason: z.string().optional(),
|
|
115
|
+
steps: z.array(z.object({ error: z.string().optional(), ok: z.boolean(), step: z.string() })).optional(),
|
|
116
|
+
work: z
|
|
117
|
+
.object({ dirty: z.array(z.string()).optional(), unknown: z.string().optional(), unpushed: z.number().optional() })
|
|
118
|
+
.optional(),
|
|
119
|
+
worker: workerSchema,
|
|
120
|
+
});
|
|
121
|
+
const attachBody = z.object({ resumed: z.boolean(), tab: z.string().nullable(), worker: workerSchema });
|
|
122
|
+
const inboxBody = z.object({ items: z.array(inboxItemSchema) });
|
|
123
|
+
const answerBody = z.object({
|
|
124
|
+
comment: z.object({ id: z.string(), url: z.string().nullable() }).nullable(),
|
|
125
|
+
item: inboxItemSchema,
|
|
126
|
+
waits: z.array(waitSchema),
|
|
127
|
+
});
|
|
128
|
+
const answerRefusal = z.object({ details: z.array(z.string()).optional(), error: z.string(), posted: z.enum(['no', 'unknown']).optional() });
|
|
129
|
+
async function addProject(paths, path) {
|
|
130
|
+
const absolute = resolve(path);
|
|
131
|
+
let result;
|
|
132
|
+
try {
|
|
133
|
+
result = addBody.parse(await expectOk(callApi(paths, { body: { path: absolute }, method: 'POST', path: '/projects' })));
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (!(error instanceof NotRunningError))
|
|
137
|
+
throw error;
|
|
138
|
+
// No daemon: write the store directly. The daemon reads it when it starts.
|
|
139
|
+
// Its rules are checked against config.json here too, as the daemon would.
|
|
140
|
+
const check = rulesService({ config: loadConfig(paths.config), listProjects: () => [], paths }).checkProfile;
|
|
141
|
+
result = withStore(paths, (store) => registerProject(store, absolute, check));
|
|
142
|
+
}
|
|
143
|
+
out(`${result.created ? 'registered' : 'updated'} ${projectLine(result.project).trim()}`);
|
|
144
|
+
}
|
|
145
|
+
// `conduct approve|revise|done`: Jason's answer to an inbox item, by id, or for approve and revise
|
|
146
|
+
// by the item whose plan waits.
|
|
147
|
+
async function answer(paths, ref, request) {
|
|
148
|
+
const target = request.action === 'done' ? inboxId(ref) : await planItem(paths, ref);
|
|
149
|
+
const { body, status } = await callApi(paths, { body: request, method: 'POST', path: `/inbox/${target}/resolve`, timeoutMs: ANSWER_TIMEOUT_MS });
|
|
150
|
+
if (status !== 200) {
|
|
151
|
+
const refusal = answerRefusal.safeParse(body).data;
|
|
152
|
+
const details = [...(refusal?.details ?? [])];
|
|
153
|
+
if (status === 502 && refusal?.posted === 'unknown')
|
|
154
|
+
details.push('the comment may be on the item already: look before answering again');
|
|
155
|
+
if (status === 404) {
|
|
156
|
+
if ((await openItems(paths)) === null)
|
|
157
|
+
throw noInbox();
|
|
158
|
+
throw new ConductorError(`no inbox item #${target}`, ['`conduct inbox` lists the open ones']);
|
|
159
|
+
}
|
|
160
|
+
throw new ConductorError(refusal?.error ?? `the API answered ${status}`, details);
|
|
161
|
+
}
|
|
162
|
+
const result = answerBody.parse(body);
|
|
163
|
+
const { item } = result;
|
|
164
|
+
if (request.action === 'done')
|
|
165
|
+
return out(`done: #${item.id} (${item.kind}${item.item ? ` ${item.item}` : ''})`);
|
|
166
|
+
out(`${request.action === 'approve' ? 'approved' : 'asked for a revision of'} ${item.item ?? `#${item.id}`}: ${result.comment?.url ?? result.comment?.id ?? 'comment posted'}`);
|
|
167
|
+
out(result.waits.length > 0
|
|
168
|
+
? `its worker's approval wait resolved; the conductor resumes it`
|
|
169
|
+
: 'no worker waits on its approval here: the worker reads the comment when it next looks');
|
|
170
|
+
}
|
|
171
|
+
async function attach(paths, ref) {
|
|
172
|
+
const worker = await findWorker(paths, ref);
|
|
173
|
+
const result = attachBody.parse(await expectOk(callApi(paths, { method: 'POST', path: `/workers/${worker.id}/attach`, timeoutMs: ATTACH_TIMEOUT_MS })));
|
|
174
|
+
out(`${result.resumed ? 'woke' : 'attached to'} ${worker.name} (session ${result.worker.handle ?? '?'})`);
|
|
175
|
+
if (result.tab)
|
|
176
|
+
out(`no tab (${result.tab}); open it yourself: claude attach ${result.worker.handle ?? worker.name}`);
|
|
177
|
+
}
|
|
178
|
+
// The pid of the daemon once it answers /health, for upgrade's restart.
|
|
179
|
+
async function awaitDaemon(paths) {
|
|
180
|
+
const deadline = Date.now() + START_WAIT_MS;
|
|
181
|
+
while (Date.now() < deadline) {
|
|
182
|
+
const pid = readPid(paths.pid);
|
|
183
|
+
if (pid !== null && (await ping(paths, pid)))
|
|
184
|
+
return pid;
|
|
185
|
+
await sleep(250);
|
|
186
|
+
}
|
|
187
|
+
throw new ConductorError(`the conductor did not answer within ${START_WAIT_MS / 1000} s`, [`see ${paths.log}`]);
|
|
188
|
+
}
|
|
189
|
+
// `conduct events`: until Ctrl+C with --follow.
|
|
190
|
+
async function events(paths, follow, since) {
|
|
191
|
+
if (since !== undefined && !/^\d+$/.test(since))
|
|
192
|
+
throw new ConductorError('--since takes an event id, such as --since 120');
|
|
193
|
+
const stop = new AbortController();
|
|
194
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGBREAK'])
|
|
195
|
+
process.once(signal, () => stop.abort());
|
|
196
|
+
await eventsCommand(paths, { follow, since: since === undefined ? undefined : Number(since) }, { out, signal: stop.signal });
|
|
197
|
+
}
|
|
198
|
+
async function expectOk(call) {
|
|
199
|
+
const { body, status } = await call;
|
|
200
|
+
if (status >= 200 && status < 300)
|
|
201
|
+
return body;
|
|
202
|
+
const error = errorBody.safeParse(body);
|
|
203
|
+
throw new ConductorError(error.success ? error.data.error : `the API answered ${status}`, error.data?.details ?? []);
|
|
204
|
+
}
|
|
205
|
+
// A worker in flight by item or name. A probe and a live worker can share an item, so a name match wins.
|
|
206
|
+
async function findWorker(paths, ref) {
|
|
207
|
+
const { workers } = fleetBody.parse(await expectOk(callApi(paths, { method: 'GET', path: '/fleet' })));
|
|
208
|
+
const byName = workers.filter((w) => w.name === ref);
|
|
209
|
+
const matches = byName.length > 0 ? byName : workers.filter((w) => w.item === ref);
|
|
210
|
+
if (matches.length === 1 && matches[0])
|
|
211
|
+
return matches[0];
|
|
212
|
+
if (matches.length === 0)
|
|
213
|
+
throw new ConductorError(`no worker "${ref}" in flight`, ['`conduct status` lists them']);
|
|
214
|
+
throw new ConductorError(`"${ref}" names several workers`, matches.map((w) => `${w.name} in ${w.project}`));
|
|
215
|
+
}
|
|
216
|
+
async function inbox(paths, json) {
|
|
217
|
+
const items = await openItems(paths);
|
|
218
|
+
if (items === null)
|
|
219
|
+
throw noInbox();
|
|
220
|
+
if (json)
|
|
221
|
+
return out(JSON.stringify(items, null, 2));
|
|
222
|
+
if (items.length === 0)
|
|
223
|
+
return out('the inbox is empty');
|
|
224
|
+
inboxLines(items, new Date()).forEach((line) => out(line));
|
|
225
|
+
}
|
|
226
|
+
// An inbox id as `conduct inbox` prints it (#12), or bare (12).
|
|
227
|
+
function inboxId(ref) {
|
|
228
|
+
const id = /^#?(\d+)$/.exec(ref)?.[1];
|
|
229
|
+
if (id === undefined)
|
|
230
|
+
throw new ConductorError(`"${ref}" is not an inbox item id`, ['`conduct inbox` lists them as #<id>']);
|
|
231
|
+
return Number(id);
|
|
232
|
+
}
|
|
233
|
+
function install() {
|
|
234
|
+
const cliPath = realpathSync(fileURLToPath(import.meta.url));
|
|
235
|
+
installStartup({ cliPath, nodePath: process.execPath });
|
|
236
|
+
out(`registered the "${TASK_NAME}" logon task: ${process.execPath} ${cliPath} serve`);
|
|
237
|
+
if (cliPath.includes(`${sep}.claude${sep}worktrees${sep}`)) {
|
|
238
|
+
out('warning: that build is in a worktree, which goes away; run install-startup from the main checkout');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async function launch(paths, item, options) {
|
|
242
|
+
const { worker } = workerBody.parse(await expectOk(callApi(paths, { body: { item, ...options }, method: 'POST', path: '/workers' })));
|
|
243
|
+
out(`launching ${worker.name} in ${worker.project} (worker ${worker.id}): worktree, then session`);
|
|
244
|
+
const deadline = Date.now() + LAUNCH_WAIT_MS;
|
|
245
|
+
while (Date.now() < deadline) {
|
|
246
|
+
await sleep(2000);
|
|
247
|
+
const detail = workerDetailBody.parse(await expectOk(callApi(paths, { method: 'GET', path: `/workers/${worker.id}` })));
|
|
248
|
+
const { status } = detail.worker;
|
|
249
|
+
if (status === 'launching')
|
|
250
|
+
continue;
|
|
251
|
+
if (status === 'failed') {
|
|
252
|
+
const failure = detail.events.find((e) => e.kind === 'worker.launch-failed')?.data ?? {};
|
|
253
|
+
throw new ConductorError(`could not launch ${worker.name}`, [
|
|
254
|
+
`${String(failure.step ?? 'launch')}: ${String(failure.error ?? 'see the log')}`,
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
out(`launched ${worker.name}: session ${detail.worker.handle ?? '?'}, worktree ${detail.worker.worktree ?? '?'}`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
throw new ConductorError(`${worker.name} is still launching after ${LAUNCH_WAIT_MS / 60_000} min`, [
|
|
261
|
+
'`conduct status` shows it; the daemon carries on',
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
async function listProjects(paths, json) {
|
|
265
|
+
let projects;
|
|
266
|
+
try {
|
|
267
|
+
projects = projectsBody.parse(await expectOk(callApi(paths, { method: 'GET', path: '/projects' }))).projects;
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
if (!(error instanceof NotRunningError))
|
|
271
|
+
throw error;
|
|
272
|
+
projects = withStore(paths, (store) => store.listProjects());
|
|
273
|
+
}
|
|
274
|
+
if (json)
|
|
275
|
+
return out(JSON.stringify(projects, null, 2));
|
|
276
|
+
if (projects.length === 0)
|
|
277
|
+
return out('no projects; add one with `conduct project add <path>`');
|
|
278
|
+
projects.forEach((p) => out(projectLine(p)));
|
|
279
|
+
}
|
|
280
|
+
async function main(argv) {
|
|
281
|
+
// `wait` has options of its own: its --version is a value, not this CLI's version.
|
|
282
|
+
if (argv[0] === 'wait')
|
|
283
|
+
return wait(argv.slice(1));
|
|
284
|
+
// `revise` takes free text, which option parsing would cut at a word such as --force. Every word
|
|
285
|
+
// after the item is the text, quoted or not: a cut-off instruction would be posted as Jason's.
|
|
286
|
+
if (argv[0] === 'revise') {
|
|
287
|
+
const [ref, ...words] = argv.slice(1);
|
|
288
|
+
const text = words.join(' ').trim();
|
|
289
|
+
if (ref === undefined || ref.startsWith('-') || !text)
|
|
290
|
+
throw new ConductorError('usage: conduct revise <item> "<what to change>"');
|
|
291
|
+
return answer(conductorPaths(), ref, { action: 'revise', text });
|
|
292
|
+
}
|
|
293
|
+
const { positionals, values } = parseArgs({
|
|
294
|
+
allowPositionals: true,
|
|
295
|
+
args: argv,
|
|
296
|
+
options: {
|
|
297
|
+
'allow-downgrade': { type: 'boolean' },
|
|
298
|
+
channel: { type: 'string' },
|
|
299
|
+
conductor: { type: 'string' },
|
|
300
|
+
follow: { type: 'boolean' },
|
|
301
|
+
force: { type: 'boolean' },
|
|
302
|
+
help: { short: 'h', type: 'boolean' },
|
|
303
|
+
json: { type: 'boolean' },
|
|
304
|
+
kind: { type: 'string' },
|
|
305
|
+
probe: { type: 'boolean' },
|
|
306
|
+
project: { type: 'string' },
|
|
307
|
+
prompt: { type: 'string' },
|
|
308
|
+
raise: { type: 'boolean' },
|
|
309
|
+
remove: { type: 'boolean' },
|
|
310
|
+
since: { type: 'string' },
|
|
311
|
+
tag: { type: 'string' },
|
|
312
|
+
text: { type: 'string' },
|
|
313
|
+
until: { type: 'string' },
|
|
314
|
+
version: { short: 'v', type: 'boolean' },
|
|
315
|
+
worker: { type: 'string' },
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
const paths = conductorPaths();
|
|
319
|
+
const [command, sub, arg] = positionals;
|
|
320
|
+
if (values.version)
|
|
321
|
+
return out(version());
|
|
322
|
+
if (values.help || command === undefined || command === 'help')
|
|
323
|
+
return out(USAGE);
|
|
324
|
+
switch (command) {
|
|
325
|
+
case 'approve':
|
|
326
|
+
if (sub === undefined)
|
|
327
|
+
throw new ConductorError('usage: conduct approve <item> [--text <text>]');
|
|
328
|
+
return answer(paths, sub, { action: 'approve', ...(values.text?.trim() ? { text: values.text.trim() } : {}) });
|
|
329
|
+
case 'attach':
|
|
330
|
+
if (sub === undefined)
|
|
331
|
+
throw new ConductorError('usage: conduct attach <worker>');
|
|
332
|
+
return attach(paths, sub);
|
|
333
|
+
case 'done':
|
|
334
|
+
if (sub === undefined)
|
|
335
|
+
throw new ConductorError('usage: conduct done <inbox id>');
|
|
336
|
+
return answer(paths, sub, { action: 'done' });
|
|
337
|
+
case 'events':
|
|
338
|
+
return events(paths, values.follow === true, values.since);
|
|
339
|
+
case 'inbox':
|
|
340
|
+
return inbox(paths, values.json === true);
|
|
341
|
+
case 'install-mod':
|
|
342
|
+
return values.remove ? removeMod(modDeps()) : installMod(fileURLToPath(new URL('../mod', import.meta.url)), modDeps());
|
|
343
|
+
case 'install-startup':
|
|
344
|
+
return values.remove ? uninstall() : install();
|
|
345
|
+
case 'launch':
|
|
346
|
+
if (sub === undefined)
|
|
347
|
+
throw new ConductorError('usage: conduct launch <item> [--project <name>] [--probe]');
|
|
348
|
+
return launch(paths, sub, {
|
|
349
|
+
conductor: values.conductor,
|
|
350
|
+
probe: values.probe,
|
|
351
|
+
project: values.project,
|
|
352
|
+
prompt: values.prompt,
|
|
353
|
+
});
|
|
354
|
+
case 'note':
|
|
355
|
+
if (sub === undefined || arg === undefined)
|
|
356
|
+
throw new ConductorError('usage: conduct note <item> <file> [--worker <worker>]');
|
|
357
|
+
process.exitCode = await noteCommand(paths, sub, arg, { worker: values.worker }, {
|
|
358
|
+
cwd: process.cwd(),
|
|
359
|
+
err: (line) => process.stderr.write(`${line}\n`),
|
|
360
|
+
out,
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
case 'notify':
|
|
364
|
+
if (sub === undefined)
|
|
365
|
+
return notifyStatus(paths, values.json === true);
|
|
366
|
+
if (sub === 'profile')
|
|
367
|
+
return notifyProfile(paths, arg, values.until);
|
|
368
|
+
if (sub === 'test')
|
|
369
|
+
return notifyTest(paths, { channel: values.channel, kind: values.kind, raise: values.raise === true });
|
|
370
|
+
throw new ConductorError('usage: conduct notify | conduct notify profile [<name>] [--until <when>] | conduct notify test');
|
|
371
|
+
case 'project':
|
|
372
|
+
if (sub === 'add' && arg !== undefined)
|
|
373
|
+
return addProject(paths, arg);
|
|
374
|
+
if (sub === 'list')
|
|
375
|
+
return listProjects(paths, values.json === true);
|
|
376
|
+
throw new ConductorError('usage: conduct project add <path> | conduct project list');
|
|
377
|
+
case 'rules':
|
|
378
|
+
if (sub === undefined)
|
|
379
|
+
return rules(paths, values.json === true);
|
|
380
|
+
if (sub === 'test' && arg !== undefined)
|
|
381
|
+
return rulesTest(paths, arg, values.json === true);
|
|
382
|
+
throw new ConductorError('usage: conduct rules [--json] | conduct rules test <file> [--json]');
|
|
383
|
+
case 'retire':
|
|
384
|
+
if (sub === undefined)
|
|
385
|
+
throw new ConductorError('usage: conduct retire <worker> [--force]');
|
|
386
|
+
return retire(paths, sub, values.force === true);
|
|
387
|
+
case 'serve':
|
|
388
|
+
return serve(paths);
|
|
389
|
+
case 'status':
|
|
390
|
+
return status(paths, values.json === true);
|
|
391
|
+
case 'stop':
|
|
392
|
+
await stopDaemon(paths);
|
|
393
|
+
return;
|
|
394
|
+
case 'upgrade':
|
|
395
|
+
return upgradeCommand(paths, values.tag, values['allow-downgrade'] === true);
|
|
396
|
+
default:
|
|
397
|
+
throw new ConductorError(`unknown command "${command}"`, ['run `conduct help` for the commands']);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function modDeps() {
|
|
401
|
+
return { claude: (args) => run('claude', args, { timeoutMs: 120_000 }), out };
|
|
402
|
+
}
|
|
403
|
+
function noInbox() {
|
|
404
|
+
return new ConductorError('the running conductor has no inbox: it is older than this conduct', [
|
|
405
|
+
'restart it on this build: conduct stop, then conduct serve (or conduct upgrade)',
|
|
406
|
+
]);
|
|
407
|
+
}
|
|
408
|
+
// GET /notify, or a ConductorError from a daemon older than the notifier.
|
|
409
|
+
async function notifyAnswer(paths, request) {
|
|
410
|
+
const answer = await callApi(paths, request);
|
|
411
|
+
if (answer.status === 404) {
|
|
412
|
+
throw new ConductorError('the running conductor has no notifier: it is older than this conduct', [
|
|
413
|
+
'restart it on this build: conduct stop, then conduct serve (or conduct upgrade)',
|
|
414
|
+
]);
|
|
415
|
+
}
|
|
416
|
+
return expectOk(Promise.resolve(answer));
|
|
417
|
+
}
|
|
418
|
+
// `conduct notify profile`: the active profile, or switch to <name>.
|
|
419
|
+
async function notifyProfile(paths, name, until) {
|
|
420
|
+
if (name === undefined) {
|
|
421
|
+
if (until !== undefined)
|
|
422
|
+
throw new ConductorError('usage: conduct notify profile <name> --until <when>');
|
|
423
|
+
return notifyStatus(paths, false);
|
|
424
|
+
}
|
|
425
|
+
const body = { name, ...(until === undefined ? {} : { until: parseUntil(until, new Date()).toISOString() }) };
|
|
426
|
+
const status = notifyStatusSchema.parse(await notifyAnswer(paths, { body, method: 'POST', path: '/notify/profile' }));
|
|
427
|
+
notifyLines(status).forEach((line) => out(line));
|
|
428
|
+
}
|
|
429
|
+
async function notifyStatus(paths, json) {
|
|
430
|
+
const status = notifyStatusSchema.parse(await notifyAnswer(paths, { method: 'GET', path: '/notify' }));
|
|
431
|
+
if (json)
|
|
432
|
+
return out(JSON.stringify(status, null, 2));
|
|
433
|
+
notifyLines(status).forEach((line) => out(line));
|
|
434
|
+
}
|
|
435
|
+
async function notifyTest(paths, options) {
|
|
436
|
+
if (options.raise && options.channel)
|
|
437
|
+
throw new ConductorError('--raise routes the item by the profile; it takes no --channel');
|
|
438
|
+
const body = { ...(options.channel ? { channel: options.channel } : {}), ...(options.kind ? { kind: options.kind } : {}), raise: options.raise };
|
|
439
|
+
// Pushover can take its 15 s, after a Credential Manager read.
|
|
440
|
+
const answer = await notifyAnswer(paths, { body, method: 'POST', path: '/notify/test', timeoutMs: 60_000 });
|
|
441
|
+
if (options.raise) {
|
|
442
|
+
const { item } = z.object({ item: inboxItemSchema }).parse(answer);
|
|
443
|
+
return out(`raised inbox item #${item.id} (${item.kind}); \`conduct done ${item.id}\` clears it`);
|
|
444
|
+
}
|
|
445
|
+
const result = testResultSchema.parse(answer);
|
|
446
|
+
if (result.channels.length === 0)
|
|
447
|
+
return out(`profile ${result.profile} sends a ${body.kind ?? 'npm-otp'} item nowhere`);
|
|
448
|
+
out(`profile ${result.profile}${result.quietNow ? ' (quiet hours now: a real item would wait for the summary)' : ''}`);
|
|
449
|
+
result.results.forEach((r) => out(` ${r.channel}: ${r.ok ? 'ok' : 'failed'}: ${r.detail}`));
|
|
450
|
+
if (result.results.some((r) => !r.ok))
|
|
451
|
+
process.exitCode = 1;
|
|
452
|
+
}
|
|
453
|
+
// npm is a .cmd shim on Windows, which starts through a shell alone. The only argument from input is
|
|
454
|
+
// the tag, which parseTag limits to dev or latest.
|
|
455
|
+
function npm(args, timeoutMs) {
|
|
456
|
+
return run('npm', args, { shell: process.platform === 'win32', timeoutMs });
|
|
457
|
+
}
|
|
458
|
+
// The open inbox items, or null from a daemon older than the inbox (a linked build ahead of it).
|
|
459
|
+
async function openItems(paths) {
|
|
460
|
+
const answer = await callApi(paths, { method: 'GET', path: '/inbox' });
|
|
461
|
+
if (answer.status === 404)
|
|
462
|
+
return null;
|
|
463
|
+
return inboxBody.parse(await expectOk(Promise.resolve(answer))).items;
|
|
464
|
+
}
|
|
465
|
+
function out(line) {
|
|
466
|
+
process.stdout.write(`${line}\n`);
|
|
467
|
+
}
|
|
468
|
+
// The open plan item of `ref`: the item (ATK-312) whose plan waits, or an inbox id written #12. A
|
|
469
|
+
// bare number is refused: it could be an issue's number, and the answer is posted as Jason's.
|
|
470
|
+
async function planItem(paths, ref) {
|
|
471
|
+
if (ref.startsWith('#'))
|
|
472
|
+
return inboxId(ref);
|
|
473
|
+
if (/^\d+$/.test(ref)) {
|
|
474
|
+
throw new ConductorError(`"${ref}" is ambiguous`, [`name the item, such as ATK-${ref}, or the inbox id as #${ref}`]);
|
|
475
|
+
}
|
|
476
|
+
const items = await openItems(paths);
|
|
477
|
+
if (items === null)
|
|
478
|
+
throw noInbox();
|
|
479
|
+
const plans = items.filter((i) => i.kind === 'plan' && i.item?.toUpperCase() === ref.toUpperCase());
|
|
480
|
+
if (plans.length === 1 && plans[0])
|
|
481
|
+
return plans[0].id;
|
|
482
|
+
if (plans.length === 0)
|
|
483
|
+
throw new ConductorError(`no plan of ${ref} waits in the inbox`, ['`conduct inbox` lists what waits']);
|
|
484
|
+
throw new ConductorError(`several plans of ${ref} wait`, plans.map((p) => `#${p.id} in ${p.project ?? '?'}: pass #${p.id}`));
|
|
485
|
+
}
|
|
486
|
+
function projectLine(p) {
|
|
487
|
+
const state = p.problem ? `${p.problem}; run \`conduct project add\` again` : p.enabled ? 'enabled' : 'disabled';
|
|
488
|
+
return ` ${p.name} ${p.root} (protocol ${p.protocol}, ${state})`;
|
|
489
|
+
}
|
|
490
|
+
async function retire(paths, ref, force) {
|
|
491
|
+
const worker = await findWorker(paths, ref);
|
|
492
|
+
const { body, status } = await callApi(paths, {
|
|
493
|
+
method: 'DELETE',
|
|
494
|
+
path: `/workers/${worker.id}${force ? '?force=true' : ''}`,
|
|
495
|
+
timeoutMs: RETIRE_TIMEOUT_MS,
|
|
496
|
+
});
|
|
497
|
+
const parsed = retireBody.safeParse(body);
|
|
498
|
+
if (!parsed.success) {
|
|
499
|
+
await expectOk(Promise.resolve({ body, status })); // a plain error answer
|
|
500
|
+
throw new ConductorError(`the API answered ${status} with an unexpected body`);
|
|
501
|
+
}
|
|
502
|
+
const result = parsed.data;
|
|
503
|
+
const steps = (result.steps ?? []).map((s) => `${s.step}: ${s.ok ? 'ok' : `failed (${s.error ?? '?'})`}`);
|
|
504
|
+
if (result.outcome === 'refused') {
|
|
505
|
+
const work = [
|
|
506
|
+
...(result.work?.unpushed ? [`${result.work.unpushed} unpushed commit(s)`] : []),
|
|
507
|
+
...(result.work?.dirty ? [`uncommitted: ${result.work.dirty.join(', ')}`] : []),
|
|
508
|
+
...(result.work?.unknown ? [`not checked: ${result.work.unknown}`] : []),
|
|
509
|
+
];
|
|
510
|
+
throw new ConductorError(`refused to retire ${worker.name}: ${result.reason ?? ''}`, work);
|
|
511
|
+
}
|
|
512
|
+
if (result.outcome === 'partial')
|
|
513
|
+
throw new ConductorError(`retired ${worker.name} only in part`, steps);
|
|
514
|
+
out(`retired ${worker.name}`);
|
|
515
|
+
steps.forEach((s) => out(` ${s}`));
|
|
516
|
+
}
|
|
517
|
+
// `conduct rules`: from the daemon, or straight from the files when it isn't running.
|
|
518
|
+
async function rules(paths, json) {
|
|
519
|
+
const view = await rulesAnswer(paths, { method: 'GET', path: '/rules' }, (body) => rulesViewSchema.parse(body), (service) => service.view());
|
|
520
|
+
if (json)
|
|
521
|
+
return out(JSON.stringify(view, null, 2));
|
|
522
|
+
rulesLines(view).forEach((line) => out(line));
|
|
523
|
+
}
|
|
524
|
+
// The daemon's answer, or, when none runs, the same thing worked out here from the files.
|
|
525
|
+
async function rulesAnswer(paths, request, parse, local) {
|
|
526
|
+
let answer;
|
|
527
|
+
try {
|
|
528
|
+
answer = await callApi(paths, request);
|
|
529
|
+
}
|
|
530
|
+
catch (error) {
|
|
531
|
+
if (!(error instanceof NotRunningError))
|
|
532
|
+
throw error;
|
|
533
|
+
process.stderr.write('conduct: the conductor is not running; read from the files\n');
|
|
534
|
+
const config = loadConfig(paths.config);
|
|
535
|
+
return withStore(paths, (store) => {
|
|
536
|
+
const service = rulesService({ config, listProjects: () => store.listProjects(), paths });
|
|
537
|
+
service.reload();
|
|
538
|
+
return local(service);
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
if (answer.status === 404) {
|
|
542
|
+
throw new ConductorError('the running conductor has no rules engine: it is older than this conduct', [
|
|
543
|
+
'restart it on this build: conduct stop, then conduct serve (or conduct upgrade)',
|
|
544
|
+
]);
|
|
545
|
+
}
|
|
546
|
+
return parse(await expectOk(Promise.resolve(answer)));
|
|
547
|
+
}
|
|
548
|
+
async function rulesTest(paths, file, json) {
|
|
549
|
+
let text;
|
|
550
|
+
try {
|
|
551
|
+
text = readFileSync(file, 'utf8');
|
|
552
|
+
}
|
|
553
|
+
catch (error) {
|
|
554
|
+
throw new ConductorError(`could not read ${file}`, [String(error)]);
|
|
555
|
+
}
|
|
556
|
+
const sample = parseJsonc(text, z.unknown(), file);
|
|
557
|
+
const result = await rulesAnswer(paths, { body: sample, method: 'POST', path: '/rules/test' }, (body) => rulesTestResultSchema.parse(body), (service) => service.test(sample));
|
|
558
|
+
if (json)
|
|
559
|
+
return out(JSON.stringify(result, null, 2));
|
|
560
|
+
testLines(result).forEach((line) => out(line));
|
|
561
|
+
}
|
|
562
|
+
async function serve(paths) {
|
|
563
|
+
const daemon = await startDaemon(paths, { version: version() });
|
|
564
|
+
out(`conductor listening on 127.0.0.1:${daemon.port} (pid ${process.pid}, home ${paths.home})`);
|
|
565
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGBREAK']) {
|
|
566
|
+
process.once(signal, () => void daemon.stop(signal));
|
|
567
|
+
}
|
|
568
|
+
// Under the logon task nobody sees stderr, so a crash goes to the log. The pid file it leaves
|
|
569
|
+
// names a dead process, which the next start replaces.
|
|
570
|
+
process.on('uncaughtException', (error) => {
|
|
571
|
+
daemon.log.log('error', 'uncaught exception', { error: error.stack ?? String(error) });
|
|
572
|
+
process.exit(1);
|
|
573
|
+
});
|
|
574
|
+
await daemon.closed;
|
|
575
|
+
}
|
|
576
|
+
function sleep(ms) {
|
|
577
|
+
return new Promise((done) => setTimeout(done, ms));
|
|
578
|
+
}
|
|
579
|
+
async function status(paths, json) {
|
|
580
|
+
let projects;
|
|
581
|
+
try {
|
|
582
|
+
projects = projectsBody.parse(await expectOk(callApi(paths, { method: 'GET', path: '/projects' }))).projects;
|
|
583
|
+
}
|
|
584
|
+
catch (error) {
|
|
585
|
+
if (!(json && error instanceof NotRunningError))
|
|
586
|
+
throw error;
|
|
587
|
+
// Scripts and the conductor mod read the answer, so "not running" is JSON too (exit 1).
|
|
588
|
+
out(JSON.stringify({ home: paths.home, running: false }, null, 2));
|
|
589
|
+
process.exitCode = 1;
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const { workers } = fleetBody.parse(await expectOk(callApi(paths, { method: 'GET', path: '/fleet' })));
|
|
593
|
+
// A linked build's CLI can be newer than the daemon still running, which has no inbox (null).
|
|
594
|
+
const items = await openItems(paths);
|
|
595
|
+
const pid = readPid(paths.pid);
|
|
596
|
+
const port = readEndpoint(paths)?.port ?? null;
|
|
597
|
+
if (json)
|
|
598
|
+
return out(JSON.stringify({ home: paths.home, inbox: items, pid, port, projects, running: true, workers }, null, 2));
|
|
599
|
+
out(`conductor running (pid ${pid ?? '?'}, port ${port ?? '?'}, home ${paths.home})`);
|
|
600
|
+
out(projects.length === 0 ? 'no projects; add one with `conduct project add <path>`' : 'projects:');
|
|
601
|
+
projects.forEach((p) => out(projectLine(p)));
|
|
602
|
+
out(workers.length === 0 ? 'no workers' : 'workers:');
|
|
603
|
+
workers.forEach((w) => out(workerLine(w)));
|
|
604
|
+
if (items === null)
|
|
605
|
+
out('inbox: not in this daemon (restart it to run this build)');
|
|
606
|
+
else
|
|
607
|
+
out(items.length === 0 ? 'inbox: empty' : `inbox: ${items.length} open (conduct inbox)`);
|
|
608
|
+
}
|
|
609
|
+
// True when it stopped a daemon, false when none ran.
|
|
610
|
+
async function stopDaemon(paths) {
|
|
611
|
+
const pid = readPid(paths.pid);
|
|
612
|
+
try {
|
|
613
|
+
await expectOk(callApi(paths, { method: 'POST', path: '/shutdown' }));
|
|
614
|
+
}
|
|
615
|
+
catch (error) {
|
|
616
|
+
if (!(error instanceof NotRunningError))
|
|
617
|
+
throw error;
|
|
618
|
+
out('the conductor is not running');
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
const deadline = Date.now() + STOP_WAIT_MS;
|
|
622
|
+
while (readEndpoint(paths) !== null && Date.now() < deadline)
|
|
623
|
+
await sleep(100);
|
|
624
|
+
if (readEndpoint(paths) !== null) {
|
|
625
|
+
throw new ConductorError(`the conductor (pid ${pid ?? '?'}) did not stop within ${STOP_WAIT_MS / 1000} s`);
|
|
626
|
+
}
|
|
627
|
+
// The port file goes before the process ends; a start (the logon task's especially) waits for it.
|
|
628
|
+
const exitDeadline = Date.now() + STOP_WAIT_MS;
|
|
629
|
+
while (pid !== null && processAlive(pid) && Date.now() < exitDeadline)
|
|
630
|
+
await sleep(100);
|
|
631
|
+
out(`stopped the conductor (pid ${pid ?? '?'})`);
|
|
632
|
+
return true;
|
|
633
|
+
}
|
|
634
|
+
function uninstall() {
|
|
635
|
+
out(removeStartup() ? `removed the "${TASK_NAME}" logon task` : `no "${TASK_NAME}" logon task to remove`);
|
|
636
|
+
}
|
|
637
|
+
async function upgradeCommand(paths, tag, allowDowngrade) {
|
|
638
|
+
const cliPath = realpathSync(fileURLToPath(import.meta.url));
|
|
639
|
+
await upgrade({ allowDowngrade, tag: parseTag(tag) }, {
|
|
640
|
+
awaitDaemon: () => awaitDaemon(paths),
|
|
641
|
+
cliPath,
|
|
642
|
+
installedVersion: version,
|
|
643
|
+
npmInstall: (spec) => npm(['install', '--global', spec], NPM_INSTALL_TIMEOUT_MS),
|
|
644
|
+
npmRoot: async () => {
|
|
645
|
+
const result = await npm(['root', '--global']);
|
|
646
|
+
if (result.code !== 0)
|
|
647
|
+
throw new ConductorError('could not ask npm for its global folder', [outputOf(result)]);
|
|
648
|
+
return realpathSync(result.stdout.trim());
|
|
649
|
+
},
|
|
650
|
+
npmView: async (spec) => {
|
|
651
|
+
const result = await npm(['view', spec, 'version']);
|
|
652
|
+
const found = result.stdout.trim();
|
|
653
|
+
if (result.code !== 0 || found === '')
|
|
654
|
+
throw new ConductorError(`npm has no ${spec}`, [outputOf(result)]);
|
|
655
|
+
return found;
|
|
656
|
+
},
|
|
657
|
+
out,
|
|
658
|
+
runTask: runStartup,
|
|
659
|
+
// A failed start shows as awaitDaemon's timeout. The daemon runs in its home, not in the
|
|
660
|
+
// caller's directory, which it would hold open (a worktree it may later remove, say).
|
|
661
|
+
serveDetached: () => spawn(process.execPath, [cliPath, 'serve'], { cwd: paths.home, detached: true, stdio: 'ignore', windowsHide: true })
|
|
662
|
+
.on('error', () => { })
|
|
663
|
+
.unref(),
|
|
664
|
+
stopDaemon: () => stopDaemon(paths),
|
|
665
|
+
// The logon task's daemon uses ~/.conductor, so it can't restart one kept in CONDUCTOR_HOME.
|
|
666
|
+
taskRunsCli: () => !process.env.CONDUCTOR_HOME && startupRunsCli(cliPath),
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
// Read from disk each time: upgrade asks again after npm has replaced the package.
|
|
670
|
+
function version() {
|
|
671
|
+
const file = fileURLToPath(new URL('../package.json', import.meta.url));
|
|
672
|
+
return z.object({ version: z.string() }).parse(JSON.parse(readFileSync(file, 'utf8'))).version;
|
|
673
|
+
}
|
|
674
|
+
async function wait(argv) {
|
|
675
|
+
const { positionals, values } = parseArgs({
|
|
676
|
+
allowPositionals: true,
|
|
677
|
+
args: argv,
|
|
678
|
+
options: {
|
|
679
|
+
commit: { type: 'string' },
|
|
680
|
+
item: { type: 'string' },
|
|
681
|
+
local: { type: 'boolean' },
|
|
682
|
+
package: { type: 'string' },
|
|
683
|
+
repo: { type: 'string' },
|
|
684
|
+
tag: { type: 'string' },
|
|
685
|
+
timeout: { type: 'string' },
|
|
686
|
+
version: { type: 'string' },
|
|
687
|
+
worker: { type: 'string' },
|
|
688
|
+
},
|
|
689
|
+
});
|
|
690
|
+
const timeout = values.timeout === undefined ? undefined : Number(values.timeout);
|
|
691
|
+
if (timeout !== undefined && !(timeout > 0))
|
|
692
|
+
throw new ConductorError('--timeout takes minutes, such as --timeout 25');
|
|
693
|
+
if (positionals.length === 0)
|
|
694
|
+
throw new ConductorError('usage: conduct wait <condition>', ['run `conduct help` for the conditions']);
|
|
695
|
+
process.exitCode = await waitCommand(conductorPaths(), positionals, { ...values, timeoutMinutes: timeout }, { cwd: process.cwd(), err: (line) => process.stderr.write(`${line}\n`), out });
|
|
696
|
+
}
|
|
697
|
+
function withStore(paths, use) {
|
|
698
|
+
mkdirSync(paths.home, { recursive: true });
|
|
699
|
+
const store = openStore(paths.db);
|
|
700
|
+
try {
|
|
701
|
+
return use(store);
|
|
702
|
+
}
|
|
703
|
+
finally {
|
|
704
|
+
store.close();
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
// One line per worker: name, project, status and session activity, its phase and waits, then spend.
|
|
708
|
+
function workerLine(w) {
|
|
709
|
+
const activity = w.status === 'running' ? `running, ${w.activity}` : w.status;
|
|
710
|
+
const waits = w.waits.map((x) => (x.resolvedAt ? `${x.condition} (resolved)` : x.condition));
|
|
711
|
+
return [
|
|
712
|
+
` ${w.name} ${w.project} ${activity}`,
|
|
713
|
+
...(w.phase ? [`phase ${w.phase}`] : []),
|
|
714
|
+
...(waits.length > 0 ? [`waits: ${waits.join(', ')}`] : []),
|
|
715
|
+
`spend ${formatTokens(w.spend.today)} today, ${formatTokens(w.spend.total)} total`,
|
|
716
|
+
].join(' · ');
|
|
717
|
+
}
|
|
718
|
+
main(process.argv.slice(2)).catch((error) => {
|
|
719
|
+
if (error instanceof ConductorError) {
|
|
720
|
+
process.stderr.write(`conduct: ${error.message}\n${error.details.map((d) => ` ${d}\n`).join('')}`);
|
|
721
|
+
}
|
|
722
|
+
else if (error instanceof Error && 'code' in error && typeof error.code === 'string' && error.code.startsWith('ERR_PARSE_ARGS')) {
|
|
723
|
+
process.stderr.write(`conduct: ${error.message}\n${USAGE}\n`);
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
process.stderr.write(`conduct: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`);
|
|
727
|
+
}
|
|
728
|
+
process.exitCode = 1;
|
|
729
|
+
});
|
|
730
|
+
//# sourceMappingURL=cli.js.map
|