agl 41.2.0 → 42.1.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.
Files changed (120) hide show
  1. package/changelog.md +41 -0
  2. package/dist_serve/bundle.js +2625 -2278
  3. package/dist_serve/bundle.js.map +2 -2
  4. package/dist_serve/bundle.js.third-party-notices.json +3 -68
  5. package/dist_serve/bundle.js.third-party-notices.txt +4 -137
  6. package/dist_ts/00_commitinfo_data.js +1 -1
  7. package/dist_ts/aglapi/aglapisurface.document.js +4 -4
  8. package/dist_ts/classes.authmodels.d.ts +2 -8
  9. package/dist_ts/classes.authmodels.js +60 -23
  10. package/dist_ts/classes.authstore.d.ts +24 -0
  11. package/dist_ts/classes.authstore.js +185 -23
  12. package/dist_ts/classes.cli.d.ts +3 -6
  13. package/dist_ts/classes.cli.js +38 -51
  14. package/dist_ts/classes.config.d.ts +17 -3
  15. package/dist_ts/classes.config.js +55 -18
  16. package/dist_ts/classes.controller.d.ts +83 -14
  17. package/dist_ts/classes.controller.js +545 -229
  18. package/dist_ts/classes.controllerdevices.d.ts +128 -0
  19. package/dist_ts/classes.controllerdevices.js +472 -0
  20. package/dist_ts/classes.controllermanagement.d.ts +42 -9
  21. package/dist_ts/classes.controllermanagement.js +72 -23
  22. package/dist_ts/classes.controllerservice.d.ts +3 -31
  23. package/dist_ts/classes.controllerservice.js +3 -47
  24. package/dist_ts/classes.mcpcontrollerhost.js +9 -3
  25. package/dist_ts/classes.processinspection.d.ts +8 -1
  26. package/dist_ts/classes.processinspection.js +92 -2
  27. package/dist_ts/classes.proxycontrol.js +6 -1
  28. package/dist_ts/classes.proxydaemon.d.ts +2 -2
  29. package/dist_ts/classes.proxydaemon.js +1 -1
  30. package/dist_ts/classes.proxystore.d.ts +2 -11
  31. package/dist_ts/classes.proxystore.js +3 -25
  32. package/dist_ts/classes.sessionfeed.d.ts +10 -1
  33. package/dist_ts/classes.sessionfeed.js +12 -2
  34. package/dist_ts/classes.tpmsecretcodec.d.ts +13 -0
  35. package/dist_ts/classes.tpmsecretcodec.js +28 -0
  36. package/dist_ts/classes.trustedproxysecrets.d.ts +91 -0
  37. package/dist_ts/classes.trustedproxysecrets.js +338 -0
  38. package/dist_ts/classes.upgradecoordinator.d.ts +14 -1
  39. package/dist_ts/classes.upgradecoordinator.js +21 -3
  40. package/dist_ts/classes.upgradetransaction.d.ts +32 -0
  41. package/dist_ts/classes.upgradetransaction.js +101 -4
  42. package/dist_ts/classes.validation.d.ts +2 -0
  43. package/dist_ts/classes.validation.js +2 -2
  44. package/dist_ts/functions.controlleradmission.d.ts +67 -0
  45. package/dist_ts/functions.controlleradmission.js +127 -0
  46. package/dist_ts/functions.deviceapi.d.ts +28 -0
  47. package/dist_ts/functions.deviceapi.js +246 -0
  48. package/dist_ts/functions.deviceproof.d.ts +33 -0
  49. package/dist_ts/functions.deviceproof.js +93 -0
  50. package/dist_ts/functions.remotecli.d.ts +27 -0
  51. package/dist_ts/functions.remotecli.js +241 -0
  52. package/dist_ts/functions.retiredenvironment.d.ts +11 -2
  53. package/dist_ts/functions.retiredenvironment.js +22 -5
  54. package/dist_ts/index.d.ts +1 -0
  55. package/dist_ts/index.js +2 -1
  56. package/dist_ts/interfaces.auth.d.ts +29 -4
  57. package/dist_ts/interfaces.auth.js +10 -1
  58. package/dist_ts/interfaces.config.d.ts +14 -0
  59. package/dist_ts/interfaces.config.js +9 -2
  60. package/dist_ts/interfaces.lifecycle.d.ts +3 -0
  61. package/dist_ts_authswitch/classes.authoritydaemon.js +6 -1
  62. package/dist_ts_interfaces/devices.d.ts +331 -0
  63. package/dist_ts_interfaces/devices.js +34 -0
  64. package/dist_ts_interfaces/index.d.ts +2 -0
  65. package/dist_ts_interfaces/index.js +3 -1
  66. package/dist_ts_interfaces/interfaces.d.ts +5 -3
  67. package/dist_ts_interfaces/interfaces.js +3 -2
  68. package/dist_ts_interfaces/management.d.ts +2 -2
  69. package/dist_ts_interfaces/management.js +2 -2
  70. package/dist_ts_interfaces/mcprequests.d.ts +1 -1
  71. package/dist_ts_interfaces/requests.d.ts +2 -2
  72. package/dist_ts_interfaces/requests.devices.d.ts +89 -0
  73. package/dist_ts_interfaces/requests.devices.js +2 -0
  74. package/dist_ts_interfaces/requests.upgrade.d.ts +0 -12
  75. package/package.json +12 -11
  76. package/readme.md +116 -24
  77. package/ts/00_commitinfo_data.ts +1 -1
  78. package/ts/aglapi/aglapisurface.document.ts +3 -3
  79. package/ts/classes.authmodels.ts +68 -25
  80. package/ts/classes.authstore.ts +183 -24
  81. package/ts/classes.cli.ts +38 -61
  82. package/ts/classes.config.ts +70 -21
  83. package/ts/classes.controller.ts +746 -329
  84. package/ts/classes.controllerdevices.ts +469 -0
  85. package/ts/classes.controllermanagement.ts +123 -49
  86. package/ts/classes.controllerservice.ts +3 -56
  87. package/ts/classes.mcpcontrollerhost.ts +10 -5
  88. package/ts/classes.processinspection.ts +84 -3
  89. package/ts/classes.proxycontrol.ts +2 -0
  90. package/ts/classes.proxydaemon.ts +3 -2
  91. package/ts/classes.proxystore.ts +4 -25
  92. package/ts/classes.sessionfeed.ts +13 -1
  93. package/ts/classes.tpmsecretcodec.ts +26 -0
  94. package/ts/classes.trustedproxysecrets.ts +317 -0
  95. package/ts/classes.upgradecoordinator.ts +33 -3
  96. package/ts/classes.upgradetransaction.ts +130 -1
  97. package/ts/classes.validation.ts +1 -1
  98. package/ts/functions.controlleradmission.ts +168 -0
  99. package/ts/functions.deviceapi.ts +295 -0
  100. package/ts/functions.deviceproof.ts +118 -0
  101. package/ts/functions.remotecli.ts +270 -0
  102. package/ts/functions.retiredenvironment.ts +25 -3
  103. package/ts/index.ts +1 -0
  104. package/ts/interfaces.auth.ts +41 -3
  105. package/ts/interfaces.config.ts +15 -0
  106. package/ts/interfaces.lifecycle.ts +3 -0
  107. package/ts_authswitch/classes.authoritydaemon.ts +2 -0
  108. package/ts_interfaces/devices.schema.json +890 -0
  109. package/ts_interfaces/devices.ts +392 -0
  110. package/ts_interfaces/index.ts +2 -0
  111. package/ts_interfaces/interfaces.ts +5 -2
  112. package/ts_interfaces/management.ts +2 -2
  113. package/ts_interfaces/mcprequests.ts +1 -1
  114. package/ts_interfaces/requests.devices.ts +95 -0
  115. package/ts_interfaces/requests.ts +10 -10
  116. package/ts_interfaces/requests.upgrade.ts +0 -16
  117. package/ts_web/00_commitinfo_data.ts +1 -1
  118. package/ts_web/classes.controllersocketclient.ts +27 -0
  119. package/ts_web/elements.devices.ts +752 -0
  120. package/ts_web/elements.harnesscontrollerapp.ts +18 -1
