@corenel/harness 0.5.0 → 0.5.1

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.
@@ -1,4 +1,9 @@
1
1
  import { SIDECAR_PROTOCOL, errMessage, isLoopbackHost, } from '@corenel/protocol';
2
+ /** How long the welcome waits on `clientDirName` before going out without it. A
3
+ * manifest write is a few milliseconds; anything near this is a wedged disk, and
4
+ * a client left with no handshake at all is worse than one that has to fall back
5
+ * to deducing its own directory. */
6
+ const DIR_NAME_TIMEOUT_MS = 3000;
2
7
  /** True for anything with a callable `.then` -- covers native Promises and any
3
8
  * other thenable a caller's `onClient` might return, without assuming it's a
4
9
  * real Promise instance (the declared type is `() => void`; an async function
@@ -91,6 +96,14 @@ export class SidecarServer {
91
96
  writable: true,
92
97
  value: void 0
93
98
  });
99
+ /** The friendly label from the same `hello`, kept beside `clientId` so the
100
+ * dirName resolver is handed the identity the manifest keys on. */
101
+ Object.defineProperty(this, "clientName", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
94
107
  /** Detach function for this connection's attendance attachment, set on a
95
108
  * successful `hello` when `opts.attendance` is wired. Called once, on channel
96
109
  * close, so a dead connection stops receiving/counting toward asks. */
@@ -100,6 +113,23 @@ export class SidecarServer {
100
113
  writable: true,
101
114
  value: void 0
102
115
  });
