aegisone 1.0.65 → 1.0.66
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/README.md +1 -1
- package/bin/aegis.js +1 -1
- package/package.json +1 -1
- package/vendor/cli/bin/aegiscode.js +47 -5
- package/vendor/cli/package.json +1 -1
- package/vendor/cli/src/app.js +1 -1
- package/vendor/cli/src/autonomous.js +574 -10
- package/vendor/cli/src/commands.js +1 -1
- package/vendor/cli/src/dream-cmd.js +3 -0
- package/vendor/cli/src/limits.js +1 -1
- package/vendor/cli/src/provenance.lock.json +3 -3
- package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/integrity.js +1 -1
- package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -1
- package/vendor/cli/vendor/desktop/main.js +82 -1
- package/vendor/cli/vendor/mcp/tools.js +1 -1
- package/vendor/desktop/lib/local/autonomous.js +1 -1
- package/vendor/desktop/lib/local/integrity.js +1 -1
- package/vendor/desktop/lib/local/queue.js +1 -1
- package/vendor/desktop/main.js +82 -1
- package/vendor/desktop/package.json +1 -1
- package/vendor/desktop/preload.js +1 -1
- package/vendor/desktop/renderer/app.js +3 -3
- package/vendor/desktop/renderer/pty-panel.js +1 -1
- package/vendor/desktop/renderer/pty-window.html +72 -0
- package/vendor/desktop/renderer/pty-window.js +3 -0
- package/vendor/desktop/renderer/stream-policy.js +1 -1
- package/vendor/desktop/renderer/style.css +55 -3
- package/vendor/desktop/vendor/faces/cli/bin/aegiscode.js +2 -2
- package/vendor/desktop/vendor/faces/cli/package.json +1 -1
- package/vendor/desktop/vendor/faces/cli/src/app.js +1 -1
- package/vendor/desktop/vendor/faces/cli/src/autonomous.js +1 -1
- package/vendor/desktop/vendor/faces/cli/src/commands.js +1 -1
- package/vendor/desktop/vendor/faces/cli/src/dream-cmd.js +3 -0
- package/vendor/desktop/vendor/faces/cli/src/limits.js +1 -1
- package/vendor/desktop/vendor/faces/cli/src/provenance.lock.json +3 -3
- package/vendor/desktop/vendor/faces/cli/vendor/desktop/lib/local/autonomous.js +1 -1
- package/vendor/desktop/vendor/faces/cli/vendor/desktop/lib/local/integrity.js +1 -1
- package/vendor/desktop/vendor/faces/cli/vendor/desktop/lib/local/queue.js +1 -1
- package/vendor/desktop/vendor/faces/cli/vendor/desktop/main.js +3 -3
- package/vendor/desktop/vendor/faces/cli/vendor/mcp/tools.js +1 -1
|
@@ -30,6 +30,9 @@
|
|
|
30
30
|
* 2 usage error, or no credential to run an unattended turn with
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
+
const { spawn, spawnSync } = require('node:child_process');
|
|
34
|
+
const os = require('node:os');
|
|
35
|
+
const fs = require('node:fs');
|
|
33
36
|
const path = require('node:path');
|
|
34
37
|
|
|
35
38
|
const { queue, autonomous } = require('./deps.js');
|
|
@@ -39,7 +42,7 @@ const { fmtElapsed } = require('./format.js');
|
|
|
39
42
|
const limits = require('./limits.js');
|
|
40
43
|
|
|
41
44
|
const SUBCOMMANDS = new Set([
|
|
42
|
-
'add', 'list', 'run', 'proceed', 'reconcile', 'clear', 'retry', 'help',
|
|
45
|
+
'add', 'list', 'run', 'proceed', 'start', 'tick', 'timer', 'report', 'reconcile', 'clear', 'retry', 'help',
|
|
43
46
|
]);
|
|
44
47
|
|
|
45
48
|
/**
|
|
@@ -48,7 +51,24 @@ const SUBCOMMANDS = new Set([
|
|
|
48
51
|
* re-arms a failed task, `reconcile` can chain straight into a drain, and
|
|
49
52
|
* `run`/`proceed` are the drain. See the guard in `runQueueCommand`.
|
|
50
53
|
*/
|
|
51
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The verbs that RUN something. `add`, `list`, `report` and `clear` decide
|
|
56
|
+
* nothing and stay available on a build that failed the interlock; these act —
|
|
57
|
+
* `retry` re-arms a failed task, `reconcile` can chain straight into a drain,
|
|
58
|
+
* and `run`/`proceed`/`start` are the drain. See the guard in
|
|
59
|
+
* `runQueueCommand`.
|
|
60
|
+
*/
|
|
61
|
+
const EXECUTES = new Set(['run', 'proceed', 'start', 'tick', 'timer', 'retry', 'reconcile']);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The load gate, applied inside a `tick` and nowhere else.
|
|
65
|
+
*
|
|
66
|
+
* A flat number rather than a multiple of the core count: the machine that runs
|
|
67
|
+
* a timer is usually also the machine somebody is working on, and "4.0" is a
|
|
68
|
+
* sentence a user can reason about ("more than four runnable things and the
|
|
69
|
+
* drain waits") where "1.5 × 8 cores" is not. `--max-load 0` disables the gate.
|
|
70
|
+
*/
|
|
71
|
+
const DEFAULT_MAX_LOAD = 4.0;
|
|
52
72
|
|
|
53
73
|
/**
|
|
54
74
|
* Parse the subcommand's own argv. Deliberately its own tiny parser rather than
|
|
@@ -58,7 +78,7 @@ const EXECUTES = new Set(['run', 'proceed', 'retry', 'reconcile']);
|
|
|
58
78
|
*/
|
|
59
79
|
function parseFlags(argv) {
|
|
60
80
|
const opts = { terms: [], json: false, flags: {} };
|
|
61
|
-
const takesValue = new Set(['cwd', 'model', 'effort', 'workers', 'max', 'max-rounds', 'source', 'class']);
|
|
81
|
+
const takesValue = new Set(['cwd', 'model', 'effort', 'workers', 'max', 'max-rounds', 'source', 'class', 'since', 'max-load', 'every']);
|
|
62
82
|
for (let i = 0; i < argv.length; i++) {
|
|
63
83
|
const a = argv[i];
|
|
64
84
|
if (a === '--json') {
|
|
@@ -68,7 +88,7 @@ function parseFlags(argv) {
|
|
|
68
88
|
if (a.startsWith('--')) {
|
|
69
89
|
const [rawName, inline] = a.slice(2).split('=');
|
|
70
90
|
const name = rawName.trim();
|
|
71
|
-
const bools = new Set(['commit', 'all', 'auto', 'stop-on-error', 'single-pass', 'help']);
|
|
91
|
+
const bools = new Set(['commit', 'all', 'auto', 'stop-on-error', 'single-pass', 'help', 'wait']);
|
|
72
92
|
if (bools.has(name)) {
|
|
73
93
|
opts.flags[name] = true;
|
|
74
94
|
continue;
|
|
@@ -319,6 +339,56 @@ async function runQueueCommand(command, argv, io = {}) {
|
|
|
319
339
|
return 0;
|
|
320
340
|
}
|
|
321
341
|
|
|
342
|
+
if (command === 'report') {
|
|
343
|
+
// THE MISSING HALF OF "SET A TIMER AND REPORT BACK".
|
|
344
|
+
//
|
|
345
|
+
// A drain writes its outcome to runs.jsonl and stdout. Nothing read it back
|
|
346
|
+
// into a conversation, so an AI that queued work and stopped had no way to
|
|
347
|
+
// learn how it went — the timer fired, the task finished, and the next turn
|
|
348
|
+
// still knew nothing. This is that surface: a plain command any host can
|
|
349
|
+
// run with an ordinary shell tool, reading the plugin's own files, so it
|
|
350
|
+
// works identically whether the model is Claude, Codex, Gemini, Cursor or
|
|
351
|
+
// anything else. `--since <id>` narrows to what happened after a task.
|
|
352
|
+
const since = args.flags.since !== undefined ? Number.parseInt(String(args.flags.since), 10) : null;
|
|
353
|
+
const limit = intFlag(args.flags, 'max') || 20;
|
|
354
|
+
const runs = queue.readRuns(process.env, { limit });
|
|
355
|
+
const items = queue.loadQueue(process.env);
|
|
356
|
+
const counts = describeCounts(items);
|
|
357
|
+
// `--since` filters by task id rather than by timestamp: the caller knows
|
|
358
|
+
// the id it queued (it printed it), and ids are monotonic, so "since #12"
|
|
359
|
+
// is the question actually being asked — "did what I queued finish?".
|
|
360
|
+
const selected = Number.isFinite(since) && since !== null ? runs.filter((r) => Number(r.id) > since) : runs;
|
|
361
|
+
if (json) {
|
|
362
|
+
emit({ ok: true, file: queue.runsPath(process.env), counts, runs: selected });
|
|
363
|
+
return 0;
|
|
364
|
+
}
|
|
365
|
+
stdout.write(`runs: ${queue.runsPath(process.env)}\n`);
|
|
366
|
+
if (!selected.length) {
|
|
367
|
+
stdout.write(
|
|
368
|
+
Number.isFinite(since) && since !== null
|
|
369
|
+
? `(nothing finished after #${since})\n`
|
|
370
|
+
: '(no finished runs yet)\n'
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
for (const r of selected) {
|
|
374
|
+
// `stopped` is a task that hit its tool-round horizon and is still
|
|
375
|
+
// pending — not a failure, and saying "failed" here would send a caller
|
|
376
|
+
// chasing work that is queued and will resume.
|
|
377
|
+
const mark = r.status === 'done' ? '✓' : r.status === 'stopped' ? '⏸' : '✗';
|
|
378
|
+
stdout.write(`${mark} #${r.id} ${String(r.status || '?').padEnd(7)} ${r.model || '(default)'} ${fmtElapsed(r.ms)}\n`);
|
|
379
|
+
stdout.write(` ${String(r.task || '').replace(/\s+/g, ' ').trim().slice(0, 160)}\n`);
|
|
380
|
+
if (r.error) stdout.write(` error: ${String(r.error).split('\n')[0]}\n`);
|
|
381
|
+
if (r.files && r.files.length) stdout.write(` files: ${r.files.slice(0, 8).join(', ')}\n`);
|
|
382
|
+
}
|
|
383
|
+
// The queue counts, so "is anything still in flight" is answered in the
|
|
384
|
+
// same breath as "what finished" — a report that says only what is done
|
|
385
|
+
// leaves the reader unable to tell "finished" from "still running".
|
|
386
|
+
stdout.write(
|
|
387
|
+
`queue: ${counts.pending} pending, ${counts.running} running, ${counts.done} done, ${counts.error} error\n`
|
|
388
|
+
);
|
|
389
|
+
return 0;
|
|
390
|
+
}
|
|
391
|
+
|
|
322
392
|
if (command === 'clear') {
|
|
323
393
|
const res = queue.clearQueue(process.env, { all: Boolean(args.flags.all) });
|
|
324
394
|
emit({ ok: true, ...res }, `aegiscode: removed ${res.removed} finished task(s); ${res.kept} kept`);
|
|
@@ -340,6 +410,198 @@ async function runQueueCommand(command, argv, io = {}) {
|
|
|
340
410
|
return 0;
|
|
341
411
|
}
|
|
342
412
|
|
|
413
|
+
// ── start: the drain, detached ───────────────────────────────────────────
|
|
414
|
+
//
|
|
415
|
+
// Held deliberately BEFORE the engine is built: this process does not run a
|
|
416
|
+
// turn, it spawns one and exits. The pattern it replaces was the reason a
|
|
417
|
+
// long drain never survived — a caller (an AI shelling out, a timer with a
|
|
418
|
+
// timeout) ran `proceed` inline and the harness killed it at its own wall
|
|
419
|
+
// clock, taking the drain and the lock with it. A detached child leads its
|
|
420
|
+
// own process group, so the kill does not reach it, and this command returns
|
|
421
|
+
// a pid in milliseconds instead of blocking for the length of the work.
|
|
422
|
+
if (command === 'start') {
|
|
423
|
+
const running = queue.loadQueue(process.env).filter((i) => i.status === 'running' && i.pid);
|
|
424
|
+
if (running.length) {
|
|
425
|
+
stderr.write(
|
|
426
|
+
`aegiscode autonomous start: #${running[0].id} is already running (pid ${running[0].pid})\n`
|
|
427
|
+
);
|
|
428
|
+
return 1;
|
|
429
|
+
}
|
|
430
|
+
const childArgs = ['autonomous', 'proceed'];
|
|
431
|
+
// Read the flag directly, not the `commit` local: that one is declared below
|
|
432
|
+
// this branch (it belongs to the model-running path), and reading it here
|
|
433
|
+
// would be a temporal-dead-zone error at runtime.
|
|
434
|
+
if (args.flags.commit) childArgs.push('--commit');
|
|
435
|
+
if (intFlag(args.flags, 'max')) childArgs.push('--max', String(intFlag(args.flags, 'max')));
|
|
436
|
+
if (args.flags['stop-on-error']) childArgs.push('--stop-on-error');
|
|
437
|
+
|
|
438
|
+
// Re-exec THIS cli through the same interpreter, not through $PATH: a
|
|
439
|
+
// globally installed `aegiscode` may be a different build or missing
|
|
440
|
+
// entirely from a detached process's environment, and a detached drain that
|
|
441
|
+
// silently ran nothing is the failure mode this whole change is about.
|
|
442
|
+
const self = process.argv[1];
|
|
443
|
+
if (!self) {
|
|
444
|
+
stderr.write('aegiscode autonomous start: cannot locate the CLI entry point to detach\n');
|
|
445
|
+
return 2;
|
|
446
|
+
}
|
|
447
|
+
let child;
|
|
448
|
+
try {
|
|
449
|
+
child = spawn(process.execPath, [self, ...childArgs], {
|
|
450
|
+
cwd,
|
|
451
|
+
env: process.env,
|
|
452
|
+
detached: true,
|
|
453
|
+
// The log is where `report` cannot help: a child that dies before it
|
|
454
|
+
// settles anything leaves nothing in runs.jsonl, and without a file its
|
|
455
|
+
// last words go to a closed pipe.
|
|
456
|
+
stdio: 'ignore',
|
|
457
|
+
});
|
|
458
|
+
} catch (e) {
|
|
459
|
+
stderr.write(`aegiscode autonomous start: could not spawn the drain — ${e.message}\n`);
|
|
460
|
+
return 1;
|
|
461
|
+
}
|
|
462
|
+
child.unref();
|
|
463
|
+
emit(
|
|
464
|
+
{ ok: true, pid: child.pid, log: queue.dataDir(process.env) },
|
|
465
|
+
`aegiscode: drain started in the background (pid ${child.pid})\n` +
|
|
466
|
+
' it writes ~/.aegiscode/runs.jsonl as tasks settle — read it back with `aegiscode autonomous report`'
|
|
467
|
+
);
|
|
468
|
+
return 0;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// ── timer: the thing that makes "set a timer" real ────────────────────
|
|
472
|
+
//
|
|
473
|
+
// Before this, `/schedule` was a stub that printed a URL and no timer was
|
|
474
|
+
// ever armed: a queued task was drained only if a human typed `proceed`, so
|
|
475
|
+
// "set a timer and report back" could not work on any model or any host. The
|
|
476
|
+
// unit below is what wakes up. Its body is `tick`, never `proceed`, because
|
|
477
|
+
// the load gate has to be INSIDE the thing the timer calls.
|
|
478
|
+
if (command === 'timer') {
|
|
479
|
+
const action = (args.terms[0] || 'status').toLowerCase();
|
|
480
|
+
const every = intFlag(args.flags, 'every') || 5;
|
|
481
|
+
const maxLoad = args.flags['max-load'] !== undefined ? Number(args.flags['max-load']) : DEFAULT_MAX_LOAD;
|
|
482
|
+
if (!Number.isFinite(maxLoad) || maxLoad < 0) {
|
|
483
|
+
stderr.write('aegiscode autonomous timer: --max-load needs a number >= 0 (0 disables the gate)\n');
|
|
484
|
+
return 2;
|
|
485
|
+
}
|
|
486
|
+
// Re-exec THIS cli, not whatever `aegiscode` resolves to inside a systemd
|
|
487
|
+
// or cron environment — a PATH there is not the user's PATH, and a timer
|
|
488
|
+
// that silently runs a different build (or nothing) is the exact failure
|
|
489
|
+
// this whole change exists to end.
|
|
490
|
+
const self = process.argv[1] ? path.resolve(process.argv[1]) : null;
|
|
491
|
+
if (!self) {
|
|
492
|
+
stderr.write('aegiscode autonomous timer: cannot locate the CLI entry point to schedule\n');
|
|
493
|
+
return 2;
|
|
494
|
+
}
|
|
495
|
+
// `tick` always commits: the timer fires unattended, and a drain that left
|
|
496
|
+
// every change as an uncommitted diff would just accumulate work nobody
|
|
497
|
+
// reviews. `--commit` is in the unit body unconditionally, once.
|
|
498
|
+
const tickArgs = ['autonomous', 'tick', '--commit', '--max-load', String(maxLoad)];
|
|
499
|
+
const plan = timerPlan({ every, maxLoad, self, tickArgs, cwd });
|
|
500
|
+
|
|
501
|
+
if (action === 'status') {
|
|
502
|
+
const written = plan.kind === 'unsupported' ? false : plan.paths.every((p) => fs.existsSync(p));
|
|
503
|
+
const state = plan.state();
|
|
504
|
+
// WRITTEN IS NOT ARMED. The units can sit on disk while the scheduler
|
|
505
|
+
// refuses to load them (no user systemd, a container, WSL), and calling
|
|
506
|
+
// that "installed" is exactly the silent-timer failure this change is
|
|
507
|
+
// about. `installed` therefore means the scheduler accepted it, and
|
|
508
|
+
// `written` says whether the files are there to be finished by hand.
|
|
509
|
+
const active = state === 'active' || state === 'loaded' || state === 'registered';
|
|
510
|
+
const installed = written && (active || plan.kind === 'schtasks');
|
|
511
|
+
// Prefer the installed unit's own values. The flags describe a timer that
|
|
512
|
+
// does not exist yet; only the files (or the scheduler's record) describe
|
|
513
|
+
// the one that is going to fire. `--every 9 status` on a 5-minute timer
|
|
514
|
+
// must not answer "9".
|
|
515
|
+
const recorded = written && typeof plan.read === 'function' ? plan.read() : null;
|
|
516
|
+
const fromUnit = Boolean(recorded && (recorded.every != null || recorded.maxLoad != null));
|
|
517
|
+
const shownEvery = recorded && recorded.every != null ? recorded.every : every;
|
|
518
|
+
const shownMaxLoad = recorded && recorded.maxLoad != null ? recorded.maxLoad : maxLoad;
|
|
519
|
+
if (json) {
|
|
520
|
+
emit({
|
|
521
|
+
ok: true, installed, written, kind: plan.kind, paths: plan.paths,
|
|
522
|
+
every: shownEvery, maxLoad: shownMaxLoad, fromUnit, state,
|
|
523
|
+
});
|
|
524
|
+
return 0;
|
|
525
|
+
}
|
|
526
|
+
stdout.write(`timer: ${plan.kind} — ${installed ? 'installed and armed' : written ? 'WRITTEN BUT NOT ARMED' : 'not installed'}\n`);
|
|
527
|
+
for (const p of plan.paths) stdout.write(` ${fs.existsSync(p) ? '✓' : '·'} ${p}\n`);
|
|
528
|
+
stdout.write(
|
|
529
|
+
` every ${shownEvery} min, load gate ${shownMaxLoad > 0 ? shownMaxLoad : 'off'}` +
|
|
530
|
+
`${fromUnit ? ' (from the installed unit)' : ''}\n`
|
|
531
|
+
);
|
|
532
|
+
if (!fromUnit && !written) {
|
|
533
|
+
stdout.write(` (requested values — nothing is installed, so nothing fires)\n`);
|
|
534
|
+
}
|
|
535
|
+
if (state) stdout.write(` state: ${state}\n`);
|
|
536
|
+
if (!installed) {
|
|
537
|
+
stdout.write(
|
|
538
|
+
written
|
|
539
|
+
? '\n the units exist but the scheduler has not loaded them. finish it:\n' +
|
|
540
|
+
' systemctl --user daemon-reload && systemctl --user enable --now aegiscode-queue.timer\n'
|
|
541
|
+
: '\n install it: aegiscode autonomous timer install\n'
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
return 0;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (action === 'install') {
|
|
548
|
+
if (plan.kind === 'unsupported') {
|
|
549
|
+
stderr.write(`aegiscode autonomous timer: this platform (${process.platform}) has no supported scheduler\n`);
|
|
550
|
+
return 2;
|
|
551
|
+
}
|
|
552
|
+
try {
|
|
553
|
+
plan.install();
|
|
554
|
+
} catch (e) {
|
|
555
|
+
stderr.write(`aegiscode autonomous timer: install failed — ${e.message}\n`);
|
|
556
|
+
return 1;
|
|
557
|
+
}
|
|
558
|
+
emit(
|
|
559
|
+
{ ok: true, kind: plan.kind, paths: plan.paths, every, maxLoad },
|
|
560
|
+
`aegiscode: timer installed — every ${every} min, load gate ${maxLoad > 0 ? maxLoad : 'off'}\n` +
|
|
561
|
+
` it runs: aegiscode autonomous tick --max-load ${maxLoad}\n` +
|
|
562
|
+
' collect what it did with `aegiscode autonomous report`'
|
|
563
|
+
);
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (action === 'remove') {
|
|
568
|
+
try {
|
|
569
|
+
plan.remove();
|
|
570
|
+
} catch (e) {
|
|
571
|
+
stderr.write(`aegiscode autonomous timer: remove failed — ${e.message}\n`);
|
|
572
|
+
return 1;
|
|
573
|
+
}
|
|
574
|
+
emit({ ok: true, kind: plan.kind, paths: plan.paths }, 'aegiscode: timer removed');
|
|
575
|
+
return 0;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
stderr.write(`aegiscode autonomous timer: unknown action \`${action}\` — install | status | remove\n`);
|
|
579
|
+
return 2;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ── tick: one gated drain, the body of the timer ────────────────────────
|
|
583
|
+
//
|
|
584
|
+
// The gate lives HERE, inside the tick, not in a wrapper: whatever arms the
|
|
585
|
+
// tick cannot be trusted to know the machine's state at the moment it fires.
|
|
586
|
+
// Over the threshold means "somebody is using this computer" — skip, exit 0,
|
|
587
|
+
// and let the next tick try. Exit 0 rather than 1 so a skip is not recorded
|
|
588
|
+
// as a failure in the timer's log; the printed line is what makes it visible.
|
|
589
|
+
if (command === 'tick') {
|
|
590
|
+
const maxLoad = args.flags['max-load'] !== undefined ? Number(args.flags['max-load']) : DEFAULT_MAX_LOAD;
|
|
591
|
+
if (!Number.isFinite(maxLoad) || maxLoad < 0) {
|
|
592
|
+
stderr.write('aegiscode autonomous tick: --max-load needs a number >= 0 (0 disables the gate)\n');
|
|
593
|
+
return 2;
|
|
594
|
+
}
|
|
595
|
+
const load = os.loadavg()[0];
|
|
596
|
+
if (maxLoad > 0 && load > maxLoad) {
|
|
597
|
+
emit(
|
|
598
|
+
{ ok: true, skipped: 'load', load: Number(load.toFixed(2)), maxLoad },
|
|
599
|
+
`aegiscode: tick skipped — load ${load.toFixed(2)} > ${maxLoad}\n`
|
|
600
|
+
);
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
343
605
|
// ── the three commands that need a model ─────────────────────────────────
|
|
344
606
|
const built = buildEngine({ io });
|
|
345
607
|
if (built.error) {
|
|
@@ -420,15 +682,39 @@ async function runQueueCommand(command, argv, io = {}) {
|
|
|
420
682
|
}
|
|
421
683
|
}
|
|
422
684
|
const commit = args.flags.commit ? true : undefined;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
685
|
+
// A DRAIN MUST RELEASE ITS LOCK ON ANY KILL THAT CAN BE CAUGHT. SIGINT was
|
|
686
|
+
// handled alone; SIGTERM and SIGHUP were not, so the ordinary ways a drain is
|
|
687
|
+
// stopped — a timer's timeout, a harness killing the tool call, a closed
|
|
688
|
+
// terminal, `systemctl stop` — skipped the `finally` in worker.proceed(),
|
|
689
|
+
// leaving queue.lock and a `running` item behind. The next tick then failed
|
|
690
|
+
// with "another drain holds the queue lock" and the queue stayed stuck. All
|
|
691
|
+
// three now unwind the same way. (SIGKILL cannot be caught; the heartbeat in
|
|
692
|
+
// queue.js is what reclaims that case.)
|
|
693
|
+
// First signal: cancel the turn and let the drain unwind, so the `finally`
|
|
694
|
+
// that releases the lock actually runs. Second signal: stop waiting and die.
|
|
695
|
+
// Without that escalation the handler would SWALLOW the signal — replacing the
|
|
696
|
+
// default action means `kill` no longer kills, and a drain wedged past its
|
|
697
|
+
// cancel would be unkillable short of SIGKILL.
|
|
698
|
+
let signals = 0;
|
|
699
|
+
const onSignal = (signal) => () => {
|
|
700
|
+
signals += 1;
|
|
701
|
+
if (signals > 1) {
|
|
702
|
+
process.removeListener('SIGINT', onSigint);
|
|
703
|
+
process.removeListener('SIGTERM', onSigterm);
|
|
704
|
+
process.removeListener('SIGHUP', onSighup);
|
|
705
|
+
process.exit(130);
|
|
706
|
+
}
|
|
427
707
|
const items = queue.loadQueue(process.env);
|
|
428
708
|
const running = items.find((i) => i.status === 'running' && i.pid === process.pid);
|
|
429
709
|
if (running) worker.cancel(running.id);
|
|
710
|
+
if (signal !== 'SIGINT') process.exitCode = 130;
|
|
430
711
|
};
|
|
712
|
+
const onSigint = onSignal('SIGINT');
|
|
713
|
+
const onSigterm = onSignal('SIGTERM');
|
|
714
|
+
const onSighup = onSignal('SIGHUP');
|
|
431
715
|
process.on('SIGINT', onSigint);
|
|
716
|
+
process.on('SIGTERM', onSigterm);
|
|
717
|
+
process.on('SIGHUP', onSighup);
|
|
432
718
|
|
|
433
719
|
try {
|
|
434
720
|
if (command === 'reconcile') {
|
|
@@ -457,7 +743,7 @@ async function runQueueCommand(command, argv, io = {}) {
|
|
|
457
743
|
return 0;
|
|
458
744
|
}
|
|
459
745
|
|
|
460
|
-
if (command === 'proceed' || command === 'run') {
|
|
746
|
+
if (command === 'proceed' || command === 'run' || command === 'tick') {
|
|
461
747
|
const max = command === 'run' ? 1 : intFlag(args.flags, 'max') || 0;
|
|
462
748
|
const res = await worker.proceed({
|
|
463
749
|
commit,
|
|
@@ -483,12 +769,277 @@ async function runQueueCommand(command, argv, io = {}) {
|
|
|
483
769
|
}
|
|
484
770
|
} finally {
|
|
485
771
|
process.removeListener('SIGINT', onSigint);
|
|
772
|
+
process.removeListener('SIGTERM', onSigterm);
|
|
773
|
+
process.removeListener('SIGHUP', onSighup);
|
|
486
774
|
}
|
|
487
775
|
|
|
488
776
|
stdout.write(usageText());
|
|
489
777
|
return 2;
|
|
490
778
|
}
|
|
491
779
|
|
|
780
|
+
/**
|
|
781
|
+
* The repeating timer that calls `tick` — the half of "set a timer, report back"
|
|
782
|
+
* that did not exist. `/schedule` was a stub pointing at a web page and nothing
|
|
783
|
+
* ever armed a wake-up, so a queued task waited for a human to type `proceed`.
|
|
784
|
+
*
|
|
785
|
+
* Three schedulers, because the plugin ships on three platforms and the house
|
|
786
|
+
* pattern (see desktop/lib/local/system.js) is to dispatch rather than pretend
|
|
787
|
+
* one tool exists everywhere. Each plan is `{kind, paths, install, remove, state}`
|
|
788
|
+
* so the caller never branches on platform itself.
|
|
789
|
+
*
|
|
790
|
+
* The body is `tick --max-load N`, never `proceed`, because the load gate has to
|
|
791
|
+
* be inside the unit the scheduler fires — the scheduler cannot know whether the
|
|
792
|
+
* machine is busy at the moment it fires.
|
|
793
|
+
*
|
|
794
|
+
* `Persistent=true` (systemd) matters more than it looks: without it a tick that
|
|
795
|
+
* came due while the machine was asleep is simply dropped, and a queue that
|
|
796
|
+
* quietly stops draining for a day is indistinguishable from one that works.
|
|
797
|
+
*/
|
|
798
|
+
function timerPlan({ every, maxLoad, self, tickArgs, cwd }) {
|
|
799
|
+
const minutes = Math.max(1, Math.floor(every));
|
|
800
|
+
const body = [process.execPath, self, ...tickArgs].map(shellQuote).join(' ');
|
|
801
|
+
const workdir = cwd || process.cwd();
|
|
802
|
+
|
|
803
|
+
if (process.platform === 'linux') {
|
|
804
|
+
const dir = path.join(os.homedir(), '.config', 'systemd', 'user');
|
|
805
|
+
const service = path.join(dir, 'aegiscode-queue.service');
|
|
806
|
+
const timer = path.join(dir, 'aegiscode-queue.timer');
|
|
807
|
+
const serviceBody = [
|
|
808
|
+
'[Unit]',
|
|
809
|
+
'Description=AEGIS Code — drain the unattended work queue (one gated tick)',
|
|
810
|
+
'',
|
|
811
|
+
'[Service]',
|
|
812
|
+
'Type=oneshot',
|
|
813
|
+
`WorkingDirectory=${workdir}`,
|
|
814
|
+
`ExecStart=${body}`,
|
|
815
|
+
'Nice=10',
|
|
816
|
+
// A tick that never returns must not block the next one forever; the
|
|
817
|
+
// worker's own lock and heartbeat reclaim the queue either way.
|
|
818
|
+
'TimeoutStartSec=6h',
|
|
819
|
+
'',
|
|
820
|
+
].join('\n');
|
|
821
|
+
const timerBody = [
|
|
822
|
+
'[Unit]',
|
|
823
|
+
'Description=AEGIS Code — the queue tick timer',
|
|
824
|
+
'',
|
|
825
|
+
'[Timer]',
|
|
826
|
+
`OnActiveSec=1min`,
|
|
827
|
+
`OnUnitActiveSec=${minutes}min`,
|
|
828
|
+
// Catch up a tick missed while the machine was off, instead of skipping it.
|
|
829
|
+
'Persistent=true',
|
|
830
|
+
'AccuracySec=30s',
|
|
831
|
+
'',
|
|
832
|
+
'[Install]',
|
|
833
|
+
'WantedBy=timers.target',
|
|
834
|
+
'',
|
|
835
|
+
].join('\n');
|
|
836
|
+
return {
|
|
837
|
+
kind: 'systemd-user',
|
|
838
|
+
paths: [service, timer],
|
|
839
|
+
install() {
|
|
840
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
841
|
+
fs.writeFileSync(service, serviceBody);
|
|
842
|
+
fs.writeFileSync(timer, timerBody);
|
|
843
|
+
// `--user` and no sudo: this is the user's own queue, and a timer that
|
|
844
|
+
// needed root would simply never be installed by the people who need it.
|
|
845
|
+
runScheduler(['systemctl', '--user', 'daemon-reload'], { ignore: true });
|
|
846
|
+
const r = runScheduler(['systemctl', '--user', 'enable', '--now', 'aegiscode-queue.timer'], { ignore: true });
|
|
847
|
+
if (r.error || r.status !== 0) {
|
|
848
|
+
// The units ARE on disk at this point, so "install failed" alone would
|
|
849
|
+
// be misleading — and leaving it there silently is the exact failure
|
|
850
|
+
// this change exists to end: a timer that looks installed and never
|
|
851
|
+
// fires. Say what was written, why the scheduler refused, and the one
|
|
852
|
+
// command that finishes the job (or the cron line, for a machine with
|
|
853
|
+
// no user systemd — WSL and most containers).
|
|
854
|
+
const why = r.error ? r.error.message : String(r.stderr || '').trim();
|
|
855
|
+
throw new Error(
|
|
856
|
+
`units were written to ${dir}, but the scheduler refused to load them: ${why}\n` +
|
|
857
|
+
' finish it yourself: systemctl --user daemon-reload && systemctl --user enable --now aegiscode-queue.timer\n' +
|
|
858
|
+
` or use cron instead: */${minutes} * * * * ${body}`
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
},
|
|
862
|
+
remove() {
|
|
863
|
+
try { runScheduler(['systemctl', '--user', 'disable', '--now', 'aegiscode-queue.timer']); } catch { /* not installed */ }
|
|
864
|
+
for (const p of [timer, service]) { try { fs.unlinkSync(p); } catch { /* absent */ } }
|
|
865
|
+
try { runScheduler(['systemctl', '--user', 'daemon-reload']); } catch { /* no systemd */ }
|
|
866
|
+
},
|
|
867
|
+
state() {
|
|
868
|
+
try {
|
|
869
|
+
const r = spawnSync('systemctl', ['--user', 'is-active', 'aegiscode-queue.timer'], { encoding: 'utf8' });
|
|
870
|
+
const s = String(r.stdout || '').trim();
|
|
871
|
+
return s || null;
|
|
872
|
+
} catch {
|
|
873
|
+
return null;
|
|
874
|
+
}
|
|
875
|
+
},
|
|
876
|
+
// What the INSTALLED unit actually says, not what this invocation asked
|
|
877
|
+
// for. `timer status --every 9` on a timer installed with `--every 5`
|
|
878
|
+
// used to print "every 9 min" — a status command that reports your flags
|
|
879
|
+
// back to you instead of the machine's state, which is the same class of
|
|
880
|
+
// lie as a timer that claims to be installed and never fires.
|
|
881
|
+
read() {
|
|
882
|
+
let installedEvery = null;
|
|
883
|
+
let installedMaxLoad = null;
|
|
884
|
+
try {
|
|
885
|
+
const t = fs.readFileSync(timer, 'utf8');
|
|
886
|
+
const m = /OnUnitActiveSec=(\d+)\s*min/.exec(t);
|
|
887
|
+
if (m) installedEvery = Number(m[1]);
|
|
888
|
+
} catch { /* not written */ }
|
|
889
|
+
try {
|
|
890
|
+
const s = fs.readFileSync(service, 'utf8');
|
|
891
|
+
const m = /--max-load\s+(\S+)/.exec(s);
|
|
892
|
+
if (m) installedMaxLoad = Number(m[1]);
|
|
893
|
+
} catch { /* not written */ }
|
|
894
|
+
return { every: installedEvery, maxLoad: installedMaxLoad };
|
|
895
|
+
},
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
if (process.platform === 'darwin') {
|
|
900
|
+
const dir = path.join(os.homedir(), 'Library', 'LaunchAgents');
|
|
901
|
+
const plist = path.join(dir, 'org.aegiscloud.aegiscode-queue.plist');
|
|
902
|
+
const label = 'org.aegiscloud.aegiscode-queue';
|
|
903
|
+
const plistBody = [
|
|
904
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
905
|
+
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
|
|
906
|
+
'<plist version="1.0">',
|
|
907
|
+
'<dict>',
|
|
908
|
+
' <key>Label</key>',
|
|
909
|
+
` <string>${label}</string>`,
|
|
910
|
+
' <key>ProgramArguments</key>',
|
|
911
|
+
' <array>',
|
|
912
|
+
` <string>${xmlEscape(process.execPath)}</string>`,
|
|
913
|
+
` <string>${xmlEscape(self)}</string>`,
|
|
914
|
+
...tickArgs.map((a) => ` <string>${xmlEscape(a)}</string>`),
|
|
915
|
+
' </array>',
|
|
916
|
+
' <key>WorkingDirectory</key>',
|
|
917
|
+
` <string>${xmlEscape(workdir)}</string>`,
|
|
918
|
+
' <key>StartInterval</key>',
|
|
919
|
+
` <integer>${minutes * 60}</integer>`,
|
|
920
|
+
// RunAtLoad replaces systemd's Persistent: launchd never runs a job that
|
|
921
|
+
// came due while the Mac was asleep, so the next login must fire once.
|
|
922
|
+
' <key>RunAtLoad</key>',
|
|
923
|
+
' <true/>',
|
|
924
|
+
' <key>StandardErrorPath</key>',
|
|
925
|
+
` <string>${xmlEscape(path.join(os.homedir(), '.aegiscode', 'timer.log'))}</string>`,
|
|
926
|
+
'</dict>',
|
|
927
|
+
'</plist>',
|
|
928
|
+
'',
|
|
929
|
+
].join('\n');
|
|
930
|
+
return {
|
|
931
|
+
kind: 'launchd',
|
|
932
|
+
paths: [plist],
|
|
933
|
+
install() {
|
|
934
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
935
|
+
fs.writeFileSync(plist, plistBody);
|
|
936
|
+
runScheduler(['launchctl', 'unload', plist], { ignore: true });
|
|
937
|
+
runScheduler(['launchctl', 'load', '-w', plist]);
|
|
938
|
+
},
|
|
939
|
+
remove() {
|
|
940
|
+
try { runScheduler(['launchctl', 'unload', '-w', plist]); } catch { /* not loaded */ }
|
|
941
|
+
try { fs.unlinkSync(plist); } catch { /* absent */ }
|
|
942
|
+
},
|
|
943
|
+
state() {
|
|
944
|
+
try {
|
|
945
|
+
const r = spawnSync('launchctl', ['list', label], { encoding: 'utf8' });
|
|
946
|
+
return r.status === 0 ? 'loaded' : 'not loaded';
|
|
947
|
+
} catch {
|
|
948
|
+
return null;
|
|
949
|
+
}
|
|
950
|
+
},
|
|
951
|
+
read() {
|
|
952
|
+
try {
|
|
953
|
+
const p = fs.readFileSync(plist, 'utf8');
|
|
954
|
+
const secs = /<key>StartInterval<\/key>\s*<integer>(\d+)<\/integer>/.exec(p);
|
|
955
|
+
const ml = /<string>--max-load<\/string>\s*<string>([^<]+)<\/string>/.exec(p);
|
|
956
|
+
return {
|
|
957
|
+
every: secs ? Math.round(Number(secs[1]) / 60) : null,
|
|
958
|
+
maxLoad: ml ? Number(ml[1]) : null,
|
|
959
|
+
};
|
|
960
|
+
} catch {
|
|
961
|
+
return { every: null, maxLoad: null };
|
|
962
|
+
}
|
|
963
|
+
},
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
if (process.platform === 'win32') {
|
|
968
|
+
const name = 'AegisCodeQueue';
|
|
969
|
+
return {
|
|
970
|
+
kind: 'schtasks',
|
|
971
|
+
paths: [],
|
|
972
|
+
install() {
|
|
973
|
+
// `/rl limited`: the drain is the user's own work, not a system task.
|
|
974
|
+
runScheduler(['schtasks', '/Create', '/tn', name, '/tr', body, '/sc', 'minute',
|
|
975
|
+
'/mo', String(minutes), '/f', '/rl', 'LIMITED']);
|
|
976
|
+
},
|
|
977
|
+
remove() {
|
|
978
|
+
try { runScheduler(['schtasks', '/Delete', '/tn', name, '/f']); } catch { /* absent */ }
|
|
979
|
+
},
|
|
980
|
+
state() {
|
|
981
|
+
try {
|
|
982
|
+
const r = spawnSync('schtasks', ['/Query', '/tn', name], { encoding: 'utf8' });
|
|
983
|
+
return r.status === 0 ? 'registered' : 'not registered';
|
|
984
|
+
} catch {
|
|
985
|
+
return null;
|
|
986
|
+
}
|
|
987
|
+
},
|
|
988
|
+
// The task XML is the only record of what was actually registered —
|
|
989
|
+
// `/Create /f` overwrites, so the flags on this invocation prove nothing.
|
|
990
|
+
read() {
|
|
991
|
+
try {
|
|
992
|
+
const r = spawnSync('schtasks', ['/Query', '/tn', name, '/XML'], { encoding: 'utf8' });
|
|
993
|
+
const xml = String(r.stdout || '');
|
|
994
|
+
const iv = /<Interval>PT(\d+)M<\/Interval>/i.exec(xml);
|
|
995
|
+
const ml = /--max-load\s+(\d+(?:\.\d+)?)/.exec(xml);
|
|
996
|
+
return { every: iv ? Number(iv[1]) : null, maxLoad: ml ? Number(ml[1]) : null };
|
|
997
|
+
} catch {
|
|
998
|
+
return { every: null, maxLoad: null };
|
|
999
|
+
}
|
|
1000
|
+
},
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
return {
|
|
1005
|
+
kind: 'unsupported',
|
|
1006
|
+
paths: [],
|
|
1007
|
+
install() {},
|
|
1008
|
+
remove() {},
|
|
1009
|
+
state: () => null,
|
|
1010
|
+
read: () => ({ every: null, maxLoad: null }),
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/** Run a scheduler binary; throw with its stderr so an install can say why. */
|
|
1015
|
+
function runScheduler(argv, { ignore = false } = {}) {
|
|
1016
|
+
const [cmd, ...rest] = argv;
|
|
1017
|
+
const r = spawnSync(cmd, rest, { encoding: 'utf8' });
|
|
1018
|
+
if (ignore) return r;
|
|
1019
|
+
if (r.error) throw new Error(`${cmd}: ${r.error.message}`);
|
|
1020
|
+
if (r.status !== 0) {
|
|
1021
|
+
throw new Error(`${cmd} ${rest.join(' ')} exited ${r.status}: ${String(r.stderr || '').trim()}`);
|
|
1022
|
+
}
|
|
1023
|
+
return r;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
/** A command line inside a unit/plist: quote only what needs quoting. */
|
|
1028
|
+
function shellQuote(s) {
|
|
1029
|
+
const v = String(s);
|
|
1030
|
+
if (!/[\s"']/.test(v)) return v;
|
|
1031
|
+
return '"' + v.replace(/"/g, '\\"') + '"';
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/** XML text escaping for the launchd plist. */
|
|
1035
|
+
function xmlEscape(s) {
|
|
1036
|
+
return String(s)
|
|
1037
|
+
.replace(/&/g, '&')
|
|
1038
|
+
.replace(/</g, '<')
|
|
1039
|
+
.replace(/>/g, '>')
|
|
1040
|
+
.replace(/"/g, '"');
|
|
1041
|
+
}
|
|
1042
|
+
|
|
492
1043
|
/** The one place this surface is documented, printed by `help` and on misuse. */
|
|
493
1044
|
function usageText() {
|
|
494
1045
|
return [
|
|
@@ -502,6 +1053,17 @@ function usageText() {
|
|
|
502
1053
|
' list show queued tasks and the tail of the runs log',
|
|
503
1054
|
' run drain ONE task, then stop',
|
|
504
1055
|
' proceed drain every pending task (--max N, --stop-on-error, --commit)',
|
|
1056
|
+
' start drain in the BACKGROUND (detached) and return its pid at once',
|
|
1057
|
+
' use this instead of `proceed` when nothing is waiting for the',
|
|
1058
|
+
' output — a long drain outlives a harness timeout this way',
|
|
1059
|
+
' (--max N, --stop-on-error, --commit)',
|
|
1060
|
+
' tick ONE drain, skipped when the machine is busy — the timer body',
|
|
1061
|
+
' (--max-load N, default 4.0; 0 disables the gate)',
|
|
1062
|
+
' timer install|status|remove the repeating timer that calls `tick`',
|
|
1063
|
+
' (--every MIN default 5, --max-load N default 4.0)',
|
|
1064
|
+
' report what finished: settled runs from the log (+ queue counts)',
|
|
1065
|
+
' --since <id> for only what happened after that task,',
|
|
1066
|
+
' --max N for more of the log',
|
|
505
1067
|
' reconcile queue the next unfinished PLAN.md phase (--auto drains it)',
|
|
506
1068
|
' retry <id> put a finished task back in the queue',
|
|
507
1069
|
' clear drop finished tasks (--all empties the queue)',
|
|
@@ -514,4 +1076,6 @@ function usageText() {
|
|
|
514
1076
|
].join('\n') + '\n';
|
|
515
1077
|
}
|
|
516
1078
|
|
|
517
|
-
module.exports = {
|
|
1079
|
+
module.exports = {
|
|
1080
|
+
runQueueCommand, parseFlags, taskLine, usageText, SUBCOMMANDS, timerPlan, DEFAULT_MAX_LOAD,
|
|
1081
|
+
};
|