@passioncode-ai/fabric-agent-adapter 0.82.0 → 0.83.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.
Files changed (114) hide show
  1. package/CHANGELOG.md +122 -3
  2. package/README.md +6 -3
  3. package/SECURITY.md +4 -3
  4. package/package.json +1 -1
  5. package/plugins/fabric-agent-adapter/.claude-plugin/plugin.json +1 -1
  6. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/SKILL.md +7 -2
  7. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/references/agents-database.md +10 -0
  8. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/references/provider-entry.md +4 -2
  9. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/references/source-and-instance.md +18 -4
  10. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/scripts/adapt_project.py +16 -0
  11. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/scripts/fabric_provider.py +31 -15
  12. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/scripts/fabric_secrets.py +37 -12
  13. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/scripts/fabric_source_check.py +21 -148
  14. package/plugins/fabric-agent-adapter/skills/adapting-projects-to-fabric/scripts/fabric_source_check_cli.py +164 -0
  15. package/plugins/fabric-agent-adapter/skills/building-fabric-services/SKILL.md +15 -8
  16. package/plugins/fabric-agent-adapter/skills/building-fabric-services/assets/dashboard-kit/fabric-button.js +3 -3
  17. package/plugins/fabric-agent-adapter/skills/building-fabric-services/assets/dashboard-kit/fabric-dashboard.css +1 -1
  18. package/plugins/fabric-agent-adapter/skills/building-fabric-services/assets/dashboard-kit/fabric-dashboard.js +2 -2
  19. package/plugins/fabric-agent-adapter/skills/building-fabric-services/assets/dashboard-kit/kit.json +9 -4
  20. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/data-agent/README.md +76 -0
  21. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/data-agent/config.example.json +28 -0
  22. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/data-agent/data_agent.py +366 -0
  23. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/support-agent/README.md +32 -17
  24. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/support-agent/config.example.json +4 -0
  25. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/support-agent/support-agent.mjs +115 -0
  26. package/plugins/fabric-agent-adapter/skills/building-fabric-services/recipes/support-agent/support_agent.py +63 -11
  27. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/a2a.md +98 -0
  28. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/agents-database.md +10 -0
  29. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/board.md +20 -9
  30. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/connectors.md +2 -2
  31. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/interop.md +14 -2
  32. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/kit-modules.md +5 -1
  33. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/lifecycle.md +8 -5
  34. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/ops-digest.md +75 -0
  35. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/package.md +7 -5
  36. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/protocol.md +25 -12
  37. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/remote-placement.md +10 -1
  38. package/plugins/fabric-agent-adapter/skills/building-fabric-services/references/surfaces-and-auth.md +70 -0
  39. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/check_service_a2a.py +158 -0
  40. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-a2a-server.mjs +274 -0
  41. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-a2a.mjs +1 -1
  42. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-auth.mjs +4 -2
  43. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-board-fixture.mjs +14 -2
  44. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-board-link.mjs +54 -11
  45. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-board-session.mjs +45 -31
  46. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-board-slots.mjs +16 -6
  47. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-connections.mjs +26 -5
  48. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-delegation.mjs +3 -2
  49. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-descriptor.mjs +18 -12
  50. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-events.mjs +6 -1
  51. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-holds.mjs +21 -7
  52. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-http.mjs +3 -5
  53. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-hub.mjs +6 -2
  54. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-interop-rules.mjs +20 -1
  55. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-interop.mjs +5 -0
  56. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-lock.mjs +15 -2
  57. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-mandate.mjs +5 -0
  58. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-openrouter.mjs +24 -3
  59. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-ops-digest.mjs +455 -0
  60. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-package.mjs +17 -2
  61. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-paths.mjs +18 -5
  62. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-peer-owner.mjs +55 -19
  63. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-secrets.mjs +15 -2
  64. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-socket-proof.mjs +180 -0
  65. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-supervisors.mjs +5 -12
  66. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric-tailnet-identity.mjs +468 -0
  67. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_a2a.py +2 -1
  68. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_a2a_server.py +404 -0
  69. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_auth.py +6 -2
  70. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_board_fixture.py +16 -3
  71. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_board_link.py +97 -9
  72. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_board_session.py +35 -48
  73. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_board_slots.py +15 -7
  74. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_connections.py +27 -6
  75. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_delegation.py +18 -13
  76. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_descriptor.py +23 -14
  77. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_events.py +3 -1
  78. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_holds.py +31 -7
  79. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_http.py +18 -16
  80. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_hub.py +9 -5
  81. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_interop.py +55 -96
  82. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_interop_rules.py +98 -1
  83. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_lock.py +15 -6
  84. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_openrouter.py +43 -1
  85. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_ops_digest.py +647 -0
  86. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_package.py +16 -4
  87. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_paths.py +20 -5
  88. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_peer_owner.py +93 -93
  89. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_secrets.py +37 -12
  90. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_socket_proof.py +390 -0
  91. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_supervisors.py +3 -8
  92. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/fabric_tailnet_identity.py +492 -0
  93. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/kit.json +365 -53
  94. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/sample-remote-service.mjs +2 -1
  95. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/sample_service.py +16 -7
  96. package/plugins/fabric-agent-adapter/skills/building-fabric-services/scripts/tailnet-groups.schema.json +69 -0
  97. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/SKILL.md +5 -4
  98. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/agent-templates.md +16 -0
  99. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/agents-database.md +10 -0
  100. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/agents-that-answer-people.md +14 -9
  101. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/models-and-providers.md +4 -2
  102. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/source-and-instance.md +18 -4
  103. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/references/where-agents-run.md +2 -1
  104. package/plugins/fabric-agent-adapter/skills/creating-fabric-agents/scripts/admit.py +162 -0
  105. package/plugins/fabric-agent-adapter/skills/observing-products/SKILL.md +1 -1
  106. package/plugins/fabric-agent-adapter/skills/working-with-fabric/SKILL.md +1 -1
  107. package/plugins/fabric-agent-adapter/skills/working-with-fabric/references/agents-database.md +10 -0
  108. package/plugins/fabric-agent-adapter/skills/working-with-fabric/references/on-demand-session.md +14 -9
  109. package/plugins/fabric-agent-adapter/skills/working-with-fabric/references/project-board.md +7 -4
  110. package/plugins/fabric-agent-adapter/skills/working-with-fabric/references/session-hooks.md +10 -5
  111. package/plugins/fabric-agent-adapter/skills/working-with-fabric/scripts/fabric_session.py +2 -0
  112. package/plugins/fabric-agent-adapter/skills/working-with-fabric/scripts/fabric_session_cli.py +16 -5
  113. package/plugins/fabric-agent-adapter/skills/working-with-fabric/scripts/fabric_session_env.py +79 -8
  114. package/plugins/fabric-agent-adapter/skills/working-with-fabric/scripts/fabric_session_paths.py +147 -45
