@memnexus-ai/mx-agent-cli 0.1.296-next.2 → 0.1.296-next.4

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 (82) hide show
  1. package/dist/__tests__/coordination-401-production-wiring.test.js +67 -1
  2. package/dist/__tests__/coordination-401-production-wiring.test.js.map +1 -1
  3. package/dist/__tests__/coordination-brief-pin.test.js +6 -2
  4. package/dist/__tests__/coordination-brief-pin.test.js.map +1 -1
  5. package/dist/__tests__/coordination-brief.test.js +58 -1
  6. package/dist/__tests__/coordination-brief.test.js.map +1 -1
  7. package/dist/__tests__/coordination-client.test.js +113 -1
  8. package/dist/__tests__/coordination-client.test.js.map +1 -1
  9. package/dist/__tests__/coordination-command.test.js +288 -2
  10. package/dist/__tests__/coordination-command.test.js.map +1 -1
  11. package/dist/__tests__/coordination-credential-endpoint.test.js +9 -7
  12. package/dist/__tests__/coordination-credential-endpoint.test.js.map +1 -1
  13. package/dist/__tests__/coordination-expected-team.test.d.ts +19 -0
  14. package/dist/__tests__/coordination-expected-team.test.d.ts.map +1 -0
  15. package/dist/__tests__/coordination-expected-team.test.js +423 -0
  16. package/dist/__tests__/coordination-expected-team.test.js.map +1 -0
  17. package/dist/__tests__/coordination-plain-shell-commands.test.js +494 -26
  18. package/dist/__tests__/coordination-plain-shell-commands.test.js.map +1 -1
  19. package/dist/__tests__/memory-log-result.test.js +36 -0
  20. package/dist/__tests__/memory-log-result.test.js.map +1 -1
  21. package/dist/__tests__/provision-completion-rows.test.js +329 -64
  22. package/dist/__tests__/provision-completion-rows.test.js.map +1 -1
  23. package/dist/__tests__/provision-resume.test.js +146 -0
  24. package/dist/__tests__/provision-resume.test.js.map +1 -1
  25. package/dist/__tests__/provision-row12-stub-server.test.d.ts +21 -0
  26. package/dist/__tests__/provision-row12-stub-server.test.d.ts.map +1 -0
  27. package/dist/__tests__/provision-row12-stub-server.test.js +262 -0
  28. package/dist/__tests__/provision-row12-stub-server.test.js.map +1 -0
  29. package/dist/__tests__/runtime-codex-adapter.test.js +33 -1
  30. package/dist/__tests__/runtime-codex-adapter.test.js.map +1 -1
  31. package/dist/__tests__/start-runtime-wiring.test.js +150 -2
  32. package/dist/__tests__/start-runtime-wiring.test.js.map +1 -1
  33. package/dist/__tests__/team-name-propagation.test.js +63 -0
  34. package/dist/__tests__/team-name-propagation.test.js.map +1 -1
  35. package/dist/__tests__/team-token-probe.test.d.ts +16 -0
  36. package/dist/__tests__/team-token-probe.test.d.ts.map +1 -0
  37. package/dist/__tests__/team-token-probe.test.js +295 -0
  38. package/dist/__tests__/team-token-probe.test.js.map +1 -0
  39. package/dist/__tests__/verify-hooks-check-all.test.js +12 -2
  40. package/dist/__tests__/verify-hooks-check-all.test.js.map +1 -1
  41. package/dist/agent-config/hooks/coordination-brief.sh +30 -6
  42. package/dist/commands/coordination.d.ts +36 -1
  43. package/dist/commands/coordination.d.ts.map +1 -1
  44. package/dist/commands/coordination.js +231 -15
  45. package/dist/commands/coordination.js.map +1 -1
  46. package/dist/commands/create.d.ts +28 -10
  47. package/dist/commands/create.d.ts.map +1 -1
  48. package/dist/commands/create.js +123 -33
  49. package/dist/commands/create.js.map +1 -1
  50. package/dist/lib/agent-session.d.ts.map +1 -1
  51. package/dist/lib/agent-session.js +4 -0
  52. package/dist/lib/agent-session.js.map +1 -1
  53. package/dist/lib/claude.d.ts.map +1 -1
  54. package/dist/lib/claude.js +7 -0
  55. package/dist/lib/claude.js.map +1 -1
  56. package/dist/lib/coordination-client.d.ts +11 -0
  57. package/dist/lib/coordination-client.d.ts.map +1 -1
  58. package/dist/lib/coordination-client.js +79 -18
  59. package/dist/lib/coordination-client.js.map +1 -1
  60. package/dist/lib/coordination-token.d.ts +20 -0
  61. package/dist/lib/coordination-token.d.ts.map +1 -1
  62. package/dist/lib/coordination-token.js +1 -1
  63. package/dist/lib/coordination-token.js.map +1 -1
  64. package/dist/lib/coordination-worktree.d.ts +0 -26
  65. package/dist/lib/coordination-worktree.d.ts.map +1 -1
  66. package/dist/lib/coordination-worktree.js +61 -5
  67. package/dist/lib/coordination-worktree.js.map +1 -1
  68. package/dist/lib/memory.d.ts.map +1 -1
  69. package/dist/lib/memory.js +15 -3
  70. package/dist/lib/memory.js.map +1 -1
  71. package/dist/lib/runtime/codex-adapter.d.ts.map +1 -1
  72. package/dist/lib/runtime/codex-adapter.js +4 -0
  73. package/dist/lib/runtime/codex-adapter.js.map +1 -1
  74. package/dist/lib/team-token-probe.d.ts +120 -0
  75. package/dist/lib/team-token-probe.d.ts.map +1 -0
  76. package/dist/lib/team-token-probe.js +202 -0
  77. package/dist/lib/team-token-probe.js.map +1 -0
  78. package/dist/lib/templates.d.ts +1 -1
  79. package/dist/lib/templates.d.ts.map +1 -1
  80. package/dist/lib/templates.js +1 -1
  81. package/dist/standards/known-issues-standard.md +380 -0
  82. package/package.json +1 -1
