@memnexus-ai/mx-agent-cli 0.1.294-next.0 → 0.1.294

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 (215) hide show
  1. package/dist/__tests__/config-sync.test.js +1 -54
  2. package/dist/__tests__/config-sync.test.js.map +1 -1
  3. package/dist/agent-config/hooks/auto-capture-precompact.sh +256 -74
  4. package/dist/agent-config/hooks/auto-capture-session-end.sh +254 -106
  5. package/dist/agent-config/hooks/auto-checkpoint.sh +5 -75
  6. package/dist/agent-config/hooks/observability-sync.sh +9 -46
  7. package/dist/commands/config-sync.d.ts +0 -37
  8. package/dist/commands/config-sync.d.ts.map +1 -1
  9. package/dist/commands/config-sync.js +18 -36
  10. package/dist/commands/config-sync.js.map +1 -1
  11. package/dist/commands/dashboard.d.ts.map +1 -1
  12. package/dist/commands/dashboard.js +2 -10
  13. package/dist/commands/dashboard.js.map +1 -1
  14. package/dist/commands/monitor.d.ts.map +1 -1
  15. package/dist/commands/monitor.js +2 -8
  16. package/dist/commands/monitor.js.map +1 -1
  17. package/dist/commands/start.d.ts +0 -4
  18. package/dist/commands/start.d.ts.map +1 -1
  19. package/dist/commands/start.js +0 -6
  20. package/dist/commands/start.js.map +1 -1
  21. package/dist/commands/update.d.ts +1 -13
  22. package/dist/commands/update.d.ts.map +1 -1
  23. package/dist/commands/update.js +1 -50
  24. package/dist/commands/update.js.map +1 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +3 -67
  27. package/dist/index.js.map +1 -1
  28. package/dist/lib/agent-session.d.ts +0 -12
  29. package/dist/lib/agent-session.d.ts.map +1 -1
  30. package/dist/lib/agent-session.js +41 -183
  31. package/dist/lib/agent-session.js.map +1 -1
  32. package/dist/lib/claude.d.ts +0 -12
  33. package/dist/lib/claude.d.ts.map +1 -1
  34. package/dist/lib/claude.js +2 -29
  35. package/dist/lib/claude.js.map +1 -1
  36. package/dist/lib/terminal-ui.d.ts.map +1 -1
  37. package/dist/lib/terminal-ui.js +1 -12
  38. package/dist/lib/terminal-ui.js.map +1 -1
  39. package/package.json +2 -3
  40. package/dist/__tests__/decision-check.test.d.ts +0 -2
  41. package/dist/__tests__/decision-check.test.d.ts.map +0 -1
  42. package/dist/__tests__/decision-check.test.js +0 -105
  43. package/dist/__tests__/decision-check.test.js.map +0 -1
  44. package/dist/__tests__/harness-extract.test.d.ts +0 -19
  45. package/dist/__tests__/harness-extract.test.d.ts.map +0 -1
  46. package/dist/__tests__/harness-extract.test.js +0 -566
  47. package/dist/__tests__/harness-extract.test.js.map +0 -1
  48. package/dist/__tests__/harness-wiring.test.d.ts +0 -16
  49. package/dist/__tests__/harness-wiring.test.d.ts.map +0 -1
  50. package/dist/__tests__/harness-wiring.test.js +0 -469
  51. package/dist/__tests__/harness-wiring.test.js.map +0 -1
  52. package/dist/__tests__/report-format.test.d.ts +0 -2
  53. package/dist/__tests__/report-format.test.d.ts.map +0 -1
  54. package/dist/__tests__/report-format.test.js +0 -219
  55. package/dist/__tests__/report-format.test.js.map +0 -1
  56. package/dist/__tests__/standards-injection.test.d.ts +0 -2
  57. package/dist/__tests__/standards-injection.test.d.ts.map +0 -1
  58. package/dist/__tests__/standards-injection.test.js +0 -157
  59. package/dist/__tests__/standards-injection.test.js.map +0 -1
  60. package/dist/commands/decision.d.ts +0 -20
  61. package/dist/commands/decision.d.ts.map +0 -1
  62. package/dist/commands/decision.js +0 -54
  63. package/dist/commands/decision.js.map +0 -1
  64. package/dist/commands/report.d.ts +0 -33
  65. package/dist/commands/report.d.ts.map +0 -1
  66. package/dist/commands/report.js +0 -73
  67. package/dist/commands/report.js.map +0 -1
  68. package/dist/lib/decision-check.d.ts +0 -48
  69. package/dist/lib/decision-check.d.ts.map +0 -1
  70. package/dist/lib/decision-check.js +0 -149
  71. package/dist/lib/decision-check.js.map +0 -1
  72. package/dist/lib/harness/console/announce.d.ts +0 -45
  73. package/dist/lib/harness/console/announce.d.ts.map +0 -1
  74. package/dist/lib/harness/console/announce.js +0 -48
  75. package/dist/lib/harness/console/announce.js.map +0 -1
  76. package/dist/lib/harness/console/announce.test.d.ts +0 -2
  77. package/dist/lib/harness/console/announce.test.d.ts.map +0 -1
  78. package/dist/lib/harness/console/announce.test.js +0 -104
  79. package/dist/lib/harness/console/announce.test.js.map +0 -1
  80. package/dist/lib/harness/console/by-session.d.ts +0 -50
  81. package/dist/lib/harness/console/by-session.d.ts.map +0 -1
  82. package/dist/lib/harness/console/by-session.js +0 -83
  83. package/dist/lib/harness/console/by-session.js.map +0 -1
  84. package/dist/lib/harness/console/by-session.test.d.ts +0 -2
  85. package/dist/lib/harness/console/by-session.test.d.ts.map +0 -1
  86. package/dist/lib/harness/console/by-session.test.js +0 -86
  87. package/dist/lib/harness/console/by-session.test.js.map +0 -1
  88. package/dist/lib/harness/console/console.d.ts +0 -67
  89. package/dist/lib/harness/console/console.d.ts.map +0 -1
  90. package/dist/lib/harness/console/console.js +0 -373
  91. package/dist/lib/harness/console/console.js.map +0 -1
  92. package/dist/lib/harness/console/console.test.d.ts +0 -2
  93. package/dist/lib/harness/console/console.test.d.ts.map +0 -1
  94. package/dist/lib/harness/console/console.test.js +0 -269
  95. package/dist/lib/harness/console/console.test.js.map +0 -1
  96. package/dist/lib/harness/console/decision-request.d.ts +0 -32
  97. package/dist/lib/harness/console/decision-request.d.ts.map +0 -1
  98. package/dist/lib/harness/console/decision-request.js +0 -112
  99. package/dist/lib/harness/console/decision-request.js.map +0 -1
  100. package/dist/lib/harness/console/decision-request.test.d.ts +0 -2
  101. package/dist/lib/harness/console/decision-request.test.d.ts.map +0 -1
  102. package/dist/lib/harness/console/decision-request.test.js +0 -75
  103. package/dist/lib/harness/console/decision-request.test.js.map +0 -1
  104. package/dist/lib/harness/console/fixtures.d.ts +0 -16
  105. package/dist/lib/harness/console/fixtures.d.ts.map +0 -1
  106. package/dist/lib/harness/console/fixtures.js +0 -49
  107. package/dist/lib/harness/console/fixtures.js.map +0 -1
  108. package/dist/lib/harness/console/index.d.ts +0 -15
  109. package/dist/lib/harness/console/index.d.ts.map +0 -1
  110. package/dist/lib/harness/console/index.js +0 -14
  111. package/dist/lib/harness/console/index.js.map +0 -1
  112. package/dist/lib/harness/console/rank.d.ts +0 -73
  113. package/dist/lib/harness/console/rank.d.ts.map +0 -1
  114. package/dist/lib/harness/console/rank.js +0 -113
  115. package/dist/lib/harness/console/rank.js.map +0 -1
  116. package/dist/lib/harness/console/rank.test.d.ts +0 -2
  117. package/dist/lib/harness/console/rank.test.d.ts.map +0 -1
  118. package/dist/lib/harness/console/rank.test.js +0 -124
  119. package/dist/lib/harness/console/rank.test.js.map +0 -1
  120. package/dist/lib/harness/console/render.d.ts +0 -22
  121. package/dist/lib/harness/console/render.d.ts.map +0 -1
  122. package/dist/lib/harness/console/render.js +0 -65
  123. package/dist/lib/harness/console/render.js.map +0 -1
  124. package/dist/lib/harness/console/render.test.d.ts +0 -2
  125. package/dist/lib/harness/console/render.test.d.ts.map +0 -1
  126. package/dist/lib/harness/console/render.test.js +0 -96
  127. package/dist/lib/harness/console/render.test.js.map +0 -1
  128. package/dist/lib/harness/console/replay-cli.test.d.ts +0 -2
  129. package/dist/lib/harness/console/replay-cli.test.d.ts.map +0 -1
  130. package/dist/lib/harness/console/replay-cli.test.js +0 -96
  131. package/dist/lib/harness/console/replay-cli.test.js.map +0 -1
  132. package/dist/lib/harness/console/replay.d.ts +0 -36
  133. package/dist/lib/harness/console/replay.d.ts.map +0 -1
  134. package/dist/lib/harness/console/replay.js +0 -59
  135. package/dist/lib/harness/console/replay.js.map +0 -1
  136. package/dist/lib/harness/console/replay.test.d.ts +0 -2
  137. package/dist/lib/harness/console/replay.test.d.ts.map +0 -1
  138. package/dist/lib/harness/console/replay.test.js +0 -81
  139. package/dist/lib/harness/console/replay.test.js.map +0 -1
  140. package/dist/lib/harness/console/turn-reader.d.ts +0 -50
  141. package/dist/lib/harness/console/turn-reader.d.ts.map +0 -1
  142. package/dist/lib/harness/console/turn-reader.js +0 -184
  143. package/dist/lib/harness/console/turn-reader.js.map +0 -1
  144. package/dist/lib/harness/console/turn-reader.test.d.ts +0 -2
  145. package/dist/lib/harness/console/turn-reader.test.d.ts.map +0 -1
  146. package/dist/lib/harness/console/turn-reader.test.js +0 -108
  147. package/dist/lib/harness/console/turn-reader.test.js.map +0 -1
  148. package/dist/lib/harness/console/types.d.ts +0 -109
  149. package/dist/lib/harness/console/types.d.ts.map +0 -1
  150. package/dist/lib/harness/console/types.js +0 -2
  151. package/dist/lib/harness/console/types.js.map +0 -1
  152. package/dist/lib/harness/contract.d.ts +0 -229
  153. package/dist/lib/harness/contract.d.ts.map +0 -1
  154. package/dist/lib/harness/contract.js +0 -22
  155. package/dist/lib/harness/contract.js.map +0 -1
  156. package/dist/lib/harness/credential.d.ts +0 -23
  157. package/dist/lib/harness/credential.d.ts.map +0 -1
  158. package/dist/lib/harness/credential.js +0 -81
  159. package/dist/lib/harness/credential.js.map +0 -1
  160. package/dist/lib/harness/credential.test.d.ts +0 -2
  161. package/dist/lib/harness/credential.test.d.ts.map +0 -1
  162. package/dist/lib/harness/credential.test.js +0 -74
  163. package/dist/lib/harness/credential.test.js.map +0 -1
  164. package/dist/lib/harness/evaluate.d.ts +0 -390
  165. package/dist/lib/harness/evaluate.d.ts.map +0 -1
  166. package/dist/lib/harness/evaluate.js +0 -988
  167. package/dist/lib/harness/evaluate.js.map +0 -1
  168. package/dist/lib/harness/evaluate.test.d.ts +0 -2
  169. package/dist/lib/harness/evaluate.test.d.ts.map +0 -1
  170. package/dist/lib/harness/evaluate.test.js +0 -621
  171. package/dist/lib/harness/evaluate.test.js.map +0 -1
  172. package/dist/lib/harness/extract.d.ts +0 -152
  173. package/dist/lib/harness/extract.d.ts.map +0 -1
  174. package/dist/lib/harness/extract.js +0 -1601
  175. package/dist/lib/harness/extract.js.map +0 -1
  176. package/dist/lib/harness/gate.d.ts +0 -48
  177. package/dist/lib/harness/gate.d.ts.map +0 -1
  178. package/dist/lib/harness/gate.js +0 -60
  179. package/dist/lib/harness/gate.js.map +0 -1
  180. package/dist/lib/harness/gate.test.d.ts +0 -2
  181. package/dist/lib/harness/gate.test.d.ts.map +0 -1
  182. package/dist/lib/harness/gate.test.js +0 -111
  183. package/dist/lib/harness/gate.test.js.map +0 -1
  184. package/dist/lib/harness/self-check.d.ts +0 -59
  185. package/dist/lib/harness/self-check.d.ts.map +0 -1
  186. package/dist/lib/harness/self-check.js +0 -56
  187. package/dist/lib/harness/self-check.js.map +0 -1
  188. package/dist/lib/harness/self-check.test.d.ts +0 -2
  189. package/dist/lib/harness/self-check.test.d.ts.map +0 -1
  190. package/dist/lib/harness/self-check.test.js +0 -43
  191. package/dist/lib/harness/self-check.test.js.map +0 -1
  192. package/dist/lib/harness/transcript.d.ts +0 -65
  193. package/dist/lib/harness/transcript.d.ts.map +0 -1
  194. package/dist/lib/harness/transcript.js +0 -144
  195. package/dist/lib/harness/transcript.js.map +0 -1
  196. package/dist/lib/harness/transcript.test.d.ts +0 -2
  197. package/dist/lib/harness/transcript.test.d.ts.map +0 -1
  198. package/dist/lib/harness/transcript.test.js +0 -118
  199. package/dist/lib/harness/transcript.test.js.map +0 -1
  200. package/dist/lib/harness-wiring.d.ts +0 -145
  201. package/dist/lib/harness-wiring.d.ts.map +0 -1
  202. package/dist/lib/harness-wiring.js +0 -280
  203. package/dist/lib/harness-wiring.js.map +0 -1
  204. package/dist/lib/report-format.d.ts +0 -132
  205. package/dist/lib/report-format.d.ts.map +0 -1
  206. package/dist/lib/report-format.js +0 -317
  207. package/dist/lib/report-format.js.map +0 -1
  208. package/dist/lib/standards.d.ts +0 -90
  209. package/dist/lib/standards.d.ts.map +0 -1
  210. package/dist/lib/standards.js +0 -194
  211. package/dist/lib/standards.js.map +0 -1
  212. package/dist/standards/decision-request.md +0 -90
  213. package/dist/standards/fail-loud-standard.md +0 -495
  214. package/dist/standards/report-format.md +0 -120
  215. package/dist/standards/writing-standard.md +0 -240