@@ -18,25 +18,34 @@ export function servicesDir(platform = process.platform) {
18
18
  return path.join(process.env.XDG_DATA_HOME || path.join(home, '.local/share'), 'passioncode-fabric/services');
19
19
  }
20
20
 
21
+ // FABRIC_DATA_HOME (FAA-192): an absolute folder on a durable volume holds the data folder, <home>/<id>, on a host whose
22
+ // own disk does not persist; logs and cache stay where the OS puts them. A relative value is refused.
23
+ function durable(id, dirs) {
24
+ const value = process.env.FABRIC_DATA_HOME;
25
+ if (!value) return dirs;
26
+ if (!path.isAbsolute(value)) throw new ServiceError('FABRIC_DATA_HOME is an absolute folder on a durable volume.');
27
+ return { ...dirs, data: path.join(value, id) };
28
+ }
29
+
21
30
  export function serviceDirs(id, platform = process.platform) {
22
31
  if (!ID.test(id)) throw new ServiceError(`Service id ${id} must match ${ID}.`);
23
32
  const home = os.homedir();
24
33
  if (platform === 'darwin') {
25
- return {
34
+ return durable(id, {
26
35
  data: path.join(home, 'Library/Application Support', id),
27
36
  logs: path.join(home, 'Library/Logs', id),
28
37
  cache: path.join(home, 'Library/Caches', id),
29
- };
38
+ });
30
39
  }
31
40
  if (platform === 'win32') {
32
41
  const data = path.join(localAppData(), id);
33
- return { data, logs: path.join(data, 'Logs'), cache: path.join(data, 'Cache') };
42
+ return durable(id, { data, logs: path.join(data, 'Logs'), cache: path.join(data, 'Cache') });
34
43
  }
35
- return {
44
+ return durable(id, {
36
45
  data: path.join(process.env.XDG_DATA_HOME || path.join(home, '.local/share'), id),
37
46
  logs: path.join(process.env.XDG_STATE_HOME || path.join(home, '.local/state'), id, 'logs'),
38
47
  cache: path.join(process.env.XDG_CACHE_HOME || path.join(home, '.cache'), id),
39
- };
48
+ });
40
49
  }
41
50
 