@@ -12,11 +12,11 @@
12
12
  import { existsSync, readFileSync, writeFileSync, readdirSync, statSync, realpathSync } from 'node:fs';
13
13
  import { spawnSync } from 'node:child_process';
14
14
  import { join } from 'node:path';
15
- import { HttpCoordinationClient, resolveClientConfig, resolveCoordinationMode, ServiceUnreachableError, ServiceHttpError, CONFIG_KEY_URL, URL_SOURCE_LABEL, resolveCoordinationUrl, } from '../lib/coordination-client.js';
15
+ import { HttpCoordinationClient, resolveClientConfig, resolveCoordinationMode, ServiceUnreachableError, ServiceHttpError, ServiceBodyError, CONFIG_KEY_URL, URL_SOURCE_LABEL, resolveCoordinationUrl, } from '../lib/coordination-client.js';
16
16
  import { resolveCoordinationToken, refreshCoordinationToken, usesCredentialEndpoint, ENV_KEY_EXPECT_PROJECT, ENV_KEY_EXPECT_TEAM, } from '../lib/coordination-token.js';
17
- import { locateCoordinationWorktree, noMarkerLines, readMarkerLine, readTeamMarker, } from '../lib/coordination-worktree.js';
17
+ import { defaultTrackedCoordinationFiles, locateCoordinationWorktree, noMarkerLines, readMarkerLine, readTeamMarker, } from '../lib/coordination-worktree.js';
18
18
  import { isValidProjectSlug, invalidProjectSlugMessage, PROJECT_SLUG_RE_SOURCE } from '../lib/slug.js';
19
- import { appendFallbackEntry, flushFallback } from '../lib/coordination-fallback.js';
19
+ import { appendFallbackEntry, flushFallback, idempotencyKeyFor } from '../lib/coordination-fallback.js';
20
20
  import { sanitizeField, renderMessages, renderRoutingDescription, renderPipelineExtractionWarning, findOverBudgetDropped, } from '../lib/coordination-render.js';
21
21
  import { escalateToOutbox, UnreadableOutboxError, readEscalationsForTeam, renderEscalationBlocks, resolveMemoryTeam, resolveProductSlug, } from '../lib/coordination-memory.js';
