@phnx-labs/agents-cli 1.20.92 → 1.21.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/CHANGELOG.md +385 -0
- package/README.md +3 -0
- package/dist/bin/agents +0 -0
- package/dist/commands/beta.js +3 -5
- package/dist/commands/doctor.js +23 -0
- package/dist/commands/events.js +91 -1
- package/dist/commands/exec.js +13 -0
- package/dist/commands/mailboxes.js +39 -1
- package/dist/commands/message.js +12 -1
- package/dist/commands/packages.js +14 -5
- package/dist/commands/projects.d.ts +38 -0
- package/dist/commands/projects.js +355 -48
- package/dist/commands/routines.js +27 -1
- package/dist/commands/rules.js +6 -0
- package/dist/commands/secrets.d.ts +17 -0
- package/dist/commands/secrets.js +198 -7
- package/dist/commands/send.d.ts +14 -12
- package/dist/commands/send.js +105 -35
- package/dist/commands/sessions.d.ts +17 -0
- package/dist/commands/sessions.js +16 -8
- package/dist/commands/setup-browser.js +5 -1
- package/dist/commands/setup-fleet.js +5 -0
- package/dist/commands/setup-preferences.d.ts +53 -0
- package/dist/commands/setup-preferences.js +142 -0
- package/dist/commands/setup.js +8 -9
- package/dist/commands/ssh.js +220 -10
- package/dist/commands/sync.d.ts +2 -2
- package/dist/commands/sync.js +13 -12
- package/dist/commands/view.js +4 -0
- package/dist/commands/watchdog.js +26 -3
- package/dist/index.js +38 -6
- package/dist/lib/activity.d.ts +15 -0
- package/dist/lib/activity.js +26 -2
- package/dist/lib/auto-dispatch.d.ts +6 -1
- package/dist/lib/auto-dispatch.js +7 -2
- package/dist/lib/beta.d.ts +1 -1
- package/dist/lib/beta.js +1 -1
- package/dist/lib/channels/send.d.ts +83 -0
- package/dist/lib/channels/send.js +112 -0
- package/dist/lib/daemon.d.ts +13 -0
- package/dist/lib/daemon.js +80 -60
- package/dist/lib/device-config.d.ts +82 -0
- package/dist/lib/device-config.js +296 -0
- package/dist/lib/event-provenance.d.ts +19 -0
- package/dist/lib/event-provenance.js +48 -0
- package/dist/lib/events-ingest.d.ts +46 -0
- package/dist/lib/events-ingest.js +182 -0
- package/dist/lib/events.d.ts +17 -5
- package/dist/lib/events.js +59 -56
- package/dist/lib/feed.d.ts +1 -1
- package/dist/lib/feed.js +2 -0
- package/dist/lib/git.d.ts +13 -2
- package/dist/lib/git.js +38 -6
- package/dist/lib/linear-project-counts.d.ts +129 -0
- package/dist/lib/linear-project-counts.js +206 -0
- package/dist/lib/linear-projects.d.ts +62 -0
- package/dist/lib/linear-projects.js +144 -0
- package/dist/lib/mailbox-gc.js +30 -7
- package/dist/lib/mailbox.d.ts +14 -1
- package/dist/lib/mailbox.js +35 -3
- package/dist/lib/menubar/MenubarHelper.app/Contents/CodeResources +0 -0
- package/dist/lib/menubar/MenubarHelper.app/Contents/MacOS/MenubarHelper +0 -0
- package/dist/lib/menubar/install-menubar.d.ts +5 -21
- package/dist/lib/menubar/install-menubar.js +35 -44
- package/dist/lib/menubar/notify-desktop.d.ts +17 -2
- package/dist/lib/menubar/notify-desktop.js +8 -2
- package/dist/lib/migrate.js +1 -1
- package/dist/lib/models.js +21 -11
- package/dist/lib/overdue.d.ts +1 -1
- package/dist/lib/overdue.js +54 -19
- package/dist/lib/project-import.d.ts +96 -0
- package/dist/lib/project-import.js +171 -0
- package/dist/lib/project-probe.d.ts +75 -0
- package/dist/lib/project-probe.js +160 -0
- package/dist/lib/project-resources.d.ts +8 -0
- package/dist/lib/project-resources.js +31 -3
- package/dist/lib/project-status.d.ts +32 -1
- package/dist/lib/project-status.js +82 -1
- package/dist/lib/projects.d.ts +6 -0
- package/dist/lib/projects.js +12 -0
- package/dist/lib/remote-agents-json.d.ts +14 -1
- package/dist/lib/remote-agents-json.js +21 -3
- package/dist/lib/routine-notify.d.ts +11 -0
- package/dist/lib/routine-notify.js +22 -0
- package/dist/lib/routines.d.ts +53 -0
- package/dist/lib/routines.js +105 -4
- package/dist/lib/rules/run-sync.d.ts +18 -0
- package/dist/lib/rules/run-sync.js +92 -0
- package/dist/lib/run-notify.js +3 -0
- package/dist/lib/scheduler.js +12 -2
- package/dist/lib/secrets/Agents CLI.app/Contents/CodeResources +0 -0
- package/dist/lib/secrets/Agents CLI.app/Contents/MacOS/Agents CLI +0 -0
- package/dist/lib/secrets/audit.d.ts +1 -1
- package/dist/lib/secrets/audit.js +53 -10
- package/dist/lib/secrets/index.d.ts +4 -5
- package/dist/lib/secrets/index.js +4 -5
- package/dist/lib/secrets/list-filter.d.ts +20 -5
- package/dist/lib/secrets/list-filter.js +22 -6
- package/dist/lib/secrets/usage-db.d.ts +106 -0
- package/dist/lib/secrets/usage-db.js +236 -0
- package/dist/lib/session/active.d.ts +16 -2
- package/dist/lib/session/active.js +8 -4
- package/dist/lib/session/bundle.js +5 -1
- package/dist/lib/session/remote-active.d.ts +5 -1
- package/dist/lib/session/remote-active.js +4 -1
- package/dist/lib/session/remote-list.d.ts +1 -15
- package/dist/lib/session/remote-list.js +22 -111
- package/dist/lib/session/sync/config.d.ts +13 -5
- package/dist/lib/session/sync/config.js +21 -13
- package/dist/lib/sqlite.js +28 -1
- package/dist/lib/startup/command-registry.d.ts +0 -2
- package/dist/lib/startup/command-registry.js +1 -5
- package/dist/lib/state.d.ts +19 -2
- package/dist/lib/state.js +99 -10
- package/dist/lib/sync-umbrella.d.ts +11 -23
- package/dist/lib/sync-umbrella.js +14 -45
- package/dist/lib/teams/agents.d.ts +35 -3
- package/dist/lib/teams/agents.js +52 -8
- package/dist/lib/teams/scheduler.d.ts +29 -2
- package/dist/lib/teams/scheduler.js +60 -16
- package/dist/lib/types.d.ts +22 -5
- package/dist/lib/versions.d.ts +6 -0
- package/dist/lib/versions.js +6 -4
- package/package.json +1 -2
- package/dist/commands/drive.d.ts +0 -10
- package/dist/commands/drive.js +0 -183
- package/dist/commands/hq.d.ts +0 -2
- package/dist/commands/hq.js +0 -58
- package/dist/commands/sessions-sync.d.ts +0 -17
- package/dist/commands/sessions-sync.js +0 -135
- package/dist/commands/sync-provision.d.ts +0 -23
- package/dist/commands/sync-provision.js +0 -107
- package/dist/lib/drive-sync.d.ts +0 -45
- package/dist/lib/drive-sync.js +0 -238
- package/dist/lib/hq/floor.d.ts +0 -87
- package/dist/lib/hq/floor.js +0 -243
- package/dist/lib/session/sync/crdt.d.ts +0 -44
- package/dist/lib/session/sync/crdt.js +0 -119
- package/dist/lib/session/sync/manifest.d.ts +0 -62
- package/dist/lib/session/sync/manifest.js +0 -100
- package/dist/lib/session/sync/provision.d.ts +0 -49
- package/dist/lib/session/sync/provision.js +0 -91
- package/dist/lib/session/sync/r2.d.ts +0 -32
- package/dist/lib/session/sync/r2.js +0 -121
- package/dist/lib/session/sync/sync.d.ts +0 -106
- package/dist/lib/session/sync/sync.js +0 -374
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { registerBuiltinProviders } from './providers/index.js';
|
|
2
|
+
import { resolveTransport } from './resolve.js';
|
|
3
|
+
const OWNER_ALIAS = 'owner';
|
|
4
|
+
/** True when the destination token means “the configured owner”. */
|
|
5
|
+
export function isOwnerAlias(to) {
|
|
6
|
+
return (to ?? '').trim().toLowerCase() === OWNER_ALIAS;
|
|
7
|
+
}
|
|
8
|
+
/** Compose body + optional URL lines (skip urls already present in the body). */
|
|
9
|
+
export function composeSendText(text, urls) {
|
|
10
|
+
const body = text.trim();
|
|
11
|
+
const extra = (urls ?? [])
|
|
12
|
+
.map((u) => u.trim())
|
|
13
|
+
.filter(Boolean)
|
|
14
|
+
.filter((u) => !body.includes(u));
|
|
15
|
+
if (extra.length === 0)
|
|
16
|
+
return body;
|
|
17
|
+
return body ? `${body}\n${extra.join('\n')}` : extra.join('\n');
|
|
18
|
+
}
|
|
19
|
+
/** Read notify.owner; null when either field is missing. */
|
|
20
|
+
export function readOwnerDest(meta) {
|
|
21
|
+
const owner = meta.notify?.owner;
|
|
22
|
+
const channel = owner?.channel?.trim();
|
|
23
|
+
const to = owner?.to?.trim();
|
|
24
|
+
if (!channel || !to)
|
|
25
|
+
return null;
|
|
26
|
+
return { channel, to };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolve CLI/config into a send envelope. Pure except for reading `meta` —
|
|
30
|
+
* no I/O, no provider registration — so unit tests do not need a real PATH.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveSendEnvelope(input, meta) {
|
|
33
|
+
const positional = (input.positionalText ?? '').trim();
|
|
34
|
+
const flagged = (input.text ?? '').trim();
|
|
35
|
+
if (positional && flagged && positional !== flagged) {
|
|
36
|
+
return {
|
|
37
|
+
ok: false,
|
|
38
|
+
error: 'Pass the message once: use --text, or a positional argument, not both with different values.',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const rawText = flagged || positional;
|
|
42
|
+
const urls = (input.urls ?? []).map((u) => u.trim()).filter(Boolean);
|
|
43
|
+
const text = composeSendText(rawText, urls);
|
|
44
|
+
if (!text) {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: 'Message is empty. Pass --text "…", a positional message, and/or --url.',
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// Owner defaults fill only missing fields (and expand the bare "owner" alias).
|
|
51
|
+
// Explicit --channel/--to always win; a complete notify.owner is NOT required
|
|
52
|
+
// when both flags are already set (same merge-then-require shape as main).
|
|
53
|
+
const ownerCfg = meta.notify?.owner;
|
|
54
|
+
const ownerChannel = ownerCfg?.channel?.trim() || '';
|
|
55
|
+
const ownerTo = ownerCfg?.to?.trim() || '';
|
|
56
|
+
let channel = (input.channel ?? '').trim();
|
|
57
|
+
let to = (input.to ?? '').trim();
|
|
58
|
+
const usedOwnerAlias = isOwnerAlias(to);
|
|
59
|
+
if (input.ownerMode || usedOwnerAlias) {
|
|
60
|
+
if (!channel)
|
|
61
|
+
channel = ownerChannel;
|
|
62
|
+
if (!to || usedOwnerAlias)
|
|
63
|
+
to = ownerTo;
|
|
64
|
+
}
|
|
65
|
+
if (!channel || !to) {
|
|
66
|
+
const hint = input.ownerMode || usedOwnerAlias
|
|
67
|
+
? 'Set notify.owner.{channel,to} in agents.yaml, or pass --channel and --to explicitly.'
|
|
68
|
+
: 'Need --channel and --to (or --to owner with notify.owner configured). ' +
|
|
69
|
+
'Example: agents send --channel desktop --to local --text "hi"';
|
|
70
|
+
return { ok: false, error: hint };
|
|
71
|
+
}
|
|
72
|
+
const attachments = [
|
|
73
|
+
...(input.attachments ?? []),
|
|
74
|
+
]
|
|
75
|
+
.map((p) => p.trim())
|
|
76
|
+
.filter(Boolean);
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
envelope: {
|
|
80
|
+
text,
|
|
81
|
+
channel,
|
|
82
|
+
to,
|
|
83
|
+
thread: input.thread?.trim() || undefined,
|
|
84
|
+
attachments: attachments.length ? attachments : undefined,
|
|
85
|
+
from: input.from?.trim() || undefined,
|
|
86
|
+
dryRun: input.dryRun,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Register providers, resolve transport, deliver. Used by the CLI and by any
|
|
92
|
+
* internal caller that already has a resolved envelope.
|
|
93
|
+
*/
|
|
94
|
+
export async function deliverEnvelope(envelope, meta) {
|
|
95
|
+
registerBuiltinProviders();
|
|
96
|
+
const provider = resolveTransport(envelope.channel, meta);
|
|
97
|
+
return provider.send(envelope.text, {
|
|
98
|
+
target: envelope.to,
|
|
99
|
+
thread: envelope.thread,
|
|
100
|
+
attachments: envelope.attachments,
|
|
101
|
+
from: envelope.from,
|
|
102
|
+
dryRun: envelope.dryRun,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** Resolve + deliver in one step (CLI happy path). */
|
|
106
|
+
export async function sendMessage(input, meta) {
|
|
107
|
+
const resolved = resolveSendEnvelope(input, meta);
|
|
108
|
+
if (!resolved.ok)
|
|
109
|
+
return { error: resolved.error };
|
|
110
|
+
const result = await deliverEnvelope(resolved.envelope, meta);
|
|
111
|
+
return { result, envelope: resolved.envelope };
|
|
112
|
+
}
|
package/dist/lib/daemon.d.ts
CHANGED
|
@@ -16,6 +16,19 @@ import { getAgentsBinPath } from './cli-entry.js';
|
|
|
16
16
|
* broker is hosting, and never clobber a reachable (healthy) broker.
|
|
17
17
|
*/
|
|
18
18
|
export declare function shouldTakeOverBroker(isHosting: boolean, brokerReachable: boolean): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* What a gate re-evaluation must do with the routines scheduler. The daemon
|
|
21
|
+
* re-evaluates `scheduler.enabled` on every SIGHUP reload so flipping the key
|
|
22
|
+
* takes effect without a daemon restart (and `routines add`'s reload signal on
|
|
23
|
+
* a re-enabled box boots the scheduler — the reload is truthful, not a no-op).
|
|
24
|
+
*
|
|
25
|
+
* running + enabled → reload (the normal SIGHUP path)
|
|
26
|
+
* running + !enabled → stop (gate flipped off since boot)
|
|
27
|
+
* !running + enabled → boot (gate flipped on since boot)
|
|
28
|
+
* !running + !enabled → none (stay dark)
|
|
29
|
+
*/
|
|
30
|
+
export type SchedulerGateTransition = 'reload' | 'stop' | 'boot' | 'none';
|
|
31
|
+
export declare function schedulerGateTransition(running: boolean, enabled: boolean): SchedulerGateTransition;
|
|
19
32
|
/** Read the stored daemon PID from disk. Returns null if not present or invalid. */
|
|
20
33
|
export declare function readDaemonPid(): number | null;
|
|
21
34
|
/** Write the daemon PID to the pid file. */
|
package/dist/lib/daemon.js
CHANGED
|
@@ -24,6 +24,7 @@ import { BrowserService } from './browser/service.js';
|
|
|
24
24
|
import { BrowserIPCServer } from './browser/ipc.js';
|
|
25
25
|
import { redactSecrets } from './redact.js';
|
|
26
26
|
import { getAgentsBinPath, getCliLaunch, BUN_VIRTUAL_ROOT } from './cli-entry.js';
|
|
27
|
+
import { isSchedulerEnabled, assertSchedulerEnabled } from './device-config.js';
|
|
27
28
|
const PID_FILE = 'daemon.pid';
|
|
28
29
|
const LOCK_FILE = 'daemon.lock';
|
|
29
30
|
const LOG_FILE = 'logs.jsonl';
|
|
@@ -53,6 +54,11 @@ const WEDGE_THRESHOLD_TICKS = 3;
|
|
|
53
54
|
export function shouldTakeOverBroker(isHosting, brokerReachable) {
|
|
54
55
|
return !isHosting && !brokerReachable;
|
|
55
56
|
}
|
|
57
|
+
export function schedulerGateTransition(running, enabled) {
|
|
58
|
+
if (running)
|
|
59
|
+
return enabled ? 'reload' : 'stop';
|
|
60
|
+
return enabled ? 'boot' : 'none';
|
|
61
|
+
}
|
|
56
62
|
function getDaemonDir() {
|
|
57
63
|
const dir = getDaemonDirRoot();
|
|
58
64
|
fs.mkdirSync(dir, { recursive: true });
|
|
@@ -398,7 +404,7 @@ export async function runDaemon() {
|
|
|
398
404
|
log('ERROR', `Stray daemon reaper failed: ${err.message}`);
|
|
399
405
|
}
|
|
400
406
|
// #416: host the secrets broker socket-first — before the scheduler and the
|
|
401
|
-
// heavy browser
|
|
407
|
+
// heavy browser services — so `agents secrets` resolves within
|
|
402
408
|
// ms of daemon start. Only host when no broker is already reachable, so we
|
|
403
409
|
// never orphan a live standalone broker's clients (that broker stays the
|
|
404
410
|
// server until it idle-exits or the daemon restarts). Best-effort: a failure
|
|
@@ -419,7 +425,23 @@ export async function runDaemon() {
|
|
|
419
425
|
catch (err) {
|
|
420
426
|
log('WARN', `Secrets broker host skipped: ${err.message}`);
|
|
421
427
|
}
|
|
422
|
-
|
|
428
|
+
// scheduler.enabled=false in this machine's device doc means NO routines fire
|
|
429
|
+
// here — the scheduler and its catchup recovery simply never start, while the
|
|
430
|
+
// daemon keeps its other duties (secrets broker, browser IPC, session sync).
|
|
431
|
+
// The refusal message is the same one the start surfaces
|
|
432
|
+
// (`routines add` auto-start, manual `routines start`) raise. The gate is
|
|
433
|
+
// re-evaluated on every SIGHUP reload (handleReload below) via
|
|
434
|
+
// schedulerGateTransition, so flipping the key never needs a daemon restart.
|
|
435
|
+
const schedulerEnabledAtBoot = isSchedulerEnabled();
|
|
436
|
+
if (!schedulerEnabledAtBoot) {
|
|
437
|
+
try {
|
|
438
|
+
assertSchedulerEnabled();
|
|
439
|
+
}
|
|
440
|
+
catch (err) {
|
|
441
|
+
log('WARN', err.message);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
const triggerJob = async (config) => {
|
|
423
445
|
const jobLabel = config.command
|
|
424
446
|
? 'command'
|
|
425
447
|
: config.workflow
|
|
@@ -458,7 +480,36 @@ export async function runDaemon() {
|
|
|
458
480
|
}
|
|
459
481
|
catch { /* best-effort */ }
|
|
460
482
|
}
|
|
461
|
-
}
|
|
483
|
+
};
|
|
484
|
+
let scheduler = null;
|
|
485
|
+
let catchupInterval;
|
|
486
|
+
// Catchup overlap guard. Declared up here (not beside catchupPass) because
|
|
487
|
+
// bootScheduler() runs before catchupPass's textual position — a `let` down
|
|
488
|
+
// there would still be in its TDZ at the first call and crash the daemon.
|
|
489
|
+
let catchingUp = false;
|
|
490
|
+
// Boot the scheduler + catchup recovery. Called at daemon start when the gate
|
|
491
|
+
// allows, and again from handleReload when the gate flips on (function
|
|
492
|
+
// declarations hoist — catchupPass below is in scope).
|
|
493
|
+
function bootScheduler() {
|
|
494
|
+
scheduler = new JobScheduler(triggerJob);
|
|
495
|
+
scheduler.loadAll();
|
|
496
|
+
const scheduled = scheduler.listScheduled();
|
|
497
|
+
log('INFO', `Loaded ${scheduled.length} jobs`);
|
|
498
|
+
for (const job of scheduled) {
|
|
499
|
+
log('INFO', ` ${job.name} -> next: ${job.nextRun?.toISOString() || 'unknown'}`);
|
|
500
|
+
}
|
|
501
|
+
void catchupPass();
|
|
502
|
+
catchupInterval = setInterval(() => { void catchupPass(); }, CATCHUP_TICK_MS);
|
|
503
|
+
}
|
|
504
|
+
// Stop the scheduler + catchup recovery (gate flipped off on reload).
|
|
505
|
+
function stopScheduler() {
|
|
506
|
+
scheduler?.stopAll();
|
|
507
|
+
scheduler = null;
|
|
508
|
+
if (catchupInterval !== undefined) {
|
|
509
|
+
clearInterval(catchupInterval);
|
|
510
|
+
catchupInterval = undefined;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
462
513
|
// Materialise opted-in project routines into the user layer on every start
|
|
463
514
|
// so a fresh daemon picks up project YAML without a separate sync step.
|
|
464
515
|
try {
|
|
@@ -470,12 +521,8 @@ export async function runDaemon() {
|
|
|
470
521
|
catch (err) {
|
|
471
522
|
log('WARN', `Project routines sync failed: ${err.message}`);
|
|
472
523
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
log('INFO', `Loaded ${scheduled.length} jobs`);
|
|
476
|
-
for (const job of scheduled) {
|
|
477
|
-
log('INFO', ` ${job.name} -> next: ${job.nextRun?.toISOString() || 'unknown'}`);
|
|
478
|
-
}
|
|
524
|
+
if (schedulerEnabledAtBoot)
|
|
525
|
+
bootScheduler();
|
|
479
526
|
// Monitor engine: event-triggered watchers, beside the cron scheduler. Same
|
|
480
527
|
// daemon, same dispatch seam — a monitor is a routine whose trigger is a
|
|
481
528
|
// watched source instead of a clock. Reloads on SIGHUP alongside the scheduler.
|
|
@@ -495,15 +542,16 @@ export async function runDaemon() {
|
|
|
495
542
|
// `catchup: false`, RUN late. Runs on a timer as well as at startup: a startup
|
|
496
543
|
// pass alone misses a fire lost while the daemon stayed up but its event loop
|
|
497
544
|
// was wedged, or one lost across an OS suspend that the process survived.
|
|
498
|
-
// Overlap guard, same shape as
|
|
545
|
+
// Overlap guard, same shape as runHealCheck below. A pass
|
|
499
546
|
// awaits executeJobDetached per job and an off-box (host/cloud) dispatch can
|
|
500
547
|
// block for a while, so a slow pass could still be working when the next tick
|
|
501
548
|
// fires. Both passes would then see a job the first has not yet reached as
|
|
502
549
|
// overdue — the miss is recorded before the await, but only for jobs already
|
|
503
550
|
// processed — and spawn it twice. The idempotency of the `missed` record
|
|
504
551
|
// guards across passes, not within one that is mid-flight.
|
|
505
|
-
|
|
506
|
-
|
|
552
|
+
// Function declaration (hoisted) so bootScheduler() can schedule it. Its
|
|
553
|
+
// guard (`catchingUp`) is declared beside `scheduler` above for TDZ safety.
|
|
554
|
+
async function catchupPass() {
|
|
507
555
|
if (catchingUp)
|
|
508
556
|
return;
|
|
509
557
|
catchingUp = true;
|
|
@@ -553,9 +601,7 @@ export async function runDaemon() {
|
|
|
553
601
|
// throw must not leave the guard latched shut for the daemon's lifetime.
|
|
554
602
|
catchingUp = false;
|
|
555
603
|
}
|
|
556
|
-
}
|
|
557
|
-
await catchupPass();
|
|
558
|
-
const catchupInterval = setInterval(() => { void catchupPass(); }, CATCHUP_TICK_MS);
|
|
604
|
+
}
|
|
559
605
|
// Before the BrowserService comes up, reap browser + tunnel processes
|
|
560
606
|
// spawned by previous daemons that are no longer alive. Without this,
|
|
561
607
|
// a daemon hard-crash (SIGKILL, OOM) would leak every browser and SSH
|
|
@@ -588,42 +634,6 @@ export async function runDaemon() {
|
|
|
588
634
|
writeHeartbeat();
|
|
589
635
|
monitorRunningJobs();
|
|
590
636
|
}, MONITOR_TICK_MS);
|
|
591
|
-
// Cross-machine session sync: push this machine's transcripts to R2 and pull
|
|
592
|
-
// every other machine's, ~every 90s. Skipped silently when the r2.backups
|
|
593
|
-
// bundle is absent. An overlap guard prevents a slow cycle from stacking.
|
|
594
|
-
let syncing = false;
|
|
595
|
-
const runSessionSync = async () => {
|
|
596
|
-
if (syncing)
|
|
597
|
-
return;
|
|
598
|
-
syncing = true;
|
|
599
|
-
try {
|
|
600
|
-
const { isBetaEnabled } = await import('./beta.js');
|
|
601
|
-
// Off by default: session sync is an opt-in beta feature. Check the beta
|
|
602
|
-
// flag FIRST so a machine that hasn't opted in skips the keychain read
|
|
603
|
-
// (isSyncConfigured) entirely, not just the network cycle.
|
|
604
|
-
if (!isBetaEnabled('session-sync'))
|
|
605
|
-
return;
|
|
606
|
-
const { isSyncConfigured } = await import('./session/sync/config.js');
|
|
607
|
-
if (!isSyncConfigured())
|
|
608
|
-
return;
|
|
609
|
-
const { syncSessions } = await import('./session/sync/sync.js');
|
|
610
|
-
const r = await syncSessions();
|
|
611
|
-
if (r.pushed || r.pulled || r.errors.length) {
|
|
612
|
-
log('INFO', `sessions sync: pushed ${r.pushed}, pulled ${r.pulled}, merged ${r.merged}` +
|
|
613
|
-
(r.errors.length ? `, ${r.errors.length} error(s): ${r.errors[0]}` : ''));
|
|
614
|
-
}
|
|
615
|
-
if (r.warnings.length)
|
|
616
|
-
log('WARN', `sessions sync: ${r.warnings[0]}`);
|
|
617
|
-
}
|
|
618
|
-
catch (err) {
|
|
619
|
-
log('ERROR', `sessions sync failed: ${err.message}`);
|
|
620
|
-
}
|
|
621
|
-
finally {
|
|
622
|
-
syncing = false;
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
const syncInterval = setInterval(() => { void runSessionSync(); }, 90_000);
|
|
626
|
-
void runSessionSync(); // kick once at startup
|
|
627
637
|
// Resource safety check: heal gaps between what DotAgents repos define and
|
|
628
638
|
// what's actually installed in each agent home — the slow rot that nothing
|
|
629
639
|
// else catches (a non-default version left stale, a Claude-invalid plugin
|
|
@@ -875,27 +885,37 @@ export async function runDaemon() {
|
|
|
875
885
|
catch (err) {
|
|
876
886
|
log('WARN', `Project routines sync failed: ${err.message}`);
|
|
877
887
|
}
|
|
878
|
-
scheduler.
|
|
879
|
-
|
|
880
|
-
|
|
888
|
+
// Re-evaluate the scheduler.enabled gate: flipping the key takes effect on
|
|
889
|
+
// this reload, no daemon restart needed. A `routines add` on a re-enabled
|
|
890
|
+
// box signals exactly this reload, which boots the scheduler — the
|
|
891
|
+
// "Scheduler reloaded" it prints is then truthful, not a dead-end.
|
|
892
|
+
const transition = schedulerGateTransition(scheduler !== null, isSchedulerEnabled());
|
|
893
|
+
if (transition === 'boot') {
|
|
894
|
+
log('INFO', 'scheduler.enabled is now on — booting the scheduler');
|
|
895
|
+
bootScheduler();
|
|
896
|
+
}
|
|
897
|
+
else if (transition === 'stop') {
|
|
898
|
+
log('WARN', 'scheduler.enabled is now off — stopping the scheduler; no routines will fire on this device');
|
|
899
|
+
stopScheduler();
|
|
900
|
+
}
|
|
901
|
+
else if (transition === 'reload') {
|
|
902
|
+
scheduler.reloadAll();
|
|
903
|
+
const reloaded = scheduler.listScheduled();
|
|
904
|
+
log('INFO', `Reloaded ${reloaded.length} jobs`);
|
|
905
|
+
}
|
|
881
906
|
try {
|
|
882
907
|
monitorEngine.reload();
|
|
883
908
|
}
|
|
884
909
|
catch (err) {
|
|
885
910
|
log('ERROR', `Monitor engine reload failed: ${err.message}`);
|
|
886
911
|
}
|
|
887
|
-
// Drop the memoized R2 config so rotated/added sync credentials are re-read
|
|
888
|
-
// on the next cycle instead of waiting for a restart.
|
|
889
|
-
void import('./session/sync/config.js').then(m => m.clearR2ConfigCache());
|
|
890
912
|
};
|
|
891
913
|
const handleShutdown = async () => {
|
|
892
914
|
log('INFO', 'Daemon shutting down');
|
|
893
|
-
|
|
915
|
+
stopScheduler();
|
|
894
916
|
monitorEngine.stop();
|
|
895
917
|
await browserIPC.stop();
|
|
896
918
|
clearInterval(monitorInterval);
|
|
897
|
-
clearInterval(catchupInterval);
|
|
898
|
-
clearInterval(syncInterval);
|
|
899
919
|
clearInterval(healInterval);
|
|
900
920
|
clearTimeout(healKickoff);
|
|
901
921
|
clearInterval(autoDispatchInterval);
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device/user config keys — typed read/write over the two-tier agents.yaml store.
|
|
3
|
+
*
|
|
4
|
+
* One registry (`CONFIG_KEYS`) maps each CLI dotted name to where it lives:
|
|
5
|
+
* - user scope → central `~/.agents/agents.yaml` under `config:` (syncs
|
|
6
|
+
* fleet-wide via `agents repo push/pull`)
|
|
7
|
+
* - device scope → `~/.agents/devices/<host>/agents.yaml` under `config:`
|
|
8
|
+
* (per-machine; mirrors how `defaultBrowserProfile` is routed)
|
|
9
|
+
*
|
|
10
|
+
* This machine's keys go through the readMeta/updateMeta funnel (state.ts) so the
|
|
11
|
+
* partition/overlay logic stays the single writer. Another device's doc is
|
|
12
|
+
* read/written in place — the devices/ tree syncs via the DotAgents repo, so
|
|
13
|
+
* editing `devices/mac-mini/agents.yaml` locally is how `configure`/`note`
|
|
14
|
+
* target a peer (`--device`-style).
|
|
15
|
+
*
|
|
16
|
+
* `browser.profile` is NOT a `config:` key — it is the existing
|
|
17
|
+
* `Meta.defaultBrowserProfile` field; the registry entry documents the mapping
|
|
18
|
+
* and set/get route to it so there is one source of truth (no duplicate key).
|
|
19
|
+
*
|
|
20
|
+
* Unset always means today's behavior.
|
|
21
|
+
*/
|
|
22
|
+
/** Which tier of the agents.yaml store a key lives in. */
|
|
23
|
+
export type ConfigScope = 'user' | 'device';
|
|
24
|
+
/** Value type of a config key — drives validation and `--json` rendering. */
|
|
25
|
+
export type ConfigType = 'string' | 'int' | 'bool' | 'string-list';
|
|
26
|
+
/** One known config key. */
|
|
27
|
+
export interface ConfigKeySpec {
|
|
28
|
+
/** CLI dotted name, e.g. `interactive.host`. */
|
|
29
|
+
name: string;
|
|
30
|
+
/** camelCase key under the YAML `config:` block. */
|
|
31
|
+
yamlKey: string;
|
|
32
|
+
scope: ConfigScope;
|
|
33
|
+
type: ConfigType;
|
|
34
|
+
/** One-line description for help/list output. */
|
|
35
|
+
description: string;
|
|
36
|
+
/**
|
|
37
|
+
* When set, the value lives in this top-level Meta field instead of the
|
|
38
|
+
* `config:` block (only `browser.profile` → `defaultBrowserProfile` today).
|
|
39
|
+
*/
|
|
40
|
+
field?: 'defaultBrowserProfile';
|
|
41
|
+
/** Extra validation beyond the type check; return an error string or null. */
|
|
42
|
+
validate?: (value: unknown) => string | null;
|
|
43
|
+
}
|
|
44
|
+
/** A key with its resolved value and the layer that set it. */
|
|
45
|
+
export interface ConfigEntry {
|
|
46
|
+
spec: ConfigKeySpec;
|
|
47
|
+
/** The stored value, or undefined when unset (unset = default behavior). */
|
|
48
|
+
value: unknown;
|
|
49
|
+
/** Which layer set it; undefined when unset. */
|
|
50
|
+
layer?: ConfigScope;
|
|
51
|
+
}
|
|
52
|
+
/** Options scoping a read/write to a specific device's doc (default: this machine). */
|
|
53
|
+
export interface ConfigTarget {
|
|
54
|
+
device?: string;
|
|
55
|
+
}
|
|
56
|
+
export declare const CONFIG_KEYS: readonly ConfigKeySpec[];
|
|
57
|
+
/** Look up a key spec by CLI dotted name, or throw listing the known keys. */
|
|
58
|
+
export declare function configKeySpec(name: string): ConfigKeySpec;
|
|
59
|
+
/** Get one config key's value and the layer that set it. */
|
|
60
|
+
export declare function getConfigValue(name: string, opts?: ConfigTarget): ConfigEntry;
|
|
61
|
+
/** List every known key with its value and the layer that set it. */
|
|
62
|
+
export declare function listConfig(opts?: ConfigTarget): ConfigEntry[];
|
|
63
|
+
/** Set a config key (validated). Device-scope keys target this machine unless `opts.device` names a peer. */
|
|
64
|
+
export declare function setConfigValue(name: string, value: unknown, opts?: ConfigTarget): void;
|
|
65
|
+
/** Unset a config key — restores default behavior. No-op when already unset. */
|
|
66
|
+
export declare function unsetConfigValue(name: string, opts?: ConfigTarget): void;
|
|
67
|
+
/** True unless this machine's device doc disables the routines scheduler. */
|
|
68
|
+
export declare function isSchedulerEnabled(): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Throw when the routines scheduler is disabled on this machine, naming the
|
|
71
|
+
* setting and the fix. The single message every scheduler-start surface
|
|
72
|
+
* (auto-start on `routines add`, manual `routines start`, the daemon's own
|
|
73
|
+
* scheduler init) refuses with.
|
|
74
|
+
*/
|
|
75
|
+
export declare function assertSchedulerEnabled(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Read the `agents.max-concurrent` cap for each named device from its synced
|
|
78
|
+
* device doc (no SSH). Devices without a cap are omitted — uncapped is the
|
|
79
|
+
* default. Used as an input to host ranking (teams placement, Factory
|
|
80
|
+
* auto-launch), never as a remote probe.
|
|
81
|
+
*/
|
|
82
|
+
export declare function readMaxConcurrentCaps(devices: string[]): Record<string, number>;
|