@@ -16,6 +16,7 @@ import {
16
16
  } from '../ts_interfaces/index.js';
17
17
  import {
18
18
  controllerProcessIdentityIsLive,
19
+ installedControllerHoldsPort,
19
20
  signalVerifiedControllerProcess,
20
21
  } from './classes.processinspection.js';
21
22
  import { AglControllerService } from './classes.controllerservice.js';
@@ -38,8 +39,13 @@ export const isControllerManagementProtocolVersion = (valueArg: unknown): valueA
38
39
  typeof valueArg === 'number' && Number.isSafeInteger(valueArg) && valueArg >= 1
39
40
  );
40
41
 
41
- /** A controller a management call is addressed to, by what it declares about its management endpoint. */
42
+ /**
43
+ * A controller a management call is addressed to: what it declares about its management endpoint, which decides
44
+ * whether this build manages it at all, and its package and version, which only a refusal names.
45
+ */
42
46
  export interface IControllerManagementTarget {
47
+ packageName: string;
48
+ packageVersion: string;
43
49
  /**
44
50
  * The management protocol the controller declares (`controllerManagementProtocolVersion`): the status's own
45
51
  * `managementProtocolVersion` once the controller runs, or what the installed package that runs it declares in
@@ -48,6 +54,51 @@ export interface IControllerManagementTarget {
48
54
  managementProtocolVersion?: number;
49
55
  }
50
56
 
57
+ /**
58
+ * The oldest management protocol this build manages a controller through. It speaks only the loopback management
59
+ * endpoint: a controller that declares none (AGL 41.0.x or older, reachable only over its TypedSocket 8 socket, which
60
+ * this build cannot speak) is refused by name, never reached some other way.
61
+ */
62
+ export const controllerManagementProtocolFloor = 1;
63
+
64
+ /** What a refusal of an older controller tells its operator, in the CLI and in the upgrade log. */
65
+ export const controllerManagementUpgradeOrder = 'AGL 41.0.x or older must upgrade to 41.1.x before 42.';
66
+
67
+ /** The first release whose controller has the loopback management endpoint. */
68
+ export const controllerManagementEndpointRelease = '41.1.0';
69
+
70
+ /** Whether a declaration is one this build manages a controller through. */
71
+ export const isManagedControllerManagementProtocol = (valueArg: unknown): boolean => (
72
+ isControllerManagementProtocolVersion(valueArg) && valueArg >= controllerManagementProtocolFloor
73
+ );
74
+
75
+ /**
76
+ * The controller a refusal names: a release by package and version, when its declaration or an upgrade transaction
77
+ * names one, or only the port, when nothing does.
78
+ */
79
+ export type TUnmanagedController =
80
+ | Pick<IControllerManagementTarget, 'packageName' | 'packageVersion'>
81
+ | { port: number };
82
+
83
+ /** The refusal of a controller this build cannot manage; its version appears only here. */
84
+ export const unmanagedControllerError = (controllerArg: TUnmanagedController): ControllerManagementError => (
85
+ new ControllerManagementError(
86
+ 'port' in controllerArg
87
+ ? `The controller on port ${controllerArg.port} has no loopback management endpoint: it is a controller older `
88
+ + `than ${controllerManagementEndpointRelease}, which this release does not manage. ${controllerManagementUpgradeOrder}`
89
+ : `The controller of ${controllerArg.packageName}@${controllerArg.packageVersion} predates the loopback management `
90
+ + `endpoint, through which alone this release manages a controller. ${controllerManagementUpgradeOrder}`,
91
+ 'management_protocol_unsupported',
92
+ )
93
+ );
94
+
95
+ /** Refuses, by name, a controller whose declaration this build cannot manage it through. */
96
+ export const assertManagedController = (targetArg: IControllerManagementTarget): void => {
97
+ if (!isManagedControllerManagementProtocol(targetArg.managementProtocolVersion)) {
98
+ throw unmanagedControllerError(targetArg);
99
+ }
100
+ };
101
+
51
102
  /**
52
103
  * A status read: the identity the status must report, and the declaration that decides how it is read, which comes
53
104
  * from the package expected to run the controller, since there is no status yet. Every caller states it, absent
@@ -55,6 +106,13 @@ export interface IControllerManagementTarget {
55
106
  */
56
107
  export interface IControllerStatusQuery extends IControllerStatusExpectation {
57
108
  managementProtocolVersion: number | undefined;
109
+ /**
110
+ * Where `packageName` and `packageVersion` come from: an upgrade transaction (`transaction`), which names the
111
+ * release it left on the port, or only the caller's own expectation (`caller`, the CLI's own build). A controller
112
+ * without the management endpoint is refused by the transaction's release, or else as one older than
113
+ * `controllerManagementEndpointRelease`, never by a version nobody read from it.
114
+ */
115
+ identity: 'transaction' | 'caller';
58
116
  }
59
117
 
60
118
  const harnessStates = ['starting', 'ready', 'stopping', 'stopped', 'failed'] as const;
@@ -224,19 +282,6 @@ export const assertControllerStatus = (
224
282
  return readableStatus as IControllerStatus;
225
283
  };
226
284
 
227
- /**
228
- * How this build reaches a controller's management methods, decided by the controller's declaration alone: over its
229
- * loopback management endpoint when it declares a management protocol, which every later release keeps whatever
230
- * transport its browser speaks, and over its controller socket when it declares none (a release older than the
231
- * endpoint, such as a source an upgrade recovery restores), which serves them in every release up to this one.
232
- * There is no second attempt over the other transport.
233
- */
234
- export const controllerManagementTransport = (
235
- targetArg: IControllerManagementTarget,
236
- ): 'http' | 'socket' => (
237
- isControllerManagementProtocolVersion(targetArg.managementProtocolVersion) ? 'http' : 'socket'
238
- );
239
-
240
285
  /** A management request the controller answered with an error, or whose answer this build cannot read. */
241
286
  export class ControllerManagementError extends Error {
242
287
  constructor(messageArg: string, public readonly code?: string) {
@@ -281,6 +326,12 @@ const readControllerManagementAnswer = (
281
326
  throw new ControllerManagementError(text, code);
282
327
  };
283
328
 
329
+ /**
330
+ * The `Allow` header of the 405 with which the controller of every release before the loopback management endpoint
331
+ * refuses a `POST` (its `requestAdmission` admits only GET and HEAD; TypedServer 14.2.1 sends the header as given).
332
+ */
333
+ const preEndpointControllerAllow = 'GET, HEAD';
334
+
284
335
  /** Sends one envelope to the management endpoint on 127.0.0.1 and returns the answer's bytes. */
285
336
  const postControllerManagementEnvelope = (
286
337
  portArg: number,
@@ -305,6 +356,16 @@ const postControllerManagementEnvelope = (
305
356
  }, (responseArg) => {
306
357
  const statusCode = responseArg.statusCode ?? 0;
307
358
  const contentType = responseArg.headers['content-type'];
359
+ if (statusCode === 405 && responseArg.headers.allow === preEndpointControllerAllow) {
360
+ // The endpoint takes every POST it admits, so this is a listener without it that answers as the controller of a
361
+ // release older than the endpoint does; whether it is one, `requestManagedController` confirms.
362
+ responseArg.resume();
363
+ reject(new ControllerManagementError(
364
+ 'The listener answered as a controller without the loopback management endpoint.',
365
+ 'management_endpoint_missing',
366
+ ));
367
+ return;
368
+ }
308
369
  if (statusCode !== 200) {
309
370
  responseArg.resume();
310
371
  reject(new ControllerManagementError(
@@ -384,41 +445,38 @@ export const requestControllerManagement = async <T extends TControllerManagemen
384
445
  return readControllerManagementAnswer(answer, identity);
385
446
  };
386
447
 
387
- /** Fires one management request over the controller socket, for a controller that declares no management protocol. */
388
- const requestControllerManagementOverSocket = async <T extends TControllerManagementTypedRequest>(
448
+ /**
449
+ * Sends one management request to a controller that declares the endpoint. One that turns out to have none is
450
+ * refused as `refusalArg` names it: by default the target, whose identity is what it declared or reported.
451
+ *
452
+ * It has none only when both signs of an older controller hold: the listener refused the `POST` with that
453
+ * controller's 405 (`Allow: GET, HEAD`), and the process holding the port's listening socket runs an installed AGL
454
+ * package's controller for this port (`installedControllerHoldsPort`). Any other listener, a 405 of its own included,
455
+ * is a refusal of HTTP (`http_refused`), which callers treat as a busy port, never as an AGL controller.
456
+ */
457
+ const requestManagedController = async <T extends TControllerManagementTypedRequest>(
389
458
  portArg: number,
459
+ controllerArg: IControllerManagementTarget,
390
460
  methodArg: T['method'],
391
461
  requestArg: T['request'],
392
462
  optionsArg: { timeoutMs: number },
463
+ refusalArg: TUnmanagedController = controllerArg,
393
464
  ): Promise<unknown> => {
394
- const signal = AbortSignal.timeout(optionsArg.timeoutMs);
395
- let socket: plugins.typedsocket.TypedSocket | undefined;
465
+ assertManagedController(controllerArg);
396
466
  try {
397
- socket = await plugins.typedsocket.TypedSocket.createClient(
398
- new plugins.typedrequest.TypedRouter(),
399
- `http://127.0.0.1:${portArg}`,
400
- { autoReconnect: false, maxRetries: 0, abortSignal: signal },
401
- );
402
- return await socket.createTypedRequest<T>(
403
- methodArg,
404
- undefined,
405
- { timeoutMs: optionsArg.timeoutMs, abortSignal: signal },
406
- ).fire(requestArg);
407
- } finally {
408
- await socket?.stop();
467
+ return await requestControllerManagement<T>(portArg, methodArg, requestArg, optionsArg);
468
+ } catch (errorArg) {
469
+ if (errorArg instanceof ControllerManagementError && errorArg.code === 'management_endpoint_missing') {
470
+ if (await installedControllerHoldsPort(portArg)) throw unmanagedControllerError(refusalArg);
471
+ throw new ControllerManagementError(
472
+ 'The controller refused the management request with HTTP 405.',
473
+ 'http_refused',
474
+ );
475
+ }
476
+ throw errorArg;
409
477
  }
410
478
  };
411
479
 
412
- const requestManagedController = <T extends TControllerManagementTypedRequest>(
413
- portArg: number,
414
- controllerArg: IControllerManagementTarget,
415
- methodArg: T['method'],
416
- requestArg: T['request'],
417
- optionsArg: { timeoutMs: number },
418
- ): Promise<unknown> => controllerManagementTransport(controllerArg) === 'http'
419
- ? requestControllerManagement<T>(portArg, methodArg, requestArg, optionsArg)
420
- : requestControllerManagementOverSocket<T>(portArg, methodArg, requestArg, optionsArg);
421
-
422
480
  export const requestControllerUpgradePrepareBegin = async (
423
481
  portArg: number,
424
482
  controllerArg: IControllerManagementTarget,
@@ -474,24 +532,40 @@ export const queryControllerStatus = async (
474
532
  portArg: number,
475
533
  queryArg: IControllerStatusQuery,
476
534
  timeoutMsArg = 2_500,
477
- ): Promise<IControllerStatus> => assertControllerStatus(
478
- await requestManagedController<IReq_ControllerStatus>(
479
- portArg,
535
+ ): Promise<IControllerStatus> => {
536
+ const status = assertControllerStatus(
537
+ await requestManagedController<IReq_ControllerStatus>(
538
+ portArg,
539
+ queryArg,
540
+ 'controller.status',
541
+ {},
542
+ { timeoutMs: timeoutMsArg },
543
+ queryArg.identity === 'transaction' ? queryArg : { port: portArg },
544
+ ),
480
545
  queryArg,
481
- 'controller.status',
482
- {},
483
- { timeoutMs: timeoutMsArg },
484
- ),
485
- queryArg,
546
+ );
547
+ // From here on the controller's own report is the gate, whatever its package declared before it ran.
548
+ assertManagedController(status);
549
+ return status;
550
+ };
551
+
552
+ /** Whether `errorArg` refuses a controller this build cannot manage, which is there all the same. */
553
+ export const isUnmanagedControllerError = (errorArg: unknown): errorArg is ControllerManagementError => (
554
+ errorArg instanceof ControllerManagementError && errorArg.code === 'management_protocol_unsupported'
486
555
  );
487
556
 
557
+ /**
558
+ * Reads the status of a controller if one this build manages answers; `undefined` when none answers or its answer is
559
+ * not one. A controller that answers but that this build cannot manage is refused, never reported as absent.
560
+ */
488
561
  export const tryQueryControllerStatus = async (
489
562
  portArg: number,
490
563
  queryArg: IControllerStatusQuery,
491
564
  ): Promise<IControllerStatus | undefined> => {
492
565
  try {
493
566
  return await queryControllerStatus(portArg, queryArg);
494
- } catch {
567
+ } catch (errorArg) {
568
+ if (isUnmanagedControllerError(errorArg)) throw errorArg;
495
569
  return undefined;
496
570
  }
497
571
  };
@@ -1,8 +1,7 @@
1
1
  import * as plugins from './plugins.js';
2
2
  import { controllerServiceUnitName } from '../ts_interfaces/index.js';
3
3
  import { resolveAGLHomePaths } from './classes.aglhome.js';
4
- import { trustedProxySecretEnvironmentVariable } from './classes.config.js';
5
- import type { IControllerRuntimeOverrides, TControllerTlsMode } from './interfaces.config.js';
4
+ import type { IControllerRuntimeOverrides } from './interfaces.config.js';
6
5
  import { prepareControllerLogFile, resolveControllerLogFilePath } from './functions.controllerlog.js';
7
6
 
8
7
  /**
@@ -15,50 +14,6 @@ export const controllerServiceStandDownExitStatus = 78;
15
14
  /** The oldest systemd whose manager reports the loaded `ExecStart` and environment a start is matched against. */
16
15
  export const controllerServiceManagerMajor = 254;
17
16
 
18
- /**
19
- * The unit cannot run a controller behind a TLS proxy: that controller needs `AGL_TRUSTED_PROXY_SECRET`, and the
20
- * unit carries no secret (its file is readable by every local user). `remedy` says how to run it detached instead.
21
- */
22
- export class ControllerServiceTlsProxyRefusal extends Error {
23
- constructor(remedyArg: string) {
24
- super(`${controllerServiceUnitName} cannot run AGL behind a TLS proxy: the unit cannot carry `
25
- + `${trustedProxySecretEnvironmentVariable}, which that mode needs. ${remedyArg}`);
26
- this.name = 'ControllerServiceTlsProxyRefusal';
27
- }
28
- }
29
-
30
- /**
31
- * Whether a failed start was refused because the unit cannot run this controller: the refusal itself, or one inside
32
- * the `AggregateError` a start throws when releasing its resources failed as well. Such a start fails the same way
33
- * every time, so the unit stands down rather than being restarted.
34
- */
35
- export const isControllerServiceTlsProxyRefusal = (errorArg: unknown): boolean =>
36
- errorArg instanceof ControllerServiceTlsProxyRefusal
37
- || (errorArg instanceof AggregateError && errorArg.errors.some(isControllerServiceTlsProxyRefusal));
38
-
39
- /** What to do when a controller behind a TLS proxy meets an installed unit: leave the unit and run detached. */
40
- export const controllerServiceDetachedRemedy = 'Run it detached: `agl service uninstall`, then `agl start`.';
41
- /** What to do when `agl service install` refuses such a controller: nothing was written, so run it detached. */
42
- export const controllerServiceInstallRemedy = 'Run it detached with `agl start`.';
43
-
44
- /**
45
- * Refuses a controller the unit could not run: one whose stored runtime configuration is in TLS-proxy mode, or
46
- * startup options that ask for that mode (`--behind-tls-proxy`, or an HTTPS `--public-origin`, which only that
47
- * mode serves).
48
- */
49
- export const assertControllerServiceTlsMode = (optionsArg: {
50
- requested?: Pick<IControllerRuntimeOverrides, 'behindTlsProxy' | 'publicOrigin'>;
51
- stored?: TControllerTlsMode;
52
- remedy: string;
53
- }): void => {
54
- const origin = optionsArg.requested?.publicOrigin;
55
- if (
56
- optionsArg.stored === 'proxy'
57
- || optionsArg.requested?.behindTlsProxy === true
58
- || (origin !== undefined && URL.canParse(origin) && new URL(origin).protocol === 'https:')
59
- ) throw new ControllerServiceTlsProxyRefusal(optionsArg.remedy);
60
- };
61
-
62
17
  /** The startup options the unit's `ExecStart` carries; `--port` aside, every one is optional. */
63
18
  export type IControllerServiceOverrides = Omit<IControllerRuntimeOverrides, 'controllerPort' | 'initialProjectDirectory'>;
64
19
 
@@ -163,7 +118,6 @@ export class ControllerServiceEnvironmentRefusal extends Error {
163
118
  */
164
119
  export const controllerServiceEnvironment = (environmentArg: NodeJS.ProcessEnv): Record<string, string> => {
165
120
  const secrets: string[] = [];
166
- if (environmentArg[trustedProxySecretEnvironmentVariable]) secrets.push(trustedProxySecretEnvironmentVariable);
167
121
  for (const name of urlVariables) {
168
122
  const value = environmentArg[name];
169
123
  if (value && urlCarriesCredential(value)) secrets.push(name);
@@ -287,7 +241,6 @@ export class AglControllerService {
287
241
  if (!Number.isInteger(optionsArg.port) || optionsArg.port < 1 || optionsArg.port > 65_535) {
288
242
  throw new Error('The controller port must be 1 to 65535.');
289
243
  }
290
- assertControllerServiceTlsMode({ requested: optionsArg.overrides, remedy: controllerServiceInstallRemedy });
291
244
  return new plugins.smartdaemon.SystemdServiceDefinition({
292
245
  unitName: controllerServiceUnitName,
293
246
  scope: 'user',
@@ -404,9 +357,8 @@ export type TControllerStartRoute =
404
357
  /**
405
358
  * The unit runs the controller only for the AGL home and port it was installed for. A start for that home and
406
359
  * port with startup options or environment the unit does not carry is refused rather than run with settings
407
- * other than the ones asked for; every other home and port starts detached. A start asking for the TLS-proxy mode,
408
- * or from a shell holding its secret, is refused: the unit cannot carry that secret. An upgrade's restart passes
409
- * no `environment`: it restarts the controller as installed, from inside the unit's own environment.
360
+ * other than the ones asked for; every other home and port starts detached. An upgrade's restart passes no
361
+ * `environment`: it restarts the controller as installed, from inside the unit's own environment.
410
362
  */
411
363
  export const resolveControllerStartRoute = async (optionsArg: {
412
364
  service: AglControllerService;
@@ -426,11 +378,6 @@ export const resolveControllerStartRoute = async (optionsArg: {
426
378
  if (installed.aglHome !== optionsArg.aglHome || installed.execStart.port !== optionsArg.port) {
427
379
  return { kind: 'detached' };
428
380
  }
429
- assertControllerServiceTlsMode({ requested: optionsArg.requested, remedy: controllerServiceDetachedRemedy });
430
- if (optionsArg.environment?.[trustedProxySecretEnvironmentVariable]) {
431
- throw new ControllerServiceTlsProxyRefusal('To start the unit, unset it in this shell; to run behind a TLS proxy, '
432
- + 'run it detached: `agl service uninstall`, then `agl start`.');
433
- }
434
381
  const differences: string[] = [];
435
382
  for (const key of Object.keys(overrideFlags) as Array<keyof IControllerServiceOverrides>) {
436
383
  const requested = optionsArg.requested[key];
@@ -189,7 +189,7 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
189
189
  >(
190
190
  typedRequestArg: T,
191
191
  optionsArg: plugins.typedrequest.ITypedRouterRouteOptions = {},
192
- ): Promise<T> {
192
+ ): Promise<T | null> {
193
193
  // The envelope is the only place the caller credential exists; typed handlers never see it.
194
194
  // Resolving here once and running the handler inside the caller context keeps authorization
195
195
  // out of every handler signature and needs no per-request bookkeeping to clean up.
@@ -201,7 +201,7 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
201
201
  : { caller: this.resolveCaller(wireCaller?.credential), release: () => undefined };
202
202
  const caller = lease.caller.subject.kind === 'worker_launch' && !typedRequestArg.method.startsWith('agl.mcp.ticket.')
203
203
  ? anonymousControllerMcpCaller : lease.caller;
204
- let response: T;
204
+ let response: T | null;
205
205
  try {
206
206
  response = await runWithControllerMcpCaller(caller, async () => super.routeAndAddResponse(typedRequestArg, optionsArg));
207
207
  } finally {
@@ -221,17 +221,22 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
221
221
  callerArg: IControllerMcpCaller,
222
222
  typedRequestArg: T,
223
223
  optionsArg: plugins.typedrequest.ITypedRouterRouteOptions,
224
- ): Promise<T> {
224
+ ): Promise<T | null> {
225
225
  return this.bounded(await runWithControllerMcpCaller(
226
226
  callerArg, async () => super.routeAndAddResponse(typedRequestArg, optionsArg),
227
227
  ));
228
228
  }
229
229
 
230
230
  /** What every answer leaves out and how large it may be. */
231
- private bounded<T extends plugins.typedrequestInterfaces.ITypedRequest>(response: T): T {
232
- if (!response) return response;
231
+ private bounded<T extends plugins.typedrequestInterfaces.ITypedRequest>(response: T | null): T | null {
232
+ // Nothing to answer: the router refused the envelope, and the transport refuses it in its own protocol.
233
+ if (response === null) return null;
233
234
  // A credential is never reflected back: the response carries exactly its documented keys.
234
235
  delete (response as unknown as Record<string, unknown>).caller;
236
+ // The private envelope's `response` is always an object, beside an `error` too: every `agl mcp` client sends `{}`
237
+ // and refuses anything else, an installed older one included. TypedRequest 9 answers `null` where TypedRequest 8
238
+ // returned the `{}` the client sent (an error, a handler that returns nothing), so the host restores it here.
239
+ if (response.response === null) (response as plugins.typedrequestInterfaces.ITypedRequest).response = {};
235
240
  if (typeof response.method === 'string' && (response.method.startsWith('agl.mcp.configuration.')
236
241
  || response.method.startsWith('agl.mcp.ticket.') || response.method.startsWith('agl.mcp.fleet.'))) {
237
242
  // TypedRequest normally echoes the request. The owner capability must not travel back in
@@ -20,7 +20,7 @@ export interface IControllerProcessCandidate {
20
20
  }
21
21
 
22
22
  export interface IControllerDataWriterProcessCandidate {
23
- kind: 'controller' | 'temp-password' | 'config' | 'flex-child';
23
+ kind: 'controller' | 'temp-password' | 'config' | 'remote' | 'flex-child';
24
24
  identity: IControllerProcessIdentity;
25
25
  /** Absolute path of the package entry file the process runs (`cli.js` or the Flex child). */
26
26
  entryPath: string;
@@ -461,7 +461,7 @@ const findInstalledControllerPackageFileArgument = (
461
461
  /** The installed `cli.js` path of a process running the given command, if any. */
462
462
  const findInstalledControllerCommandEntryPath = (
463
463
  identityArg: IControllerProcessIdentity,
464
- commandArg: '__serve' | 'foreground' | 'temp-password' | 'config',
464
+ commandArg: '__serve' | 'foreground' | 'temp-password' | 'config' | 'remote',
465
465
  ): string | undefined => {
466
466
  const cli = findInstalledControllerPackageFileArgument(identityArg.arguments, 'cli.js');
467
467
  return cli && identityArg.arguments[cli.index + 1] === commandArg ? cli.path : undefined;
@@ -595,6 +595,87 @@ export const listControllerProcessesForCli = async (
595
595
  return candidates.sort((leftArg, rightArg) => leftArg.identity.pid - rightArg.identity.pid);
596
596
  };
597
597
 
598
+ /**
599
+ * The listening sockets (their inodes) that take a connection to `127.0.0.1:<port>` on the addresses a controller
600
+ * binds: `127.0.0.1` in local mode and `0.0.0.0` behind a TLS proxy, as every release before the loopback management
601
+ * endpoint does.
602
+ */
603
+ const readLinuxControllerListenerInodes = async (portArg: number): Promise<Set<string>> => {
604
+ const port = portArg.toString(16).toUpperCase().padStart(4, '0');
605
+ const endpoints = new Set([`0100007F:${port}`, `00000000:${port}`]);
606
+ const inodes = new Set<string>();
607
+ for (const line of (await plugins.fs.promises.readFile('/proc/net/tcp', 'utf8')).split('\n').slice(1)) {
608
+ const fields = line.trim().split(/\s+/u);
609
+ if (fields.length >= 10 && endpoints.has(fields[1]) && fields[3] === '0A') inodes.add(fields[9]);
610
+ }
611
+ return inodes;
612
+ };
613
+
614
+ /** Whether the Linux process holds one of the sockets `inodesArg` names; one it cannot inspect holds none. */
615
+ const linuxProcessHoldsSocket = async (pidArg: number, inodesArg: ReadonlySet<string>): Promise<boolean> => {
616
+ const directory = `/proc/${pidArg}/fd`;
617
+ let names: string[];
618
+ try {
619
+ names = await plugins.fs.promises.readdir(directory);
620
+ } catch (errorArg) {
621
+ if (['ENOENT', 'ESRCH', 'EACCES', 'EPERM'].includes((errorArg as NodeJS.ErrnoException).code ?? '')) return false;
622
+ throw errorArg;
623
+ }
624
+ for (const name of names) {
625
+ let target: string;
626
+ try {
627
+ target = await plugins.fs.promises.readlink(plugins.path.join(directory, name));
628
+ } catch (errorArg) {
629
+ const code = (errorArg as NodeJS.ErrnoException).code ?? '';
630
+ if (code === 'ENOENT') continue;
631
+ if (['ESRCH', 'EACCES', 'EPERM'].includes(code)) return false;
632
+ throw errorArg;
633
+ }
634
+ const socket = /^socket:\[(\d+)\]$/u.exec(target);
635
+ if (socket && inodesArg.has(socket[1])) return true;
636
+ }
637
+ return false;
638
+ };
639
+
640
+ /** Whether the macOS process listens on `127.0.0.1:<port>` or on every address of that port. */
641
+ const macOsProcessHoldsControllerListener = async (pidArg: number, portArg: number): Promise<boolean> => {
642
+ const output = await execFile('/usr/sbin/lsof', [
643
+ '-nP', '-a', '-p', String(pidArg), `-iTCP:${portArg}`, '-sTCP:LISTEN', '-Fpn',
644
+ ], { emptyExitCode: 1, timeoutMs: 1_500, maxBufferBytes: 64 * 1024 });
645
+ const lines = output.split(/\r?\n/u);
646
+ return lines.includes(`p${pidArg}`)
647
+ && lines.some((lineArg) => lineArg === `n127.0.0.1:${portArg}` || lineArg === `n*:${portArg}`);
648
+ };
649
+
650
+ /**
651
+ * Whether the listener on `127.0.0.1:<port>` is an AGL controller of an installed package: a process running that
652
+ * package's `cli.js` (`node_modules/agl` or `node_modules/@modelprofile.com/harness-controller`) with the controller
653
+ * command (`__serve`, also as the service unit runs it, or `foreground`) for this port holds the port's listening
654
+ * socket. Linux reads the kernel's socket table, macOS asks `lsof`; elsewhere nothing confirms it.
655
+ */
656
+ export const installedControllerHoldsPort = async (portArg: number): Promise<boolean> => {
657
+ if (process.platform !== 'linux' && process.platform !== 'darwin') return false;
658
+ const candidates: number[] = [];
659
+ await visitProcessIdentities(async (identity) => {
660
+ const runsController = findInstalledControllerCommandEntryPath(identity, '__serve') !== undefined
661
+ || findInstalledControllerCommandEntryPath(identity, 'foreground') !== undefined;
662
+ if (runsController && readEffectiveArgvPort(identity.arguments) === portArg) candidates.push(identity.pid);
663
+ });
664
+ if (candidates.length === 0) return false;
665
+ if (process.platform === 'darwin') {
666
+ for (const pid of candidates) {
667
+ if (await macOsProcessHoldsControllerListener(pid, portArg)) return true;
668
+ }
669
+ return false;
670
+ }
671
+ const inodes = await readLinuxControllerListenerInodes(portArg);
672
+ if (inodes.size === 0) return false;
673
+ for (const pid of candidates) {
674
+ if (await linuxProcessHoldsSocket(pid, inodes)) return true;
675
+ }
676
+ return false;
677
+ };
678
+
598
679
  export const listControllerDataWriterProcessesForCli = async (
599
680
  cliPathArg: string,
600
681
  optionsArg: { includeInstalledPackagePaths?: boolean } = {},
@@ -631,7 +712,7 @@ export const listControllerDataWriterProcessesForCliPaths = async (
631
712
  ?? findInstalledControllerCommandEntryPath(identityArg, 'foreground');
632
713
  if (installedEntryPath) return { kind: 'controller', entryPath: installedEntryPath };
633
714
  }
634
- for (const command of ['temp-password', 'config'] as const) {
715
+ for (const command of ['temp-password', 'config', 'remote'] as const) {
635
716
  for (const cliPath of cliPaths) {
636
717
  if (await processIdentityHasCliCommand(identityArg, cliPath, command)) {
637
718
  return { kind: command, entryPath: cliPath };
@@ -421,6 +421,8 @@ export class AglProxyControlServer {
421
421
  void this.router.routeAndAddResponse(request, { trustedLocalData: { proxyConnection: connection },
422
422
  trustedAbortSignal: abort.signal })
423
423
  .then((responseArg) => {
424
+ // Nothing to answer (the router refused the envelope): refuse it by closing, never with a bare null.
425
+ if (responseArg === null) { socketArg.destroy(); return; }
424
426
  if (!socketArg.writable) return;
425
427
  const raw = JSON.stringify(responseArg) + '\n';
426
428
  if (Buffer.byteLength(raw) > proxyControlMaximumFrameBytes) { socketArg.destroy(); return; }
@@ -10,7 +10,8 @@ import { aglProxyConfinedClient, aglProxyConfinedOrigin, proxyClientNamePattern,
10
10
  import { AglProxyControlRefusal, AglProxyControlServer, type IAglProxyConfinedGrant, type IAglProxyConnection,
11
11
  type IAglProxyStatus, type IReq_AglProxyCatalog, type IReq_AglProxyConfined, type IReq_AglProxyReclaim,
12
12
  type TAglProxyLeaseCatalog } from './classes.proxycontrol.js';
13
- import { ProxyStore, type IProxySecretCodec } from './classes.proxystore.js';
13
+ import { ProxyStore } from './classes.proxystore.js';
14
+ import type { ITpmSecretCodec } from './classes.tpmsecretcodec.js';
14
15
  import type { InferenceUsageStore } from './classes.inferenceusage.js';
15
16
  import { prepareProxyCredentialDirectory, proxyCredentialFile, removeProxyCredentialFile,
16
17
  writeProxyCredentialFile } from './functions.proxycredentialfile.js';
@@ -23,7 +24,7 @@ export interface IAglProxyDaemonOptions {
23
24
  credentialDirectory: string;
24
25
  authorityPaths: IAuthoritySocketPaths;
25
26
  port: number;
26
- codec?: IProxySecretCodec;
27
+ codec?: ITpmSecretCodec;
27
28
  providerFetch?: typeof fetch;
28
29
  limits?: IInferenceGatewayLimits;
29
30
  /** How long a lease kept across a restart waits for its holder; `proxyLeaseReclaimWindowMs` unless a test shortens it. */
@@ -1,5 +1,6 @@
1
1
  import * as plugins from './plugins.js';
2
2
  import { EmbeddedControllerDatabase } from './classes.embeddeddb.js';
3
+ import { TpmSecretCodec, type ITpmSecretCodec } from './classes.tpmsecretcodec.js';
3
4
  import { InferenceAffinityModel, InferenceAffinityStore } from './classes.inferenceaffinity.js';
4
5
  import { InferenceSessionModel } from './classes.inferencesession.js';
5
6
  import { InferencePlacementModel, InferencePlacementStore } from './classes.inferenceplacement.js';
@@ -22,28 +23,6 @@ const maximumProxyClients = 32;
22
23
  /** The gateway admits 128 credentials in all; no more leases than that are ever kept. */
23
24
  const maximumProxyLeases = 128;
24
25
 
25
- /** Seals a client credential for this user and machine; only its ciphertext is stored. */
26
- export interface IProxySecretCodec {
27
- seal(nameArg: string, plaintextArg: Uint8Array): Promise<string>;
28
- unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array>;
29
- }
30
-
31
- /** TPM2-sealed credentials through `systemd-creds`, bound to their name. */
32
- export class ProxyTpmSecretCodec implements IProxySecretCodec {
33
- public async seal(nameArg: string, plaintextArg: Uint8Array): Promise<string> {
34
- const ciphertext = await plugins.smartsecret.sealSmartSecretTpm2Credential(nameArg, plaintextArg);
35
- try { return Buffer.from(ciphertext).toString('base64url'); }
36
- finally { ciphertext.fill(0); }
37
- }
38
-
39
- public async unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array> {
40
- if (!/^[A-Za-z0-9_-]{1,131072}$/.test(ciphertextArg)) throw new Error('Invalid sealed proxy credential.');
41
- const bytes = Buffer.from(ciphertextArg, 'base64url');
42
- try { return await plugins.smartsecret.unsealSmartSecretTpm2Credential(nameArg, bytes); }
43
- finally { bytes.fill(0); }
44
- }
45
- }
46
-
47
26
  export interface IProxyIdentityDocument {
48
27
  id: string;
49
28
  /** Names this proxy's authority bindings; independent of its port, so a restart reclaims exactly its own. */
@@ -215,7 +194,7 @@ let activeOwner: object | undefined;
215
194
  export interface IProxyStoreOptions {
216
195
  /** The proxy's own NoSQLDB storage root; no other process opens it. */
217
196
  dataDirectory: string;
218
- codec?: IProxySecretCodec;
197
+ codec?: ITpmSecretCodec;
219
198
  }
220
199
 
221
200
  export interface IProxyIdentity {
@@ -250,7 +229,7 @@ export interface IProxyLease {
250
229
  * is refused, so stopping the proxy never stops an engine someone else uses.
251
230
  */
252
231
  export class ProxyStore {
253
- private readonly codec: IProxySecretCodec;
232
+ private readonly codec: ITpmSecretCodec;
254
233
  private readonly owner = {};
255
234
  private embedded?: EmbeddedControllerDatabase;
256
235
  private database?: plugins.nosqldb.SmartdataDb;
@@ -258,7 +237,7 @@ export class ProxyStore {
258
237
 
259
238
  constructor(private readonly options: IProxyStoreOptions) {
260
239
  if (!plugins.path.isAbsolute(options.dataDirectory)) throw new Error('The proxy data directory must be absolute.');
261
- this.codec = options.codec ?? new ProxyTpmSecretCodec();
240
+ this.codec = options.codec ?? new TpmSecretCodec();
262
241
  }
263
242
 
264
243
  public async open(): Promise<IProxyIdentity> {
@@ -138,7 +138,7 @@ const threadName = (titleArg: string): string => {
138
138
  * A row's id. A Codex session is named by its raw thread id: the fleet and the shared app-server name it so, while a
139
139
  * tracked conversation carries the id qualified with its connection profile.
140
140
  */
141
- const sessionFeedRowId = (harnessArg: TControllerSessionFeedHarness, nativeIdArg: string): string => (
141
+ export const sessionFeedRowId = (harnessArg: TControllerSessionFeedHarness, nativeIdArg: string): string => (
142
142
  `${harnessArg}:${harnessArg === 'codex' ? codexQualifiedIdentity(nativeIdArg)?.rawThreadId ?? nativeIdArg : nativeIdArg}`
143
143
  );
144
144
  const workerState = (viewArg: IControllerFleetWorkerView): TControllerSessionFeedState => {
@@ -257,6 +257,18 @@ export class SessionFeed {
257
257
  return { epoch: this.epoch, revision: this.revision, rows: [...this.published.values()].map((entry) => entry.row) };
258
258
  }
259
259
 
260
+ /** A row as the feed published it last, without reading anything: what events about the session may name. */
261
+ public publishedRow(rowIdArg: string): IControllerSessionFeedRow | undefined {
262
+ return this.published.get(rowIdArg)?.row;
263
+ }
264
+
265
+ /** A row after reading every stale source, as `list()` reads; undefined while the feed does not list it. */
266
+ public async find(rowIdArg: string): Promise<IControllerSessionFeedRow | undefined> {
267
+ this.abort.signal.throwIfAborted();
268
+ await this.flush(true);
269
+ return this.published.get(rowIdArg)?.row;
270
+ }
271
+
260
272
  /**
261
273
  * Acknowledges the failed row `rowIdArg` as the owner saw it, published with `updatedAtArg`: the row leaves the feed
262
274
  * while it stays failed and its `updatedAt` is not newer. Acknowledging the same row and `updatedAt` again changes
@@ -0,0 +1,26 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ /** Seals a secret for this user and machine under a name it is bound to; only its ciphertext is stored. */
4
+ export interface ITpmSecretCodec {
5
+ seal(nameArg: string, plaintextArg: Uint8Array): Promise<string>;
6
+ unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array>;
7
+ }
8
+
9
+ /**
10
+ * TPM2-sealed secrets through `systemd-creds`, bound to their name: the inference proxy's client credentials and
11
+ * signature key, and the controller's trusted-proxy secret.
12
+ */
13
+ export class TpmSecretCodec implements ITpmSecretCodec {
14
+ public async seal(nameArg: string, plaintextArg: Uint8Array): Promise<string> {
15
+ const ciphertext = await plugins.smartsecret.sealSmartSecretTpm2Credential(nameArg, plaintextArg);
16
+ try { return Buffer.from(ciphertext).toString('base64url'); }
17
+ finally { ciphertext.fill(0); }
18
+ }
19
+
20
+ public async unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array> {
21
+ if (!/^[A-Za-z0-9_-]{1,131072}$/.test(ciphertextArg)) throw new Error('Invalid sealed secret.');
22
+ const bytes = Buffer.from(ciphertextArg, 'base64url');
23
+ try { return await plugins.smartsecret.unsealSmartSecretTpm2Credential(nameArg, bytes); }
24
+ finally { bytes.fill(0); }
25
+ }
26
+ }