22
22
  import { loadProjectConfig } from '../lib/project-config.js';
@@ -114,6 +114,10 @@ function readMarkerTeam() {
114
114
  * none of the three sources yields a team, exit non-zero with a fix hint.
115
115
  */
116
116
  function resolveTeam(explicit) {
117
+ // #548: a session started as one team never sends as the team a wrong marker
118
+ // names. Runs before the marker is used for `from`, and before any fallback
119
+ // entry is written under it.
120
+ refuseOnExpectedTeamMismatch();
117
121
  const team = explicit || readMarkerTeam() || process.env.CLAUDE_WORKTREE_NAME;
118
122
  if (!team) {
119
123
  console.error('Error: could not determine your team for the coordination `from` field. ' +
@@ -131,6 +135,135 @@ function resolveTeam(explicit) {
131
135
  }
132
136
  return team;
133
137
  }
138
+ /**
139
+ * Stable first words of the expected-team refusal (#548). The session-start hook
140
+ * greps the CLI's stderr for this (coordination-brief.sh), so keep the two in step.
141
+ */
142
+ export const EXPECTED_TEAM_MISMATCH_MARKER = 'coordination: WRONG TEAM';
143
+ /** A team name for a refusal line: printed only when it is a valid slug (#525 security review). */
144
+ function shownTeam(team) {
145
+ return isValidProjectSlug(team) ? `"${team}"` : 'a value that is not a valid team slug';
146
+ }
147
+ /**
148
+ * The expected-team interlock (#548, the fix #292 proposed for #527 defect 1).
149
+ *
150
+ * `mx-agent start <team>` puts the team it was asked to start into the session
151
+ * environment as MX_COORD_EXPECT_TEAM (`lib/claude.ts`, `lib/agent-session.ts`,
152
+ * `lib/runtime/codex-adapter.ts`). That value comes from the command line, not
153
+ * from any file in the worktree, so it is independent of the marker. When it is
154
+ * set and non-empty and the marker names a different team, this returns the
155
+ * refusal lines. Otherwise it returns undefined and the caller behaves as before.
156
+ *
157
+ * WHAT THIS IS NOT. It does not choose the team a command acts as: the marker
158
+ * still names that team (MF-2, `lib/coordination-token.ts` header). It does not
159
+ * read CLAUDE_TEAM_NAME, the worktree name or any other heuristic. It can only
160
+ * refuse, so it cannot select a wrong team.
161
+ *
162
+ * Cases where it returns undefined, so today's handling applies unchanged:
163
+ * - MX_COORD_EXPECT_TEAM absent or empty;
164
+ * - no worktree located, or no marker, or a marker that is unreadable, empty or
165
+ * refused by `readTeamMarker` (each command already reports those itself);
166
+ * - the marker names the expected team.
167
+ *
168
+ * It reads the marker only. It reads no credential cache and requests nothing.
169
+ * `prepareClient` runs it before any credential resolution, which covers every
170
+ * queue command; `resolveTeam` and `runStatus` also run it, because escalate
171
+ * builds its `from` and status reads the cache without `prepareClient`. So a
172
+ * mismatch makes no token request and uses no cached or configured token.
173
+ */
174
+ export function expectedTeamRefusal(env = process.env, locate) {
175
+ const expected = env[ENV_KEY_EXPECT_TEAM];
176
+ if (!expected)
177
+ return undefined;
178
+ let located;
179
+ let marker;
180
+ try {
181
+ located = locateCoordinationWorktree(env, locate);
182
+ if (!located.ok)
183
+ return undefined;
184
+ marker = readTeamMarker(located, locate);
185
+ }
186
+ catch {
187
+ return undefined;
188
+ }
189
+ if (marker.kind !== 'ok')
190
+ return undefined;
191
+ // `from` uses the trimmed line (readMarkerTeam) and the token path strips all
192
+ // whitespace (firstLineNoSpace). Both must equal the expectation.
193
+ const trimmed = marker.line.trim();
194
+ if (!trimmed)
195
+ return undefined;
196
+ if (trimmed === expected && marker.line.replace(/\s+/g, '') === expected)
197
+ return undefined;
198
+ const markerPath = sanitizeField(join(located.root, '.mx-coordination-team'));
199
+ const want = isValidProjectSlug(expected) ? expected : '<team>';
200
+ return [
201
+ `${EXPECTED_TEAM_MISMATCH_MARKER}: refusing to act as team ${shownTeam(trimmed)}. ` +
202
+ `The .mx-coordination-team marker at ${markerPath} names team ${shownTeam(trimmed)}, ` +
203
+ `but this session was started as team ${shownTeam(expected)} (${ENV_KEY_EXPECT_TEAM}, set by \`mx-agent start\`). ` +
204
+ 'No credential was requested and no cached token was read.',
205
+ ...expectedTeamRemedy(located.root, want, locate),
206
+ ];
207
+ }
208
+ /**
209
+ * The remedy under the expected-team refusal, by whether git tracks the marker
210
+ * (red-team on PR #549). Asked with the same call `cwdMarkerRefusal` uses
211
+ * (`defaultTrackedCoordinationFiles`, or the injected `trackedFiles`), and only
212
+ * on the refusal path, so a matching marker runs no git.
213
+ *
214
+ * - Untracked: delete it and run `start`, which writes the marker only when the
215
+ * file is absent.
216
+ * - Tracked or staged: every worktree on the branch shares that one file.
217
+ * Deleting it and letting `start` write this team's name leaves the tracked
218
+ * file modified, and a later `commit -a` would give every worktree this
219
+ * team's name. So no delete line: point at the tracked-marker steps that
220
+ * `coordination status` prints from a plain terminal, where the marker is
221
+ * located from the current directory and `cwdMarkerRefusal` runs.
222
+ * - git could not say: the delete line is not printed either, because it is
223
+ * unsafe in the tracked case; the operator checks first.
224
+ */
225
+ function expectedTeamRemedy(root, want, locate) {
226
+ let tracked;
227
+ try {
228
+ tracked = (locate?.trackedFiles ?? defaultTrackedCoordinationFiles)(root);
229
+ }
230
+ catch {
231
+ tracked = undefined;
232
+ }
233
+ const statusSteps = 'run `mx-agent coordination status` from a terminal that `mx-agent start` did not open, ' +
234
+ 'with that worktree as the current directory, and follow the tracked-marker steps it prints';
235
+ if (tracked === undefined) {
236
+ return [
237
+ ' git could not say whether the marker is tracked, so no fix is printed here. ' +
238
+ 'Run `git ls-files -- .mx-coordination-team` in that worktree. ' +
239
+ `If it lists the file, every worktree on the branch shares it: ${statusSteps}. ` +
240
+ `If it lists nothing, delete the marker and run \`mx-agent start ${want}\`.`,
241
+ ];
242
+ }
243
+ if (tracked.some((f) => f.toLowerCase() === '.mx-coordination-team')) {
244
+ return [
245
+ ' The marker is tracked or staged in git, so every worktree on this branch shares that one file. ' +
246
+ `Writing this team's name into it would modify the shared file, and committing that ` +
247
+ `would give every worktree team "${want}". To fix it, ${statusSteps}.`,
248
+ ];
249
+ }
250
+ return [
251
+ ` To fix it, make that marker name team "${want}". If the marker is wrong, delete it and run ` +
252
+ `\`mx-agent start ${want}\`, which writes the marker only when the file is absent.`,
253
+ ];
254
+ }
255
+ /**
256
+ * Print the expected-team refusal and exit 1, or return when there is none.
257
+ * Exit 1, not 2: the check ran and found a problem (fail-loud standard).
258
+ */
259
+ function refuseOnExpectedTeamMismatch(locate) {
260
+ const lines = expectedTeamRefusal(process.env, locate);
261
+ if (!lines)
262
+ return;
263
+ for (const line of lines)
264
+ console.error(line);
265
+ process.exit(1);
266
+ }
134
267
  /**
135
268
  * Resolve the memory-mode product slug, failing CLOSED on an invalid
136
269
  * explicitly-set value (KI-COORDMT-CLIENT-SLUG-VALIDATION). The default
@@ -384,7 +517,7 @@ export function withTokenRefresh(inner, rebuild, refresh, initialToken) {
384
517
  * still goes through env — it is read by the session-start hook in another process,
385
518
  * which is the whole point of it.
386
519
  */
387
- export async function prepareClient(deps) {
520
+ export async function prepareClient(deps, opts = {}) {
388
521
  if (deps?.client) {
389
522
  // Provisioning is still skipped for an injected client, but the 401 recovery
390
523
  // wrapper is NOT: it is the single path every command goes through, and a
@@ -396,6 +529,31 @@ export async function prepareClient(deps) {
396
529
  const injected = deps.client;
397
530
  return withTokenRefresh(injected, () => injected, deps.refreshToken);
398
531
  }
532
+ // #548: every queue command reaches a credential through here, so the
533
+ // expected-team check runs here, before credential resolution of any kind (env,
534
+ // config, cache, Key Vault or the credential endpoint). It covers watch,
535
+ // respond, obligations, ack and history as well as brief and drain, and a
536
+ // future command cannot miss it. On a Key Vault project this is the only check:
537
+ // resolveIdentity's #220 interlock runs on the credential-endpoint branch only
538
+ // (lib/coordination-token.ts), so without it a wrong marker served another
539
+ // team's cached token (security review on PR #549, Should Fix).
540
+ //
541
+ // TWO OUTCOMES, BY CALLER. The coordination commands come through
542
+ // `commandClient` and exit 1, so the refusal is a failed command, never the
543
+ // memory-mode fall-back. The default, for every other caller, prints the same
544
+ // refusal and returns null. Three callers outside this file
545
+ // (commands/fleet-tmux.ts, lib/fleet-data.ts, commands/session.ts) rely on
546
+ // "returns null, never throws": exiting here would end a fleet view or turn
547
+ // `session pause`'s ok:false into a process exit. A future command that calls
548
+ // prepareClient directly still gets no client and a printed refusal.
549
+ const refusal = expectedTeamRefusal();
550
+ if (refusal) {
551
+ for (const line of refusal)
552
+ console.error(line);
553
+ if (opts.onWrongTeam === 'exit')
554
+ process.exit(1);
555
+ return null;
556
+ }
399
557
  // SF-A never-crash contract: resolveCoordinationToken is documented never-throws,
400
558
  // but its config read can still touch the filesystem (mkdirSync on an unwritable/
401
559
  // read-only HOME → EACCES/EROFS). Keep the WHOLE resolve+build inside the
@@ -496,6 +654,13 @@ export async function prepareClient(deps) {
496
654
  return null;
497
655
  }
498
656
  }
657
+ /**
658
+ * `prepareClient` for a coordination command (#548): a session started as one
659
+ * team exits 1 on a marker that names another, before any credential.
660
+ */
661
+ function commandClient(deps) {
662
+ return prepareClient(deps, { onWrongTeam: 'exit' });
663
+ }
499
664
  /**
500
665
  * Opportunistically flush any queued-locally entries. Surfaces a flush FAILURE
501
666
  * as a stderr warning (MAJOR-4) — never silent — but does not fail the primary
@@ -861,10 +1026,27 @@ export async function runEscalate(opts, deps) {
861
1026
  return runEscalateMemory(opts, body, deps);
862
1027
  }
863
1028
  const team = resolveTeam(opts.team);
864
- const entry = { team, to: opts.to, priority: opts.priority, body, from: team };
865
- const client = await prepareClient(deps);
1029
+ // One timestamp for both the live send and any fallback entry (#560 amendment,
1030
+ // Must Fix 1). The key is computed from it the way appendFallbackEntry computes
1031
+ // it, so a send that the service enqueued before answering 502/503/504 and the
1032
+ // later flush of the queued copy carry the same key. The service's dedup is
1033
+ // fail-open: the second copy is delivered and flagged as a repeat, not dropped.
1034
+ // At-least-once delivery is the accepted outcome.
1035
+ const localTimestamp = new Date().toISOString();
1036
+ const priority = opts.priority;
1037
+ const entry = { team, to: opts.to, priority, body, from: team, localTimestamp };
1038
+ const idempotencyKey = idempotencyKeyFor(team, opts.to, priority, localTimestamp, body);
1039
+ // A broadcast is never queued locally (#560 amendment, Must Fix 2): a broadcast
1040
+ // that failed once has no single recipient to retry against.
1041
+ const isBroadcast = opts.to === 'all';
1042
+ const client = await commandClient(deps);
866
1043
  if (!client) {
867
1044
  // No token/URL could be provisioned (prepareClient already printed the reason).
1045
+ if (isBroadcast) {
1046
+ console.error(`Error: escalation to "all" was NOT delivered: no coordination client was available. A broadcast is never queued locally; send it again once the service is reachable.`);
1047
+ process.exit(1);
1048
+ return;
1049
+ }
868
1050
  // Degrade to the local fallback so the message is never lost.
869
1051
  writeFallbackOrFail(entry);
870
1052
  console.log('queued locally, will flush on reconnect');
@@ -873,11 +1055,22 @@ export async function runEscalate(opts, deps) {
873
1055
  try {
874
1056
  // Opportunistically flush anything left from a prior outage.
875
1057
  await opportunisticFlush(client);
876
- const res = await client.enqueue({ to: opts.to, priority: opts.priority, body, from: team });
1058
+ const res = await client.enqueue({ to: opts.to, priority, body, from: team, idempotencyKey });
877
1059
  console.log(`escalated: ${res.messageId} -> ${opts.to} [${opts.priority}]${res.duplicate ? ' (duplicate)' : ''}`);
878
1060
  }
879
1061
  catch (err) {
880
- if (err instanceof ServiceUnreachableError) {
1062
+ // Queue locally only what a retry can fix: the service was unreachable, or
1063
+ // the ingress answered 502, 503 or 504. A 500 is not queued: it is the
1064
+ // service's own failure on this request, and a retry is as likely to repeat it.
1065
+ const status = err instanceof ServiceHttpError ? err.status : undefined;
1066
+ const retryable = err instanceof ServiceUnreachableError || status === 502 || status === 503 || status === 504;
1067
+ if (retryable) {
1068
+ if (isBroadcast) {
1069
+ const why = status !== undefined ? `the coordination service answered HTTP ${status}` : 'the coordination service was unreachable';
1070
+ console.error(`Error: escalation to "all" was NOT delivered: ${why}. A broadcast is never queued locally; send it again once the service is reachable.`);
1071
+ process.exit(1);
1072
+ return;
1073
+ }
881
1074
  writeFallbackOrFail(entry);
882
1075
  console.log('queued locally, will flush on reconnect');
883
1076
  return;
@@ -896,6 +1089,21 @@ export async function runEscalate(opts, deps) {
896
1089
  return;
897
1090
  }
898
1091
  }
1092
+ if (err instanceof ServiceBodyError) {
1093
+ // A 2xx whose body is not JSON (#577 red-team finding 3). The service may
1094
+ // have enqueued the message, so it is neither queued nor called undelivered.
1095
+ console.error(`Error: escalation to "${opts.to}" was NOT confirmed: the coordination service answered ${err.status} with a body that is not JSON; ` +
1096
+ 'it may have been delivered. Check `mx-agent coordination history` before resending.');
1097
+ process.exit(1);
1098
+ return;
1099
+ }
1100
+ if (err instanceof ServiceHttpError) {
1101
+ // Every other non-2xx: a 4xx a retry cannot fix, or a 500. Not queued.
1102
+ const detail = err.message === `HTTP ${err.status}` ? '' : `\n${err.message}`;
1103
+ console.error(`Error: escalation to "${opts.to}" was NOT delivered: the coordination service answered HTTP ${err.status}.${detail}`);
1104
+ process.exit(1);
1105
+ return;
1106
+ }
899
1107
  console.error(err instanceof Error ? err.message : String(err));
900
1108
  process.exit(1);
901
1109
  }
@@ -934,7 +1142,8 @@ export async function runDrain(opts, deps) {
934
1142
  memoryModeQueueOnlyNote('drain');
935
1143
  return; // exit 0 — no crash
936
1144
  }
937
- const client = await prepareClient(deps);
1145
+ // #548: commandClient runs the expected-team check before any credential.
1146
+ const client = await commandClient(deps);
938
1147
  if (!client)
939
1148
  return; // could not provision — fall back (prepareClient printed why)
940
1149
  // Flush any queued-locally entries now that the service is reachable.
@@ -988,7 +1197,8 @@ export async function runBrief(opts = {}, deps) {
988
1197
  if (resolveCoordinationMode() === 'memory') {
989
1198
  return runBriefMemory(opts, deps);
990
1199
  }
991
- const client = await prepareClient(deps);
1200
+ // #548: commandClient runs the expected-team check before any credential.
1201
+ const client = await commandClient(deps);
992
1202
  if (!client)
993
1203
  return; // could not provision — fall back (prepareClient printed why)
994
1204
  await opportunisticFlush(client);
@@ -1092,7 +1302,7 @@ export async function runWatch(opts = {}, deps) {
1092
1302
  memoryModeQueueOnlyNote('watch');
1093
1303
  return; // exit 0 — no crash
1094
1304
  }
1095
- const client = await prepareClient(deps);
1305
+ const client = await commandClient(deps);
1096
1306
  if (!client)
1097
1307
  return; // could not provision — fall back (prepareClient printed why)
1098
1308
  const intervalMs = opts.intervalMs ?? envInt('MX_COORD_PRIORITY_POLL_MS', 15_000);
@@ -1185,7 +1395,7 @@ export async function runRespond(opts, deps) {
1185
1395
  console.error('Note: "open" and "acked" are set automatically by the system.');
1186
1396
  process.exit(1);
1187
1397
  }
1188
- const client = await prepareClient(deps);
1398
+ const client = await commandClient(deps);
1189
1399
  if (!client)
1190
1400
  return;
1191
1401
  try {
@@ -1214,7 +1424,7 @@ export async function runObligations(deps) {
1214
1424
  memoryModeQueueOnlyNote('obligations');
1215
1425
  return;
1216
1426
  }
1217
- const client = await prepareClient(deps);
1427
+ const client = await commandClient(deps);
1218
1428
  if (!client)
1219
1429
  return;
1220
1430
  try {
@@ -1257,7 +1467,7 @@ export async function runAck(messageId, deps) {
1257
1467
  memoryModeQueueOnlyNote('ack');
1258
1468
  return; // exit 0 — no crash
1259
1469
  }
1260
- const client = await prepareClient(deps);
1470
+ const client = await commandClient(deps);
1261
1471
  if (!client)
1262
1472
  return; // could not provision — fall back (prepareClient printed why)
1263
1473
  await client.ack(messageId);
@@ -1340,7 +1550,7 @@ export async function runHistory(opts = {}, deps) {
1340
1550
  memoryModeQueueOnlyNote('history');
1341
1551
  return; // exit 0 — no crash
1342
1552
  }
1343
- const client = await prepareClient(deps);
1553
+ const client = await commandClient(deps);
1344
1554
  if (!client)
1345
1555
  return; // could not provision — fall back (prepareClient printed why)
1346
1556
  const collected = [];
@@ -2043,6 +2253,12 @@ function describeLiveFailure(err) {
2043
2253
  * that hangs or throws is worse than one that says it could not tell.
2044
2254
  */
2045
2255
  export async function runStatus(deps) {
2256
+ // 0. #548: a session started as one team does not report another team's
2257
+ // queue state. Refuses before the cache check and the registration check,
2258
+ // either of which can read or request a credential. Memory mode resolves no
2259
+ // credential and is unchanged.
2260
+ if (resolveCoordinationMode() !== 'memory')
2261
+ refuseOnExpectedTeamMismatch(deps?.locate);
2046
2262
  // 1. Resolve project slug.
2047
2263
  const projectSlug = resolveProductSlug();
2048
2264
  // 2. Resolve team slug from the marker file. CLAUDE_WORKTREE_PATH first; in a