@rubytech/create-maxy-code 0.1.330 → 0.1.332

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 (156) hide show
  1. package/dist/index.js +19 -2
  2. package/package.json +1 -1
  3. package/payload/platform/config/brand.json +1 -0
  4. package/payload/platform/lib/account-enumeration/dist/index.d.ts +33 -0
  5. package/payload/platform/lib/account-enumeration/dist/index.d.ts.map +1 -1
  6. package/payload/platform/lib/account-enumeration/dist/index.js +76 -0
  7. package/payload/platform/lib/account-enumeration/dist/index.js.map +1 -1
  8. package/payload/platform/lib/account-enumeration/src/index.ts +78 -0
  9. package/payload/platform/lib/mcp-spawn-tee/dist/index.d.ts +24 -24
  10. package/payload/platform/lib/mcp-spawn-tee/dist/index.js +109 -75
  11. package/payload/platform/lib/mcp-spawn-tee/dist/index.js.map +1 -1
  12. package/payload/platform/lib/mcp-spawn-tee/src/__tests__/spawn-tee.test.ts +56 -19
  13. package/payload/platform/lib/mcp-spawn-tee/src/index.ts +112 -77
  14. package/payload/platform/neo4j/schema.cypher +58 -4
  15. package/payload/platform/plugins/admin/.claude-plugin/plugin.json +1 -1
  16. package/payload/platform/plugins/admin/PLUGIN.md +13 -1
  17. package/payload/platform/plugins/admin/lib/mcp-spawn-tee/index.js +109 -75
  18. package/payload/platform/plugins/admin/mcp/dist/index.js +102 -0
  19. package/payload/platform/plugins/admin/mcp/dist/index.js.map +1 -1
  20. package/payload/platform/plugins/admin/mcp/dist/tools/__tests__/account-lifecycle.test.d.ts +2 -0
  21. package/payload/platform/plugins/admin/mcp/dist/tools/__tests__/account-lifecycle.test.d.ts.map +1 -0
  22. package/payload/platform/plugins/admin/mcp/dist/tools/__tests__/account-lifecycle.test.js +57 -0
  23. package/payload/platform/plugins/admin/mcp/dist/tools/__tests__/account-lifecycle.test.js.map +1 -0
  24. package/payload/platform/plugins/admin/mcp/dist/tools/account-lifecycle.d.ts +34 -0
  25. package/payload/platform/plugins/admin/mcp/dist/tools/account-lifecycle.d.ts.map +1 -0
  26. package/payload/platform/plugins/admin/mcp/dist/tools/account-lifecycle.js +78 -0
  27. package/payload/platform/plugins/admin/mcp/dist/tools/account-lifecycle.js.map +1 -0
  28. package/payload/platform/plugins/admin/skills/plainly/SKILL.md +7 -0
  29. package/payload/platform/plugins/admin/skills/platform-architecture/SKILL.md +2 -2
  30. package/payload/platform/plugins/aeo/lib/mcp-spawn-tee/index.js +109 -75
  31. package/payload/platform/plugins/browser/lib/mcp-spawn-tee/index.js +109 -75
  32. package/payload/platform/plugins/contacts/lib/mcp-spawn-tee/index.js +109 -75
  33. package/payload/platform/plugins/contacts/mcp/dist/index.js +1 -1
  34. package/payload/platform/plugins/contacts/mcp/dist/index.js.map +1 -1
  35. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/contact-lookup.test.d.ts +2 -0
  36. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/contact-lookup.test.d.ts.map +1 -0
  37. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/contact-lookup.test.js +71 -0
  38. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/contact-lookup.test.js.map +1 -0
  39. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-create.test.d.ts +2 -0
  40. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-create.test.d.ts.map +1 -0
  41. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-create.test.js +112 -0
  42. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-create.test.js.map +1 -0
  43. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-manage.test.d.ts +2 -0
  44. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-manage.test.d.ts.map +1 -0
  45. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-manage.test.js +102 -0
  46. package/payload/platform/plugins/contacts/mcp/dist/tools/__tests__/group-manage.test.js.map +1 -0
  47. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-lookup.d.ts +1 -0
  48. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-lookup.d.ts.map +1 -1
  49. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-lookup.js +10 -3
  50. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-lookup.js.map +1 -1
  51. package/payload/platform/plugins/contacts/mcp/dist/tools/group-create.d.ts.map +1 -1
  52. package/payload/platform/plugins/contacts/mcp/dist/tools/group-create.js +7 -2
  53. package/payload/platform/plugins/contacts/mcp/dist/tools/group-create.js.map +1 -1
  54. package/payload/platform/plugins/contacts/mcp/dist/tools/group-manage.d.ts.map +1 -1
  55. package/payload/platform/plugins/contacts/mcp/dist/tools/group-manage.js +5 -4
  56. package/payload/platform/plugins/contacts/mcp/dist/tools/group-manage.js.map +1 -1
  57. package/payload/platform/plugins/contacts/mcp/package.json +4 -2
  58. package/payload/platform/plugins/docs/references/admin-ui.md +1 -1
  59. package/payload/platform/plugins/email/lib/mcp-spawn-tee/index.js +109 -75
  60. package/payload/platform/plugins/memory/.claude-plugin/plugin.json +1 -1
  61. package/payload/platform/plugins/memory/PLUGIN.md +1 -1
  62. package/payload/platform/plugins/memory/lib/mcp-spawn-tee/index.js +109 -75
  63. package/payload/platform/plugins/memory/mcp/dist/index.js +3 -1
  64. package/payload/platform/plugins/memory/mcp/dist/index.js.map +1 -1
  65. package/payload/platform/plugins/memory/mcp/dist/tools/__tests__/memory-lookup-by-name.test.js +39 -0
  66. package/payload/platform/plugins/memory/mcp/dist/tools/__tests__/memory-lookup-by-name.test.js.map +1 -1
  67. package/payload/platform/plugins/memory/mcp/dist/tools/memory-lookup-by-name.d.ts.map +1 -1
  68. package/payload/platform/plugins/memory/mcp/dist/tools/memory-lookup-by-name.js +13 -1
  69. package/payload/platform/plugins/memory/mcp/dist/tools/memory-lookup-by-name.js.map +1 -1
  70. package/payload/platform/plugins/memory/references/schema-estate-agent.md +17 -5
  71. package/payload/platform/plugins/outlook/lib/mcp-spawn-tee/index.js +109 -75
  72. package/payload/platform/plugins/quickbooks/lib/mcp-spawn-tee/index.js +109 -75
  73. package/payload/platform/plugins/replicate/lib/mcp-spawn-tee/index.js +109 -75
  74. package/payload/platform/plugins/scheduling/lib/mcp-spawn-tee/index.js +109 -75
  75. package/payload/platform/plugins/telegram/lib/mcp-spawn-tee/index.js +109 -75
  76. package/payload/platform/plugins/url-get/lib/mcp-spawn-tee/index.js +109 -75
  77. package/payload/platform/plugins/whatsapp/lib/mcp-spawn-tee/index.js +109 -75
  78. package/payload/platform/plugins/work/lib/mcp-spawn-tee/index.js +109 -75
  79. package/payload/platform/plugins/workflows/lib/mcp-spawn-tee/index.js +109 -75
  80. package/payload/platform/scripts/__tests__/provision-role-stamp.test.sh +40 -0
  81. package/payload/platform/scripts/__tests__/sweep-durability.test.sh +103 -0
  82. package/payload/platform/scripts/lib/provision-account-dir.sh +319 -0
  83. package/payload/platform/scripts/lib/resolve-account-dir.sh +79 -132
  84. package/payload/platform/scripts/seed-neo4j.sh +18 -0
  85. package/payload/platform/scripts/setup-account.sh +28 -361
  86. package/payload/platform/services/claude-session-manager/dist/canonical-tool-names.generated.d.ts.map +1 -1
  87. package/payload/platform/services/claude-session-manager/dist/canonical-tool-names.generated.js +4 -0
  88. package/payload/platform/services/claude-session-manager/dist/canonical-tool-names.generated.js.map +1 -1
  89. package/payload/platform/services/claude-session-manager/dist/config.d.ts +18 -0
  90. package/payload/platform/services/claude-session-manager/dist/config.d.ts.map +1 -1
  91. package/payload/platform/services/claude-session-manager/dist/config.js +63 -7
  92. package/payload/platform/services/claude-session-manager/dist/config.js.map +1 -1
  93. package/payload/platform/services/claude-session-manager/dist/http-server.d.ts.map +1 -1
  94. package/payload/platform/services/claude-session-manager/dist/http-server.js +30 -2
  95. package/payload/platform/services/claude-session-manager/dist/http-server.js.map +1 -1
  96. package/payload/platform/services/claude-session-manager/dist/index.js +16 -1
  97. package/payload/platform/services/claude-session-manager/dist/index.js.map +1 -1
  98. package/payload/platform/services/claude-session-manager/dist/rc-daemon.d.ts.map +1 -1
  99. package/payload/platform/services/claude-session-manager/dist/rc-daemon.js +7 -0
  100. package/payload/platform/services/claude-session-manager/dist/rc-daemon.js.map +1 -1
  101. package/payload/platform/services/claude-session-manager/dist/session-sidecar.d.ts +5 -0
  102. package/payload/platform/services/claude-session-manager/dist/session-sidecar.d.ts.map +1 -1
  103. package/payload/platform/services/claude-session-manager/dist/session-sidecar.js +8 -0
  104. package/payload/platform/services/claude-session-manager/dist/session-sidecar.js.map +1 -1
  105. package/payload/platform/services/claude-session-manager/dist/stuck-turn.d.ts +6 -0
  106. package/payload/platform/services/claude-session-manager/dist/stuck-turn.d.ts.map +1 -1
  107. package/payload/platform/services/claude-session-manager/dist/stuck-turn.js +17 -5
  108. package/payload/platform/services/claude-session-manager/dist/stuck-turn.js.map +1 -1
  109. package/payload/platform/services/webchat-channel/dist/instructions.d.ts.map +1 -1
  110. package/payload/platform/services/webchat-channel/dist/instructions.js +25 -1
  111. package/payload/platform/services/webchat-channel/dist/instructions.js.map +1 -1
  112. package/payload/platform/services/webchat-channel/dist/notification.d.ts +17 -0
  113. package/payload/platform/services/webchat-channel/dist/notification.d.ts.map +1 -1
  114. package/payload/platform/services/webchat-channel/dist/notification.js +14 -0
  115. package/payload/platform/services/webchat-channel/dist/notification.js.map +1 -1
  116. package/payload/platform/services/webchat-channel/dist/server.d.ts.map +1 -1
  117. package/payload/platform/services/webchat-channel/dist/server.js +66 -2
  118. package/payload/platform/services/webchat-channel/dist/server.js.map +1 -1
  119. package/payload/platform/services/webchat-channel/dist/turn-follow.d.ts +28 -0
  120. package/payload/platform/services/webchat-channel/dist/turn-follow.d.ts.map +1 -0
  121. package/payload/platform/services/webchat-channel/dist/turn-follow.js +188 -0
  122. package/payload/platform/services/webchat-channel/dist/turn-follow.js.map +1 -0
  123. package/payload/platform/services/whatsapp-channel/dist/notification.d.ts.map +1 -1
  124. package/payload/platform/services/whatsapp-channel/dist/notification.js +24 -1
  125. package/payload/platform/services/whatsapp-channel/dist/notification.js.map +1 -1
  126. package/payload/platform/templates/account.json +2 -0
  127. package/payload/premium-plugins/writer-craft/lib/mcp-spawn-tee/index.js +109 -75
  128. package/payload/server/public/assets/AdminLoginScreens-DexSvtjD.js +1 -0
  129. package/payload/server/public/assets/AdminShell-Bf9tndNg.js +1 -0
  130. package/payload/server/public/assets/{Checkbox-1flpB9H0.js → Checkbox-D58GsKoQ.js} +1 -1
  131. package/payload/server/public/assets/{OperatorConversations-DrPqB9Ym.css → OperatorConversations-CDdp2nVn.css} +1 -1
  132. package/payload/server/public/assets/OperatorConversations-RmqANYz8.js +9 -0
  133. package/payload/server/public/assets/{admin-BWJTvagN.js → admin-CH5Vkkt4.js} +1 -1
  134. package/payload/server/public/assets/{browser-XM5e-fn1.js → browser-BwDtzSlr.js} +1 -1
  135. package/payload/server/public/assets/chat-3GY0LmYH.js +1 -0
  136. package/payload/server/public/assets/data-CttrzhfL.js +1 -0
  137. package/payload/server/public/assets/{graph-CIIz9Dmh.js → graph-CZSdF2jv.js} +3 -3
  138. package/payload/server/public/assets/{graph-labels-BRKvhE0e.js → graph-labels-Bi0fu8Ns.js} +1 -1
  139. package/payload/server/public/assets/operator-BEivxmRy.js +1 -0
  140. package/payload/server/public/assets/page-DUvT07SB.js +30 -0
  141. package/payload/server/public/assets/{public-BPlZnopr.js → public-B3eIlVbd.js} +1 -1
  142. package/payload/server/public/browser.html +4 -4
  143. package/payload/server/public/chat.html +5 -5
  144. package/payload/server/public/data.html +4 -4
  145. package/payload/server/public/graph.html +6 -6
  146. package/payload/server/public/index.html +6 -6
  147. package/payload/server/public/operator.html +7 -7
  148. package/payload/server/public/public.html +5 -5
  149. package/payload/server/server.js +605 -435
  150. package/payload/server/public/assets/AdminLoginScreens-BnuaXvxN.js +0 -1
  151. package/payload/server/public/assets/AdminShell-Br0DJQNu.js +0 -1
  152. package/payload/server/public/assets/OperatorConversations-C8sITz2_.js +0 -9
  153. package/payload/server/public/assets/chat-COFMxaoC.js +0 -1
  154. package/payload/server/public/assets/data-BHtJozT2.js +0 -1
  155. package/payload/server/public/assets/operator-9hbxrXKm.js +0 -1
  156. package/payload/server/public/assets/page-CXkBfOhG.js +0 -30