116
+ /** The synced directory THIS connection belongs to, once resolved -- the same
117
+ * value echoed in `welcome.dirName`. Retained so `hosts-list` can stamp
118
+ * `self` and `hosts-forget` can refuse the caller's own tree from the
119
+ * daemon's own knowledge rather than from anything the client claimed. */
120
+ Object.defineProperty(this, "dirName", {
121
+ enumerable: true,
122
+ configurable: true,
123
+ writable: true,
124
+ value: void 0
125
+ });
126
+ /** Detach for this connection's entry in the shared hosts live set. */
127
+ Object.defineProperty(this, "hostsDetach", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
103
133
  /** Detach callbacks for runs this connection is watching, so a dropped socket
104
134
  * stops the hub fanning out into a dead channel. */
105
135
  Object.defineProperty(this, "runDetachers", {
@@ -247,6 +277,10 @@ export class SidecarServer {
247
277
  this.ch.onClose?.(() => {
248
278
  this.attendedDetach?.();
249
279
  this.attendedDetach = undefined;
280
+ // Leave the live set, or this directory reads as connected for ever and
281
+ // could never be reclaimed -- the refusal would outlive the connection.
282
+ this.hostsDetach?.();
283
+ this.hostsDetach = undefined;
250
284
  // A dropped connection must stop receiving run events, or the hub keeps
251
285
  // fanning out into a dead channel for the rest of the run.
252
286
  for (const detach of this.runDetachers)
@@ -452,6 +486,40 @@ export class SidecarServer {
452
486
  case 'host-sync-delete':
453
487
  void this.onHostSyncDelete(m);
454
488
  return;
489
+ case 'agent-pause-list':
490
+ // `ok: false`, not an empty list: telling an unauthenticated caller
491
+ // "this daemon is holding no pauses" is a claim it has not earned, the
492
+ // same reasoning as room-list below.
493
+ if (!this.authed) {
494
+ this.ch.send({ t: 'agent-pause-list-result', id: m.id, ok: false, error: 'not authenticated' });
495
+ return;
496
+ }
497
+ this.onAgentPauseList(m);
498
+ return;
499
+ case 'agent-pause-set':
500
+ if (!this.authed) {
501
+ this.ch.send({ t: 'agent-pause-set-result', id: m.id, ok: false, error: 'not authenticated' });
502
+ return;
503
+ }
504
+ void this.onAgentPauseSet(m);
505
+ return;
506
+ case 'hosts-list':
507
+ // `ok: false`, not an empty estate: telling an unauthenticated caller
508
+ // "this daemon holds no synced hosts" is a claim it has not earned --
509
+ // the same reasoning as agent-pause-list above.
510
+ if (!this.authed) {
511
+ this.ch.send({ t: 'hosts-list-result', id: m.id, ok: false, error: 'not authenticated' });
512
+ return;
513
+ }
514
+ void this.onHostsList(m);
515
+ return;
516
+ case 'hosts-forget':
517
+ if (!this.authed) {
518
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: false, error: 'not authenticated' });
519
+ return;
520
+ }
521
+ void this.onHostsForget(m);
522
+ return;
455
523
  case 'room-open':
456
524
  if (!this.authed) {
457
525
  this.ch.send({ t: 'room-open-result', id: m.id, ok: false, problems: ['not authenticated'] });
@@ -492,6 +560,34 @@ export class SidecarServer {
492
560
  }
493
561
  void this.onRoomResume(m);
494
562
  return;
563
+ case 'room-interject':
564
+ if (!this.authed) {
565
+ this.ch.send({ t: 'room-interject-result', id: m.id, ok: false, error: 'not authenticated' });
566
+ return;
567
+ }
568
+ void this.onRoomInterject(m);
569
+ return;
570
+ case 'room-findings':
571
+ if (!this.authed) {
572
+ this.ch.send({ t: 'room-findings-result', id: m.id, ok: false, error: 'not authenticated' });
573
+ return;
574
+ }
575
+ void this.onRoomFindings(m);
576
+ return;
577
+ case 'room-finding-edit':
578
+ if (!this.authed) {
579
+ this.ch.send({ t: 'room-finding-edit-result', id: m.id, ok: false, error: 'not authenticated' });
580
+ return;
581
+ }
582
+ void this.onRoomFindingEdit(m);
583
+ return;
584
+ case 'room-close':
585
+ if (!this.authed) {
586
+ this.ch.send({ t: 'room-close-result', id: m.id, ok: false, error: 'not authenticated' });
587
+ return;
588
+ }
589
+ void this.onRoomClose(m);
590
+ return;
495
591
  default:
496
592
  // welcome / denied / invoke-result / host-call / event / run-accepted /
497
593
  // run-done / permission-ask / permission-resolved are host-side.
@@ -523,6 +619,7 @@ export class SidecarServer {
523
619
  const name = typeof m.clientName === 'string' && m.clientName
524
620
  ? m.clientName.slice(0, MAX_IDENTITY_FIELD_LEN)
525
621
  : clientId;
622
+ this.clientName = name;
526
623
  // Best-effort, fire-and-forget: a throwing/rejecting `onClient` must never
527
624
  // deny/break an otherwise valid, authenticated handshake -- identity
528
625
  // recording is informational, not part of the auth decision. Covers BOTH
@@ -552,6 +649,52 @@ export class SidecarServer {
552
649
  });
553
650
  }
554
651
  this.opts.log?.(`paired — ${this.tools.size} ${this.tools.size === 1 ? 'tool' : 'tools'} advertised (host dir: ${this.hostStateDir})`);
652
+ /* THE ONE THING THAT WAITS. The welcome carries the synced directory name
653
+ * this daemon resolved for this connection, and the client has no other way
654
+ * to learn it -- it could previously only deduce it, and got null whenever
655
+ * no workspace was open or two hosts held the same workspace id, leaving a
656
+ * management surface unable to say which registrations were its own.
657
+ *
658
+ * Bounded, and every failure ends the same way as an older daemon: no
659
+ * `dirName` on the welcome. A handshake that never arrives would be a far
660
+ * worse trade than a client that falls back to deducing. */
661
+ const identity = this.clientId === undefined
662
+ ? undefined
663
+ : { clientId: this.clientId, name: this.clientName ?? this.clientId };
664
+ if (this.opts.clientDirName && identity) {
665
+ void this.resolveDirName(this.opts.clientDirName, identity).then((dirName) => {
666
+ this.sendWelcome(dirName);
667
+ });
668
+ return;
669
+ }
670
+ this.sendWelcome(null);
671
+ }
672
+ /** Resolve the connection's synced directory name, or null for every failure
673
+ * mode there is (throw, rejection, non-string, timeout). */
674
+ async resolveDirName(resolve, identity) {
675
+ try {
676
+ const timed = new Promise((res) => setTimeout(() => res(null), DIR_NAME_TIMEOUT_MS));
677
+ const got = await Promise.race([Promise.resolve(resolve(identity)), timed]);
678
+ return typeof got === 'string' && got ? got : null;
679
+ }
680
+ catch (err) {
681
+ this.opts.log?.(`clientDirName failed: ${errMessage(err)}`);
682
+ return null;
683
+ }
684
+ }
685
+ sendWelcome(dirName) {
686
+ /* Remembered BEFORE the welcome goes out, because `self` and the
687
+ * forget-your-own-tree refusal are both answered from it. A daemon too old
688
+ * to resolve one, or a client with no identity, leaves it undefined -- and
689
+ * then every entry reads `self: false` and nothing is refused on that
690
+ * ground, which is correct: this connection genuinely has no directory. */
691
+ if (dirName) {
692
+ this.dirName = dirName;
693
+ // Only a resolved directory can be attached: an unidentified connection
694
+ // is not "connected as" anything, and marking it would pin a tree it
695
+ // has no claim to.
696
+ this.hostsDetach = this.opts.hosts?.attach(dirName);
697
+ }
555
698
  const tools = [...this.tools.values()].map(({ run: _run, ...decl }) => decl);
556
699
  // Advertise the loopback HTTP proxy so the host can route local-LLM calls
557
700
  // through us (server-side fetch, no browser CORS) instead of failing, and
@@ -583,8 +726,21 @@ export class SidecarServer {
583
726
  // operator declared somewhere a checkout may be written, so a client only
584
727
  // offers "start from a repository" where it would actually work.
585
728
  ...(this.opts.sessions?.canClone ? ['session-clone'] : []),
729
+ // Whether this daemon HOLDS pauses. Advertised only with a real store
730
+ // behind it, so a client offers "Pause" only where pausing would actually
731
+ // stop the agent -- the alternative is a button that reports success on a
732
+ // daemon that keeps firing.
733
+ ...(this.opts.pauses ? ['agent-pause'] : []),
734
+ // Whether this daemon can report and reclaim its synced host directories.
735
+ // Advertised only with a real manifest behind it, so a client never
736
+ // offers a destructive control to a daemon that would refuse it.
737
+ ...(this.opts.hosts ? ['hosts-admin'] : []),
586
738
  ];
587
- this.ch.send({ t: 'welcome', tools, features, ...(this.opts.info ? { info: this.opts.info } : {}) });
739
+ this.ch.send({
740
+ t: 'welcome', tools, features,
741
+ ...(this.opts.info ? { info: this.opts.info } : {}),
742
+ ...(dirName ? { dirName } : {}),
743
+ });
588
744
  }
589
745
  /** Proxy a host HTTP request server-side. Restricted to LOOPBACK targets — the
590
746
  * sidecar must never be usable as an SSRF pivot to internal/metadata hosts. The
@@ -1159,6 +1315,103 @@ export class SidecarServer {
1159
1315
  this.ch.send({ t: 'roots-add-result', id: m.id, ok: false, error: errMessage(e) });
1160
1316
  }
1161
1317
  }
1318
+ /** Every synced host directory, with `self` stamped from THIS connection's
1319
+ * own resolved directory -- never from anything the client sent. */
1320
+ async onHostsList(m) {
1321
+ if (!this.opts.hosts) {
1322
+ this.ch.send({ t: 'hosts-list-result', id: m.id, ok: false, error: 'this daemon does not administer synced hosts' });
1323
+ return;
1324
+ }
1325
+ try {
1326
+ this.ch.send({ t: 'hosts-list-result', id: m.id, ok: true, hosts: this.stampSelf(await this.opts.hosts.list()) });
1327
+ }
1328
+ catch (err) {
1329
+ this.ch.send({ t: 'hosts-list-result', id: m.id, ok: false, error: errMessage(err) });
1330
+ }
1331
+ }
1332
+ stampSelf(hosts) {
1333
+ return hosts.map((h) => ({ ...h, self: this.dirName !== undefined && h.dirName === this.dirName }));
1334
+ }
1335
+ /**
1336
+ * Delete one synced tree and its manifest entry.
1337
+ *
1338
+ * THE REFUSALS ARE THE FEATURE. A client's UI is not a guard -- this message
1339
+ * carries a `dirName` the client chose, so both rules that make the operation
1340
+ * safe are enforced here: the caller's OWN directory is refused outright, and
1341
+ * the port refuses any directory a client is attached as. The first is
1342
+ * checked here rather than left to the second because it is answerable from
1343
+ * this connection's own identity and yields the more useful sentence.
1344
+ */
1345
+ async onHostsForget(m) {
1346
+ if (!this.opts.hosts) {
1347
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: false, error: 'this daemon does not administer synced hosts' });
1348
+ return;
1349
+ }
1350
+ if (typeof m.dirName !== 'string' || !m.dirName) {
1351
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: false, error: 'dirName must be a non-empty string' });
1352
+ return;
1353
+ }
1354
+ if (this.dirName !== undefined && m.dirName === this.dirName) {
1355
+ this.ch.send({
1356
+ t: 'hosts-forget-result', id: m.id, ok: false,
1357
+ error: 'That is this connection’s own synced directory. Disconnect this client first, then remove it from another one.',
1358
+ });
1359
+ return;
1360
+ }
1361
+ try {
1362
+ const outcome = await this.opts.hosts.forget(m.dirName);
1363
+ if (!outcome.ok) {
1364
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: false, error: outcome.reason });
1365
+ return;
1366
+ }
1367
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: true, hosts: this.stampSelf(await this.opts.hosts.list()) });
1368
+ }
1369
+ catch (err) {
1370
+ this.ch.send({ t: 'hosts-forget-result', id: m.id, ok: false, error: errMessage(err) });
1371
+ }
1372
+ }
1373
+ onAgentPauseList(m) {
1374
+ if (!this.opts.pauses) {
1375
+ // An honest empty answer for a daemon without the capability: it is
1376
+ // holding no pauses, which is true. The feature gate is what tells a
1377
+ // client whether it may OFFER the control.
1378
+ this.ch.send({ t: 'agent-pause-list-result', id: m.id, ok: true, pauses: [] });
1379
+ return;
1380
+ }
1381
+ try {
1382
+ this.ch.send({ t: 'agent-pause-list-result', id: m.id, ok: true, pauses: this.opts.pauses.list() });
1383
+ }
1384
+ catch (err) {
1385
+ this.ch.send({ t: 'agent-pause-list-result', id: m.id, ok: false, error: errMessage(err) });
1386
+ }
1387
+ }
1388
+ async onAgentPauseSet(m) {
1389
+ if (!this.opts.pauses) {
1390
+ // NOT an empty success. Accepting a pause this daemon cannot hold would
1391
+ // leave the operator believing an agent had been stopped while it kept
1392
+ // firing on the hour -- the exact failure the feature exists to end.
1393
+ this.ch.send({ t: 'agent-pause-set-result', id: m.id, ok: false, error: 'this daemon does not hold agent pauses' });
1394
+ return;
1395
+ }
1396
+ // Runtime-validate: the message arrived via `JSON.parse(...) as SidecarMsg`,
1397
+ // so the declared types guarantee nothing. Both fields become part of a
1398
+ // persisted key.
1399
+ if (typeof m.owner !== 'string' || typeof m.agent !== 'string' || !m.agent) {
1400
+ this.ch.send({ t: 'agent-pause-set-result', id: m.id, ok: false, error: 'owner must be a string and agent a non-empty string' });
1401
+ return;
1402
+ }
1403
+ try {
1404
+ const pauses = await this.opts.pauses.set(m.owner, m.agent, m.paused === true);
1405
+ // Republish before replying: the caller's own row and every other
1406
+ // connected client's must change together, or a second browser keeps
1407
+ // offering "Pause" on an agent already paused.
1408
+ this.opts.pauses.refresh();
1409
+ this.ch.send({ t: 'agent-pause-set-result', id: m.id, ok: true, pauses });
1410
+ }
1411
+ catch (err) {
1412
+ this.ch.send({ t: 'agent-pause-set-result', id: m.id, ok: false, error: errMessage(err) });
1413
+ }
1414
+ }
1162
1415
  async onRoomOpen(m) {
1163
1416
  if (!this.opts.rooms) {
1164
1417
  this.ch.send({ t: 'room-open-result', id: m.id, ok: false, problems: ['this sidecar does not host rooms'] });
@@ -1236,7 +1489,7 @@ export class SidecarServer {
1236
1489
  return;
1237
1490
  }
1238
1491
  try {
1239
- const room = await this.opts.rooms.answer(m.roomId, m.participantId, m.text);
1492
+ const room = await this.opts.rooms.answer(m.roomId, m.participantId, m.text, m.addressed);
1240
1493
  this.ch.send({ t: 'room-answer-result', id: m.id, ok: true, room });
1241
1494
  }
1242
1495
  catch (err) {
@@ -1245,6 +1498,64 @@ export class SidecarServer {
1245
1498
  this.ch.send({ t: 'room-answer-result', id: m.id, ok: false, error: errMessage(err) });
1246
1499
  }
1247
1500
  }
1501
+ async onRoomInterject(m) {
1502
+ if (!this.opts.rooms) {
1503
+ this.ch.send({ t: 'room-interject-result', id: m.id, ok: false, error: 'this sidecar does not host rooms' });
1504
+ return;
1505
+ }
1506
+ try {
1507
+ const room = await this.opts.rooms.interject(m.roomId, m.participantId, m.text);
1508
+ this.ch.send({ t: 'room-interject-result', id: m.id, ok: true, room });
1509
+ }
1510
+ catch (err) {
1511
+ // A room failure must never kill the connection; the client gets a
1512
+ // refusal it can render instead of a socket that went quiet.
1513
+ this.ch.send({ t: 'room-interject-result', id: m.id, ok: false, error: errMessage(err) });
1514
+ }
1515
+ }
1516
+ async onRoomClose(m) {
1517
+ if (!this.opts.rooms) {
1518
+ this.ch.send({ t: 'room-close-result', id: m.id, ok: false, error: 'this sidecar does not host rooms' });
1519
+ return;
1520
+ }
1521
+ try {
1522
+ const room = await this.opts.rooms.close(m.roomId, m.participantId, m.reason);
1523
+ this.ch.send({ t: 'room-close-result', id: m.id, ok: true, room });
1524
+ }
1525
+ catch (err) {
1526
+ this.ch.send({ t: 'room-close-result', id: m.id, ok: false, error: errMessage(err) });
1527
+ }
1528
+ }
1529
+ async onRoomFindings(m) {
1530
+ if (!this.opts.rooms) {
1531
+ // A daemon without the capability hosts no rooms, so it holds no
1532
+ // findings -- an honest empty document, the same answer `room-list`
1533
+ // gives. The catch below is the different thing: a real read that failed.
1534
+ this.ch.send({ t: 'room-findings-result', id: m.id, ok: true, findings: [] });
1535
+ return;
1536
+ }
1537
+ try {
1538
+ this.ch.send({ t: 'room-findings-result', id: m.id, ok: true, findings: await this.opts.rooms.findings(m.roomId) });
1539
+ }
1540
+ catch (err) {
1541
+ this.ch.send({ t: 'room-findings-result', id: m.id, ok: false, error: errMessage(err) });
1542
+ }
1543
+ }
1544
+ async onRoomFindingEdit(m) {
1545
+ if (!this.opts.rooms) {
1546
+ this.ch.send({ t: 'room-finding-edit-result', id: m.id, ok: false, error: 'this sidecar does not host rooms' });
1547
+ return;
1548
+ }
1549
+ try {
1550
+ const findings = await this.opts.rooms.editFinding(m.roomId, m.participantId, m.edit);
1551
+ this.ch.send({ t: 'room-finding-edit-result', id: m.id, ok: true, findings });
1552
+ }
1553
+ catch (err) {
1554
+ // A refused edit must never kill the connection; the person gets the
1555
+ // reason their edit did not land.
1556
+ this.ch.send({ t: 'room-finding-edit-result', id: m.id, ok: false, error: errMessage(err) });
1557
+ }
1558
+ }
1248
1559
  async onHostSyncPut(m) {
1249
1560
  const identity = this.requireHostSyncIdentity();
1250
1561
  if ('error' in identity) {