@phnx-labs/agents-cli 1.22.121 → 1.22.122
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 +55 -0
- package/README.md +6 -45
- package/dist/cli/command-registry.js +0 -2
- package/dist/commands/daemon.js +7 -8
- package/dist/commands/mailboxes.js +1 -1
- package/dist/commands/projects.js +3 -3
- package/dist/commands/setup-mine.js +0 -1
- package/dist/commands/sync.js +101 -1
- package/dist/lib/daemon/daemon.js +1 -16
- package/dist/lib/daemon-services.d.ts +1 -1
- package/dist/lib/daemon-services.js +0 -5
- package/dist/lib/daemon-webhooks.d.ts +1 -1
- package/dist/lib/daemon-webhooks.js +1 -1
- package/dist/lib/device-config.js +1 -1
- package/dist/lib/github/project-prs.js +2 -2
- package/dist/lib/hosts/passthrough.js +0 -1
- package/dist/lib/plugins/plugins.d.ts +7 -1
- package/dist/lib/plugins/plugins.js +26 -5
- package/dist/lib/pr-land-detach.js +2 -2
- package/dist/lib/refresh.js +4 -1
- package/dist/lib/scheduling/routines.d.ts +1 -1
- package/dist/lib/scheduling/routines.js +1 -1
- package/dist/lib/session/db.d.ts +1 -1
- package/dist/lib/session/db.js +51 -28
- package/dist/lib/staleness/index.js +12 -0
- package/dist/lib/staleness/types.d.ts +5 -0
- package/dist/lib/startup/command-registry.js +1 -1
- package/dist/lib/state.d.ts +0 -3
- package/dist/lib/state.js +0 -6
- package/dist/lib/sync-delete.d.ts +21 -0
- package/dist/lib/sync-delete.js +113 -0
- package/package.json +1 -3
- package/dist/commands/monitors.d.ts +0 -2
- package/dist/commands/monitors.js +0 -1070
- package/dist/lib/daemon/monitor-engine-service.d.ts +0 -13
- package/dist/lib/daemon/monitor-engine-service.js +0 -25
- package/dist/lib/monitors/config.d.ts +0 -77
- package/dist/lib/monitors/config.js +0 -405
- package/dist/lib/monitors/dispatch.d.ts +0 -10
- package/dist/lib/monitors/dispatch.js +0 -72
- package/dist/lib/monitors/engine.d.ts +0 -45
- package/dist/lib/monitors/engine.js +0 -243
- package/dist/lib/monitors/fingerprint.d.ts +0 -3
- package/dist/lib/monitors/fingerprint.js +0 -43
- package/dist/lib/monitors/pid-watch.d.ts +0 -4
- package/dist/lib/monitors/pid-watch.js +0 -14
- package/dist/lib/monitors/remote.d.ts +0 -31
- package/dist/lib/monitors/remote.js +0 -63
- package/dist/lib/monitors/sources/command.d.ts +0 -4
- package/dist/lib/monitors/sources/command.js +0 -20
- package/dist/lib/monitors/sources/device.d.ts +0 -3
- package/dist/lib/monitors/sources/device.js +0 -31
- package/dist/lib/monitors/sources/failure.d.ts +0 -5
- package/dist/lib/monitors/sources/failure.js +0 -25
- package/dist/lib/monitors/sources/file.d.ts +0 -4
- package/dist/lib/monitors/sources/file.js +0 -46
- package/dist/lib/monitors/sources/http.d.ts +0 -3
- package/dist/lib/monitors/sources/http.js +0 -25
- package/dist/lib/monitors/sources/index.d.ts +0 -5
- package/dist/lib/monitors/sources/index.js +0 -22
- package/dist/lib/monitors/sources/poll.d.ts +0 -1
- package/dist/lib/monitors/sources/poll.js +0 -1
- package/dist/lib/monitors/sources/types.d.ts +0 -9
- package/dist/lib/monitors/sources/types.js +0 -1
- package/dist/lib/monitors/sources/webhook.d.ts +0 -3
- package/dist/lib/monitors/sources/webhook.js +0 -3
- package/dist/lib/monitors/sources/ws.d.ts +0 -4
- package/dist/lib/monitors/sources/ws.js +0 -35
- package/dist/lib/monitors/state.d.ts +0 -51
- package/dist/lib/monitors/state.js +0 -221
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import WSWebSocket from 'ws';
|
|
2
|
-
export function evaluate(_source) {
|
|
3
|
-
return Promise.resolve(null);
|
|
4
|
-
}
|
|
5
|
-
export function subscribe(source, onObs) {
|
|
6
|
-
const url = source.wsUrl;
|
|
7
|
-
if (!url)
|
|
8
|
-
return () => { };
|
|
9
|
-
let closed = false;
|
|
10
|
-
let socket = null;
|
|
11
|
-
const connect = () => {
|
|
12
|
-
if (closed)
|
|
13
|
-
return;
|
|
14
|
-
socket = new WSWebSocket(url, { maxPayload: 8 * 1024 * 1024 });
|
|
15
|
-
socket.onmessage = (ev) => onObs({ raw: String(ev.data), meta: { kind: 'frame' } });
|
|
16
|
-
socket.onclose = () => {
|
|
17
|
-
if (!closed)
|
|
18
|
-
setTimeout(connect, 5_000);
|
|
19
|
-
};
|
|
20
|
-
socket.onerror = () => {
|
|
21
|
-
try {
|
|
22
|
-
socket?.close();
|
|
23
|
-
}
|
|
24
|
-
catch { }
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
connect();
|
|
28
|
-
return () => {
|
|
29
|
-
closed = true;
|
|
30
|
-
try {
|
|
31
|
-
socket?.close();
|
|
32
|
-
}
|
|
33
|
-
catch { }
|
|
34
|
-
};
|
|
35
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { type RunMeta } from '../scheduling/routines.js';
|
|
2
|
-
import type { MonitorEvent } from './config.js';
|
|
3
|
-
interface MonitorState {
|
|
4
|
-
monitorName: string;
|
|
5
|
-
lastHash: string;
|
|
6
|
-
lastValue: string;
|
|
7
|
-
lastSeenAt: string;
|
|
8
|
-
lastFiredAt?: string;
|
|
9
|
-
fireTimes?: number[];
|
|
10
|
-
}
|
|
11
|
-
export interface MonitorLiveness {
|
|
12
|
-
monitorName: string;
|
|
13
|
-
lastCheckedAt: string;
|
|
14
|
-
checkCount: number;
|
|
15
|
-
lastError?: string;
|
|
16
|
-
consecutiveErrors: number;
|
|
17
|
-
droughtNotifiedAt?: string;
|
|
18
|
-
}
|
|
19
|
-
export declare function getMonitorHistoryDir(name: string): string;
|
|
20
|
-
export declare function readState(name: string): MonitorState | null;
|
|
21
|
-
export declare function writeState(name: string, value: string, dedupeKey?: string, extra?: Partial<Pick<MonitorState, 'lastFiredAt' | 'fireTimes'>>): MonitorState;
|
|
22
|
-
export declare function dedupeSignature(observation: string, dedupeKey?: string): string;
|
|
23
|
-
export declare function hasChanged(name: string, observation: string, dedupeKey?: string): boolean;
|
|
24
|
-
export declare function readLiveness(name: string): MonitorLiveness | null;
|
|
25
|
-
export declare function recordCheck(name: string, checkedAt: string, error?: string): MonitorLiveness;
|
|
26
|
-
export declare function markDroughtNotified(name: string, at: string): void;
|
|
27
|
-
export declare function recordFireTime(name: string, now: number, windowMs: number): number[];
|
|
28
|
-
export declare function writeFireRecord(event: MonitorEvent, meta?: Record<string, unknown>): string;
|
|
29
|
-
interface FireRecord extends MonitorEvent {
|
|
30
|
-
runId?: string;
|
|
31
|
-
action?: string;
|
|
32
|
-
ok?: boolean;
|
|
33
|
-
error?: string;
|
|
34
|
-
runStatusAtFire?: RunMeta['status'];
|
|
35
|
-
postcondition?: string;
|
|
36
|
-
postconditionOk?: boolean;
|
|
37
|
-
postconditionError?: string;
|
|
38
|
-
}
|
|
39
|
-
export declare function listFires(name: string): FireRecord[];
|
|
40
|
-
interface ReconciledFireOutcome {
|
|
41
|
-
ok: boolean;
|
|
42
|
-
runStatus?: RunMeta['status'];
|
|
43
|
-
effect?: 'met' | 'none';
|
|
44
|
-
error?: string;
|
|
45
|
-
}
|
|
46
|
-
export declare function evaluatePostcondition(command: string): {
|
|
47
|
-
ok: boolean;
|
|
48
|
-
error?: string;
|
|
49
|
-
};
|
|
50
|
-
export declare function resolveFireOutcome(jobName: string, fire: FireRecord): ReconciledFireOutcome;
|
|
51
|
-
export {};
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { createHash } from 'crypto';
|
|
4
|
-
import { spawnSync } from 'child_process';
|
|
5
|
-
import { getMonitorsHistoryDir, ensureAgentsDir } from '../state.js';
|
|
6
|
-
import { safeJoin } from '../paths.js';
|
|
7
|
-
import { readRunMeta } from '../scheduling/routines.js';
|
|
8
|
-
const MAX_STORED_VALUE = 4096;
|
|
9
|
-
export function getMonitorHistoryDir(name) {
|
|
10
|
-
return safeJoin(getMonitorsHistoryDir(), name);
|
|
11
|
-
}
|
|
12
|
-
function getStatePath(name) {
|
|
13
|
-
return path.join(getMonitorHistoryDir(name), 'state.json');
|
|
14
|
-
}
|
|
15
|
-
function getLivenessPath(name) {
|
|
16
|
-
return path.join(getMonitorHistoryDir(name), 'liveness.json');
|
|
17
|
-
}
|
|
18
|
-
function getMonitorFiresDir(name) {
|
|
19
|
-
return path.join(getMonitorHistoryDir(name), 'fires');
|
|
20
|
-
}
|
|
21
|
-
export function readState(name) {
|
|
22
|
-
const statePath = getStatePath(name);
|
|
23
|
-
if (!fs.existsSync(statePath))
|
|
24
|
-
return null;
|
|
25
|
-
try {
|
|
26
|
-
return JSON.parse(fs.readFileSync(statePath, 'utf-8'));
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function writeStateRaw(state) {
|
|
33
|
-
ensureAgentsDir();
|
|
34
|
-
const dir = getMonitorHistoryDir(state.monitorName);
|
|
35
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
36
|
-
const statePath = path.join(dir, 'state.json');
|
|
37
|
-
const tmp = `${statePath}.tmp-${process.pid}`;
|
|
38
|
-
fs.writeFileSync(tmp, JSON.stringify(state, null, 2), 'utf-8');
|
|
39
|
-
fs.renameSync(tmp, statePath);
|
|
40
|
-
}
|
|
41
|
-
export function writeState(name, value, dedupeKey, extra = {}) {
|
|
42
|
-
const prev = readState(name);
|
|
43
|
-
const state = {
|
|
44
|
-
monitorName: name,
|
|
45
|
-
lastHash: hashSignature(value, dedupeKey),
|
|
46
|
-
lastValue: value.length > MAX_STORED_VALUE ? value.slice(0, MAX_STORED_VALUE) : value,
|
|
47
|
-
lastSeenAt: new Date().toISOString(),
|
|
48
|
-
...(prev?.lastFiredAt ? { lastFiredAt: prev.lastFiredAt } : {}),
|
|
49
|
-
...(prev?.fireTimes ? { fireTimes: prev.fireTimes } : {}),
|
|
50
|
-
...extra,
|
|
51
|
-
};
|
|
52
|
-
writeStateRaw(state);
|
|
53
|
-
return state;
|
|
54
|
-
}
|
|
55
|
-
export function dedupeSignature(observation, dedupeKey) {
|
|
56
|
-
if (!dedupeKey)
|
|
57
|
-
return observation;
|
|
58
|
-
try {
|
|
59
|
-
const m = new RegExp(dedupeKey).exec(observation);
|
|
60
|
-
if (m)
|
|
61
|
-
return m[1] ?? m[0];
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
}
|
|
65
|
-
return observation;
|
|
66
|
-
}
|
|
67
|
-
function hashSignature(observation, dedupeKey) {
|
|
68
|
-
return createHash('sha256').update(dedupeSignature(observation, dedupeKey)).digest('hex');
|
|
69
|
-
}
|
|
70
|
-
export function hasChanged(name, observation, dedupeKey) {
|
|
71
|
-
const prev = readState(name);
|
|
72
|
-
if (!prev)
|
|
73
|
-
return true;
|
|
74
|
-
return prev.lastHash !== hashSignature(observation, dedupeKey);
|
|
75
|
-
}
|
|
76
|
-
export function readLiveness(name) {
|
|
77
|
-
const livenessPath = getLivenessPath(name);
|
|
78
|
-
if (!fs.existsSync(livenessPath))
|
|
79
|
-
return null;
|
|
80
|
-
try {
|
|
81
|
-
return JSON.parse(fs.readFileSync(livenessPath, 'utf-8'));
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
export function recordCheck(name, checkedAt, error) {
|
|
88
|
-
const prev = readLiveness(name);
|
|
89
|
-
const consecutiveErrors = error ? (prev?.consecutiveErrors ?? 0) + 1 : 0;
|
|
90
|
-
const liveness = {
|
|
91
|
-
monitorName: name,
|
|
92
|
-
lastCheckedAt: checkedAt,
|
|
93
|
-
checkCount: (prev?.checkCount ?? 0) + 1,
|
|
94
|
-
consecutiveErrors,
|
|
95
|
-
...(error ? { lastError: error } : {}),
|
|
96
|
-
...(error && prev?.droughtNotifiedAt ? { droughtNotifiedAt: prev.droughtNotifiedAt } : {}),
|
|
97
|
-
};
|
|
98
|
-
ensureAgentsDir();
|
|
99
|
-
const dir = getMonitorHistoryDir(name);
|
|
100
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
101
|
-
const livenessPath = path.join(dir, 'liveness.json');
|
|
102
|
-
const tmp = `${livenessPath}.tmp-${process.pid}`;
|
|
103
|
-
fs.writeFileSync(tmp, JSON.stringify(liveness, null, 2), 'utf-8');
|
|
104
|
-
fs.renameSync(tmp, livenessPath);
|
|
105
|
-
return liveness;
|
|
106
|
-
}
|
|
107
|
-
// Notify once per drought; a subsequent successful recordCheck clears this marker.
|
|
108
|
-
export function markDroughtNotified(name, at) {
|
|
109
|
-
const prev = readLiveness(name);
|
|
110
|
-
if (!prev)
|
|
111
|
-
return;
|
|
112
|
-
const livenessPath = getLivenessPath(name);
|
|
113
|
-
const next = { ...prev, droughtNotifiedAt: at };
|
|
114
|
-
const tmp = `${livenessPath}.tmp-${process.pid}`;
|
|
115
|
-
fs.writeFileSync(tmp, JSON.stringify(next, null, 2), 'utf-8');
|
|
116
|
-
fs.renameSync(tmp, livenessPath);
|
|
117
|
-
}
|
|
118
|
-
export function recordFireTime(name, now, windowMs) {
|
|
119
|
-
const prev = readState(name);
|
|
120
|
-
const times = [...(prev?.fireTimes ?? []), now].filter((t) => now - t <= windowMs);
|
|
121
|
-
return times;
|
|
122
|
-
}
|
|
123
|
-
export function writeFireRecord(event, meta = {}) {
|
|
124
|
-
ensureAgentsDir();
|
|
125
|
-
const fireId = event.firedAt.replace(/[:.]/g, '-');
|
|
126
|
-
const fireDir = safeJoin(getMonitorFiresDir(event.monitorName), fireId);
|
|
127
|
-
fs.mkdirSync(fireDir, { recursive: true });
|
|
128
|
-
fs.writeFileSync(path.join(fireDir, 'event.json'), JSON.stringify({ ...event, ...meta }, null, 2), 'utf-8');
|
|
129
|
-
return fireId;
|
|
130
|
-
}
|
|
131
|
-
export function listFires(name) {
|
|
132
|
-
const dir = getMonitorFiresDir(name);
|
|
133
|
-
if (!fs.existsSync(dir))
|
|
134
|
-
return [];
|
|
135
|
-
const ids = fs
|
|
136
|
-
.readdirSync(dir, { withFileTypes: true })
|
|
137
|
-
.filter((e) => e.isDirectory())
|
|
138
|
-
.map((e) => e.name)
|
|
139
|
-
.sort();
|
|
140
|
-
const fires = [];
|
|
141
|
-
for (const id of ids) {
|
|
142
|
-
const eventPath = path.join(dir, id, 'event.json');
|
|
143
|
-
if (!fs.existsSync(eventPath))
|
|
144
|
-
continue;
|
|
145
|
-
try {
|
|
146
|
-
fires.push(JSON.parse(fs.readFileSync(eventPath, 'utf-8')));
|
|
147
|
-
}
|
|
148
|
-
catch {
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return fires;
|
|
152
|
-
}
|
|
153
|
-
const POSTCONDITION_TIMEOUT_MS = 15_000;
|
|
154
|
-
export function evaluatePostcondition(command) {
|
|
155
|
-
const trimmed = command.trim();
|
|
156
|
-
if (!trimmed)
|
|
157
|
-
return { ok: false, error: 'postcondition not met: empty command' };
|
|
158
|
-
const [bin, args] = process.platform === 'win32'
|
|
159
|
-
? ['cmd', ['/c', trimmed]]
|
|
160
|
-
: ['/bin/sh', ['-c', trimmed]];
|
|
161
|
-
const result = spawnSync(bin, args, {
|
|
162
|
-
encoding: 'utf-8',
|
|
163
|
-
timeout: POSTCONDITION_TIMEOUT_MS,
|
|
164
|
-
maxBuffer: 8 * 1024 * 1024,
|
|
165
|
-
env: { ...process.env, CLICOLOR: '0', NO_COLOR: '1', FORCE_COLOR: '0' },
|
|
166
|
-
});
|
|
167
|
-
if (result.status === 0)
|
|
168
|
-
return { ok: true };
|
|
169
|
-
if (result.error) {
|
|
170
|
-
const err = result.error;
|
|
171
|
-
if (err.code === 'ETIMEDOUT') {
|
|
172
|
-
return { ok: false, error: `postcondition not met: timed out after ${POSTCONDITION_TIMEOUT_MS / 1000}s` };
|
|
173
|
-
}
|
|
174
|
-
return { ok: false, error: `postcondition not met: ${err.message}` };
|
|
175
|
-
}
|
|
176
|
-
const detail = (result.stderr || result.stdout || '').trim().replace(/\s+/g, ' ').slice(0, 200);
|
|
177
|
-
const exit = result.status ?? 'unknown';
|
|
178
|
-
return { ok: false, error: `postcondition not met${detail ? `: ${detail}` : ` (exit ${exit})`}` };
|
|
179
|
-
}
|
|
180
|
-
function persistPostcondition(fire, result) {
|
|
181
|
-
writeFireRecord(fire, {
|
|
182
|
-
postconditionOk: result.ok,
|
|
183
|
-
...(result.error ? { postconditionError: result.error } : {}),
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
// Fire-time success is provisional: re-read the run, require any postcondition, and persist that result once.
|
|
187
|
-
export function resolveFireOutcome(jobName, fire) {
|
|
188
|
-
if (!fire.runId)
|
|
189
|
-
return { ok: fire.ok !== false };
|
|
190
|
-
const run = readRunMeta(jobName, fire.runId);
|
|
191
|
-
if (!run)
|
|
192
|
-
return { ok: fire.ok !== false };
|
|
193
|
-
if (run.status === 'running')
|
|
194
|
-
return { ok: true, runStatus: run.status };
|
|
195
|
-
if (run.status !== 'completed')
|
|
196
|
-
return { ok: false, runStatus: run.status };
|
|
197
|
-
if (!fire.postcondition)
|
|
198
|
-
return { ok: true, runStatus: 'completed' };
|
|
199
|
-
if (fire.postconditionOk === true) {
|
|
200
|
-
return { ok: true, runStatus: 'completed', effect: 'met' };
|
|
201
|
-
}
|
|
202
|
-
if (fire.postconditionOk === false) {
|
|
203
|
-
return {
|
|
204
|
-
ok: false,
|
|
205
|
-
runStatus: 'completed',
|
|
206
|
-
effect: 'none',
|
|
207
|
-
...(fire.postconditionError ? { error: fire.postconditionError } : {}),
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
const result = evaluatePostcondition(fire.postcondition);
|
|
211
|
-
persistPostcondition(fire, result);
|
|
212
|
-
fire.postconditionOk = result.ok;
|
|
213
|
-
if (result.error)
|
|
214
|
-
fire.postconditionError = result.error;
|
|
215
|
-
return {
|
|
216
|
-
ok: result.ok,
|
|
217
|
-
runStatus: 'completed',
|
|
218
|
-
effect: result.ok ? 'met' : 'none',
|
|
219
|
-
...(result.error ? { error: result.error } : {}),
|
|
220
|
-
};
|
|
221
|
-
}
|