42
51
  /**
@@ -50,6 +59,10 @@ export function instanceDirs(id, instance = 'default', platform = process.platfo
50
59
  const folder = `${id}.${instance}`;
51
60
  if (platform === 'win32') {
52
61
  const data = path.join(path.dirname(personal.data), folder);
62
+ if (process.env.FABRIC_DATA_HOME) { // logs and cache stay on the local disk, beside the default's
63
+ const local = path.join(path.dirname(path.dirname(personal.logs)), folder);
64
+ return { data, logs: path.join(local, 'Logs'), cache: path.join(local, 'Cache') };
65
+ }
53
66
  return { data, logs: path.join(data, 'Logs'), cache: path.join(data, 'Cache') };
54
67
  }
55
68
  if (platform === 'darwin') return Object.fromEntries(Object.entries(personal).map(([key, dir]) => [key, path.join(path.dirname(dir), folder)]));
@@ -1,6 +1,8 @@
1
1
  // Which OS account owns each end of a loopback TCP connection (DEC-0154 HA-2, FAA-226, layer L0); the twin of
2
- // fabric_peer_owner.py, which is Project Observatory's peer_owner 1.1.0 vendored by commit. Node 18+, no dependency;
3
- // nothing here imports another kit module.
2
+ // fabric_peer_owner.py, which is Project Observatory's peer_owner vendored by commit (its 1.3.1 line). Node 18+, no
3
+ // dependency; the one kit module it may load is fabric-socket-proof.mjs beside it (DEC-0155 RT-2, FAA-292): on a
4
+ // restricted macOS table a server end or listener one of this account's own processes holds is this account's, and
5
+ // without that file this one answers as before.
4
6
  //
5
7
  // WHY. Loopback is one per computer, not one per OS account: every account's processes reach every listener on
6
8
  // 127.0.0.1, so a loopback socket proves nothing about who is on the other end. A client about to send a credential
@@ -47,7 +49,7 @@ import net from 'node:net';
47
49
  import os from 'node:os';
48
50
  import { fileURLToPath } from 'node:url';
49
51
 
50
- export const PEER_OWNER_VERSION = '1.2.0+kit.6'; // the Python twin's, Observatory's 1.2.0 with the kit's differences
52
+ export const PEER_OWNER_VERSION = '1.3.1+kit.7'; // the Python twin's, Observatory's 1.3.1 line with the kit's differences
51
53
  export const THIS_ACCOUNT = 'this-account';
52
54
  export const ANOTHER_ACCOUNT = 'another-account';
53
55
  export const SYSTEM = 'system';
@@ -478,10 +480,36 @@ async function oneOwner(found, how, opts, noneReason = null) {
478
480
  return owner(UNKNOWN, { method: how, reason: 'several-rows' });
479
481
  }
480
482
 
483
+ // fabric-socket-proof.mjs beside this file, loaded once (DEC-0155 RT-2, FAA-292); null when it is not there.
484
+ let socketProof;
485
+
486
+ // `fallback`, unless it is a restricted macOS table's unknown and fabric-socket-proof finds the socket (`accepted`) or
487
+ // the 127.0.0.1 listener (`ownListener`, FAA-272) in this account's own processes; else it stands. A test that injects
488
+ // a table (`read`, `run`) reaches this host's processes only through the `socketProof` it names.
489
+ async function proved(ask, args, fallback, opts = {}) {
490
+ if (fallback.kind !== UNKNOWN || !String(fallback.reason ?? '').startsWith(TABLE_RESTRICTED)) return fallback;
491
+ if ((opts.platform ?? process.platform) !== 'darwin') return fallback;
492
+ let module = opts.socketProof;
493
+ if (module === undefined) {
494
+ if (opts.read || opts.run) return fallback;
495
+ if (socketProof === undefined) socketProof = await import('./fabric-socket-proof.mjs').catch(() => null);
496
+ module = socketProof;
497
+ }
498
+ let p = null;
499
+ try {
500
+ p = module ? await module[ask](...args) : null;
501
+ } catch {
502
+ p = null; // a proof that fails proves nothing; the table's answer stands (fail closed)
503
+ }
504
+ return p?.proved ? owner(THIS_ACCOUNT, { uid: opts.ownUid ?? ownUid(), pid: p.pid, method: p.method, reason: p.reason }) : fallback;
505
+ }
506
+
481
507
  // The owner of the CLIENT socket of the connection between `server` and `client` ([host, port] each), as a server sees
482
508
  // them: the row whose local address is the client's and whose remote address is the server's.
483
- export function peerAt(server, client, opts) {
484
- return ownerOf(address(client[0], client[1]), address(server[0], server[1]), opts);
509
+ export async function peerAt(server, client, opts) {
510
+ const c = address(client[0], client[1]);
511
+ const s = address(server[0], server[1]);
512
+ return proved('accepted', [c, s, { wait: false }], await ownerOf(c, s, opts), opts);
485
513
  }
486
514
 
487
515
  // For a server's accepted socket: who connected. Reads nothing from the socket.
@@ -494,9 +522,11 @@ export function peer(socket, opts) {
494
522
  // address is this socket's own, the server process's accepted socket, so a listener swapped after listen cannot pass
495
523
  // for the one that answers. On Linux a connection not yet accept()ed has no owner of its own (inode 0): its listener
496
524
  // answers (the contract's CT-254, HA-2's Linux source).
497
- export function serverEnd(socket, opts) {
498
- if (!socket.remoteAddress || !socket.localAddress) return Promise.resolve(owner(UNKNOWN, { reason: 'socket: not connected' }));
499
- return ownerOf(address(socket.remoteAddress, socket.remotePort), address(socket.localAddress, socket.localPort), opts, true);
525
+ export async function serverEnd(socket, opts) {
526
+ if (!socket.remoteAddress || !socket.localAddress) return owner(UNKNOWN, { reason: 'socket: not connected' });
527
+ const s = address(socket.remoteAddress, socket.remotePort);
528
+ const own = address(socket.localAddress, socket.localPort);
529
+ return proved('accepted', [s, own, { wait: true }], await ownerOf(s, own, opts, true), opts);
500
530
  }
501
531
 
502
532
  const LISTEN_ORDER = ['127.0.0.1', '*', '::1'];
@@ -524,12 +554,12 @@ export async function listener(port, opts = {}) {
524
554
  try {
525
555
  table = await (read ?? readTable)(opts);
526
556
  } catch (err) {
527
- const who = owner(UNKNOWN, { method: method(), reason: err instanceof TableUnreadable ? err.message : `table: ${err.name}` });
557
+ const who = await proved('ownListener', [port], owner(UNKNOWN, { method: method(), reason: err instanceof TableUnreadable ? err.message : `table: ${err.name}` }), opts);
528
558
  if (!injected) listeners.set(port, [at, who]);
529
559
  return who;
530
560
  }
531
- const who = restricted(table.rows, table.how, opts) ? await restrictedListener(table.rows, port, table.how, opts)
532
- : await oneOwner(pickListener(table.rows, port), table.how, opts);
561
+ const who = await proved('ownListener', [port], restricted(table.rows, table.how, opts)
562
+ ? await restrictedListener(table.rows, port, table.how, opts) : await oneOwner(pickListener(table.rows, port), table.how, opts), opts);
533
563
  if (!injected) listeners.set(port, [at, who]);
534
564
  return who;
535
565
  }
@@ -552,18 +582,23 @@ export function refusal(who, port, expectPid = null) {
552
582
  return null;
553
583
  }
554
584
 
555
- // macOS, when the socket table is restricted (FAA-267, DEC-0155 RT-3): whether process `pid`, this account's, listens on
556
- // exactly 127.0.0.1:<port> (never the IPv4 wildcard, which lsof prints as it prints an IPv6-only one, nor ::1; FAA-312).
585
+ // macOS, when the socket table is restricted (FAA-267, DEC-0155 RT-3): whether process `pid` listens on exactly
586
+ // 127.0.0.1:<port> (never the IPv4 wildcard, which lsof prints as it prints an IPv6-only one, nor ::1; FAA-312).
557
587
  // `lsof -p` asks the process itself, which the restriction does not hide. Only a caller that started `pid` itself (a
558
- // launchd job it installed) may vouch with it, and never a set-uid one (its real and effective uid differ, DEC-0155).
559
- export async function pidListens(pid, port, { run = runProgram, platform = process.platform, ownUid: mine, ownEuid } = {}) {
588
+ // launchd job it installed) may vouch with it, and never a set-uid one for this account (its real and effective uid
589
+ // differ, DEC-0155). `owner`: the uid `pid` must run as, this account's by default, or null when the caller checks it
590
+ // itself (a LaunchDaemon's UserName, FAA-286); anything else is a RangeError.
591
+ export async function pidListens(pid, port, { run = runProgram, platform = process.platform, ownUid: mine, ownEuid, owner: want = THIS_ACCOUNT } = {}) {
592
+ if (want !== null && want !== THIS_ACCOUNT && !(Number.isInteger(want) && want >= 0)) throw new RangeError('owner is this-account, a uid or null');
560
593
  if (platform !== 'darwin' || !Number.isInteger(pid) || pid <= 1) return false;
561
594
  const real = mine ?? ownUid();
562
595
  const effective = ownEuid ?? (typeof process.geteuid === 'function' ? process.geteuid() : real);
563
- if (effective !== real) return false;
596
+ if (want === THIS_ACCOUNT && effective !== real) return false;
564
597
  try {
565
- const uid = parsePsUid(await run(PS, ['-o', 'uid=', '-p', String(pid)]));
566
- if (uid === null || uid !== real) return false;
598
+ if (want !== null) {
599
+ const uid = parsePsUid(await run(PS, ['-o', 'uid=', '-p', String(pid)]));
600
+ if (uid === null || uid !== (want === THIS_ACCOUNT ? real : want)) return false;
601
+ }
567
602
  const out = await run(LSOF, ['-nP', '-a', '-p', String(pid), '-i4TCP', '-sTCP:LISTEN', '-Fn']);
568
603
  return parseLsofListeners(out).some(([host, p]) => host === '127.0.0.1' && p === port);
569
604
  } catch {
@@ -591,8 +626,9 @@ export function checkedSocket(port, { host = '127.0.0.1', timeoutMs = 10000, exp
591
626
  // A reader that throws (an injected one; the readers themselves answer unknown) fails closed.
592
627
  who = owner(UNKNOWN, { method: method(), reason: `check: ${err?.name ?? 'Error'}` });
593
628
  }
629
+ // The vouch proves a listener on exactly 127.0.0.1, so only a connection that reached it (FAA-273 item 12).
594
630
  if (who.kind === UNKNOWN && String(who.reason ?? '').startsWith(TABLE_RESTRICTED) && vouchPid
595
- && await pidListens(vouchPid, port, ask)) {
631
+ && normHost(socket.remoteAddress) === '127.0.0.1' && await pidListens(vouchPid, port, ask)) {
596
632
  // The table hides other processes (FAA-267); the process this caller started holds the listener.
597
633
  who = owner(THIS_ACCOUNT, { uid: ask.ownUid ?? ownUid(), pid: vouchPid, method: 'lsof-pid' });
598
634
  }
@@ -124,12 +124,25 @@ const REPOSITORY_AUTHORITY = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//;
124
124
  // boundary, so composed after a separator look-behind it would miss a key after `-`, `.` or `_`.
125
125
  const REPOSITORY_KEYS = 'sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|xox[abposr]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|glpat-[A-Za-z0-9_-]{20,}';
126
126
  const KEY_AT_A_WORD = new RegExp(`(?:^|(?<=[._-]))(?:${REPOSITORY_KEYS})`);
127
- const JWT_AT_A_WORD = new RegExp(`(?:^|(?<=[._-]))${JWT_SHAPE.source}`);
127
+ /** Whether JWT_SHAPE matches at a word's start: the text's start, or after a `.`, `_` or `-` (FAA-321), read in one
128
+ * pass as jwtSpans: only a run's first `eyJ` at a word's start is tried (fabric_secrets.jwt_at_a_word). */
129
+ export function jwtAtAWord(text) {
130
+ for (let at = text.indexOf('eyJ'); at >= 0;) {
131
+ if (at > 0 && !'._-'.includes(text[at - 1])) { at = text.indexOf('eyJ', at + 1); continue; }
132
+ const one = runEnd(text, at + 3);
133
+ if (one - at - 3 >= 2 && text[one] === '.') {
134
+ const two = runEnd(text, one + 1);
135
+ if (two - one - 1 >= 2 && text[two] === '.') return true;
136
+ }
137
+ at = text.indexOf('eyJ', one);
138
+ }
139
+ return false;
140
+ }
128
141
  const SPECIAL_QUERY = /^(?:https?|wss?|ftp|file):/i; // whose query also encodes `'`, as the URL standard does