@@ -150,87 +150,13 @@ else
150
150
  SEGMENT=$(cat "$TRANSCRIPT_PATH")
151
151
  fi
152
152
 
153
- # ── 3a. Subagent transcripts (#5436) ─────────────────────────────────────
154
- #
155
- # Claude Code writes a subagent's tool calls to its own file, never to the
156
- # lead's transcript. The layout is a sibling directory of the session file:
157
- #
158
- # <project-dir>/<session-id>.jsonl the lead
159
- # <project-dir>/<session-id>/subagents/agent-*.jsonl one per subagent
160
- #
161
- # So a filter over the lead's transcript alone cannot see a delegated write or a
162
- # delegated commit. Measured over 5,713 real transcripts, subagents made 11,627
163
- # of 12,343 repository writes (94%) against the lead's 716. That is why every
164
- # auto-captured memory written between 2026-07-11 and 2026-08-13 reported
165
- # "Files modified: None". Committing is delegated for the same reason, so the
166
- # commit count at section 3b read 0 in 19 of 20 sampled memories.
167
- #
168
- # SCOPE. Exactly two fields read these files: the Write/Edit target paths below
169
- # and the `git commit` count in section 3b. Nothing else does. In particular the
170
- # lead's BASH_COMMANDS still feeds PR_REFS alone, because PR_REFS saves
171
- # `gh pr create|merge` command lines verbatim and a `--body` argument carries
172
- # prose. Widening that to subagents would put more verbatim transcript text into
173
- # MemNexus, which this change deliberately does not do.
174
- #
175
- # The directory is absent for a session that spawned no subagent. That is the
176
- # common case and not an error.
177
- SUBAGENT_DIR="${TRANSCRIPT_PATH%.jsonl}/subagents"
178
- SUBAGENT_FILES=()
179
- if [[ -d "$SUBAGENT_DIR" ]]; then
180
- # Match the lead's since-last-save window: a subagent transcript written before
181
- # the previous capture was already reported by that capture. On the first
182
- # capture no state file exists and every subagent file is in scope. This also
183
- # bounds the read, which matters because this hook fires on every Stop.
184
- _mx_newer=()
185
- [[ -f "$STATE_FILE" ]] && _mx_newer=(-newer "$STATE_FILE")
186
- # STATED CAP, not a silent one. The largest subagent count in the fleet's 301
187
- # real sessions is 189, so 200 is above the observed range. A session past the
188
- # cap reports the first 200 files and undercounts the rest.
189
- while IFS= read -r _mx_f; do
190
- [[ -n "$_mx_f" ]] && SUBAGENT_FILES+=("$_mx_f")
191
- done < <(find "$SUBAGENT_DIR" -maxdepth 1 -type f -name 'agent-*.jsonl' \
192
- "${_mx_newer[@]}" 2>/dev/null \
193
- | head -n "${AUTO_CAPTURE_MAX_SUBAGENTS:-200}")
194
- fi
195
-
196
- # `-R` with `fromjson?` skips a malformed line instead of aborting the whole
197
- # extraction. The lead's path below keeps plain `jq -r`, which aborts; that is
198
- # left as it was. This path reads many files at once, so one bad line in one of
199
- # them must not empty the result. The archive holds 15 unparseable lines across
200
- # 5,713 files, which makes that a real case rather than a hypothetical one.
201
- SUBAGENT_WRITES=""
202
- if (( ${#SUBAGENT_FILES[@]} > 0 )); then
203
- SUBAGENT_WRITES=$(jq -R -r 'fromjson? // empty |
204
- select(.type == "assistant") |
205
- .message.content[]? |
206
- select(.type == "tool_use" and (.name == "Write" or .name == "Edit")) |
207
- .input.file_path // empty
208
- ' "${SUBAGENT_FILES[@]}" 2>/dev/null || true)
209
- fi
210
-
211
- # Files modified in this segment, by the lead and by its subagents
212
- LEAD_WRITES=$(echo "$SEGMENT" | jq -r '
153
+ # Files modified in this segment
154
+ FILES_MODIFIED=$(echo "$SEGMENT" | jq -r '
213
155
  select(.type == "assistant") |
214
156
  .message.content[]? |
215
157
  select(.type == "tool_use" and (.name == "Write" or .name == "Edit")) |
216
158
  .input.file_path // empty
217
- ' 2>/dev/null | grep -v '^$' || true)
218
-
219
- _MX_ALL_WRITES=$(printf '%s\n%s\n' "$LEAD_WRITES" "$SUBAGENT_WRITES" \
220
- | grep -v '^$' | sort -u)
221
-
222
- # Repository paths first, scratchpad paths after, one shared cap of 20.
223
- #
224
- # Ordering matters now that subagent writes are in scope. A fifth of subagent
225
- # writes target a scratchpad under /tmp/claude-1000/<project>/<session>/, and
226
- # "/tmp" sorts ahead of "/workspace", so a plain `sort -u | head -20` filled the
227
- # list with temporary files and dropped every repository path. The field would
228
- # have stopped saying "None" while still not naming the work. This reorders one
229
- # list; it captures no path it did not already capture, and the cap is the
230
- # same 20 as before.
231
- FILES_MODIFIED=$( { printf '%s\n' "$_MX_ALL_WRITES" | grep -v '^/tmp/' || true; \
232
- printf '%s\n' "$_MX_ALL_WRITES" | grep '^/tmp/' || true; } \
233
- | grep -v '^$' | head -20)
159
+ ' 2>/dev/null | grep -v '^$' | sort -u | head -20)
234
160
 
235
161
  # Tool call count
236
162
  TOOL_COUNT=$(echo "$SEGMENT" | jq -r '
@@ -254,35 +180,7 @@ BASH_COMMANDS=$(echo "$SEGMENT" | jq -r '
254
180
  .input.command // empty
255
181
  ' 2>/dev/null)
256
182
 
257
- # ── 3b. Commit count (#5436) ─────────────────────────────────────────────
258
- #
259
- # The line this replaces had two defects. It counted only the lead's Bash calls,
260
- # and committing is usually delegated, so 19 of 20 sampled memories reported 0.
261
- # And `grep -c` prints its count AND exits 1 when nothing matches, so the old
262
- # `|| echo "0"` fired as well and the variable became two lines. That is the
263
- # doubled "0" every one of those memories rendered.
264
- #
265
- # `|| true` keeps the assignment off the ERR trap installed at the top of this
266
- # file without adding a second line: grep has already printed its count.
267
- #
268
- # SUBAGENT_BASH is counted and never saved. No command text from it reaches the
269
- # memory: PR_REFS below reads the lead's BASH_COMMANDS only.
270
- GIT_COMMITS_LEAD=$(printf '%s\n' "$BASH_COMMANDS" | grep -cE 'git commit' 2>/dev/null || true)
271
-
272
- SUBAGENT_BASH=""
273
- if (( ${#SUBAGENT_FILES[@]} > 0 )); then
274
- SUBAGENT_BASH=$(jq -R -r 'fromjson? // empty |
275
- select(.type == "assistant") |
276
- .message.content[]? |
277
- select(.type == "tool_use" and .name == "Bash") |
278
- .input.command // empty
279
- ' "${SUBAGENT_FILES[@]}" 2>/dev/null || true)
280
- fi
281
- GIT_COMMITS_SUB=$(printf '%s\n' "$SUBAGENT_BASH" | grep -cE 'git commit' 2>/dev/null || true)
282
-
283
- [[ "$GIT_COMMITS_LEAD" =~ ^[0-9]+$ ]] || GIT_COMMITS_LEAD=0
284
- [[ "$GIT_COMMITS_SUB" =~ ^[0-9]+$ ]] || GIT_COMMITS_SUB=0
285
- GIT_COMMITS=$(( GIT_COMMITS_LEAD + GIT_COMMITS_SUB ))
183
+ GIT_COMMITS=$(echo "$BASH_COMMANDS" | grep -cE 'git commit' 2>/dev/null || echo "0")
286
184
  PR_REFS=$(echo "$BASH_COMMANDS" | grep -oE 'gh pr (create|merge) [^\n]*' 2>/dev/null | head -5)
287
185
 
288
186
  # PR and issue references. Extracted in a pipeline so the raw transcript text is
@@ -346,6 +244,255 @@ fi
346
244
  # Truncate
347
245
  CONTENT=$(echo "$CONTENT" | head -c 5000)
348
246
 
247
+ # ── 4b. Resolve project scope (codeContext) ──────────────────────────────
248
+ # Auto-captured memories carried no scope at all, so they leaked into every
249
+ # team's scoped searches and polluted build_context (#5158, #4957). PR #3281
250
+ # fixed that for the commit path only; this is the same resolution for the
251
+ # capture hooks, using the same sources and the same precedence. Ported from
252
+ # the reviewed customer hook cli/hooks/capture-session-end.sh — port from that
253
+ # file's FINAL state, not from an earlier draft: two of the properties below
254
+ # were blocking security findings.
255
+ #
256
+ # Mirrors resolveCommitCodeContext() in cli/src/lib/commit-context/code-context.ts:
257
+ # 1. MX_CODE_CONTEXT env var — operator-controlled, higher trust. It wins over
258
+ # the file (CWE-345): a checked-in file from an untrusted clone must not
259
+ # override the scope the operator set explicitly.
260
+ # 2. <projectRoot>/.mx-code-context.json — repo-controlled, lower trust.
261
+ # 3. Neither present or neither usable -> NO scope. The memory is created
262
+ # unlabeled, exactly as it was before this block existed. No default is
263
+ # invented: a guessed label is invisible to the scope that should see it
264
+ # and visible to one that should not, which is worse than no label.
265
+ #
266
+ # Every command in this block must end up returning 0. The EXIT/ERR trap set in
267
+ # section 3 exits the hook, so a non-zero status here would abandon the capture
268
+ # over a missing config file — a far worse regression than the unlabeled
269
+ # memories this fixes. The absent-config path is the common one today.
270
+ CODE_CONTEXT_MAX_BYTES=65536
271
+
272
+ # Walk up for the git project root, mirroring findProjectRoot() in
273
+ # cli/src/lib/commit-context/local-cache.ts. `-e`, not `-d`: inside a git
274
+ # worktree `.git` is a FILE, and the worktree root — not the main repo — is the
275
+ # project root (the TS uses fs.existsSync, which makes no such distinction).
276
+ # In the fleet this is the ONLY case that occurs: every agent works in a
277
+ # worktree, so `-d` would silently resolve no scope on every machine.
278
+ find_project_root() { # <start-dir> -> project root on stdout, or nothing
279
+ local dir
280
+ dir=$(cd "$1" 2>/dev/null && pwd -P) || return 0
281
+ while [[ -n "$dir" && "$dir" != "/" ]]; do
282
+ if [[ -e "$dir/.git" ]]; then
283
+ printf '%s' "$dir"
284
+ return 0
285
+ fi
286
+ dir=$(dirname "$dir" 2>/dev/null) || return 0
287
+ done
288
+ return 0
289
+ }
290
+
291
+ # Keep only the six known keys whose values are non-empty strings, cap each,
292
+ # and emit NOTHING when nothing usable remains — so an empty or scope-free
293
+ # object is never passed as if it were a label. Malformed JSON makes jq exit
294
+ # non-zero with no output, which lands in the same "no scope" branch.
295
+ #
296
+ # `-s` (slurp) is load-bearing, not a style choice. jq accepts a STREAM of JSON
297
+ # documents: without `-s` the filter runs once per document and `jq -c` prints
298
+ # one line each, so a config holding two concatenated or newline-separated
299
+ # objects produced a MULTI-LINE string that was passed verbatim as one
300
+ # --code-context argument. parseCodeContextFlag() (cli/src/commands/memories.ts)
301
+ # JSON.parse()s it and process.exit(1)s before any network call — the capture is
302
+ # lost silently, triggered by cloning a repo. The `!= "{}"` guard below did not
303
+ # catch it either: `{"zzz":1}{"team":"b"}` normalizes to the two lines
304
+ # `{}` + `{"team":"b"}`, and that whole string is not `{}`. Slurping into an
305
+ # array and taking the first element means exactly one line can ever be emitted.
306
+ #
307
+ # The cap is 100 CODEPOINTS, deliberately below the server's 200. The server's
308
+ # CodeContextSchema uses Zod `.max(200)`, which counts UTF-16 code units, while
309
+ # jq's `[0:N]` slices by codepoint. Every astral-plane character (emoji, CJK
310
+ # extensions, mathematical alphanumerics) is 1 codepoint but 2 UTF-16 units, so
311
+ # a `[0:200]` cap emitted values of up to 400 units — over the server maximum,
312
+ # a 400 from the API, and the same silent capture loss. Since no codepoint
313
+ # exceeds 2 UTF-16 units, 100 codepoints can never exceed 200 units. Scope
314
+ # labels are short names (product/team/service); the extra truncation is
315
+ # unreachable in practice, and truncating is recoverable where a rejected
316
+ # request is not. Do NOT "restore" this to 200.
317
+ normalize_code_context() { # JSON on stdin -> normalized JSON object, or nothing
318
+ local out
319
+ out=$(jq -s -c '
320
+ (.[0] // {}) as $raw
321
+ | if ($raw | type) == "object" then
322
+ reduce ("product","repo","service","component","team","role") as $k ({};
323
+ if ($raw[$k] | type) == "string" and ($raw[$k] | length) > 0
324
+ then .[$k] = ($raw[$k][0:100])
325
+ else . end)
326
+ else {} end
327
+ ' 2>/dev/null) || return 0
328
+ # The newline test is belt-and-braces: `-s` already guarantees one document,
329
+ # and jq -c escapes newlines inside string values. Nothing multi-line may ever
330
+ # reach --code-context again, whatever the filter above does.
331
+ if [[ -n "$out" && "$out" != "{}" && "$out" != "null" && "$out" != *$'\n'* ]]; then
332
+ printf '%s' "$out"
333
+ fi
334
+ return 0
335
+ }
336
+
337
+ CODE_CONTEXT_JSON=""
338
+
339
+ # 1. Env var. Tested for presence only — the value is never echoed or logged.
340
+ if [[ -n "${MX_CODE_CONTEXT:-}" ]]; then
341
+ CODE_CONTEXT_JSON=$(printf '%s' "$MX_CODE_CONTEXT" | normalize_code_context) || CODE_CONTEXT_JSON=""
342
+ fi
343
+
344
+ # 2. Repo file — consulted only when the env var yielded nothing usable.
345
+ if [[ -z "$CODE_CONTEXT_JSON" ]]; then
346
+ # ORDER IS LOAD-BEARING: harness-provided project dir, then the worktree path,
347
+ # then the cwd. Each source is TRIED IN TURN, and the transcript's directory
348
+ # is not a source at all. Both properties are corrections to earlier forms and
349
+ # both are pinned by the suite; read this before rewriting the expression.
350
+ #
351
+ # WHY EACH SOURCE IS TRIED SEPARATELY. The earlier form collapsed all three
352
+ # into ONE call: `find_project_root "${CLAUDE_PROJECT_DIR:-${CLAUDE_WORKTREE_PATH:-$(pwd)}}"`.
353
+ # `:-` substitutes only when a variable is UNSET or EMPTY — never when it is
354
+ # set to something useless. So a CLAUDE_PROJECT_DIR that is set but resolves
355
+ # to no repository (a stale path, a deleted directory, a path outside any
356
+ # checkout) consumed the single attempt and the cwd was NEVER consulted.
357
+ # Measured: CLAUDE_PROJECT_DIR=/proc and =/nonexistent/zz each produced
358
+ # {"product":"home","team":"DOTFILES-WRONG-SCOPE"} from the $HOME config.
359
+ # The loop below consults the next source instead, which `:-` cannot express.
360
+ #
361
+ # WHY THE TRANSCRIPT DIRECTORY IS NOT A SOURCE. It has no legitimate resolving
362
+ # power, and the reason is DEPTH-INDEPENDENT — do not restate it as a claim
363
+ # about the transcript's nesting. An earlier version of this comment argued
364
+ # from a flat ~/.claude/projects/<slug>/<id>.jsonl layout and enumerated the
365
+ # three directories a walk could reach. That premise is false here. Measured
366
+ # on this machine: transcripts sit at BOTH depth 2 and depth 4 under
367
+ # ~/.claude/projects — 310 at depth 2, 5621 deeper — so any argument of the
368
+ # form "the walk can only ever reach these three named directories" does not
369
+ # hold. The premise that does hold, and that does not depend on depth:
370
+ #
371
+ # .git anywhere under ~/.claude, any depth: 0 (also no .hg, no .svn)
372
+ #
373
+ # An upward walk from any transcript directory traverses only directories
374
+ # inside ~/.claude, none of which carries a repository marker, so it cannot
375
+ # terminate inside that subtree at any depth. That leaves $HOME as its ONLY
376
+ # reachable non-empty outcome, which is precisely the defect this block exists
377
+ # to prevent: keeping dotfiles in git is common, so $HOME/.git exists on many
378
+ # machines, and with $HOME/.mx-code-context.json present every capture from
379
+ # every worktree on the machine was labelled from the home config. A
380
+ # confidently wrong label is worse than no label — invisible to the scope that
381
+ # should see it, visible to one that must not. Do not reintroduce it.
382
+ #
383
+ # CAVEAT, stated because this is a claim about a directory OUTSIDE this
384
+ # repository: it could stop being true, and nothing in this codebase would
385
+ # notice if a repository ever appeared under ~/.claude. Removing the leg does
386
+ # not depend on the caveat holding — the leg is simply gone — but any future
387
+ # argument to reinstate it must re-measure rather than cite this line.
388
+ #
389
+ # WHY $HOME IS REFUSED OUTRIGHT, AND WHY BOTH SIDES ARE CANONICALIZED. Belt
390
+ # and braces for the residual where the cwd itself is $HOME or a directory
391
+ # beneath it whose nearest .git is $HOME's. It also closes a consequence that
392
+ # is easy to miss: CC_ROOT is the basis of the symlink containment test below,
393
+ # so a CC_ROOT of $HOME silently widens "contained in the project" to
394
+ # "anywhere in the home directory", and $HOME/.mx-code-context.json symlinked
395
+ # to any other file under $HOME would resolve and be used. The cost is not
396
+ # supporting a user whose real project IS their home directory; that is the
397
+ # same confusion we want rejected.
398
+ #
399
+ # The comparison MUST canonicalize $HOME. CC_ROOT always arrives canonical —
400
+ # find_project_root builds it with `cd … && pwd -P` — while $HOME arrives
401
+ # verbatim from the environment, so a literal `==` against "$HOME" misses on
402
+ # every spelling that is not byte-identical. Four spellings were measured
403
+ # leaking the home label through a symlinked $HOME/.mx-code-context.json: a
404
+ # trailing slash ("/home/x/"), a "/." suffix ("/home/x/."), $HOME set to a
405
+ # SYMLINK to the real home, and a cwd reached through that same symlink.
406
+ # Symlinked home directories are routine — macOS firmlinks, NFS corporate
407
+ # homes, container images mapping /home/user onto another volume — so the
408
+ # trigger is a normal configuration and the miss is silent: the refusal does
409
+ # not fire, and the containment widening above returns with it. `cd … && pwd -P`
410
+ # is the same primitive find_project_root uses and handles all four in one step.
411
+ #
412
+ # HOME unset, or pointing at nothing, leaves _home_real empty and no refusal
413
+ # fires. That is unchanged from the previous `-n "${HOME:-}"` guard, not a
414
+ # regression. The `|| _home_real=""` is REQUIRED, not decorative: without it a
415
+ # failing `cd` leaves the assignment non-zero and last on its path, which fires
416
+ # the ERR trap and abandons the capture.
417
+ #
418
+ # ERR-TRAP SAFETY — verified, not assumed, because a regression here fires
419
+ # `trap 'exit 0' ERR` and silently abandons the capture. The loop body's only
420
+ # status-bearing statement is a `$(...)` assignment of find_project_root, which
421
+ # returns 0 on every path including the `cd` failure. The break is written as
422
+ # an `if` block for the same reason the $HOME refusal is. The earlier one-line
423
+ # form `[[ -n "$CC_ROOT" ]] && break` returns 1 whenever the loop CONTINUES —
424
+ # an earlier version of this comment claimed it returned 0, which was wrong. It
425
+ # was safe only POSITIONALLY: as a non-final command of an `&&` list it is
426
+ # exempt under the errexit rules, so nothing fired. That safety depends on
427
+ # where the statement sits and would evaporate the moment this loop became the
428
+ # last statement of a function: the function would return 1 and any `$(...)`
429
+ # call of it would fire the trap. The `if` form returns 0 and removes the
430
+ # positional dependence.
431
+ #
432
+ # The same reasoning is why the $HOME refusal is an `if` block ON PURPOSE: the
433
+ # one-line `[[ "$CC_ROOT" == "$_home_real" ]] && CC_ROOT=""` returns 1 when the
434
+ # test is false, and was measured firing the ERR trap when it landed last in an
435
+ # enclosing block. `pwd 2>/dev/null` keeps a deleted cwd from printing "error
436
+ # retrieving current directory" onto the hook's real stderr. This safety comes
437
+ # from find_project_root always returning 0, not from this hook omitting
438
+ # `set -E`, so it survives someone adding `set -E` later. Do not introduce a
439
+ # bare command on this path.
440
+ CC_ROOT=""
441
+ for _cand in "${CLAUDE_PROJECT_DIR:-}" "${CLAUDE_WORKTREE_PATH:-}" "$(pwd 2>/dev/null)"; do
442
+ [[ -n "$_cand" ]] || continue
443
+ CC_ROOT=$(find_project_root "$_cand")
444
+ if [[ -n "$CC_ROOT" ]]; then break; fi
445
+ done
446
+ _home_real=$(cd "${HOME:-/nonexistent}" 2>/dev/null && pwd -P) || _home_real=""
447
+ if [[ -n "$_home_real" && "$CC_ROOT" == "$_home_real" ]]; then
448
+ CC_ROOT=""
449
+ fi
450
+ CC_FILE="${CC_ROOT}/.mx-code-context.json"
451
+ if [[ -n "$CC_ROOT" ]]; then
452
+ # CWE-59. `-f`, `-r` and the read all follow symlinks, so a repo could check
453
+ # in `.mx-code-context.json` as a symlink to any file on the machine and the
454
+ # six allowlisted keys of it would be uploaded to MemNexus. Resolve the path
455
+ # and require the result to stay inside the project root. Read through the
456
+ # RESOLVED path, so the link is not traversed a second time.
457
+ CC_REAL=$(realpath "$CC_FILE" 2>/dev/null || printf '')
458
+ if [[ -n "$CC_REAL" && "$CC_REAL" == "$CC_ROOT"/* && -f "$CC_REAL" && -r "$CC_REAL" ]]; then
459
+ # One open, bounded at the source (same 64KB bound as the TS resolver).
460
+ # The previous form size-checked with `wc -c` and then re-opened for jq:
461
+ # two opens on a path an untrusted repo controls, so a concurrent writer
462
+ # could present a small file to `wc` and a huge one to `jq` (TOCTOU) on a
463
+ # hot per-turn path. `head -c` bounds the read itself, which changes >64KB
464
+ # from "skipped" to "truncated, then rejected as invalid JSON" — the same
465
+ # observable outcome, no scope.
466
+ #
467
+ # `2>/dev/null` precedes the input redirect ON PURPOSE. Redirections apply
468
+ # left to right, so with `<"$CC_REAL" 2>/dev/null` a file that turned
469
+ # unreadable between the `-r` test and the open makes bash print the full
470
+ # path and "Permission denied" to the hook's REAL stderr, before the
471
+ # discard takes effect. Verified: reversing these two words reintroduces
472
+ # that leak. Do not reorder.
473
+ #
474
+ # The bytes are PIPED into jq, never routed through a variable. Bash
475
+ # strips NUL bytes out of command-substitution output and warns while
476
+ # doing it — from the parent shell reading the substitution, so the
477
+ # `2>/dev/null` above does not suppress that warning. Via a variable,
478
+ # `{"team":"a<NUL>b"}` printed a warning naming this script and line to
479
+ # the hook's real stderr, and then RESOLVED to team "ab": input the file
480
+ # does not contain, silently repaired before validation instead of
481
+ # rejected by it. Piping keeps the NUL inside jq, which rejects the
482
+ # document as malformed. Do not reintroduce an intermediate variable.
483
+ CODE_CONTEXT_JSON=$(head -c "$CODE_CONTEXT_MAX_BYTES" 2>/dev/null <"$CC_REAL" | normalize_code_context) || CODE_CONTEXT_JSON=""
484
+ fi
485
+ fi
486
+ fi
487
+
488
+ # Nothing resolved -> pass nothing at all, so the create command line is
489
+ # byte-identical to the pre-scope version. The `[@]+` guard keeps an empty array
490
+ # safe to expand under `set -u` on bash < 4.4.
491
+ CODE_CONTEXT_ARGS=()
492
+ if [[ -n "$CODE_CONTEXT_JSON" ]]; then
493
+ CODE_CONTEXT_ARGS=(--code-context "$CODE_CONTEXT_JSON")
494
+ fi
495
+
349
496
  # ── 5. Verbatim-transcript guard (fail-closed, fail-loud) ───────────────
350
497
  #
351
498
  # SCOPE — TEXT BLOCKS ONLY. This guard re-extracts
@@ -425,6 +572,7 @@ if mx_verbatim_guard "$TRANSCRIPT_PATH" "$CONTENT"; then
425
572
  --conversation-id "NEW" \
426
573
  --content "$CONTENT" \
427
574
  --topics "auto-captured,session-summary" \
575
+ "${CODE_CONTEXT_ARGS[@]+"${CODE_CONTEXT_ARGS[@]}"}" \
428
576
  >/dev/null 2>&1 || true
429
577
  fi
430
578
 
@@ -44,67 +44,14 @@ command -v mx >/dev/null 2>&1 || exit 0
44
44
  # captured deliberately and are not free text; the point is that the guard's
45
45
  # silence about them is a scope limit, not evidence they are safe.
46
46
 
47
- # Subagent transcripts (#5436)
48
- #
49
- # A subagent's tool calls are written to its own file, never to the lead's
50
- # transcript:
51
- #
52
- # <project-dir>/<session-id>.jsonl the lead
53
- # <project-dir>/<session-id>/subagents/agent-*.jsonl one per subagent
54
- #
55
- # Subagents made 11,627 of 12,343 repository writes (94%) across 5,713 measured
56
- # transcripts, so a checkpoint built from the lead alone names few of the files
57
- # the session actually touched. Only file paths are read here, which is what
58
- # this hook already captures.
59
- #
60
- # RECENCY, matching the lead's `tail -500`: take the most recently written
61
- # subagent transcripts and the last 500 lines of each. A checkpoint answers
62
- # "what was I just doing", so the newest work is the part worth keeping.
63
- #
64
- # STATED CAP, not a silent one. 20 files at 500 lines bounds the read; the
65
- # fleet's largest session has 189 subagent transcripts, so a busy session
66
- # contributes its 20 most recent and not the rest.
67
- SUBAGENT_DIR="${TRANSCRIPT_PATH%.jsonl}/subagents"
68
- SUBAGENT_TOUCHED=""
69
- if [[ -d "$SUBAGENT_DIR" ]]; then
70
- _mx_files=()
71
- while IFS= read -r _mx_f; do
72
- [[ -n "$_mx_f" ]] && _mx_files+=("$SUBAGENT_DIR/$_mx_f")
73
- done < <(ls -1t "$SUBAGENT_DIR" 2>/dev/null | grep -E '^agent-.*[.]jsonl$' \
74
- | head -n "${AUTO_CHECKPOINT_MAX_SUBAGENTS:-20}")
75
- if (( ${#_mx_files[@]} > 0 )); then
76
- # `-R` with `fromjson?` skips a malformed line rather than aborting the whole
77
- # extraction, which matters when reading many files at once.
78
- SUBAGENT_TOUCHED=$(for _mx_f in "${_mx_files[@]}"; do tail -500 "$_mx_f" 2>/dev/null; done \
79
- | jq -R -r 'fromjson? // empty |
80
- select(.type == "assistant") |
81
- .message.content[]? |
82
- select(.type == "tool_use") |
83
- .input.file_path // empty
84
- ' 2>/dev/null || true)
85
- fi
86
- fi
87
-
88
- # Files touched (from tool_use entries: Read, Edit, Write, Glob), by the lead
89
- # and by its subagents
90
- LEAD_TOUCHED=$(tail -500 "$TRANSCRIPT_PATH" | \
47
+ # Files touched (from tool_use entries: Read, Edit, Write, Glob)
48
+ FILES_TOUCHED=$(tail -500 "$TRANSCRIPT_PATH" | \
91
49
  jq -r '
92
50
  select(.type == "assistant") |
93
51
  .message.content[]? |
94
52
  select(.type == "tool_use") |
95
53
  .input.file_path // empty
96
- ' 2>/dev/null | grep -v '^$' || true)
97
-
98
- _MX_ALL_TOUCHED=$(printf '%s\n%s\n' "$LEAD_TOUCHED" "$SUBAGENT_TOUCHED" \
99
- | grep -v '^$' | sort -u)
100
-
101
- # Repository paths first, scratchpad paths after, one shared cap of 30. "/tmp"
102
- # sorts ahead of "/workspace", so a plain sort would fill the list with
103
- # temporary files and drop the repository paths a resuming session needs.
104
- # Nothing new is captured; one list is reordered and the cap is unchanged.
105
- FILES_TOUCHED=$( { printf '%s\n' "$_MX_ALL_TOUCHED" | grep -v '^/tmp/' || true; \
106
- printf '%s\n' "$_MX_ALL_TOUCHED" | grep '^/tmp/' || true; } \
107
- | grep -v '^$' | head -30)
54
+ ' 2>/dev/null | grep -v '^$' | sort -u | head -30)
108
55
 
109
56
  # Git branch and PR references
110
57
  GIT_BRANCH="${CLAUDE_WORKTREE_BRANCH:-}"
@@ -127,27 +74,10 @@ WORKTREE_NAME="${CLAUDE_WORKTREE_NAME:-unknown}"
127
74
 
128
75
  # ── 3. Format checkpoint memory ─────────────────────────────────────────
129
76
 
130
- # Build files list, one bare path per line
131
- #
132
- # NO "- " BULLET, AND THAT IS LOAD-BEARING (#5436). Section 4's guard treats a
133
- # transcript line as prose when it is at least 40 characters and contains a
134
- # space, then refuses the save if any such line appears in the assembled
135
- # content. Assistants routinely write bulleted path lists in their own prose:
136
- #
137
- # - /workspace/.worktrees/marketing-20260313/marketing-site/app/globals.css
138
- #
139
- # Rendering the same path with the same "- " prefix reproduces that line
140
- # exactly, so the guard matched and the checkpoint was refused. Measured on the
141
- # 20-session sample in the pull request, that silently dropped 2 checkpoints
142
- # once subagent paths widened this list.
143
- #
144
- # A bare path cannot collide: it contains no space, so it can never qualify as
145
- # a candidate pattern, and the bulleted form of it is not a substring of this
146
- # content. This changes how the list renders and changes nothing about which
147
- # paths are captured. The guard itself is untouched.
77
+ # Build files list (indented)
148
78
  FILES_LIST=""
149
79
  if [[ -n "$FILES_TOUCHED" ]]; then
150
- FILES_LIST="$FILES_TOUCHED"
80
+ FILES_LIST=$(echo "$FILES_TOUCHED" | sed 's/^/- /')
151
81
  fi
152
82
 
153
83
  CONTENT="[Auto-Checkpoint] Context compaction (${TRIGGER}) — ${WORKTREE_NAME}
@@ -87,65 +87,28 @@ set -euo pipefail
87
87
  WORKTREE_PATH="${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || echo '')}"
88
88
  [ -z "$WORKTREE_PATH" ] && exit 0
89
89
 
90
+ API_URL="https://api.dev.memnexus.ai/internal/observability"
90
91
  LOG_DIR="$HOME/.mx-agent/logs/startup"
91
92
  WORKTREE_NAME="$(basename "$WORKTREE_PATH")"
92
93
  LOG_FILE="$LOG_DIR/${WORKTREE_NAME}.jsonl"
93
94
 
94
- # One writer for the startup JSONL, so every outcome this hook can report goes
95
- # through the same sanitizer. Args: level, message, exit_code, detail.
96
- log_startup() {
97
- mkdir -p "$LOG_DIR" 2>/dev/null || true
98
- local ts safe
99
- ts=$(date -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || echo "unknown")
100
- # Strip control characters, escape JSON metacharacters, truncate.
101
- safe=$(printf '%s' "$4" | tr -cd '[:print:]\n' | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g' | head -c 500)
102
- printf '{"ts":"%s","level":"%s","source":"observability-sync","msg":"%s","exit_code":%d,"detail":"%s"}\n' \
103
- "$ts" "$1" "$2" "$3" "$safe" >> "$LOG_FILE" 2>/dev/null || true
104
- }
105
-
106
- # The observability endpoint is operator configuration, not a constant.
107
- #
108
- # It used to be hardcoded to MemNexus's gateway, and this hook ships in the
109
- # published package, so every install posted its session telemetry to
110
- # memnexus.ai whether or not the operator ran MemNexus. Resolution order is now
111
- # the environment, then `~/.mx-agent/config.json`, and an unset endpoint skips
112
- # the sync and says so rather than defaulting to somebody else's host.
113
- #
114
- # `mx-agent sync` reads the config value itself, so the check here only has to
115
- # answer whether the key exists. It greps rather than parsing, because a jq
116
- # dependency on a hook path is defect #5434 and this one must not acquire one.
117
- # A key present with an empty value reaches `sync`, which validates the scheme
118
- # and reports its own error.
119
- CONFIG_FILE="$HOME/.mx-agent/config.json"
120
- API_URL="${MX_OBSERVABILITY_API_URL:-}"
121
- if [ -z "$API_URL" ] && ! grep -q '"observability-api-url"' "$CONFIG_FILE" 2>/dev/null; then
122
- # Could-not-run, which is neither passed nor failed. Recorded so it surfaces
123
- # at next session start rather than reading as a quiet success.
124
- log_startup "warn" "observability endpoint not configured, sync skipped" 0 \
125
- "set it with: mx-agent config set observability-api-url <url> (or export MX_OBSERVABILITY_API_URL)"
126
- exit 0
127
- fi
128
-
129
95
  # Run the sync, capture exit code
130
96
  SYNC_OUTPUT=""
131
97
  SYNC_EXIT=0
132
98
  # Bounded: this is a network call to the observability API. Without a bound a
133
99
  # hung or black-holed connection holds the Stop hook open. On timeout `timeout`
134
- # exits 124, which reaches the existing failure path below, logged to the
100
+ # exits 124, which lands on the existing failure path below — logged to the
135
101
  # startup JSONL, still exit 0. Fail-open contract unchanged.
136
- #
137
- # With no environment override the flag is omitted entirely rather than passed
138
- # empty, so `mx-agent sync` resolves the endpoint from config itself and there
139
- # is one resolution order rather than two.
140
- if [ -n "$API_URL" ]; then
141
- SYNC_OUTPUT=$(timeout "${OBS_SYNC_TIMEOUT_SECS:-25}" mx-agent sync --api-url "$API_URL" --quiet 2>&1) || SYNC_EXIT=$?
142
- else
143
- SYNC_OUTPUT=$(timeout "${OBS_SYNC_TIMEOUT_SECS:-25}" mx-agent sync --quiet 2>&1) || SYNC_EXIT=$?
144
- fi
102
+ SYNC_OUTPUT=$(timeout "${OBS_SYNC_TIMEOUT_SECS:-25}" mx-agent sync --api-url "$API_URL" --quiet 2>&1) || SYNC_EXIT=$?
145
103
 
146
104
  if [ "$SYNC_EXIT" -ne 0 ]; then
147
105
  # Log failure to the local startup JSONL (v2.58 infrastructure)
148
- log_startup "warn" "sync failed at session end" "$SYNC_EXIT" "$SYNC_OUTPUT"
106
+ mkdir -p "$LOG_DIR" 2>/dev/null || true
107
+ TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || echo "unknown")
108
+ # Sanitize output: strip control characters, escape JSON metacharacters, truncate
109
+ SAFE_OUTPUT=$(printf '%s' "$SYNC_OUTPUT" | tr -cd '[:print:]\n' | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g' | head -c 500)
110
+ printf '{"ts":"%s","level":"warn","source":"observability-sync","msg":"sync failed at session end","exit_code":%d,"detail":"%s"}\n' \
111
+ "$TIMESTAMP" "$SYNC_EXIT" "$SAFE_OUTPUT" >> "$LOG_FILE" 2>/dev/null || true
149
112
  fi
150
113
 
151
114
  # Always exit 0 — fail-open contract
@@ -21,21 +21,12 @@
21
21
  * lagging /workspace main worktree, so a dry-run can legitimately show
22
22
  * adds/edits until this merges.)
23
23
  */
24
- import { resolveProjectConfig } from '../lib/project-config.js';
25
24
  export interface ConfigSyncOptions {
26
25
  apply?: boolean;
27
26
  force?: boolean;
28
27
  allowDirty?: boolean;
29
28
  }
30
29
  type Action = 'add' | 'update' | 'unchanged' | 'skip-local';
31
- interface Plan {
32
- rel: string;
33
- action: Action;
34
- }
35
- interface SyncState {
36
- generation: number | null;
37
- files: Record<string, string>;
38
- }
39
30
  /**
40
31
  * Classify one bundle file against the project copy using the recorded baseline.
41
32
  * ours = hash of the project's current file (null if absent)
@@ -43,34 +34,6 @@ interface SyncState {
43
34
  * theirs = hash of the bundle file
44
35
  */
45
36
  export declare function classifyFile(ours: string | null, base: string | null, theirs: string): Action;
46
- /**
47
- * The plan, computed without printing or exiting.
48
- *
49
- * `runConfigSync` prints it and `mx-agent update` reports a one-line summary
50
- * from it. A command that runs outside a project has to be able to ask this
51
- * question and get an answer rather than a `process.exit`, which is why the
52
- * failure cases are returned instead of thrown.
53
- */
54
- export type ConfigSyncPlan = {
55
- ok: false;
56
- reason: 'no-project-root' | 'no-bundle' | 'no-project-config';
57
- detail: string;
58
- } | {
59
- ok: true;
60
- projectRoot: string;
61
- projectConfigDir: string;
62
- cfg: ReturnType<typeof resolveProjectConfig>;
63
- bundleDir: string;
64
- bundleGen: number | null;
65
- projectGen: number | null;
66
- state: SyncState;
67
- bundleHashes: Record<string, string>;
68
- plans: Plan[];
69
- counts: Record<Action, number>;
70
- /** Files the bundle would add or update, ignoring locally-edited ones. */
71
- pending: number;
72
- };
73
- export declare function planConfigSync(): ConfigSyncPlan;
74
37
  export declare function runConfigSync(options?: ConfigSyncOptions): void;
75
38
  export {};
76
39
  //# sourceMappingURL=config-sync.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config-sync.d.ts","sourceRoot":"","sources":["../../src/commands/config-sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAOH,OAAO,EAAqB,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAYnF,MAAM,WAAW,iBAAiB;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,KAAK,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,WAAW,GAAG,YAAY,CAAC;AAE5D,UAAU,IAAI;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,SAAS;IACjB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAgBD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK7F;AAkBD;;;;;;;GAOG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,iBAAiB,GAAG,WAAW,GAAG,mBAAmB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC5F;IACE,EAAE,EAAE,IAAI,CAAC;IACT,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;IACzB,GAAG,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,SAAS,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,0EAA0E;IAC1E,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEN,wBAAgB,cAAc,IAAI,cAAc,CAyC/C;AAED,wBAAgB,aAAa,CAAC,OAAO,GAAE,iBAAsB,GAAG,IAAI,CAqHnE"}
1
+ {"version":3,"file":"config-sync.d.ts","sourceRoot":"","sources":["../../src/commands/config-sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAmBH,MAAM,WAAW,iBAAiB;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,KAAK,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,WAAW,GAAG,YAAY,CAAC;AA0B5D;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK7F;AAkBD,wBAAgB,aAAa,CAAC,OAAO,GAAE,iBAAsB,GAAG,IAAI,CA8InE"}