pulse-hub 3.5.0 → 3.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/CHANGELOG.md +6 -0
- package/backend/.env.example +52 -52
- package/backend/agent.js +372 -372
- package/backend/cli.js +23 -1
- package/backend/emails/env-backup.html +52 -52
- package/backend/emails/invite.html +53 -53
- package/backend/lib/agent-rpc.js +72 -72
- package/backend/lib/async-route.js +17 -17
- package/backend/lib/authz/bootstrap.js +35 -35
- package/backend/lib/authz/catalog.js +291 -291
- package/backend/lib/authz/delegation.js +104 -104
- package/backend/lib/authz/effective.js +104 -104
- package/backend/lib/authz/engine.js +251 -251
- package/backend/lib/authz/index.js +134 -134
- package/backend/lib/authz/migrate.js +871 -871
- package/backend/lib/authz/mutate.js +87 -87
- package/backend/lib/authz/preview.js +327 -327
- package/backend/lib/authz/scope.js +282 -282
- package/backend/lib/env-mask.js +131 -131
- package/backend/lib/invite-helpers.js +83 -83
- package/backend/lib/invites.js +160 -160
- package/backend/lib/logs.js +244 -244
- package/backend/lib/metrics.js +249 -249
- package/backend/lib/relay-state.js +564 -564
- package/backend/lib/session.js +48 -48
- package/backend/middleware/authz.js +65 -65
- package/backend/public/assets/favicon.svg +6 -6
- package/backend/public/assets/index.js +1 -1
- package/backend/public/index.html +23 -24
- package/backend/routes/access.js +123 -123
- package/backend/routes/audit.js +41 -41
- package/backend/routes/auth.js +207 -207
- package/backend/routes/invites-public.js +91 -91
- package/backend/routes/invites.js +121 -121
- package/backend/routes/logs.js +68 -68
- package/backend/routes/metrics.js +76 -76
- package/backend/routes/policies.js +240 -240
- package/backend/routes/teams.js +221 -221
- package/backend/routes/users.js +100 -100
- package/backend/seed-admins.js +68 -68
- package/backend/server.js +106 -106
- package/backend/ws/agent-relay.js +159 -159
- package/package.json +1 -1
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
// The hub side of the hub/agent WebSocket relay: accepts an agent's 'hello', tracks its
|
|
2
|
-
// live connection in lib/relay-state.js, and routes 'metrics'/'logs_chunk'/'log_line'
|
|
3
|
-
// frames to whichever browser tabs are subscribed. See docs/architecture.md for the
|
|
4
|
-
// wire protocol and .claude/rules/agent-connectivity-heartbeat.md for why "connected"
|
|
5
|
-
// is a ws-level ping/pong heartbeat, not message recency.
|
|
6
|
-
|
|
7
|
-
const { WebSocketServer } = require('ws');
|
|
8
|
-
const db = require('../lib/db');
|
|
9
|
-
const config = require('../config');
|
|
10
|
-
const { verifyInstanceToken } = require('../lib/instance-registry');
|
|
11
|
-
const {
|
|
12
|
-
agentState,
|
|
13
|
-
pendingLogRequests,
|
|
14
|
-
broadcastLogLine,
|
|
15
|
-
sendToAgent,
|
|
16
|
-
desiredMetricsInterval,
|
|
17
|
-
broadcastMetrics,
|
|
18
|
-
markInstancesDirty,
|
|
19
|
-
broadcastInstancesNow,
|
|
20
|
-
appendInstanceHistorySample,
|
|
21
|
-
resolveAgentRequest,
|
|
22
|
-
failPendingAgentRequests,
|
|
23
|
-
} = require('../lib/relay-state');
|
|
24
|
-
|
|
25
|
-
const wss = new WebSocketServer({ noServer: true });
|
|
26
|
-
|
|
27
|
-
// The hub's own agent (the instance marked is_self) updates itself automatically when it
|
|
28
|
-
// connects on an older bundle than the hub — operators have no other reason to hold it
|
|
29
|
-
// back, and it's the one agent they can't easily reach to update by hand. Other agents
|
|
30
|
-
// stay operator-triggered. The cooldown stops a loop if an update "succeeds" but the
|
|
31
|
-
// reconnecting agent still reports a different version.
|
|
32
|
-
const SELF_AUTO_UPDATE_COOLDOWN_MS = 10 * 60 * 1000;
|
|
33
|
-
const lastSelfAutoUpdate = new Map(); // instanceId -> timestamp
|
|
34
|
-
function maybeAutoUpdateSelf(instanceId, bundleHash) {
|
|
35
|
-
if (!bundleHash || bundleHash === config.HUB_BUNDLE_HASH) return;
|
|
36
|
-
const inst = db.getInstance(instanceId);
|
|
37
|
-
if (!inst || !inst.is_self) return;
|
|
38
|
-
const last = lastSelfAutoUpdate.get(instanceId) || 0;
|
|
39
|
-
if (Date.now() - last < SELF_AUTO_UPDATE_COOLDOWN_MS) return;
|
|
40
|
-
lastSelfAutoUpdate.set(instanceId, Date.now());
|
|
41
|
-
console.log(
|
|
42
|
-
`Self-monitoring agent ${instanceId} is on an older version — triggering automatic update.`,
|
|
43
|
-
);
|
|
44
|
-
sendToAgent(instanceId, { type: 'self_update' });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function sanitizeAgentTags(tags) {
|
|
48
|
-
if (!Array.isArray(tags)) return [];
|
|
49
|
-
const out = new Set();
|
|
50
|
-
for (const t of tags) {
|
|
51
|
-
if (typeof t !== 'string') continue;
|
|
52
|
-
const v = t.trim();
|
|
53
|
-
if (v && v.length <= 100) out.add(v);
|
|
54
|
-
if (out.size >= 50) break;
|
|
55
|
-
}
|
|
56
|
-
return [...out];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
wss.on('connection', (ws) => {
|
|
60
|
-
let authedInstanceId = null;
|
|
61
|
-
ws.isAlive = true;
|
|
62
|
-
ws.on('pong', () => {
|
|
63
|
-
ws.isAlive = true;
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
ws.on('message', (raw) => {
|
|
67
|
-
let msg;
|
|
68
|
-
try {
|
|
69
|
-
msg = JSON.parse(raw);
|
|
70
|
-
} catch (e) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (msg.type === 'hello') {
|
|
75
|
-
if (!verifyInstanceToken(msg.instanceId, msg.token)) {
|
|
76
|
-
ws.send(JSON.stringify({ type: 'hello_reject', reason: 'invalid instance id or token' }));
|
|
77
|
-
ws.close();
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
authedInstanceId = msg.instanceId;
|
|
81
|
-
const existing = agentState.get(authedInstanceId);
|
|
82
|
-
agentState.set(authedInstanceId, {
|
|
83
|
-
ws,
|
|
84
|
-
lastMetrics: existing ? existing.lastMetrics : null,
|
|
85
|
-
// bundleHash drives update-staleness detection (see maybeAutoUpdateSelf and
|
|
86
|
-
// lib/relay-state.js's updateAvailable); appVersion is the human-facing semver
|
|
87
|
-
// shown in the UI — see .claude/rules/version-bump-policy.md. Kept distinct on
|
|
88
|
-
// purpose: an agent whose code hasn't changed can go a while without an
|
|
89
|
-
// appVersion bump, but bundleHash always reflects what's actually running.
|
|
90
|
-
bundleHash: msg.bundleHash || 'unknown',
|
|
91
|
-
appVersion: msg.appVersion || 'unknown',
|
|
92
|
-
// Optional request types this agent build understands (see lib/relay-state.js's
|
|
93
|
-
// agentHasCapability) — absent on an agent older than the feature.
|
|
94
|
-
capabilities: Array.isArray(msg.capabilities) ? msg.capabilities : [],
|
|
95
|
-
});
|
|
96
|
-
db.markInstanceFirstConnected(authedInstanceId);
|
|
97
|
-
// Agent-reported tags are LABELS (instance_tags, source='agent') — descriptive only.
|
|
98
|
-
// They must never reach instance_auth_tags: access tags are what scopes match on, and
|
|
99
|
-
// an agent (or anyone holding its token) must not be able to move a server into
|
|
100
|
-
// someone's scope by claiming a tag. Sanitized here because a non-string would make
|
|
101
|
-
// the sqlite insert throw inside this message handler.
|
|
102
|
-
db.setInstanceAgentTags(authedInstanceId, sanitizeAgentTags(msg.tags));
|
|
103
|
-
ws.send(JSON.stringify({ type: 'hello_ack' }));
|
|
104
|
-
sendToAgent(authedInstanceId, {
|
|
105
|
-
type: 'set_interval',
|
|
106
|
-
ms: desiredMetricsInterval(authedInstanceId),
|
|
107
|
-
});
|
|
108
|
-
broadcastInstancesNow();
|
|
109
|
-
maybeAutoUpdateSelf(authedInstanceId, msg.bundleHash);
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (!authedInstanceId) return; // ignore anything before a valid hello
|
|
114
|
-
const state = agentState.get(authedInstanceId);
|
|
115
|
-
|
|
116
|
-
if (msg.type === 'metrics') {
|
|
117
|
-
const metrics = { ...msg };
|
|
118
|
-
delete metrics.type;
|
|
119
|
-
if (state) state.lastMetrics = metrics;
|
|
120
|
-
appendInstanceHistorySample(authedInstanceId, metrics);
|
|
121
|
-
broadcastMetrics(authedInstanceId, metrics);
|
|
122
|
-
markInstancesDirty();
|
|
123
|
-
} else if (msg.type === 'logs_chunk') {
|
|
124
|
-
const pending = pendingLogRequests.get(msg.requestId);
|
|
125
|
-
if (pending) {
|
|
126
|
-
clearTimeout(pending.timer);
|
|
127
|
-
pending.resolve({ records: msg.records, total: msg.total });
|
|
128
|
-
pendingLogRequests.delete(msg.requestId);
|
|
129
|
-
}
|
|
130
|
-
} else if (msg.type === 'rpc_result') {
|
|
131
|
-
resolveAgentRequest(authedInstanceId, msg);
|
|
132
|
-
} else if (msg.type === 'log_line') {
|
|
133
|
-
broadcastLogLine(authedInstanceId, msg.kind, msg.app, msg.stream, msg.record);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
ws.on('close', () => {
|
|
138
|
-
if (authedInstanceId) {
|
|
139
|
-
const state = agentState.get(authedInstanceId);
|
|
140
|
-
if (state && state.ws === ws) {
|
|
141
|
-
state.ws = null;
|
|
142
|
-
failPendingAgentRequests(authedInstanceId, 'OFFLINE', 'The agent disconnected.');
|
|
143
|
-
}
|
|
144
|
-
broadcastInstancesNow();
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// Detects half-open connections (e.g. the monitored box lost power or its network
|
|
150
|
-
// dropped mid-session) that never deliver a clean 'close' event on their own.
|
|
151
|
-
setInterval(() => {
|
|
152
|
-
wss.clients.forEach((ws) => {
|
|
153
|
-
if (ws.isAlive === false) return ws.terminate();
|
|
154
|
-
ws.isAlive = false;
|
|
155
|
-
ws.ping();
|
|
156
|
-
});
|
|
157
|
-
}, config.HEARTBEAT_INTERVAL_MS).unref();
|
|
158
|
-
|
|
159
|
-
module.exports = wss;
|
|
1
|
+
// The hub side of the hub/agent WebSocket relay: accepts an agent's 'hello', tracks its
|
|
2
|
+
// live connection in lib/relay-state.js, and routes 'metrics'/'logs_chunk'/'log_line'
|
|
3
|
+
// frames to whichever browser tabs are subscribed. See docs/architecture.md for the
|
|
4
|
+
// wire protocol and .claude/rules/agent-connectivity-heartbeat.md for why "connected"
|
|
5
|
+
// is a ws-level ping/pong heartbeat, not message recency.
|
|
6
|
+
|
|
7
|
+
const { WebSocketServer } = require('ws');
|
|
8
|
+
const db = require('../lib/db');
|
|
9
|
+
const config = require('../config');
|
|
10
|
+
const { verifyInstanceToken } = require('../lib/instance-registry');
|
|
11
|
+
const {
|
|
12
|
+
agentState,
|
|
13
|
+
pendingLogRequests,
|
|
14
|
+
broadcastLogLine,
|
|
15
|
+
sendToAgent,
|
|
16
|
+
desiredMetricsInterval,
|
|
17
|
+
broadcastMetrics,
|
|
18
|
+
markInstancesDirty,
|
|
19
|
+
broadcastInstancesNow,
|
|
20
|
+
appendInstanceHistorySample,
|
|
21
|
+
resolveAgentRequest,
|
|
22
|
+
failPendingAgentRequests,
|
|
23
|
+
} = require('../lib/relay-state');
|
|
24
|
+
|
|
25
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
26
|
+
|
|
27
|
+
// The hub's own agent (the instance marked is_self) updates itself automatically when it
|
|
28
|
+
// connects on an older bundle than the hub — operators have no other reason to hold it
|
|
29
|
+
// back, and it's the one agent they can't easily reach to update by hand. Other agents
|
|
30
|
+
// stay operator-triggered. The cooldown stops a loop if an update "succeeds" but the
|
|
31
|
+
// reconnecting agent still reports a different version.
|
|
32
|
+
const SELF_AUTO_UPDATE_COOLDOWN_MS = 10 * 60 * 1000;
|
|
33
|
+
const lastSelfAutoUpdate = new Map(); // instanceId -> timestamp
|
|
34
|
+
function maybeAutoUpdateSelf(instanceId, bundleHash) {
|
|
35
|
+
if (!bundleHash || bundleHash === config.HUB_BUNDLE_HASH) return;
|
|
36
|
+
const inst = db.getInstance(instanceId);
|
|
37
|
+
if (!inst || !inst.is_self) return;
|
|
38
|
+
const last = lastSelfAutoUpdate.get(instanceId) || 0;
|
|
39
|
+
if (Date.now() - last < SELF_AUTO_UPDATE_COOLDOWN_MS) return;
|
|
40
|
+
lastSelfAutoUpdate.set(instanceId, Date.now());
|
|
41
|
+
console.log(
|
|
42
|
+
`Self-monitoring agent ${instanceId} is on an older version — triggering automatic update.`,
|
|
43
|
+
);
|
|
44
|
+
sendToAgent(instanceId, { type: 'self_update' });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function sanitizeAgentTags(tags) {
|
|
48
|
+
if (!Array.isArray(tags)) return [];
|
|
49
|
+
const out = new Set();
|
|
50
|
+
for (const t of tags) {
|
|
51
|
+
if (typeof t !== 'string') continue;
|
|
52
|
+
const v = t.trim();
|
|
53
|
+
if (v && v.length <= 100) out.add(v);
|
|
54
|
+
if (out.size >= 50) break;
|
|
55
|
+
}
|
|
56
|
+
return [...out];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
wss.on('connection', (ws) => {
|
|
60
|
+
let authedInstanceId = null;
|
|
61
|
+
ws.isAlive = true;
|
|
62
|
+
ws.on('pong', () => {
|
|
63
|
+
ws.isAlive = true;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
ws.on('message', (raw) => {
|
|
67
|
+
let msg;
|
|
68
|
+
try {
|
|
69
|
+
msg = JSON.parse(raw);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (msg.type === 'hello') {
|
|
75
|
+
if (!verifyInstanceToken(msg.instanceId, msg.token)) {
|
|
76
|
+
ws.send(JSON.stringify({ type: 'hello_reject', reason: 'invalid instance id or token' }));
|
|
77
|
+
ws.close();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
authedInstanceId = msg.instanceId;
|
|
81
|
+
const existing = agentState.get(authedInstanceId);
|
|
82
|
+
agentState.set(authedInstanceId, {
|
|
83
|
+
ws,
|
|
84
|
+
lastMetrics: existing ? existing.lastMetrics : null,
|
|
85
|
+
// bundleHash drives update-staleness detection (see maybeAutoUpdateSelf and
|
|
86
|
+
// lib/relay-state.js's updateAvailable); appVersion is the human-facing semver
|
|
87
|
+
// shown in the UI — see .claude/rules/version-bump-policy.md. Kept distinct on
|
|
88
|
+
// purpose: an agent whose code hasn't changed can go a while without an
|
|
89
|
+
// appVersion bump, but bundleHash always reflects what's actually running.
|
|
90
|
+
bundleHash: msg.bundleHash || 'unknown',
|
|
91
|
+
appVersion: msg.appVersion || 'unknown',
|
|
92
|
+
// Optional request types this agent build understands (see lib/relay-state.js's
|
|
93
|
+
// agentHasCapability) — absent on an agent older than the feature.
|
|
94
|
+
capabilities: Array.isArray(msg.capabilities) ? msg.capabilities : [],
|
|
95
|
+
});
|
|
96
|
+
db.markInstanceFirstConnected(authedInstanceId);
|
|
97
|
+
// Agent-reported tags are LABELS (instance_tags, source='agent') — descriptive only.
|
|
98
|
+
// They must never reach instance_auth_tags: access tags are what scopes match on, and
|
|
99
|
+
// an agent (or anyone holding its token) must not be able to move a server into
|
|
100
|
+
// someone's scope by claiming a tag. Sanitized here because a non-string would make
|
|
101
|
+
// the sqlite insert throw inside this message handler.
|
|
102
|
+
db.setInstanceAgentTags(authedInstanceId, sanitizeAgentTags(msg.tags));
|
|
103
|
+
ws.send(JSON.stringify({ type: 'hello_ack' }));
|
|
104
|
+
sendToAgent(authedInstanceId, {
|
|
105
|
+
type: 'set_interval',
|
|
106
|
+
ms: desiredMetricsInterval(authedInstanceId),
|
|
107
|
+
});
|
|
108
|
+
broadcastInstancesNow();
|
|
109
|
+
maybeAutoUpdateSelf(authedInstanceId, msg.bundleHash);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!authedInstanceId) return; // ignore anything before a valid hello
|
|
114
|
+
const state = agentState.get(authedInstanceId);
|
|
115
|
+
|
|
116
|
+
if (msg.type === 'metrics') {
|
|
117
|
+
const metrics = { ...msg };
|
|
118
|
+
delete metrics.type;
|
|
119
|
+
if (state) state.lastMetrics = metrics;
|
|
120
|
+
appendInstanceHistorySample(authedInstanceId, metrics);
|
|
121
|
+
broadcastMetrics(authedInstanceId, metrics);
|
|
122
|
+
markInstancesDirty();
|
|
123
|
+
} else if (msg.type === 'logs_chunk') {
|
|
124
|
+
const pending = pendingLogRequests.get(msg.requestId);
|
|
125
|
+
if (pending) {
|
|
126
|
+
clearTimeout(pending.timer);
|
|
127
|
+
pending.resolve({ records: msg.records, total: msg.total });
|
|
128
|
+
pendingLogRequests.delete(msg.requestId);
|
|
129
|
+
}
|
|
130
|
+
} else if (msg.type === 'rpc_result') {
|
|
131
|
+
resolveAgentRequest(authedInstanceId, msg);
|
|
132
|
+
} else if (msg.type === 'log_line') {
|
|
133
|
+
broadcastLogLine(authedInstanceId, msg.kind, msg.app, msg.stream, msg.record);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
ws.on('close', () => {
|
|
138
|
+
if (authedInstanceId) {
|
|
139
|
+
const state = agentState.get(authedInstanceId);
|
|
140
|
+
if (state && state.ws === ws) {
|
|
141
|
+
state.ws = null;
|
|
142
|
+
failPendingAgentRequests(authedInstanceId, 'OFFLINE', 'The agent disconnected.');
|
|
143
|
+
}
|
|
144
|
+
broadcastInstancesNow();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Detects half-open connections (e.g. the monitored box lost power or its network
|
|
150
|
+
// dropped mid-session) that never deliver a clean 'close' event on their own.
|
|
151
|
+
setInterval(() => {
|
|
152
|
+
wss.clients.forEach((ws) => {
|
|
153
|
+
if (ws.isAlive === false) return ws.terminate();
|
|
154
|
+
ws.isAlive = false;
|
|
155
|
+
ws.ping();
|
|
156
|
+
});
|
|
157
|
+
}, config.HEARTBEAT_INTERVAL_MS).unref();
|
|
158
|
+
|
|
159
|
+
module.exports = wss;
|