@agentsignalnet/claude-plugin 0.2.76 → 0.2.77
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/.claude-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/scripts/dashboard.mjs +173 -0
package/package.json
CHANGED
package/scripts/dashboard.mjs
CHANGED
|
@@ -35,6 +35,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
35
35
|
import { readFileSync } from 'node:fs';
|
|
36
36
|
import { homedir } from 'node:os';
|
|
37
37
|
import { join } from 'node:path';
|
|
38
|
+
import { fileURLToPath } from 'node:url';
|
|
38
39
|
import { mkdirSync, writeFileSync, readdirSync, statSync, unlinkSync } from 'node:fs';
|
|
39
40
|
import { randomUUID as uuid } from 'node:crypto';
|
|
40
41
|
import {
|
|
@@ -160,6 +161,77 @@ const token = randomUUID();
|
|
|
160
161
|
/** Where the token lives after the first visit. Namespaced: localhost is shared. */
|
|
161
162
|
const COOKIE = 'agentsignal_viewer';
|
|
162
163
|
|
|
164
|
+
/**
|
|
165
|
+
* THE LOCK, so a newer build replaces the one already on this port.
|
|
166
|
+
*
|
|
167
|
+
* A dashboard is a long-lived process nobody restarts on purpose. Update the
|
|
168
|
+
* plugin and the running one keeps serving the old code -- indefinitely, and
|
|
169
|
+
* silently, because it works. Worse, the port-in-use message added earlier says
|
|
170
|
+
* "that is usually a dashboard already running, find the link it printed",
|
|
171
|
+
* which is exactly the wrong advice after an update: the running one is the
|
|
172
|
+
* stale one, and finding its link is how you keep using it.
|
|
173
|
+
*
|
|
174
|
+
* The watcher solved this for itself long ago and this is the same machinery:
|
|
175
|
+
* compare build times, take over when newer, stand down when not.
|
|
176
|
+
*
|
|
177
|
+
* KEYED ON THE PORT, which is the difference from the watcher. One watcher per
|
|
178
|
+
* machine is right; one dashboard per machine is NOT -- a second company needs
|
|
179
|
+
* a second dashboard, and the one-company rule says so in as many words. Two on
|
|
180
|
+
* different ports are two scopes, deliberately. Two on the SAME port are an
|
|
181
|
+
* accident, and only that case is settled here.
|
|
182
|
+
*
|
|
183
|
+
* PORT=0 takes no lock at all. It means "any free port", so it collides with
|
|
184
|
+
* nothing and supersedes nothing; the tests run that way, and a test that
|
|
185
|
+
* killed the developer's dashboard would be a memorable way to learn this.
|
|
186
|
+
*/
|
|
187
|
+
const lockRoot = process.env.AGENTSIGNAL_STATE_DIR || join(homedir(), '.agentsignal');
|
|
188
|
+
const lockPath = port ? join(lockRoot, `dashboard-${port}.pid`) : null;
|
|
189
|
+
const lockMetaPath = port ? join(lockRoot, `dashboard-${port}.meta.json`) : null;
|
|
190
|
+
|
|
191
|
+
/** When THIS build was written, which is what decides who is newer. */
|
|
192
|
+
function buildTime() {
|
|
193
|
+
try {
|
|
194
|
+
return statSync(fileURLToPath(import.meta.url)).mtimeMs;
|
|
195
|
+
} catch {
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The live dashboard holding this port, if there is one. */
|
|
201
|
+
function holder() {
|
|
202
|
+
if (!lockPath) return null;
|
|
203
|
+
let pid;
|
|
204
|
+
try {
|
|
205
|
+
pid = Number(readFileSync(lockPath, 'utf8').trim());
|
|
206
|
+
if (!Number.isInteger(pid) || pid <= 0 || pid === process.pid) return null;
|
|
207
|
+
// ESRCH: a pidfile left by a machine that lost power looks exactly like a
|
|
208
|
+
// live one until you ask.
|
|
209
|
+
process.kill(pid, 0);
|
|
210
|
+
} catch {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const meta = JSON.parse(readFileSync(lockMetaPath, 'utf8'));
|
|
215
|
+
if (meta?.pid === pid) return { pid, script: meta.script ?? null, mtime: meta.mtime ?? 0 };
|
|
216
|
+
} catch {
|
|
217
|
+
/* absent, unreadable, or about somebody else */
|
|
218
|
+
}
|
|
219
|
+
return { pid, script: null, mtime: 0 };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function goneWithin(pid, ms) {
|
|
223
|
+
const deadline = Date.now() + ms;
|
|
224
|
+
while (Date.now() < deadline) {
|
|
225
|
+
try {
|
|
226
|
+
process.kill(pid, 0);
|
|
227
|
+
} catch {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
|
|
163
235
|
/**
|
|
164
236
|
* RE-READING A PENDING ROW, briefly, because delivery state changes after the
|
|
165
237
|
* row is cached and `since` guarantees it is never looked at again.
|
|
@@ -4188,12 +4260,113 @@ server.on('error', (error) => {
|
|
|
4188
4260
|
process.exit(1);
|
|
4189
4261
|
});
|
|
4190
4262
|
|
|
4263
|
+
/**
|
|
4264
|
+
* Take the port from an older build, or stand down to a newer one.
|
|
4265
|
+
*
|
|
4266
|
+
* Runs before listen, so nothing is half-bound either way, and the answer is
|
|
4267
|
+
* decided on BUILD TIME rather than start time: an old process started five
|
|
4268
|
+
* minutes ago is still old, and a new one started an hour ago is still new.
|
|
4269
|
+
* Start time would make "whoever restarted last wins", which is how a stale
|
|
4270
|
+
* build reclaims a port from the fix for it.
|
|
4271
|
+
*/
|
|
4272
|
+
async function settlePort() {
|
|
4273
|
+
const running = holder();
|
|
4274
|
+
if (!running) return;
|
|
4275
|
+
|
|
4276
|
+
const mine = buildTime();
|
|
4277
|
+
if (!(mine > running.mtime)) {
|
|
4278
|
+
/*
|
|
4279
|
+
NOT OLDER, so leave it alone. It is already serving, and its link works;
|
|
4280
|
+
replacing it would kill a page somebody has open to hand them an
|
|
4281
|
+
identical one. This is the message the port-in-use path used to give and
|
|
4282
|
+
it is right HERE, where the running build is not stale.
|
|
4283
|
+
*/
|
|
4284
|
+
console.error(`Port ${port} is already served by a dashboard of the same build or newer (pid ${running.pid}).`);
|
|
4285
|
+
console.error('Nothing to update, so it has been left alone — find the link it printed');
|
|
4286
|
+
console.error(`when it started, or stop it first. To run another alongside it: PORT=${port + 1} …`);
|
|
4287
|
+
process.exit(0);
|
|
4288
|
+
}
|
|
4289
|
+
|
|
4290
|
+
console.error(`superseding pid ${running.pid} (${running.script ?? 'an older dashboard'})`);
|
|
4291
|
+
try {
|
|
4292
|
+
// SIGTERM: it has a socket to close and pages to stop serving, and a
|
|
4293
|
+
// signal it could handle is worth sending politely first.
|
|
4294
|
+
process.kill(running.pid, 'SIGTERM');
|
|
4295
|
+
} catch {
|
|
4296
|
+
/* it went on its own between the check and the signal */
|
|
4297
|
+
}
|
|
4298
|
+
|
|
4299
|
+
if (!(await goneWithin(running.pid, 5000))) {
|
|
4300
|
+
// Two dashboards on one port cannot both bind, so carrying on would fail
|
|
4301
|
+
// at listen with a message about the port rather than about this.
|
|
4302
|
+
console.error(`pid ${running.pid} did not exit; leaving it alone rather than fighting for the port.`);
|
|
4303
|
+
process.exit(1);
|
|
4304
|
+
}
|
|
4305
|
+
}
|
|
4306
|
+
|
|
4307
|
+
/**
|
|
4308
|
+
* Whether the lock on disk is OURS.
|
|
4309
|
+
*
|
|
4310
|
+
* A process that stands down must not tidy up on its way out: the files it
|
|
4311
|
+
* would delete belong to the dashboard still serving, and removing them leaves
|
|
4312
|
+
* a live process with no lock — so the next start finds no holder, supersedes
|
|
4313
|
+
* nothing, and collides on the port instead.
|
|
4314
|
+
*
|
|
4315
|
+
* Found by the test rather than by reading: two on different ports, and the
|
|
4316
|
+
* first port's lock had disappeared while its process was still answering.
|
|
4317
|
+
*/
|
|
4318
|
+
let holdsLock = false;
|
|
4319
|
+
|
|
4320
|
+
/** Written after the bind, so the file never names a process that failed to start. */
|
|
4321
|
+
function takeLock() {
|
|
4322
|
+
if (!lockPath) return;
|
|
4323
|
+
try {
|
|
4324
|
+
mkdirSync(lockRoot, { recursive: true });
|
|
4325
|
+
writeFileSync(
|
|
4326
|
+
lockMetaPath,
|
|
4327
|
+
JSON.stringify({ pid: process.pid, script: fileURLToPath(import.meta.url), mtime: buildTime(), port: bound }),
|
|
4328
|
+
{ mode: 0o600 },
|
|
4329
|
+
);
|
|
4330
|
+
writeFileSync(lockPath, String(process.pid), { mode: 0o600 });
|
|
4331
|
+
holdsLock = true;
|
|
4332
|
+
} catch {
|
|
4333
|
+
/* an unwritable state directory is not a reason to refuse to serve */
|
|
4334
|
+
}
|
|
4335
|
+
}
|
|
4336
|
+
|
|
4337
|
+
/**
|
|
4338
|
+
* Put the lock down on the way out, so the next start reads an empty port
|
|
4339
|
+
* rather than asking the operating system about a pid that has been recycled.
|
|
4340
|
+
* Best effort: a SIGKILL leaves it behind, which `holder()` handles by asking
|
|
4341
|
+
* whether the pid is alive rather than trusting the file.
|
|
4342
|
+
*/
|
|
4343
|
+
function dropLock() {
|
|
4344
|
+
if (!lockPath || !holdsLock) return;
|
|
4345
|
+
for (const path of [lockPath, lockMetaPath]) {
|
|
4346
|
+
try {
|
|
4347
|
+
unlinkSync(path);
|
|
4348
|
+
} catch {
|
|
4349
|
+
/* already gone */
|
|
4350
|
+
}
|
|
4351
|
+
}
|
|
4352
|
+
}
|
|
4353
|
+
process.on('exit', dropLock);
|
|
4354
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
4355
|
+
process.on(signal, () => {
|
|
4356
|
+
dropLock();
|
|
4357
|
+
process.exit(0);
|
|
4358
|
+
});
|
|
4359
|
+
}
|
|
4360
|
+
|
|
4361
|
+
await settlePort();
|
|
4362
|
+
|
|
4191
4363
|
server.listen(port, '127.0.0.1', () => {
|
|
4192
4364
|
// The port the OS actually gave us, not the one asked for. They differ
|
|
4193
4365
|
// whenever PORT is 0 -- which is how anything automated starts a server
|
|
4194
4366
|
// without picking a number and hoping -- and printing the request rather
|
|
4195
4367
|
// than the result hands out a link to port 0, which connects to nothing.
|
|
4196
4368
|
bound = server.address()?.port ?? port;
|
|
4369
|
+
takeLock();
|
|
4197
4370
|
console.error(`Watching ${baseUrl} on http://127.0.0.1:${bound}/?t=${token}`);
|
|
4198
4371
|
console.error('Loopback only, and the link carries a one-run token: it can now send and answer.');
|
|
4199
4372
|
console.error('The key stays in this process; the page only ever talks to this server.');
|