129
142
 
130
143
  /** Whether a path segment reads as a repository's name: lower case, or letters with no digit, with no provider key's
131
144
  * or JWT's shape at its start or after a `.`, `_` or `-` (0.62.0 readings 9 and 10), so a letters-only key is a key. */
132
- export const repositoryName = (piece) => REPOSITORY_NAME.test(piece) && !KEY_AT_A_WORD.test(piece) && !JWT_AT_A_WORD.test(piece);
145
+ export const repositoryName = (piece) => REPOSITORY_NAME.test(piece) && !KEY_AT_A_WORD.test(piece) && !jwtAtAWord(piece);
133
146
 
134
147
  /** Every `%XX` escape of an ASCII character decoded, the rest as written (fabric_secrets.ascii_decoded, readings 11). */
135
148
  export const asciiDecoded = (text) => text.replace(/%([0-7][0-9A-Fa-f])/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
@@ -0,0 +1,180 @@
1
+ // This OS account's own TCP sockets, read from its own processes (layer L0; DEC-0155 RT-2, FAA-292; FAA-272); the twin
2
+ // of fabric_socket_proof.py. Node 18+, no dependency; nothing here imports another kit module.
3
+ //
4
+ // macOS 27.2 shows an ad-hoc-signed runtime (Homebrew's Node) only its own sockets, so fabric-peer-owner reads such a
5
+ // table as table-restricted and cannot tell who accepted a connection. The question can still be asked of this
6
+ // account's own processes, which the restriction leaves readable: whether one of them holds the very socket that
7
+ // accepted the connection (RT-2, the accepted-socket proof), or a listener bound exactly to 127.0.0.1:<port> (HA-2's
8
+ // macOS row, FAA-272). Another account's process is not listed, so a proof only ever says "this account's" or "not
9
+ // proved"; never another account's, the system's or nobody's.
10
+ //
11
+ // await accepted(server, client, { wait }) the socket at `server` connected to `client` is in one of this
12
+ // account's processes (`wait`: read again for ACCEPT_WAIT_MS until the
13
+ // server has called accept())
14
+ // await ownListener(port) a listener of this account on exactly 127.0.0.1:<port>
15
+ //
16
+ // Node has no ctypes, so the twin reads RT-2's other named source, `lsof -nP -a -u <uid> -iTCP:<port> -sTCP:<state>
17
+ // -F pun`, matching the exact address pair and the numeric uid, where the Python twin reads the same records through
18
+ // libproc (lsof only when libproc is unavailable). The twins agree on answers, not on the program they run (DEC-0155
19
+ // Compatibility); test/fixtures/peer-owner/socket-proof-cases.json holds both. The proof is "a process of this uid
20
+ // holds the descriptor", weaker than a table's so_uid: a socket passed with SCM_RIGHTS to a process of this uid reads
21
+ // as this account's, which a caller that knows the server's pid closes with expectPid. One socket several of this
22
+ // account's processes hold is proved with pid null, reason several-processes. A process whose real and effective uids
23
+ // differ is refused (setuid-context). Reasons: table-restricted (read, nothing of this account matches) or
24
+ // `table-restricted: <detail>` (the proof could not be read). Bounded by TIMEOUT_MS and MAX_OUTPUT_BYTES.
25
+
26
+ import { execFile } from 'node:child_process';
27
+ import net from 'node:net';
28
+
29
+ export const TABLE_RESTRICTED = 'table-restricted';
30
+ export const LSOF_METHOD = 'lsof';
31
+ export const LSOF = '/usr/sbin/lsof';
32
+ export const TIMEOUT_MS = 2000;
33
+ export const ACCEPT_WAIT_MS = 500;
34
+ export const MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
35
+
36
+ const proof = (proved, pid, method, reason) => Object.freeze({ proved, pid, method, reason });
37
+ const digits = (text) => /^[0-9]+$/.test(text);
38
+
39
+ // One spelling per address, as fabric-peer-owner's normHost (the same rule, kept here so this module stands alone): an
40
+ // IPv4-mapped IPv6 address is its IPv4 form, the unspecified address `*`, an IPv6 address RFC 5952's.
41
+ function host(text) {
42
+ let h = String(text ?? '').replace(/^\[(.*)\]$/, '$1');
43
+ if (h === '*' || h === '') return '*';
44
+ if (h === 'localhost') return '127.0.0.1';
45
+ h = h.split('%', 1)[0];
46
+ if (net.isIPv4(h)) return h === '0.0.0.0' ? '*' : h.split('.').map((x) => String(Number(x))).join('.');
47
+ if (!net.isIPv6(h)) return h;
48
+ const t = new URL(`http://[${h}]/`).hostname.slice(1, -1);
49
+ if (t === '::') return '*';
50
+ const mapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(t);
51
+ if (!mapped) return t;
52
+ const hi = parseInt(mapped[1], 16);
53
+ const lo = parseInt(mapped[2], 16);
54
+ const v4 = [hi >> 8, hi & 255, lo >> 8, lo & 255].join('.');
55
+ return v4 === '0.0.0.0' ? '*' : v4;
56
+ }
57
+
58
+ // Whether `new URL(...).hostname` names this computer's loopback: localhost or a name under .localhost, ::1, or an IPv4
59
+ // address in 127/8 (new URL has already read 127.1, 2130706433, 0x7f.1 and a trailing dot as 127.x.y.z); never an
60
+ // IPv4-mapped address, which new URL writes in hex. The one rule fabric-http's checkUrl and fabric-delegation's key set
61
+ // source read (FAA-187 (15)); the twin of fabric_socket_proof.loopback_host.
62
+ export function loopbackHost(hostname) {
63
+ const h = String(hostname ?? '').toLowerCase().replace(/^\[(.*)\]$/, '$1');
64
+ if (h === 'localhost' || h.endsWith('.localhost') || h === '::1') return true;
65
+ return /^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/.test(h) && h.split('.').every((x) => Number(x) <= 255);
66
+ }
67
+
68
+ function lsofAddress(text) {
69
+ const at = text.lastIndexOf(':');
70
+ if (at < 0) return null;
71
+ const port = text.slice(at + 1);
72
+ if (!digits(port) || Number(port) > 65535) return null;
73
+ return [host(text.slice(0, at)), Number(port)];
74
+ }
75
+
76
+ // `lsof -nP -a -u <uid> -iTCP:<port> -sTCP:<state> -F pun`: the `n` lines of every process block whose `u` line is
77
+ // exactly `uid` (a block of another uid, or with no uid, is not read). A name with `->` is a connection, one without a
78
+ // listener. The same socket listed by several processes (one address pair) is one socket with pid null.
79
+ export function parseLsofSockets(text, uid) {
80
+ const found = new Map();
81
+ let pid = null;
82
+ let owner = null;
83
+ for (const line of String(text).split(/\r?\n/)) {
84
+ const tag = line.slice(0, 1);
85
+ const value = line.slice(1);
86
+ if (tag === 'p') {
87
+ pid = digits(value) ? Number(value) : null;
88
+ owner = null;
89
+ } else if (tag === 'u') {
90
+ owner = digits(value) ? Number(value) : null;
91
+ } else if (tag === 'n' && pid !== null && owner === uid) {
92
+ const arrow = value.indexOf('->');
93
+ const local = lsofAddress(arrow < 0 ? value : value.slice(0, arrow));
94
+ const remote = arrow < 0 ? ['*', 0] : lsofAddress(value.slice(arrow + 2));
95
+ if (!local || !remote) continue;
96
+ const key = JSON.stringify([local, remote, arrow < 0]);
97
+ if (!found.has(key)) found.set(key, { local, remote, listening: arrow < 0, pids: [] });
98
+ found.get(key).pids.push(pid);
99
+ }
100
+ }
101
+ return [...found.values()].map(({ local, remote, listening, pids }) => ({
102
+ local, remote, listening, pid: new Set(pids).size === 1 ? pids[0] : null,
103
+ }));
104
+ }
105
+
106
+ const same = (a, b) => a[0] === b[0] && a[1] === b[1];
107
+
108
+ function proved(hits, how) {
109
+ if (hits.length === 0) return proof(false, null, how, TABLE_RESTRICTED);
110
+ const pids = new Set(hits.map((s) => s.pid));
111
+ if (pids.size === 1 && !pids.has(null)) return proof(true, hits[0].pid, how, null);
112
+ return proof(true, null, how, 'several-processes');
113
+ }
114
+
115
+ // RT-2 over this account's sockets: the one at `server` connected to `client`; two with that pair are several-rows.
116
+ export function acceptedIn(sockets, server, client, how) {
117
+ const hits = sockets.filter((s) => !s.listening && same(s.local, server) && same(s.remote, client));
118
+ if (hits.length > 1) return proof(false, null, how, 'several-rows');
119
+ return proved(hits, how);
120
+ }
121
+
122
+ // FAA-272 over this account's sockets: a listener bound exactly to 127.0.0.1:<port> (DEC-0155 RT-4: our wildcard or
123
+ // ::1 listener, and root's, cannot be told from a hidden one).
124
+ export function listenerIn(sockets, port, how) {
125
+ return proved(sockets.filter((s) => s.listening && same(s.local, ['127.0.0.1', port])), how);
126
+ }
127
+
128
+ class ProofUnreadable extends Error {}
129
+
130
+ // One bounded run of lsof; its exit 1 with nothing printed is "no socket matched", not a failure.
131
+ export function runLsof(file, args, timeout = TIMEOUT_MS) {
132
+ return new Promise((resolve, reject) => {
133
+ execFile(file, args, { timeout, maxBuffer: MAX_OUTPUT_BYTES, encoding: 'utf8' }, (err, stdout) => {
134
+ if (!err) return resolve(stdout);
135
+ if (err.code === 1 && !String(stdout ?? '').trim()) return resolve('');
136
+ const why = typeof err.code === 'number' ? `exit ${err.code}` : (err.killed ? 'timeout' : (err.code ?? 'error'));
137
+ reject(new ProofUnreadable(`lsof: ${err.code === 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ? `output over ${MAX_OUTPUT_BYTES} bytes` : why}`));
138
+ });
139
+ });
140
+ }
141
+
142
+ // The reader a question uses: lsof on macOS, none elsewhere.
143
+ export const method = (platform = process.platform) => (platform === 'darwin' ? LSOF_METHOD : 'none');
144
+
145
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
146
+
147
+ async function ask(port, state, pick, waitMs, { platform = process.platform, uid, euid, run = runLsof, now = () => Date.now() } = {}) {
148
+ const start = now();
149
+ if (platform !== 'darwin') return proof(false, null, 'none', `${TABLE_RESTRICTED}: unsupported-platform`);
150
+ const real = uid ?? process.getuid();
151
+ const effective = euid ?? (typeof process.geteuid === 'function' ? process.geteuid() : real);
152
+ if (real !== effective) return proof(false, null, LSOF_METHOD, `${TABLE_RESTRICTED}: setuid-context`);
153
+ let pause = 5;
154
+ for (;;) {
155
+ let have;
156
+ try {
157
+ have = parseLsofSockets(await run(LSOF, ['-nP', '-a', '-u', String(real), `-iTCP:${port}`, `-sTCP:${state}`, '-F', 'pun']), real);
158
+ } catch (err) {
159
+ return proof(false, null, LSOF_METHOD, `${TABLE_RESTRICTED}: ${err?.message ?? 'lsof: error'}`);
160
+ }
161
+ const answer = pick(have);
162
+ if (answer.proved || now() + pause > start + Math.min(waitMs, TIMEOUT_MS)) return answer;
163
+ await sleep(pause);
164
+ pause = Math.min(pause * 2, 50);
165
+ }
166
+ }
167
+
168
+ // RT-2: whether the socket at `server` connected to `client` ([host, port] each) is in one of this account's processes.
169
+ // `wait`: read again for ACCEPT_WAIT_MS, since the server's accepted socket exists only once it calls accept().
170
+ // `platform`, `uid`, `euid`, `run` and `now` are the tests'.
171
+ export function accepted(server, client, { wait = false, ...opts } = {}) {
172
+ const s = [host(server[0]), Number(server[1])];
173
+ const c = [host(client[0]), Number(client[1])];
174
+ return ask(s[1], 'ESTABLISHED', (have) => acceptedIn(have, s, c, LSOF_METHOD), wait ? ACCEPT_WAIT_MS : 0, opts);
175
+ }
176
+
177
+ // FAA-272: whether one of this account's processes listens on exactly 127.0.0.1:<port>.
178
+ export function ownListener(port, opts = {}) {
179
+ return ask(Number(port), 'LISTEN', (have) => listenerIn(have, Number(port), LSOF_METHOD), 0, opts);
180
+ }
@@ -9,7 +9,7 @@ import { DRAIN_SECONDS, SUPERVISOR_ENV, ServiceError, TASK, UNIT } from './fabri
9
9
  import { atomicWrite, instanceDirs } from './fabric-paths.mjs';
10
10
  import { SUPERVISOR_ARG, markerArguments } from './fabric-lock.mjs';
11
11
  import { URI } from './fabric-backups.mjs';
12
- import { CheckedAgent, LSOF, ListenerRefused, PS, SERVER_NOT_OWN, TABLE_RESTRICTED, UNKNOWN, parseLsofListeners, parsePsUid,
12
+ import { CheckedAgent, ListenerRefused, PS, SERVER_NOT_OWN, TABLE_RESTRICTED, UNKNOWN, parsePsUid, pidListens,
13
13
  runProgram, serverEnd } from './fabric-peer-owner.mjs';
14
14
  import http from 'node:http';
15
15
  import net from 'node:net';
@@ -60,7 +60,7 @@ function noMarkerArgument(argv) {
60
60
  // An environment value: read path-aware (secretArgument) only when it reads as a path (it starts as one, has at least two
61
61
  // separators and a lower-case word between them), so a release folder under Application Support is no key (FAA-266) while
62
62
  // a base64 key whose `/` splits it stays one; any other value keeps the strict secretValue.
63
- const secretEnvValue = (value) => {
63
+ export const secretEnvValue = (value) => {
64
64
  const pieces = value.split(/[\\/]/);
65
65
  const path = /^(?:[/~.]|[A-Za-z]:[\\/])/.test(value) && pieces.length >= 3 && pieces.some((x) => /^[a-z][a-z0-9_-]{2,}$/.test(x));
66
66
  return path ? secretArgument(value) : secretValue(value);
@@ -609,16 +609,9 @@ function loopbackWellKnown(port, agent, host = '127.0.0.1') {
609
609
  }
610
610
 
611
611
  // Whether launchd's job `pid` listens on exactly 127.0.0.1:<port>, never the wildcard, asked of the process itself (`lsof -p`),
612
- // which a restricted socket table does not hide. Only for a LaunchDaemon root bootstrapped, whose pid launchd named.
613
- async function jobListens(pid, port, run = runProgram, platform = process.platform) {
614
- if (platform !== 'darwin' || !Number.isInteger(pid) || pid <= 1) return false;
615
- try {
616
- const out = await run(LSOF, ['-nP', '-a', '-p', String(pid), '-i4TCP', '-sTCP:LISTEN', '-Fn']);
617
- return parseLsofListeners(out).some(([host, p]) => host === '127.0.0.1' && p === port); // exact bind, as RT-3
618
- } catch {
619
- return false;
620
- }
621
- }
612
+ // which a restricted socket table does not hide. Only for a LaunchDaemon root bootstrapped, whose pid launchd named. The
613
+ // peer owner's one lsof reader, the uid left to the caller, which checks the job's UserName itself (FAA-286 item 1).
614
+ const jobListens = (pid, port, run = runProgram, platform = process.platform) => pidListens(pid, port, { run, platform, owner: null });
622
615
 
623
616
  // The uid account `name` has on this machine (`id -u`), or null when it has none.
624
617
  function accountUid(name, run) {