aegiscode 6.5.8 → 6.6.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/bin/aegiscode.js +39 -0
- package/package.json +1 -1
- package/scripts/predist.mjs +8 -0
- package/src/app.js +164 -19
- package/src/autonomous.js +372 -0
- package/src/deps.js +12 -1
- package/vendor/client/update.js +76 -11
- package/vendor/desktop/lib/local/autonomous.js +600 -0
- package/vendor/desktop/lib/local/git-scope.js +163 -9
- package/vendor/desktop/lib/local/queue.js +534 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `aegiscode autonomous …` — the unattended host of the work queue.
|
|
5
|
+
*
|
|
6
|
+
* The queue itself (desktop/lib/local/queue.js) is a file, and the worker
|
|
7
|
+
* (desktop/lib/local/autonomous.js) drives the SAME engine the chat surfaces
|
|
8
|
+
* use. What is left for this module is the host: a subcommand surface a human
|
|
9
|
+
* or a timer can call, with the exit codes a script needs.
|
|
10
|
+
*
|
|
11
|
+
* Why the terminal is the unattended home rather than the GUI: a cron job, a
|
|
12
|
+
* systemd timer and `ssh box 'aegiscode autonomous proceed'` all need a process
|
|
13
|
+
* that starts, drains, and exits with a status — not a window somebody has to
|
|
14
|
+
* click. The desktop host can drain too, but only when the user asks it to.
|
|
15
|
+
*
|
|
16
|
+
* Reused, never reimplemented: the model call is cli/src/engine.js's
|
|
17
|
+
* createEngine (same tool loop as the GUI, confirm gate FORCED OFF), the task
|
|
18
|
+
* list is deps.queue (the file the GUI writes), and the commit rule is
|
|
19
|
+
* git-scope.js's scopedCommit. This file is argv → those calls, and the honest
|
|
20
|
+
* report of what came back.
|
|
21
|
+
*
|
|
22
|
+
* Exit codes (they are the point of a headless host):
|
|
23
|
+
* 0 everything asked for happened
|
|
24
|
+
* 1 a task failed, or another drain holds the lock
|
|
25
|
+
* 2 usage error, or no credential to run an unattended turn with
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const path = require('node:path');
|
|
29
|
+
|
|
30
|
+
const { queue, autonomous } = require('./deps.js');
|
|
31
|
+
const { createEngine } = require('./engine.js');
|
|
32
|
+
const { fmtElapsed } = require('./format.js');
|
|
33
|
+
|
|
34
|
+
const SUBCOMMANDS = new Set([
|
|
35
|
+
'add', 'list', 'run', 'proceed', 'reconcile', 'clear', 'retry', 'help',
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parse the subcommand's own argv. Deliberately its own tiny parser rather than
|
|
40
|
+
* bin/aegiscode.js's: the top-level parser owns the one-shot/session flags, and
|
|
41
|
+
* a queue task's text must survive untouched (`aegiscode autonomous add "fix
|
|
42
|
+
* --json in the parser"` is a task, not a flag).
|
|
43
|
+
*/
|
|
44
|
+
function parseFlags(argv) {
|
|
45
|
+
const opts = { terms: [], json: false, flags: {} };
|
|
46
|
+
const takesValue = new Set(['cwd', 'model', 'effort', 'workers', 'max', 'max-rounds', 'source']);
|
|
47
|
+
for (let i = 0; i < argv.length; i++) {
|
|
48
|
+
const a = argv[i];
|
|
49
|
+
if (a === '--json') {
|
|
50
|
+
opts.json = true;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (a.startsWith('--')) {
|
|
54
|
+
const [rawName, inline] = a.slice(2).split('=');
|
|
55
|
+
const name = rawName.trim();
|
|
56
|
+
const bools = new Set(['commit', 'all', 'auto', 'stop-on-error', 'single-pass', 'help']);
|
|
57
|
+
if (bools.has(name)) {
|
|
58
|
+
opts.flags[name] = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (!takesValue.has(name)) throw new Error(`unknown option: --${name}`);
|
|
62
|
+
const value = inline !== undefined ? inline : argv[++i];
|
|
63
|
+
if (value === undefined) throw new Error(`--${name} needs a value`);
|
|
64
|
+
opts.flags[name] = value;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
opts.terms.push(a);
|
|
68
|
+
}
|
|
69
|
+
return opts;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** `--max-rounds` / `--max-rounds=`: both spellings, one number. */
|
|
73
|
+
function intFlag(flags, name) {
|
|
74
|
+
const raw = flags[name];
|
|
75
|
+
if (raw === undefined || raw === '') return null;
|
|
76
|
+
const n = Number.parseInt(raw, 10);
|
|
77
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The row a human reads for one queued task. */
|
|
81
|
+
function taskLine(item) {
|
|
82
|
+
const model = item.model || '(default)';
|
|
83
|
+
const status = String(item.status || '?').padEnd(7);
|
|
84
|
+
const at = `#${item.id}`.padEnd(4);
|
|
85
|
+
const task = String(item.task || '').replace(/\s+/g, ' ').trim();
|
|
86
|
+
const clipped = task.length > 72 ? `${task.slice(0, 71)}…` : task;
|
|
87
|
+
return `${at} ${status} ${model} ${clipped}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function describeCounts(items) {
|
|
91
|
+
const counts = { pending: 0, running: 0, done: 0, error: 0 };
|
|
92
|
+
for (const i of items) if (counts[i.status] !== undefined) counts[i.status] += 1;
|
|
93
|
+
return counts;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Human summary of a drain, from the outcomes the worker returned. Reported per
|
|
98
|
+
* task, because the one thing a timer's log has to answer is "which task
|
|
99
|
+
* failed, and why".
|
|
100
|
+
*/
|
|
101
|
+
function printRunReport(ran, io) {
|
|
102
|
+
for (const r of ran) {
|
|
103
|
+
const mark = r.ok ? '✓' : '✗';
|
|
104
|
+
io.stdout.write(`aegiscode: ${mark} #${r.id} ${r.ok ? 'done' : 'failed'} (${fmtElapsed(r.ms)})`);
|
|
105
|
+
if (r.commit && r.commit.ok && r.commit.staged) {
|
|
106
|
+
io.stdout.write(` — committed ${r.commit.staged} path(s)`);
|
|
107
|
+
} else if (r.commit && r.commit.skipped && r.commit.reason) {
|
|
108
|
+
io.stdout.write(` — no commit (${r.commit.reason})`);
|
|
109
|
+
}
|
|
110
|
+
io.stdout.write('\n');
|
|
111
|
+
if (!r.ok && r.error) io.stderr.write(`aegiscode: #${r.id}: ${r.error}\n`);
|
|
112
|
+
if (r.ok && r.output) {
|
|
113
|
+
const text = String(r.output).trim();
|
|
114
|
+
// The report the task wrote, kept to a readable length: a drain's log is
|
|
115
|
+
// read by a human deciding whether to trust the run.
|
|
116
|
+
io.stdout.write(`${text.length > 4000 ? `${text.slice(0, 4000)}\n… [output clipped]` : text}\n`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The engine for an unattended turn: the CLI's own engine wrapper, with the
|
|
123
|
+
* approval gate pinned OFF.
|
|
124
|
+
*
|
|
125
|
+
* Not a nicety — a requirement of the task. There is nobody to click a card in
|
|
126
|
+
* a drain, and the engine's own answer to an unanswered card is to refuse the
|
|
127
|
+
* tool, so a gated drain would either hang the task or silently do nothing.
|
|
128
|
+
* `autonomous.js` still watches for an approval frame and fails the task
|
|
129
|
+
* loudly, which is how a future caller who forgets this gets told.
|
|
130
|
+
*/
|
|
131
|
+
function buildEngine({ io, quiet = false }) {
|
|
132
|
+
const credentials = require('./credentials.js');
|
|
133
|
+
const client = require('./deps.js').createClient(credentials.clientOptions());
|
|
134
|
+
if (!client.apiKey) {
|
|
135
|
+
return { error: `no AEGIS account key — ${credentials.HOW_TO_SET}, or set $${credentials.KEY_ENV}` };
|
|
136
|
+
}
|
|
137
|
+
const engine = createEngine({ client, getConfirmMode: () => false });
|
|
138
|
+
return { client, engine };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A progress sink for a headless drain: one line per step, never a chat delta. */
|
|
142
|
+
function progressSink(io, { json }) {
|
|
143
|
+
if (json) return () => {};
|
|
144
|
+
return (event) => {
|
|
145
|
+
if (!event || typeof event !== 'object') return;
|
|
146
|
+
if (event.type === 'start') {
|
|
147
|
+
io.stdout.write(`aegiscode: #${event.taskId} running on ${event.model} in ${event.cwd}\n`);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (event.type === 'tool' && event.tool && event.tool.phase === 'run') {
|
|
151
|
+
const args = event.tool.args || {};
|
|
152
|
+
// The engine spells a written file `file_path`, not `path` — knowledge
|
|
153
|
+
// the shared worker owns in writtenPath(). Asking it instead of
|
|
154
|
+
// re-listing the argument names here is what stops this line from
|
|
155
|
+
// printing `⚙ writeFile` with nothing after it, which is precisely what
|
|
156
|
+
// it did: an unattended drain could not say which file it wrote.
|
|
157
|
+
const what = args.command || args.pattern || autonomous.writtenPath(event.tool) || '';
|
|
158
|
+
const shown = String(what).slice(0, 100);
|
|
159
|
+
io.stderr.write(` ⚙ ${event.tool.name}${shown ? ` ${shown}` : ''}\n`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (event.type === 'healed') {
|
|
163
|
+
io.stdout.write(`aegiscode: PLAN.md status list healed for phase(s) ${event.phases.join(', ')}\n`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (event.type === 'recovered') {
|
|
167
|
+
io.stdout.write(`aegiscode: re-queued task(s) ${event.ids.join(', ')} from a dead worker\n`);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Run one queue subcommand.
|
|
174
|
+
*
|
|
175
|
+
* @param {string} command add|list|run|proceed|reconcile|clear|retry
|
|
176
|
+
* @param {string[]} argv the subcommand's own arguments
|
|
177
|
+
* @param {{stdout:any,stderr:any,json?:boolean}} io
|
|
178
|
+
* @returns {Promise<number>} process exit code
|
|
179
|
+
*/
|
|
180
|
+
async function runQueueCommand(command, argv, io = {}) {
|
|
181
|
+
const stdout = io.stdout || process.stdout;
|
|
182
|
+
const stderr = io.stderr || process.stderr;
|
|
183
|
+
const out = { stdout, stderr };
|
|
184
|
+
|
|
185
|
+
let args;
|
|
186
|
+
try {
|
|
187
|
+
args = parseFlags(argv || []);
|
|
188
|
+
} catch (e) {
|
|
189
|
+
stderr.write(`aegiscode autonomous: ${e.message}\n`);
|
|
190
|
+
return 2;
|
|
191
|
+
}
|
|
192
|
+
const json = Boolean(io.json || args.json);
|
|
193
|
+
const emit = (payload, text) => {
|
|
194
|
+
if (json) stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
195
|
+
else if (text !== undefined) stdout.write(text.endsWith('\n') ? text : `${text}\n`);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
if (command === 'help' || !SUBCOMMANDS.has(command)) {
|
|
199
|
+
stdout.write(usageText());
|
|
200
|
+
return command === 'help' ? 0 : 2;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const cwd = args.flags.cwd ? path.resolve(String(args.flags.cwd)) : process.cwd();
|
|
204
|
+
|
|
205
|
+
if (command === 'add') {
|
|
206
|
+
const task = args.terms.join(' ').trim();
|
|
207
|
+
if (!task) {
|
|
208
|
+
stderr.write('aegiscode autonomous add: a task needs text — `aegiscode autonomous add "…"`\n');
|
|
209
|
+
return 2;
|
|
210
|
+
}
|
|
211
|
+
let item;
|
|
212
|
+
try {
|
|
213
|
+
item = queue.addTask(process.env, {
|
|
214
|
+
task,
|
|
215
|
+
cwd,
|
|
216
|
+
model: args.flags.model || null,
|
|
217
|
+
effort: args.flags.effort || null,
|
|
218
|
+
workers: intFlag(args.flags, 'workers'),
|
|
219
|
+
maxRounds: intFlag(args.flags, 'max-rounds'),
|
|
220
|
+
singlePass: Boolean(args.flags['single-pass']),
|
|
221
|
+
commit: Boolean(args.flags.commit),
|
|
222
|
+
source: args.flags.source || 'cli',
|
|
223
|
+
});
|
|
224
|
+
} catch (e) {
|
|
225
|
+
stderr.write(`aegiscode autonomous add: ${e.message}\n`);
|
|
226
|
+
return 2;
|
|
227
|
+
}
|
|
228
|
+
emit({ ok: true, id: item.id, item, queue: queue.queuePath(process.env) },
|
|
229
|
+
`aegiscode: queued #${item.id} — ${queue.pendingCount(queue.loadQueue(process.env))} pending`);
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (command === 'list') {
|
|
234
|
+
const items = queue.loadQueue(process.env);
|
|
235
|
+
const counts = describeCounts(items);
|
|
236
|
+
if (json) {
|
|
237
|
+
emit({ ok: true, file: queue.queuePath(process.env), counts, tasks: items, runs: queue.readRuns(process.env, { limit: 10 }) });
|
|
238
|
+
} else {
|
|
239
|
+
stdout.write(`queue: ${queue.queuePath(process.env)}\n`);
|
|
240
|
+
stdout.write(`tasks: ${items.length} (${counts.pending} pending, ${counts.running} running, ${counts.done} done, ${counts.error} error)\n`);
|
|
241
|
+
for (const item of items) stdout.write(`${taskLine(item)}\n`);
|
|
242
|
+
if (!items.length) stdout.write('(empty)\n');
|
|
243
|
+
}
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (command === 'clear') {
|
|
248
|
+
const res = queue.clearQueue(process.env, { all: Boolean(args.flags.all) });
|
|
249
|
+
emit({ ok: true, ...res }, `aegiscode: removed ${res.removed} finished task(s); ${res.kept} kept`);
|
|
250
|
+
return 0;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (command === 'retry') {
|
|
254
|
+
const id = args.terms[0];
|
|
255
|
+
if (!id) {
|
|
256
|
+
stderr.write('aegiscode autonomous retry: which task? `aegiscode autonomous retry <id>`\n');
|
|
257
|
+
return 2;
|
|
258
|
+
}
|
|
259
|
+
const item = queue.retryTask(process.env, id);
|
|
260
|
+
if (!item) {
|
|
261
|
+
stderr.write(`aegiscode autonomous retry: no task #${id}\n`);
|
|
262
|
+
return 1;
|
|
263
|
+
}
|
|
264
|
+
emit({ ok: true, id: item.id, item }, `aegiscode: #${item.id} is pending again`);
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ── the three commands that need a model ─────────────────────────────────
|
|
269
|
+
const built = buildEngine({ io });
|
|
270
|
+
if (built.error) {
|
|
271
|
+
stderr.write(`aegiscode autonomous: ${built.error}\n`);
|
|
272
|
+
return 2;
|
|
273
|
+
}
|
|
274
|
+
const worker = autonomous.createQueueWorker({
|
|
275
|
+
engine: built.engine,
|
|
276
|
+
env: process.env,
|
|
277
|
+
log: progressSink(out, { json }),
|
|
278
|
+
});
|
|
279
|
+
const commit = args.flags.commit ? true : undefined;
|
|
280
|
+
const onSigint = () => {
|
|
281
|
+
// Ctrl-C stops the RUNNING turn, not the process: the task is settled back
|
|
282
|
+
// to `error` with a reason, so the queue does not keep a `running` item
|
|
283
|
+
// whose worker is gone.
|
|
284
|
+
const items = queue.loadQueue(process.env);
|
|
285
|
+
const running = items.find((i) => i.status === 'running' && i.pid === process.pid);
|
|
286
|
+
if (running) worker.cancel(running.id);
|
|
287
|
+
};
|
|
288
|
+
process.on('SIGINT', onSigint);
|
|
289
|
+
|
|
290
|
+
try {
|
|
291
|
+
if (command === 'reconcile') {
|
|
292
|
+
const res = await worker.reconcile({
|
|
293
|
+
cwd,
|
|
294
|
+
auto: Boolean(args.flags.auto),
|
|
295
|
+
commit,
|
|
296
|
+
max: intFlag(args.flags, 'max') || 0,
|
|
297
|
+
stopOnError: Boolean(args.flags['stop-on-error']),
|
|
298
|
+
});
|
|
299
|
+
// A reconcile that could not run has to SAY WHY on stderr. It used to
|
|
300
|
+
// fall through to the success line below, which printed
|
|
301
|
+
// "aegiscode: phase undefined queued" (res.phase is absent when there is
|
|
302
|
+
// no plan) and dropped res.error on the floor — the timer's log recorded
|
|
303
|
+
// a queueing that never happened and nothing about the missing PLAN.md
|
|
304
|
+
// that caused it. Exit code and message now agree.
|
|
305
|
+
if (!res.ok) {
|
|
306
|
+
stderr.write(`aegiscode autonomous reconcile: ${res.error || 'reconcile failed'}\n`);
|
|
307
|
+
emit(res, undefined);
|
|
308
|
+
return 1;
|
|
309
|
+
}
|
|
310
|
+
if (!json) printRunReport(res.ran || [], out);
|
|
311
|
+
emit(res, res.exhausted
|
|
312
|
+
? 'aegiscode: every phase in PLAN.md is done — nothing to queue'
|
|
313
|
+
: `aegiscode: phase ${res.phase} queued${res.ran ? ` — ${res.ran.length} task(s) run` : ''}`);
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (command === 'proceed' || command === 'run') {
|
|
318
|
+
const max = command === 'run' ? 1 : intFlag(args.flags, 'max') || 0;
|
|
319
|
+
const res = await worker.proceed({
|
|
320
|
+
commit,
|
|
321
|
+
max,
|
|
322
|
+
stopOnError: Boolean(args.flags['stop-on-error']),
|
|
323
|
+
});
|
|
324
|
+
if (res.locked) {
|
|
325
|
+
stderr.write(
|
|
326
|
+
`aegiscode autonomous: another drain holds the queue lock (pid ${res.holder && res.holder.pid})\n`
|
|
327
|
+
);
|
|
328
|
+
emit({ ok: false, locked: true, holder: res.holder }, undefined);
|
|
329
|
+
return 1;
|
|
330
|
+
}
|
|
331
|
+
if (!json) printRunReport(res.ran, out);
|
|
332
|
+
const failed = res.ran.filter((r) => !r.ok).length;
|
|
333
|
+
emit(
|
|
334
|
+
{ ok: failed === 0, ran: res.ran.map((r) => ({ id: r.id, ok: r.ok, error: r.error || null, ms: r.ms })), failed },
|
|
335
|
+
res.ran.length
|
|
336
|
+
? `aegiscode: ${res.ran.length} task(s) run${failed ? `, ${failed} failed` : ''}`
|
|
337
|
+
: 'aegiscode: nothing pending'
|
|
338
|
+
);
|
|
339
|
+
return failed ? 1 : 0;
|
|
340
|
+
}
|
|
341
|
+
} finally {
|
|
342
|
+
process.removeListener('SIGINT', onSigint);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
stdout.write(usageText());
|
|
346
|
+
return 2;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The one place this surface is documented, printed by `help` and on misuse. */
|
|
350
|
+
function usageText() {
|
|
351
|
+
return [
|
|
352
|
+
'aegiscode autonomous <command> — the unattended work queue',
|
|
353
|
+
'',
|
|
354
|
+
'Commands:',
|
|
355
|
+
' add "<task>" queue a task (--cwd, --model, --effort, --commit,',
|
|
356
|
+
' --single-pass, --workers N, --max-rounds N)',
|
|
357
|
+
' list show queued tasks and the tail of the runs log',
|
|
358
|
+
' run drain ONE task, then stop',
|
|
359
|
+
' proceed drain every pending task (--max N, --stop-on-error, --commit)',
|
|
360
|
+
' reconcile queue the next unfinished PLAN.md phase (--auto drains it)',
|
|
361
|
+
' retry <id> put a finished task back in the queue',
|
|
362
|
+
' clear drop finished tasks (--all empties the queue)',
|
|
363
|
+
'',
|
|
364
|
+
'Options:',
|
|
365
|
+
' --json emit JSON instead of text (also on add/list/clear/retry)',
|
|
366
|
+
'',
|
|
367
|
+
'A drain runs with tool approval DISABLED (nobody is there to click a card),',
|
|
368
|
+
'one task at a time, and commits only the paths the task itself changed.',
|
|
369
|
+
].join('\n') + '\n';
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
module.exports = { runQueueCommand, parseFlags, taskLine, usageText, SUBCOMMANDS };
|
package/src/deps.js
CHANGED
|
@@ -68,6 +68,13 @@ const promptPath = resolveShared(path.join('desktop', 'lib', 'local', 'prompt.js
|
|
|
68
68
|
// The npm update checker, shared with the desktop host: two package names, one
|
|
69
69
|
// implementation, so a fix to the check reaches both.
|
|
70
70
|
const updatePath = resolveShared(path.join('client', 'update.js'));
|
|
71
|
+
// The autonomous work queue: the durable task list (`queue.js`) and the
|
|
72
|
+
// unattended worker that drives the same engine (`autonomous.js`). Shared with
|
|
73
|
+
// the desktop host rather than reimplemented, for the reason engine.js is —
|
|
74
|
+
// `aegiscode autonomous proceed` on a systemd timer and the GUI's Drain button
|
|
75
|
+
// must work the SAME queue with the SAME rules (scoped commits included).
|
|
76
|
+
const queuePath = resolveShared(path.join('desktop', 'lib', 'local', 'queue.js'));
|
|
77
|
+
const autonomousPath = resolveShared(path.join('desktop', 'lib', 'local', 'autonomous.js'));
|
|
71
78
|
|
|
72
79
|
const { createClient } = require(clientPath);
|
|
73
80
|
const { createTools } = require(toolsPath);
|
|
@@ -76,6 +83,8 @@ const { createLocalEngine } = require(enginePath);
|
|
|
76
83
|
const { agentRoles, agentRoleLabel } = require(agentsPath);
|
|
77
84
|
const { buildSystemPrompt } = require(promptPath);
|
|
78
85
|
const updater = require(updatePath);
|
|
86
|
+
const queue = require(queuePath);
|
|
87
|
+
const autonomous = require(autonomousPath);
|
|
79
88
|
|
|
80
89
|
module.exports = {
|
|
81
90
|
resolveShared,
|
|
@@ -87,6 +96,8 @@ module.exports = {
|
|
|
87
96
|
agentRoleLabel,
|
|
88
97
|
buildSystemPrompt,
|
|
89
98
|
updater,
|
|
90
|
-
|
|
99
|
+
queue,
|
|
100
|
+
autonomous,
|
|
101
|
+
paths: { client: clientPath, tools: toolsPath, usage: usagePath, engine: enginePath, agents: agentsPath, prompt: promptPath, update: updatePath, queue: queuePath, autonomous: autonomousPath },
|
|
91
102
|
roots,
|
|
92
103
|
};
|
package/vendor/client/update.js
CHANGED
|
@@ -47,8 +47,46 @@ function isNewer(latest, current) {
|
|
|
47
47
|
return false;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Why a check produced no version. Every one of these used to collapse into a
|
|
52
|
+
* bare `null`, which is enough for the CLI ("no notice") but NOT enough for a
|
|
53
|
+
* host that shows the user a sentence about it: the desktop rendered all six
|
|
54
|
+
* as "registry unreachable", so a 2.5s timeout on a slow VPN was reported as
|
|
55
|
+
* fact about the registry — a false accusation, and unactionable.
|
|
56
|
+
*/
|
|
57
|
+
const REASONS = Object.freeze({
|
|
58
|
+
TIMEOUT: 'timeout',
|
|
59
|
+
NETWORK: 'network',
|
|
60
|
+
NOT_FOUND: 'not-found',
|
|
61
|
+
FORBIDDEN: 'forbidden',
|
|
62
|
+
HTTP: 'http',
|
|
63
|
+
MALFORMED: 'malformed',
|
|
64
|
+
TOO_LARGE: 'too-large',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Does this failure mean "the network let us down" (worth retrying silently, and
|
|
69
|
+
* NOT worth telling the user about) rather than "the registry answered, and the
|
|
70
|
+
* answer was no" (a real condition the user may need to act on)?
|
|
71
|
+
*
|
|
72
|
+
* `status` is the HTTP status when there was one, so 5xx/429 count as transient
|
|
73
|
+
* while 404/403 do not.
|
|
74
|
+
*/
|
|
75
|
+
function isTransientReason(reason, status = null) {
|
|
76
|
+
if (reason === REASONS.TIMEOUT || reason === REASONS.NETWORK) return true;
|
|
77
|
+
if (reason === REASONS.HTTP) return !status || status >= 500 || status === 429;
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Fetch the registry's `latest` dist-tag, saying WHY when it fails.
|
|
83
|
+
*
|
|
84
|
+
* Resolves `{ ok, latest, reason, status }` and never rejects: `ok` is the
|
|
85
|
+
* version question, `reason` (one of REASONS) is the diagnosis. Use this when
|
|
86
|
+
* you intend to tell someone what happened; use `fetchLatest` when you only
|
|
87
|
+
* care whether there is a notice.
|
|
88
|
+
*/
|
|
89
|
+
function fetchLatestDetailed({ pkg = PKG, timeoutMs = TIMEOUT_MS } = {}) {
|
|
52
90
|
return new Promise((resolve) => {
|
|
53
91
|
let settled = false;
|
|
54
92
|
const done = (v) => {
|
|
@@ -57,6 +95,9 @@ function fetchLatest({ pkg = PKG, timeoutMs = TIMEOUT_MS } = {}) {
|
|
|
57
95
|
resolve(v);
|
|
58
96
|
}
|
|
59
97
|
};
|
|
98
|
+
const fail = (reason, status = null) =>
|
|
99
|
+
done({ ok: false, latest: null, reason, status });
|
|
100
|
+
const succeed = (latest) => done({ ok: true, latest, reason: null, status: 200 });
|
|
60
101
|
try {
|
|
61
102
|
// The abbreviated metadata document: a few KB instead of the full
|
|
62
103
|
// packument, which for a package with this many releases is megabytes.
|
|
@@ -64,9 +105,12 @@ function fetchLatest({ pkg = PKG, timeoutMs = TIMEOUT_MS } = {}) {
|
|
|
64
105
|
`${REGISTRY}/${pkg}`,
|
|
65
106
|
{ headers: { accept: 'application/vnd.npm.install-v1+json' }, timeout: timeoutMs },
|
|
66
107
|
(res) => {
|
|
67
|
-
|
|
108
|
+
const status = res.statusCode;
|
|
109
|
+
if (status !== 200) {
|
|
68
110
|
res.resume();
|
|
69
|
-
return
|
|
111
|
+
if (status === 404) return fail(REASONS.NOT_FOUND, status);
|
|
112
|
+
if (status === 401 || status === 403) return fail(REASONS.FORBIDDEN, status);
|
|
113
|
+
return fail(REASONS.HTTP, status);
|
|
70
114
|
}
|
|
71
115
|
let body = '';
|
|
72
116
|
res.setEncoding('utf8');
|
|
@@ -74,30 +118,41 @@ function fetchLatest({ pkg = PKG, timeoutMs = TIMEOUT_MS } = {}) {
|
|
|
74
118
|
body += c;
|
|
75
119
|
if (body.length > 2_000_000) {
|
|
76
120
|
req.destroy();
|
|
77
|
-
|
|
121
|
+
fail(REASONS.TOO_LARGE, status);
|
|
78
122
|
}
|
|
79
123
|
});
|
|
80
124
|
res.on('end', () => {
|
|
81
125
|
try {
|
|
82
126
|
const tags = JSON.parse(body)['dist-tags'];
|
|
83
|
-
|
|
127
|
+
const latest = (tags && tags.latest) || null;
|
|
128
|
+
// A 200 that parses but names no `latest` is a malformed
|
|
129
|
+
// document, not a slow network — do not invite a retry for it.
|
|
130
|
+
if (!latest) return fail(REASONS.MALFORMED, status);
|
|
131
|
+
succeed(latest);
|
|
84
132
|
} catch {
|
|
85
|
-
|
|
133
|
+
fail(REASONS.MALFORMED, status);
|
|
86
134
|
}
|
|
87
135
|
});
|
|
88
136
|
}
|
|
89
137
|
);
|
|
90
138
|
req.on('timeout', () => {
|
|
91
139
|
req.destroy();
|
|
92
|
-
|
|
140
|
+
fail(REASONS.TIMEOUT);
|
|
93
141
|
});
|
|
94
|
-
|
|
142
|
+
// Every socket-level failure — DNS (ENOTFOUND), refused (ECONNREFUSED),
|
|
143
|
+
// TLS, proxy, reset mid-body — is the same story to a user: the network.
|
|
144
|
+
req.on('error', () => fail(REASONS.NETWORK));
|
|
95
145
|
} catch {
|
|
96
|
-
|
|
146
|
+
fail(REASONS.NETWORK);
|
|
97
147
|
}
|
|
98
148
|
});
|
|
99
149
|
}
|
|
100
150
|
|
|
151
|
+
/** Fetch the registry's `latest` dist-tag. Resolves null on any failure. */
|
|
152
|
+
function fetchLatest(opts = {}) {
|
|
153
|
+
return fetchLatestDetailed(opts).then((r) => (r.ok ? r.latest : null));
|
|
154
|
+
}
|
|
155
|
+
|
|
101
156
|
/**
|
|
102
157
|
* The notice to show, from cache. Never waits on the network.
|
|
103
158
|
*
|
|
@@ -137,4 +192,14 @@ function updateLine({ current, latest, behind, pkg = PKG }) {
|
|
|
137
192
|
return `Update available: ${current} → ${latest} run: npm i -g ${pkg}@latest`;
|
|
138
193
|
}
|
|
139
194
|
|
|
140
|
-
module.exports = {
|
|
195
|
+
module.exports = {
|
|
196
|
+
isNewer,
|
|
197
|
+
fetchLatest,
|
|
198
|
+
fetchLatestDetailed,
|
|
199
|
+
isTransientReason,
|
|
200
|
+
updateNotice,
|
|
201
|
+
updateLine,
|
|
202
|
+
PKG,
|
|
203
|
+
CHECK_INTERVAL_MS,
|
|
204
|
+
REASONS,
|
|
205
|
+
};
|