package/dist/index.js CHANGED
@@ -2848,14 +2848,31 @@ function registerSpecialistAgents() {
2848
2848
  // time for the post-install banner.
2849
2849
  // ---------------------------------------------------------------------------
2850
2850
  function resolveInstallAccountId() {
2851
+ // Managed-service model: prefer the single `role:"house"` account; fall back
2852
+ // to the sole account in the pre-migration window. Returns "" on no accounts
2853
+ // or genuine drift (zero-house with >1, or multiple houses).
2851
2854
  const accountsDir = join(INSTALL_DIR, "data/accounts");
2852
2855
  if (!existsSync(accountsDir))
2853
2856
  return "";
2854
2857
  try {
2858
+ const candidates = [];
2859
+ const houses = [];
2855
2860
  for (const d of readdirSync(accountsDir)) {
2856
- if (existsSync(join(accountsDir, d, "account.json")))
2857
- return d;
2861
+ const configPath = join(accountsDir, d, "account.json");
2862
+ if (!existsSync(configPath))
2863
+ continue;
2864
+ candidates.push(d);
2865
+ try {
2866
+ const cfg = JSON.parse(readFileSync(configPath, "utf-8"));
2867
+ if (cfg.role === "house")
2868
+ houses.push(d);
2869
+ }
2870
+ catch { /* unparseable — not a house candidate */ }
2858
2871
  }
2872
+ if (houses.length === 1)
2873
+ return houses[0];
2874
+ if (houses.length === 0 && candidates.length === 1)
2875
+ return candidates[0];
2859
2876
  }
2860
2877
  catch { /* directory unreadable */ }
2861
2878
  return "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rubytech/create-maxy-code",
3
- "version": "0.1.330",
3
+ "version": "0.1.332",
4
4
  "description": "Install Maxy — AI for Productive People",
5
5
  "bin": {
6
6
  "create-maxy-code": "./dist/index.js"
@@ -17,6 +17,7 @@
17
17
  "commercialMode": false,
18
18
  "vertical": "schema-knowledge-work",
19
19
  "shipsPremiumBundles": ["venture-studio", "writer-craft", "teaching", "management-consulting"],
20
+ "primaryContainer": { "label": "Project", "term": "Projects" },
20
21
 
21
22
  "defaultColors": {
22
23
  "primary": "#7C8C72",
@@ -6,6 +6,39 @@
6
6
  * be hiding).
7
7
  */
8
8
  export declare function enumerateValidAccountIds(accountsDir: string): string[];
9
+ /**
10
+ * Returns the single `role:"house"` account UUID under `accountsDir`.
11
+ *
12
+ * The registry is the enumerated set of valid account dirs (see
13
+ * `enumerateValidAccountIds`); among them, exactly one must carry
14
+ * `role:"house"`. Throws on zero-house or multiple-house — both are drift the
15
+ * managed-service invariant forbids (`houses=<n>` in the message is the failure
16
+ * signature operators grep for).
17
+ *
18
+ * Deliberately NOT cached: house designation can change at runtime when the
19
+ * operator provisions/archives accounts via the lifecycle tools, so the role
20
+ * lookup reads `account.json` fresh each call. (`enumerateValidAccountIds` is
21
+ * still cached for the dir-validity scan; that set is stable for the process.)
22
+ *
23
+ * Legacy tolerance is intentionally NOT here: a pre-migration account with no
24
+ * `role` does not count as house. `setup-account.sh` stamps `role:"house"` on a
25
+ * sole unlabelled account at seed time, so by manager boot the invariant holds.
26
+ */
27
+ export declare function resolveHouseAccountId(accountsDir: string): string;
28
+ /**
29
+ * Like `resolveHouseAccountId`, but tolerant of the pre-migration window: when
30
+ * no account carries `role:"house"` AND exactly one valid account exists, that
31
+ * sole account is returned (it is about to be stamped `role:"house"` by
32
+ * `setup-account.sh`). This is the resolver every "the install's account" call
33
+ * site uses (boot account dir, installer account id, channel persist), so a
34
+ * legacy single-account install never throws while waiting for the seed-time
35
+ * role stamp — the WhatsApp persist path in particular must not drop a customer
36
+ * message in that window.
37
+ *
38
+ * Still loud-fails on genuine drift: zero accounts, multiple houses, or
39
+ * zero-house with more than one account (`houses=<n> total=<k>` signature).
40
+ */
41
+ export declare function resolveHouseOrSoleAccountId(accountsDir: string): string;
9
42
  /**
10
43
  * Resolves the canonical accounts dir from `MAXY_PLATFORM_ROOT`. Mirrors the
11
44
  * pattern at platform/ui/app/lib/claude-agent/account.ts:9
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,CAgCtE;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAS9C;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,mCAAmC,GAC3C,SAAS,GACT,oBAAoB,GACpB,UAAU,CAAC;AAEf,MAAM,MAAM,sBAAsB,GAC9B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,GAC9C;IACE,EAAE,EAAE,KAAK,CAAC;IACV,MAAM,EAAE,mCAAmC,CAAC;IAC5C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAEN,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,EAAE,MAAM,EAAE,GAChB,sBAAsB,CAWxB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,CAgCtE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAsBjE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAqBvE;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAS9C;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,mCAAmC,GAC3C,SAAS,GACT,oBAAoB,GACpB,UAAU,CAAC;AAEf,MAAM,MAAM,sBAAsB,GAC9B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,GAC9C;IACE,EAAE,EAAE,KAAK,CAAC;IACV,MAAM,EAAE,mCAAmC,CAAC;IAC5C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAEN,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,EAAE,MAAM,EAAE,GAChB,sBAAsB,CAWxB"}
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enumerateValidAccountIds = enumerateValidAccountIds;
4
+ exports.resolveHouseAccountId = resolveHouseAccountId;
5
+ exports.resolveHouseOrSoleAccountId = resolveHouseOrSoleAccountId;
4
6
  exports.getAccountsDirFromEnv = getAccountsDirFromEnv;
5
7
  exports._resetEnumerationCache = _resetEnumerationCache;
6
8
  exports.validateAccountIdEnv = validateAccountIdEnv;
@@ -71,6 +73,80 @@ function enumerateValidAccountIds(accountsDir) {
71
73
  cache.set(accountsDir, valid);
72
74
  return valid;
73
75
  }
76
+ /**
77
+ * Returns the single `role:"house"` account UUID under `accountsDir`.
78
+ *
79
+ * The registry is the enumerated set of valid account dirs (see
80
+ * `enumerateValidAccountIds`); among them, exactly one must carry
81
+ * `role:"house"`. Throws on zero-house or multiple-house — both are drift the
82
+ * managed-service invariant forbids (`houses=<n>` in the message is the failure
83
+ * signature operators grep for).
84
+ *
85
+ * Deliberately NOT cached: house designation can change at runtime when the
86
+ * operator provisions/archives accounts via the lifecycle tools, so the role
87
+ * lookup reads `account.json` fresh each call. (`enumerateValidAccountIds` is
88
+ * still cached for the dir-validity scan; that set is stable for the process.)
89
+ *
90
+ * Legacy tolerance is intentionally NOT here: a pre-migration account with no
91
+ * `role` does not count as house. `setup-account.sh` stamps `role:"house"` on a
92
+ * sole unlabelled account at seed time, so by manager boot the invariant holds.
93
+ */
94
+ function resolveHouseAccountId(accountsDir) {
95
+ const valid = enumerateValidAccountIds(accountsDir);
96
+ const houses = [];
97
+ for (const id of valid) {
98
+ const configPath = (0, node_path_1.resolve)(accountsDir, id, "account.json");
99
+ try {
100
+ const cfg = JSON.parse((0, node_fs_1.readFileSync)(configPath, "utf-8"));
101
+ if (cfg.role === "house")
102
+ houses.push(id);
103
+ }
104
+ catch {
105
+ // already excluded from `valid` if unparseable; a race here just skips it
106
+ }
107
+ }
108
+ if (houses.length !== 1) {
109
+ throw new Error(`[account-registry] resolveHouseAccountId: houses=${houses.length} under ${accountsDir} ` +
110
+ `(${houses.join(", ") || "none"}) — exactly one role:"house" account is required. ` +
111
+ `Loud-fail rather than picking one silently.`);
112
+ }
113
+ return houses[0];
114
+ }
115
+ /**
116
+ * Like `resolveHouseAccountId`, but tolerant of the pre-migration window: when
117
+ * no account carries `role:"house"` AND exactly one valid account exists, that
118
+ * sole account is returned (it is about to be stamped `role:"house"` by
119
+ * `setup-account.sh`). This is the resolver every "the install's account" call
120
+ * site uses (boot account dir, installer account id, channel persist), so a
121
+ * legacy single-account install never throws while waiting for the seed-time
122
+ * role stamp — the WhatsApp persist path in particular must not drop a customer
123
+ * message in that window.
124
+ *
125
+ * Still loud-fails on genuine drift: zero accounts, multiple houses, or
126
+ * zero-house with more than one account (`houses=<n> total=<k>` signature).
127
+ */
128
+ function resolveHouseOrSoleAccountId(accountsDir) {
129
+ const valid = enumerateValidAccountIds(accountsDir);
130
+ const houses = [];
131
+ for (const id of valid) {
132
+ const configPath = (0, node_path_1.resolve)(accountsDir, id, "account.json");
133
+ try {
134
+ const cfg = JSON.parse((0, node_fs_1.readFileSync)(configPath, "utf-8"));
135
+ if (cfg.role === "house")
136
+ houses.push(id);
137
+ }
138
+ catch {
139
+ // already excluded from `valid` if unparseable
140
+ }
141
+ }
142
+ if (houses.length === 1)
143
+ return houses[0];
144
+ if (houses.length === 0 && valid.length === 1)
145
+ return valid[0];
146
+ throw new Error(`[account-registry] resolveHouseOrSoleAccountId: houses=${houses.length} total=${valid.length} ` +
147
+ `under ${accountsDir} — expected exactly one role:"house" (or a single unlabelled account). ` +
148
+ `Loud-fail rather than picking one silently.`);
149
+ }
74
150
  /**
75
151
  * Resolves the canonical accounts dir from `MAXY_PLATFORM_ROOT`. Mirrors the
76
152
  * pattern at platform/ui/app/lib/claude-agent/account.ts:9
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAoCA,4DAgCC;AAUD,sDASC;AAMD,wDAEC;AA8BD,oDAcC;AA3ID;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qCAAoD;AACpD,yCAAoC;AAEpC,MAAM,OAAO,GACX,iEAAiE,CAAC;AAEpE,MAAM,KAAK,GAAG,IAAI,GAAG,EAAoB,CAAC;AAE1C;;;;;;GAMG;AACH,SAAgB,wBAAwB,CAAC,WAAmB;IAC1D,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACtC,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IAExC,IAAI,KAAe,CAAC;IACpB,IAAI,CAAC;QACH,KAAK,GAAG,IAAA,qBAAW,EAAC,WAAW,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAK,GAA6B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACrD,KAAK,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;YAC3B,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,IA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ED;;;;;;;GAOG;AACH,SAAgB,qBAAqB;IACnC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;IAC5C,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CACb,4EAA4E;YAC1E,kFAAkF,CACrF,CAAC;IACJ,CAAC;IACD,OAAO,IAAA,mBAAO,EAAC,IAAI,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC;AAC9C,CAAC;AAED;;;GAGG;AACH,SAAgB,sBAAsB;IACpC,KAAK,CAAC,KAAK,EAAE,CAAC;AAChB,CAAC;AA8BD,SAAgB,oBAAoB,CAClC,QAA4B,EAC5B,OAAiB;IAEjB,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAChE,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;IAC/E,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;QAChC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;IACrE,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAChD,CAAC"}
@@ -68,6 +68,84 @@ export function enumerateValidAccountIds(accountsDir: string): string[] {
68
68
  return valid;
69
69
  }
70
70
 
71
+ /**
72
+ * Returns the single `role:"house"` account UUID under `accountsDir`.
73
+ *
74
+ * The registry is the enumerated set of valid account dirs (see
75
+ * `enumerateValidAccountIds`); among them, exactly one must carry
76
+ * `role:"house"`. Throws on zero-house or multiple-house — both are drift the
77
+ * managed-service invariant forbids (`houses=<n>` in the message is the failure
78
+ * signature operators grep for).
79
+ *
80
+ * Deliberately NOT cached: house designation can change at runtime when the
81
+ * operator provisions/archives accounts via the lifecycle tools, so the role
82
+ * lookup reads `account.json` fresh each call. (`enumerateValidAccountIds` is
83
+ * still cached for the dir-validity scan; that set is stable for the process.)
84
+ *
85
+ * Legacy tolerance is intentionally NOT here: a pre-migration account with no
86
+ * `role` does not count as house. `setup-account.sh` stamps `role:"house"` on a
87
+ * sole unlabelled account at seed time, so by manager boot the invariant holds.
88
+ */
89
+ export function resolveHouseAccountId(accountsDir: string): string {
90
+ const valid = enumerateValidAccountIds(accountsDir);
91
+ const houses: string[] = [];
92
+ for (const id of valid) {
93
+ const configPath = resolve(accountsDir, id, "account.json");
94
+ try {
95
+ const cfg = JSON.parse(readFileSync(configPath, "utf-8")) as {
96
+ role?: unknown;
97
+ };
98
+ if (cfg.role === "house") houses.push(id);
99
+ } catch {
100
+ // already excluded from `valid` if unparseable; a race here just skips it
101
+ }
102
+ }
103
+ if (houses.length !== 1) {
104
+ throw new Error(
105
+ `[account-registry] resolveHouseAccountId: houses=${houses.length} under ${accountsDir} ` +
106
+ `(${houses.join(", ") || "none"}) — exactly one role:"house" account is required. ` +
107
+ `Loud-fail rather than picking one silently.`,
108
+ );
109
+ }
110
+ return houses[0];
111
+ }
112
+
113
+ /**
114
+ * Like `resolveHouseAccountId`, but tolerant of the pre-migration window: when
115
+ * no account carries `role:"house"` AND exactly one valid account exists, that
116
+ * sole account is returned (it is about to be stamped `role:"house"` by
117
+ * `setup-account.sh`). This is the resolver every "the install's account" call
118
+ * site uses (boot account dir, installer account id, channel persist), so a
119
+ * legacy single-account install never throws while waiting for the seed-time
120
+ * role stamp — the WhatsApp persist path in particular must not drop a customer
121
+ * message in that window.
122
+ *
123
+ * Still loud-fails on genuine drift: zero accounts, multiple houses, or
124
+ * zero-house with more than one account (`houses=<n> total=<k>` signature).
125
+ */
126
+ export function resolveHouseOrSoleAccountId(accountsDir: string): string {
127
+ const valid = enumerateValidAccountIds(accountsDir);
128
+ const houses: string[] = [];
129
+ for (const id of valid) {
130
+ const configPath = resolve(accountsDir, id, "account.json");
131
+ try {
132
+ const cfg = JSON.parse(readFileSync(configPath, "utf-8")) as {
133
+ role?: unknown;
134
+ };
135
+ if (cfg.role === "house") houses.push(id);
136
+ } catch {
137
+ // already excluded from `valid` if unparseable
138
+ }
139
+ }
140
+ if (houses.length === 1) return houses[0];
141
+ if (houses.length === 0 && valid.length === 1) return valid[0];
142
+ throw new Error(
143
+ `[account-registry] resolveHouseOrSoleAccountId: houses=${houses.length} total=${valid.length} ` +
144
+ `under ${accountsDir} — expected exactly one role:"house" (or a single unlabelled account). ` +
145
+ `Loud-fail rather than picking one silently.`,
146
+ );
147
+ }
148
+
71
149
  /**
72
150
  * Resolves the canonical accounts dir from `MAXY_PLATFORM_ROOT`. Mirrors the
73
151
  * pattern at platform/ui/app/lib/claude-agent/account.ts:9
@@ -1,41 +1,41 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * MCP spawn-tee — parent-side stderr capture + lifecycle observability.
3
+ * MCP spawn-tee — in-process stderr capture + lifecycle observability.
4
4
  *
5
5
  * Claude Code spawns each MCP server itself; the platform never holds a
6
- * ChildProcess handle. This wrapper sits between Claude Code and the real
7
- * MCP server: Claude Code runs `node <this> <real-entry>`, and the wrapper
8
- * spawns the real entry with `stdio:['inherit','inherit','pipe']` so stdin
9
- * and stdout (the JSON-RPC channel) pass through byte-identical. Only stderr
10
- * is intercepted, and only for logging.
6
+ * ChildProcess handle. This shim sits between Claude Code and the real MCP
7
+ * server: Claude Code runs `node <this> <real-entry>`, and the shim runs the
8
+ * real entry IN ITS OWN PROCESS via dynamic import() — one node runtime, not
9
+ * two. Before importing, it replaces process.stderr.write with a tee so every
10
+ * stderr byte the server writes is mirrored to the log sinks; stdin and stdout
11
+ * (the JSON-RPC channel) are never touched.
11
12
  *
12
- * Because the wrapper IS the helper's parent, it observes the helper's true
13
- * exit code, signal, and lifetime for every death mode — including a SIGKILL
14
- * or a transport-layer crash that an in-process handler would miss.
13
+ * Claude Code CLI shim (this file) = node running <real-entry> in-process
14
+ * stdin/stdout : untouched (JSON-RPC channel)
15
+ * stderr : process.stderr.write teed → per-date + per-session + passthrough
15
16
  *
16
- * Claude Code CLI → wrapper (this file) child = node <real-entry>
17
- * child stdin/stdout : inherited (Claude Code pipe, untouched)
18
- * child stderr : piped → per-date log + per-session log + passthrough
19
- *
20
- * Destinations (Task 706):
17
+ * Destinations (Task 706) unchanged:
21
18
  * - `${LOG_DIR}/mcp-<name>-stderr-<date>.log` — per-date raw (back-compat;
22
19
  * the in-process mcp-stderr-tee and the [mcp-init-error] probe read it).
23
20
  * - `${LOG_DIR}/mcp-<name>-<SESSION_ID>.log` — per-session raw + lifecycle
24
- * lines; wrapper-only, so it never mixes the enumeration copy with the
25
- * live copy. Authoritative sink. Absent when SESSION_ID is unset
26
- * (enumeration spawn) — then the per-date file is the fallback.
21
+ * lines. Authoritative sink. Absent when SESSION_ID is unset (enumeration
22
+ * spawn) then the per-date file is the fallback.
27
23
  * - `server.log` via the loopback log-ingest route — best-effort mirror of
28
- * the [mcp-helper] lifecycle lines. A dropped POST loses nothing because
29
- * the per-session file already holds the line (written synchronously).
24
+ * the [mcp-helper] lifecycle lines.
30
25
  *
31
- * Lifecycle lines, correlation key `session=<id8> server=<name>`:
26
+ * Lifecycle lines, correlation key `session=<id8> server=<name>` — unchanged:
32
27
  * [mcp-helper] op=spawn ... pid= entry=
33
28
  * [mcp-helper] op=boot ... head=<first stderr bytes>
34
- * [mcp-helper] op=exit ... code= signal= lifetimeMs= stderr-tail= (always)
29
+ * [mcp-helper] op=exit ... code= signal= lifetimeMs= stderr-tail=
30
+ *
31
+ * Process model (Task 989): the shim IS the server's process. op=exit fires
32
+ * from process.on("exit"), so it covers a normal exit, a non-zero exit, an
33
+ * uncaught throw (code 1), and a catchable-signal exit (SIGTERM/SIGINT/SIGHUP,
34
+ * whose handlers record the signal name). An external SIGKILL is uncatchable
35
+ * and leaves no op=exit line — the per-session stderr tail already on disk and
36
+ * Claude Code's own transport-drop are the evidence for that death mode.
35
37
  *
36
- * The wrapper never writes to fd 1 (stdout) — that is the JSON-RPC channel.
37
- * SIGTERM/SIGINT are forwarded to the child so a Claude Code shutdown does
38
- * not orphan it; the child exit code/signal is propagated verbatim.
38
+ * The shim never writes to fd 1 (stdout) — that is the JSON-RPC channel.
39
39
  */
40
40
  export {};
41
41
  //# sourceMappingURL=index.d.ts.map
@@ -1,47 +1,47 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  /**
4
- * MCP spawn-tee — parent-side stderr capture + lifecycle observability.
4
+ * MCP spawn-tee — in-process stderr capture + lifecycle observability.
5
5
  *
6
6
  * Claude Code spawns each MCP server itself; the platform never holds a
7
- * ChildProcess handle. This wrapper sits between Claude Code and the real
8
- * MCP server: Claude Code runs `node <this> <real-entry>`, and the wrapper
9
- * spawns the real entry with `stdio:['inherit','inherit','pipe']` so stdin
10
- * and stdout (the JSON-RPC channel) pass through byte-identical. Only stderr
11
- * is intercepted, and only for logging.
7
+ * ChildProcess handle. This shim sits between Claude Code and the real MCP
8
+ * server: Claude Code runs `node <this> <real-entry>`, and the shim runs the
9
+ * real entry IN ITS OWN PROCESS via dynamic import() — one node runtime, not
10
+ * two. Before importing, it replaces process.stderr.write with a tee so every
11
+ * stderr byte the server writes is mirrored to the log sinks; stdin and stdout
12
+ * (the JSON-RPC channel) are never touched.
12
13
  *
13
- * Because the wrapper IS the helper's parent, it observes the helper's true
14
- * exit code, signal, and lifetime for every death mode — including a SIGKILL
15
- * or a transport-layer crash that an in-process handler would miss.
14
+ * Claude Code CLI shim (this file) = node running <real-entry> in-process
15
+ * stdin/stdout : untouched (JSON-RPC channel)
16
+ * stderr : process.stderr.write teed → per-date + per-session + passthrough
16
17
  *
17
- * Claude Code CLI → wrapper (this file) child = node <real-entry>
18
- * child stdin/stdout : inherited (Claude Code pipe, untouched)
19
- * child stderr : piped → per-date log + per-session log + passthrough
20
- *
21
- * Destinations (Task 706):
18
+ * Destinations (Task 706) unchanged:
22
19
  * - `${LOG_DIR}/mcp-<name>-stderr-<date>.log` — per-date raw (back-compat;
23
20
  * the in-process mcp-stderr-tee and the [mcp-init-error] probe read it).
24
21
  * - `${LOG_DIR}/mcp-<name>-<SESSION_ID>.log` — per-session raw + lifecycle
25
- * lines; wrapper-only, so it never mixes the enumeration copy with the
26
- * live copy. Authoritative sink. Absent when SESSION_ID is unset
27
- * (enumeration spawn) — then the per-date file is the fallback.
22
+ * lines. Authoritative sink. Absent when SESSION_ID is unset (enumeration
23
+ * spawn) then the per-date file is the fallback.
28
24
  * - `server.log` via the loopback log-ingest route — best-effort mirror of
29
- * the [mcp-helper] lifecycle lines. A dropped POST loses nothing because
30
- * the per-session file already holds the line (written synchronously).
25
+ * the [mcp-helper] lifecycle lines.
31
26
  *
32
- * Lifecycle lines, correlation key `session=<id8> server=<name>`:
27
+ * Lifecycle lines, correlation key `session=<id8> server=<name>` — unchanged:
33
28
  * [mcp-helper] op=spawn ... pid= entry=
34
29
  * [mcp-helper] op=boot ... head=<first stderr bytes>
35
- * [mcp-helper] op=exit ... code= signal= lifetimeMs= stderr-tail= (always)
30
+ * [mcp-helper] op=exit ... code= signal= lifetimeMs= stderr-tail=
31
+ *
32
+ * Process model (Task 989): the shim IS the server's process. op=exit fires
33
+ * from process.on("exit"), so it covers a normal exit, a non-zero exit, an
34
+ * uncaught throw (code 1), and a catchable-signal exit (SIGTERM/SIGINT/SIGHUP,
35
+ * whose handlers record the signal name). An external SIGKILL is uncatchable
36
+ * and leaves no op=exit line — the per-session stderr tail already on disk and
37
+ * Claude Code's own transport-drop are the evidence for that death mode.
36
38
  *
37
- * The wrapper never writes to fd 1 (stdout) — that is the JSON-RPC channel.
38
- * SIGTERM/SIGINT are forwarded to the child so a Claude Code shutdown does
39
- * not orphan it; the child exit code/signal is propagated verbatim.
39
+ * The shim never writes to fd 1 (stdout) — that is the JSON-RPC channel.
40
40
  */
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- const node_child_process_1 = require("node:child_process");
43
42
  const node_fs_1 = require("node:fs");
44
43
  const node_path_1 = require("node:path");
44
+ const node_url_1 = require("node:url");
45
45
  const SERVER_NAME = process.env.MCP_SPAWN_TEE_NAME ?? "unknown";
46
46
  const LOG_DIR = process.env.LOG_DIR;
47
47
  const SESSION_ID = process.env.SESSION_ID;
@@ -49,24 +49,37 @@ const PLATFORM_PORT = process.env.PLATFORM_PORT;
49
49
  const ENTRY = process.argv[2];
50
50
  const SESSION_ID8 = SESSION_ID ? SESSION_ID.slice(0, 8) : "—";
51
51
  const spawnStamp = Date.now();
52
- // Per-session raw + lifecycle log (Task 706). Wrapper-only. Empty SESSION_ID
53
- // (enumeration spawn) → undefined, and the per-date file is the fallback.
52
+ // Per-session raw + lifecycle log (Task 706). Empty SESSION_ID (enumeration
53
+ // spawn) → undefined, and the per-date file is the fallback.
54
54
  const perSessionPath = LOG_DIR && SESSION_ID
55
55
  ? (0, node_path_1.resolve)(LOG_DIR, `mcp-${SERVER_NAME}-${SESSION_ID}.log`)
56
56
  : undefined;
57
57
  const perDatePath = LOG_DIR
58
58
  ? (0, node_path_1.resolve)(LOG_DIR, `mcp-${SERVER_NAME}-stderr-${new Date(spawnStamp).toISOString().slice(0, 10)}.log`)
59
59
  : undefined;
60
- // Rolling tail of child stderr for op=exit. Capped so a chatty child cannot
60
+ // Rolling tail of entry stderr for op=exit. Capped so a chatty server cannot
61
61
  // grow the buffer without bound.
62
62
  const TAIL_CAP = 2048;
63
63
  let stderrTail = "";
64
64
  let bootEmitted = false;
65
+ let exitEmitted = false;
66
+ let exitSignal;
67
+ // The real stderr writer, captured before the tee replaces it. Lifecycle lines
68
+ // and stderr passthrough both go through this so they are never re-teed into
69
+ // the per-date sink nor recursed back into the patched writer.
70
+ const rawStderrWrite = process.stderr.write.bind(process.stderr);
71
+ // LOG_DIR is created once, lazily, on the first successful append — not per
72
+ // chunk. teeWrite runs on the server's own stderr hot path now, so a per-call
73
+ // mkdirSync would be two syscalls per log line for nothing.
74
+ let logDirReady = false;
65
75
  function appendSafe(path, data) {
66
76
  if (!path || !LOG_DIR)
67
77
  return;
68
78
  try {
69
- (0, node_fs_1.mkdirSync)(LOG_DIR, { recursive: true });
79
+ if (!logDirReady) {
80
+ (0, node_fs_1.mkdirSync)(LOG_DIR, { recursive: true });
81
+ logDirReady = true;
82
+ }
70
83
  (0, node_fs_1.appendFileSync)(path, data);
71
84
  }
72
85
  catch {
@@ -74,15 +87,16 @@ function appendSafe(path, data) {
74
87
  }
75
88
  }
76
89
  // Best-effort mirror of a lifecycle line to server.log via the loopback
77
- // log-ingest route. Fire-and-forget: the per-session file is authoritative,
78
- // so a dropped POST (unreachable port, process exiting) loses nothing.
90
+ // log-ingest route. Fire-and-forget: the per-session file is authoritative, so
91
+ // a dropped POST loses nothing. On the "exit" event the loop is stopping, so
92
+ // the op=exit mirror may not flush — the per-session line, written sync, holds.
79
93
  function postToServerLog(suffix, level) {
80
94
  if (!PLATFORM_PORT)
81
95
  return;
82
96
  try {
83
97
  const ctrl = new AbortController();
84
98
  const t = setTimeout(() => ctrl.abort(), 500);
85
- t.unref?.(); // never let the abort timer keep the wrapper's event loop alive
99
+ t.unref?.(); // never let the abort timer keep the shim's event loop alive
86
100
  void fetch(`http://127.0.0.1:${PLATFORM_PORT}/api/admin/log-ingest`, {
87
101
  method: "POST",
88
102
  headers: { "content-type": "application/json" },
@@ -95,14 +109,15 @@ function postToServerLog(suffix, level) {
95
109
  }
96
110
  }
97
111
  // Emit one [mcp-helper] lifecycle line: authoritative per-session file (sync,
98
- // survives process.exit), the wrapper's own stderr (journald / Claude Code
99
- // visibility), and a best-effort server.log mirror. `suffix` is the line body
100
- // after the tag and carries no newline (the log-ingest route rejects newlines).
112
+ // survives process.exit), the shim's own stderr via the RAW writer (journald /
113
+ // Claude Code visibility, never re-teed), and a best-effort server.log mirror.
114
+ // `suffix` is the line body after the tag and carries no newline (the
115
+ // log-ingest route rejects newlines).
101
116
  function emitLifecycle(suffix, level) {
102
117
  const line = `[mcp-helper] ${suffix}\n`;
103
118
  appendSafe(perSessionPath, line);
104
119
  try {
105
- process.stderr.write(line);
120
+ rawStderrWrite(line);
106
121
  }
107
122
  catch { /* stderr closed */ }
108
123
  postToServerLog(suffix, level);
@@ -111,49 +126,68 @@ if (!ENTRY) {
111
126
  emitLifecycle(`op=error session=${SESSION_ID8} server=${SERVER_NAME} reason="no entry given (argv[2] missing)"`, "error");
112
127
  process.exit(2);
113
128
  }
114
- const child = (0, node_child_process_1.spawn)(process.execPath, [ENTRY], {
115
- stdio: ["inherit", "inherit", "pipe"],
116
- env: process.env,
117
- });
118
- emitLifecycle(`op=spawn session=${SESSION_ID8} server=${SERVER_NAME} pid=${child.pid ?? -1} entry=${ENTRY}`, "info");
119
- child.on("error", (err) => {
120
- const msg = err instanceof Error ? err.message : String(err);
121
- emitLifecycle(`op=error session=${SESSION_ID8} server=${SERVER_NAME} reason=${JSON.stringify(`spawn error: ${msg}`)}`, "error");
122
- process.exit(127);
129
+ // Replace process.stderr.write with a tee: mirror every server stderr byte to
130
+ // the per-date and per-session sinks, keep a rolling tail for op=exit, emit
131
+ // op=boot on the first bytes, then pass the write through to the real stderr.
132
+ const teeWrite = ((...args) => {
133
+ const chunk = args[0];
134
+ appendSafe(perDatePath, chunk); // per-date raw (back-compat)
135
+ appendSafe(perSessionPath, chunk); // per-session raw (Task 706)
136
+ const text = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8");
137
+ stderrTail = (stderrTail + text).slice(-TAIL_CAP);
138
+ if (!bootEmitted) {
139
+ bootEmitted = true;
140
+ const head = text.split("\n")[0].slice(0, 200);
141
+ emitLifecycle(`op=boot session=${SESSION_ID8} server=${SERVER_NAME} head=${JSON.stringify(head)}`, "info");
142
+ }
143
+ return rawStderrWrite(...args); // passthrough
123
144
  });
124
- if (child.stderr) {
125
- child.stderr.on("data", (chunk) => {
126
- appendSafe(perDatePath, chunk); // per-date raw (back-compat)
127
- appendSafe(perSessionPath, chunk); // per-session raw (Task 706)
128
- try {
129
- process.stderr.write(chunk);
130
- }
131
- catch { /* stderr closed */ } // passthrough
132
- stderrTail = (stderrTail + chunk.toString("utf8")).slice(-TAIL_CAP);
133
- if (!bootEmitted) {
134
- bootEmitted = true;
135
- const head = chunk.toString("utf8").split("\n")[0].slice(0, 200);
136
- emitLifecycle(`op=boot session=${SESSION_ID8} server=${SERVER_NAME} head=${JSON.stringify(head)}`, "info");
145
+ process.stderr.write = teeWrite;
146
+ // Catchable-signal handling. The shim drives the exit ONLY when it is the sole
147
+ // listener for the signal — i.e. the imported entry installed no handler of its
148
+ // own. In that case it records the signal (so op=exit carries signal=<sig>) and
149
+ // exits with 128+signum, mirroring the prior wrapper's exit-status convention.
150
+ //
151
+ // When the entry DID install a handler, the shim defers entirely and records
152
+ // nothing: the entry's handler decides the exit code, and op=exit reflects that
153
+ // exit verbatim. This matches the prior two-process model, where a child that
154
+ // caught the signal and exited cleanly was observed as code=0 signal=— (a clean
155
+ // self-exit), not as a signal death. Setting exitSignal here unconditionally
156
+ // would mislabel every graceful signal-driven shutdown as a signal kill.
157
+ function onSignal(sig, code) {
158
+ return () => {
159
+ if (process.listenerCount(sig) === 1) {
160
+ exitSignal = sig;
161
+ process.exit(code);
137
162
  }
138
- });
163
+ };
139
164
  }
140
- const forward = (signal) => { if (!child.killed)
141
- child.kill(signal); };
142
- process.on("SIGTERM", () => forward("SIGTERM"));
143
- process.on("SIGINT", () => forward("SIGINT"));
144
- // Signal number, so a signal-killed child propagates as 128+signum (the
145
- // shell convention). `code` is null on a signal exit, so `128 + (code ?? 0)`
146
- // would collapse every signal to 128 and lose the signal identity in the
147
- // wrapper's own exit status.
148
- const SIGNUM = { SIGHUP: 1, SIGINT: 2, SIGQUIT: 3, SIGKILL: 9, SIGTERM: 15 };
149
- child.on("exit", (code, signal) => {
165
+ process.on("SIGTERM", onSignal("SIGTERM", 143));
166
+ process.on("SIGINT", onSignal("SIGINT", 130));
167
+ process.on("SIGHUP", onSignal("SIGHUP", 129));
168
+ // op=exit, emitted once from the process "exit" event. Sync-only — the loop is
169
+ // stopping. When a catchable signal caused the exit, report code=— signal=<sig>
170
+ // to match the prior wrapper's format; otherwise code=<exit code> signal=—.
171
+ process.on("exit", (code) => {
172
+ if (exitEmitted)
173
+ return;
174
+ exitEmitted = true;
150
175
  const lifetimeMs = Date.now() - spawnStamp;
151
176
  const tail = stderrTail.slice(-200);
152
- const level = signal || (code ?? 0) !== 0 ? "error" : "info";
153
- emitLifecycle(`op=exit session=${SESSION_ID8} server=${SERVER_NAME} pid=${child.pid ?? -1} ` +
154
- `code=${code ?? "—"} signal=${signal ?? "—"} lifetimeMs=${lifetimeMs} stderr-tail=${JSON.stringify(tail)}`, level);
155
- if (signal)
156
- process.exit(128 + (SIGNUM[signal] ?? 0));
157
- process.exit(code ?? 0);
177
+ const codeField = exitSignal ? "" : String(code);
178
+ const level = exitSignal || code !== 0 ? "error" : "info";
179
+ emitLifecycle(`op=exit session=${SESSION_ID8} server=${SERVER_NAME} pid=${process.pid} ` +
180
+ `code=${codeField} signal=${exitSignal ?? "—"} lifetimeMs=${lifetimeMs} stderr-tail=${JSON.stringify(tail)}`, level);
181
+ });
182
+ emitLifecycle(`op=spawn session=${SESSION_ID8} server=${SERVER_NAME} pid=${process.pid} entry=${ENTRY}`, "info");
183
+ // Run the real MCP server in THIS process. Dynamic import() (not top-level
184
+ // await — this file compiles to CommonJS) loads the ESM entry; a load-time
185
+ // failure mirrors the old child spawn-error path (op=error, exit 127). The
186
+ // op=exit handler is suppressed on this path so op=error stays the sole record.
187
+ import((0, node_url_1.pathToFileURL)(ENTRY).href).catch((err) => {
188
+ const msg = err instanceof Error ? err.message : String(err);
189
+ exitEmitted = true;
190
+ emitLifecycle(`op=error session=${SESSION_ID8} server=${SERVER_NAME} reason=${JSON.stringify(`import error: ${msg}`)}`, "error");
191
+ process.exit(127);
158
192
  });
159
193
  //# sourceMappingURL=index.js.map